Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cr0wg4n committed May 26, 2021
1 parent b575edd commit 2bcba60
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 77 deletions.
88 changes: 64 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,89 @@ URL = "http://localhost:5601"
USERNAME = "XXXX"
PASSWORD = "XXXX"

kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD)
```

### Create Space
```python
""
id = "demo"
name = "demo"
description = "descripcion del espacio de pruebas"
color = "#000000"
space = kibana.space(id=id, name=name, description=description, color=color)
space_response = space.create()
```
out:
```bash
""
```

### Create Object (index-pattern)
```python
""
```
out:
```bash
""
pattern_json = {
"title":"demo*",
"timeFieldName": "@timestamp", #timefiledname is important, it taken as a reference to time
"fields":"[]"
}
kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD)
index_pattern_response = kibana.object(space_id="demo").create('index-pattern', attribs=pattern_json)
```

### Create Object (visualization)
```python
""
type = "metric"
title = "Hello this is a basic metric visualization"
index_pattern_id = "XXXX-XXX-XXXX" # every visualization needs an index pattern to work
visualization = Visualization(type=type, title=title, index_pattern_id=index_pattern).create()
visualization_response = kibana.object(space_id="demo").create('visualization', body=visualization).json()
```
out:
```bash
""
### Visualization Modelation
```python
index_pattern = "XXXXX-XXXXXX-XXXXXX"
type = "line"
title = "Hello this is a basic line visualization"
visualization = Visualization(type=type, title=title, index_pattern_id=index_pattern)
visulization_json = visualization.create()
```
### Create Object (dashboard)

### Panel Modelation
```python
""
width=48
height=12
pos_x=0
pos_y=1
panel = Panel("panel_0", width, height, pos_x, pos_y, visualization_id=visualization_id)
panel_json = panel.create()
references = panel.get_references()
```
out:
```bash
""

### Create Object (dashboard)
```python
index_pattern = "XXXXX-XXXXXX-XXXXXX"
type = "line"
title = "Hello this is a basic line visualization"
visualization = Visualization(type=type, title=title, index_pattern_id=index_pattern).create()
visualization_response = kibana.object(space_id="demo").create('visualization', body=visualization).json()
visualization_id = visualization_response["id"]
panel = Panel("panel_0", 48, 12, 0, 2, visualization_id=visualization_id)
panels = [panel.create()]
references = [panel.get_reference()]
dashboard = Dashboard(title="Demo Dashboard", panels=panels, references=references)
dashboard_response = dashboard.create()
```

### List all objects
```python
""
objects_response = kibana.object(space_id="demo").all() # All objects
print(objects_response.json())
# Filter by types: "visualization", "dashboard", "search", "index-pattern",
# "config", "timelion-sheet", "url", "query", "canvas-element", "canvas-workpad", "lens",
# "infrastructure-ui-source", "metrics-explorer-view", "inventory-view"
objects_response = kibana.object(space_id="demo").all(type="index-pattern") # Type in specific
print(objects_response.json())

```
out:
```bash
""

### Import Objects
```python
file = open("demo.ndjson", 'r')
response = kibana.object().loads(file=file)
file.close()
```

## Development
Expand Down
106 changes: 53 additions & 53 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,76 +10,77 @@ class TestStringMethods(unittest.TestCase):

def test_url_parser(self):
pass
# kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD)
# url = kibana.url(URL, "1", "2", "3")
# self.assertEqual("http://localhost:5601/1/2/3", url)
kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD)
url = kibana.url(URL, "1", "2", "3")
self.assertEqual("http://localhost:5601/1/2/3", url)

def test_create_space(self):
pass
# kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD)
# id = f"test-{int(random.randint(0,100)*0.33)}"
# name = "test" + id
# description = "descripcion del espacio de pruebas"
# color = "#000000"
# response = kibana.space(id=id, name=name, description=description, color=color).create()
# response_json = {
# "id": id,
# "name": name,
# "description": description,
# "color": color,
# "disabledFeatures": []
# }
# self.assertEqual(response.json(), response_json)
kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD)
id = f"test-{int(random.randint(0,100)*0.33)}"
name = "test" + id
description = "descripcion del espacio de pruebas"
color = "#000000"
response = kibana.space(id=id, name=name, description=description, color=color).create()
response_json = {
"id": id,
"name": name,
"description": description,
"color": color,
"disabledFeatures": []
}
self.assertEqual(response.json(), response_json)

def test_create_index_pattern(self):
pass
# pattern_json = {
# "title":"demo*",
# "timeFieldName": "@timestamp",
# "fields":"[]"
# }
# kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD)
# res = kibana.object(space_id="demo", attribs=pattern_json).create('index-pattern')
# self.assertEqual(res.json()["attributes"], pattern_json)
pattern_json = {
"title":"demo*",
"timeFieldName": "@timestamp",
"fields":"[]"
}
kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD)
res = kibana.object(space_id="demo").create('index-pattern', attribs=pattern_json)
self.assertEqual(res.json()["attributes"], pattern_json)

def test_import(self):
pass
# CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
# FILE_PATH = os.path.join(CURRENT_DIR, 'exported_data.ndjson')
# file = open(FILE_PATH, 'r')
# kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD)
# response = kibana.object().loads(file=file)
# file.close()
# print(response.json())
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
FILE_PATH = os.path.join(CURRENT_DIR, 'exported_data.ndjson')
file = open(FILE_PATH, 'r')
kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD)
response = kibana.object().loads(file=file)
file.close()
print(response.json())

def test_get_all_objects(self):
pass
# kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD)
# response = kibana.object(space_id="demo").all(type="index-pattern")
# print(response.json())
kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD)
response = kibana.object(space_id="demo").all(type="index-pattern")
print(response.json())

def test_create_panel(self):
pass
# test = {'version': '7.8.0', 'gridData': {'x': 0, 'y': 12, 'w': 48, 'h': 12, 'i': 'holamundo'}, 'panelIndex': 'holamundo', 'embeddableConfig': {}, 'panelRefName': 'panel_0'}
# result = Panel("panel_0", 48, 12, 0, 12, id="holamundo", visualization_id="asdasdasd")
# print(result.get_reference())
# self.assertEqual(test, result.create())
test = {'version': '7.8.0', 'gridData': {'x': 0, 'y': 12, 'w': 48, 'h': 12, 'i': 'holamundo'}, 'panelIndex': 'holamundo', 'embeddableConfig': {}, 'panelRefName': 'panel_0'}
result = Panel("panel_0", 48, 12, 0, 12, id="holamundo", visualization_id="XXXXXXXXXXXX")
references = result.get_reference()
print(references)
self.assertEqual(test, result.create())

def test_create_visualization(self):
pass
# pattern_json = {
# "title":"demo*",
# "timeFieldName": "@timestamp",
# "fields":"[]"
# }
# kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD)
# res = kibana.object(space_id="demo", attribs=pattern_json).create('index-pattern').json()
# index_pattern = res["id"]
# type = "line"
# title = "hello this is a visualization :D 2"
# visualization = Visualization(type=type, title=title, index_pattern_id=index_pattern).create()
# res = kibana.object(space_id="demo").create('visualization', body=visualization).json()
# print(res)
pattern_json = {
"title":"demo*",
"timeFieldName": "@timestamp",
"fields":"[]"
}
kibana = Kibana(base_url=URL, username=USERNAME, password=PASSWORD)
res = kibana.object(space_id="demo", attribs=pattern_json).create('index-pattern').json()
index_pattern = res["id"]
type = "line"
title = "hello this is a visualization :D 2"
visualization = Visualization(type=type, title=title, index_pattern_id=index_pattern).create()
res = kibana.object(space_id="demo").create('visualization', body=visualization).json()
print(res)

def test_create_dashboard(self):
pass
Expand All @@ -99,7 +100,6 @@ def test_create_dashboard(self):
panel = Panel("panel_0", 48, 12, 0, 2, visualization_id=visualization_id)
panels = [panel.create()]
references = [panel.get_reference()]
print(panels, references)
dasboard = Dashboard(title="hola mundo", panels=panels, references=references, query="user.name: mat*").create()
res = kibana.object(space_id="demo").create('dashboard', body=dasboard).json()
print(res)
Expand Down

0 comments on commit 2bcba60

Please sign in to comment.