Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
138 changes: 138 additions & 0 deletions tests/rest/tests_controllers/common_testing_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
"""Common testing methods"""

import unittest
import requests


class CommonTestinglMethods(unittest.TestCase):
"""Base class for mons, osds and pools controllers test cases"""

clusters_data = None
response = None
data = None
type = None
expected_attributes = None

def setUp(self):
self.clusters_data = requests.get("http://0.0.0.0:5000/kujira/api/v1/calamari/clusters").json()
self.response = requests.get("http://0.0.0.0:5000/kujira/api/v1/calamari/" + self.type)
self.data = self.response.json()

def tearDown(self):
self.clusters_data = None
self.response = None
self.data = None

def test_expected_response(self):
"""Test for expected response:
status code= 200,
response is dictionary."""
self.assertEqual(self.response.status_code, 200)
self.assertTrue(isinstance(self.data, dict))

def test_consist_data_key(self):
"""Testing does response consist data key:
{
# DATA KEY - "data"
"data": [
{
...
},
...
]
}
"""
self.assertIn('data', self.data)

def test_expected_data_structure(self):
"""Testing does reponse structure is the same as expected:
{
"data": [
{
# IS DICTIONARY
"attributes":{
# IS DICTIONARY
...
},
...
},
...
]
}
"""
self.assertTrue(isinstance(self.data['data'], list))
for j in range(len(self.data['data'])):
self.assertTrue(isinstance(self.data['data'][j], dict))
self.assertTrue(isinstance(self.data['data'][j]['attributes'], dict))

def test_keys_in__data_list(self):
"""Testing keys in data list of dictionaries:
{
"data": [
{
# RESPONSE CONSIST KEY "attributes"
"attributes":{
...
},
# RESPONSE CONSIST KEY "type"
"type": "some value",
# RESPONSE CONSIST KEY "id"
"id": "some value"
},
...
]
}
"""
for j in range(len(self.data['data'])):
self.assertIn('attributes', self.data['data'][j])
self.assertIn('type', self.data['data'][j])
self.assertIn('id', self.data['data'][j])

def test_type(self):
"""Testing response type"""
for j in range(len(self.data['data'])):
self.assertEqual(self.data['data'][j]['type'], self.type)

def test_expected_attributes_keys(self):
""" Testing keys in "attributes" dictionary"""
if self.type == "mons":
self.expected_attributes = []
self.expected_attributes.append('name')
self.expected_attributes.append('in-quorum')
self.expected_attributes.append('addr')
self.expected_attributes.append('rank')
self.expected_attributes.append('server')
elif self.type == "pools":
self.expected_attributes = []
self.expected_attributes.append('full')
self.expected_attributes.append('name')
self.expected_attributes.append('id')
self.expected_attributes.append('crush-ruleset')
self.expected_attributes.append('crash-replay-interval')
self.expected_attributes.append('hashpspool')
self.expected_attributes.append('pg-num')
self.expected_attributes.append('quota-max-bytes')
self.expected_attributes.append('size')
self.expected_attributes.append('pgp-num')
self.expected_attributes.append('min-size')
self.expected_attributes.append('quota-max-objects')
elif self.type == "osds":
self.expected_attributes = []
self.expected_attributes.append('crush-node-ancestry')
self.expected_attributes.append('uuid')
self.expected_attributes.append('public-addr')
self.expected_attributes.append('reweight')
self.expected_attributes.append('valid-commands')
self.expected_attributes.append('up')
self.expected_attributes.append('server')
self.expected_attributes.append('cluster-addr')
self.expected_attributes.append('in')
self.expected_attributes.append('pools')
self.expected_attributes.append('id')
for j in range(len(self.data['data'])):
for k in range(len(self.expected_attributes)):
self.assertIn(self.expected_attributes[k], self.data['data'][j]['attributes'])


if __name__ == '__main__':
unittest.main()
42 changes: 42 additions & 0 deletions tests/rest/tests_controllers/test_calamari_api_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Calamari API connection tests"""

import unittest
import config
from requests import ConnectionError
from kujira.rest.lib.calamari_client import CalamariClient
from kujira.rest.lib.parsing_methods import create_error_422


class CalamariAPIConnectionTestCase(unittest.TestCase):
"""Calamari API connection test case"""

def test_server_is_up_and_running(self):
""" Calamari API request test:
expected response status code - 200
"""
constr = CalamariClient(api_url=config.CALAMARI_API_URL,
username=config.CALAMARI_API_USER,
password=config.CALAMARI_API_PWD,
timeout=config.CALAMARI_API_TIMEOUT)
client = constr.authenticate()
response = client.get(constr._api_url, timeout=constr._timeout)
self.assertEqual(response.status_code, 200)

def test_error_422(self):
""" Testing request with wrong Calamari API url:
422 error is expected
"""
try:
constr = CalamariClient(api_url="http://wrong_calamari_api_url/api/v2/",
username=config.CALAMARI_API_USER,
password=config.CALAMARI_API_PWD,
timeout=config.CALAMARI_API_TIMEOUT)
client = constr.authenticate()
response = client.get(constr._api_url, timeout=constr._timeout)
except ConnectionError as err:
response = create_error_422(constr._api_url, str(err))
self.assertEqual(response.status_code, 422)


if __name__ == '__main__':
unittest.main()
131 changes: 131 additions & 0 deletions tests/rest/tests_controllers/test_clusters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
"""Clusters controller test
- kujira-api must already be running
"""

import unittest
import requests


class ClustersTestCase(unittest.TestCase):
"""Clusters controllers test case"""

response = None
data = None

def setUp(self):
self.response = requests.get("http://0.0.0.0:5000/kujira/api/v1/calamari/clusters")
self.data = self.response.json()

def tearDown(self):
self.response = None
self.data = None

def test_expected_response(self):
"""Test for expected response:
status code= 200,
response is dictionary
"""
self.assertEqual(self.response.status_code, 200)
self.assertTrue(isinstance(self.data, dict))

def test_consist_data_key(self):
"""Testing does response consist data key:
{
# DATA KEY - "data"
"data":{
...
}
}
"""
self.assertIn('data', self.data)

def test_expected_data_structure(self):
"""Testing does reponse structure is the same as expected:
{
"data":{
# IS DICTIONARY
"attributes":{
# IS DICTIONARY
...
},
...
}
}
"""
self.assertTrue(isinstance(self.data['data'], dict))
self.assertTrue(isinstance(self.data['data']['attributes'], dict))

def test_keys_in_data_dictionary(self):
"""Testing keys in data dictionary:
{
"data":{
# RESPONSE CONSIST KEY "attributes"
"attributes":{
...
},
# RESPONSE CONSIST KEY "type"
"type": "some value",
# RESPONSE CONSIST KEY "id"
"id": "some value"
}
}
"""
self.assertIn('attributes', self.data['data'])
self.assertIn('type', self.data['data'])
self.assertIn('id', self.data['data'])

def test_type(self):
""" "type": "clusters" test:
{
"data":{
...
# RESPONSE CONSIST KEY "type" WITH "clusters VALUE"
type": "clusters",
...
}
}
"""
self.assertEqual(self.data['data']['type'], 'clusters')

def test_expected_attributes_keys(self):
""" Testing keys in "attributes" dictionary:
{
"data":{
"attributes":{
# "attributes" KEY VALUE IS DICTIONARY WITH
# "epoch", "health", "id", "name" KEYS
"epoch": "some value",
"health": "some value",
"id": "some value",
"name": "some value"
},
...
}
}
"""
self.assertIn('epoch', self.data['data']['attributes'])
self.assertIn('health', self.data['data']['attributes'])
self.assertIn('id', self.data['data']['attributes'])
self.assertIn('name', self.data['data']['attributes'])

def test_name(self):
""" "name": "ceph" test:
{
"data":{
"attributes":{
...
# "ceph" IS EXPECTED VALUE OF "name" KEY
"name": "ceph"
},
...
}
}
"""
self.assertEqual(self.data['data']['attributes']['name'], 'ceph')


if __name__ == '__main__':
unittest.main()



23 changes: 23 additions & 0 deletions tests/rest/tests_controllers/test_mons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Mons controller tests
kujira-api must already be running
"""

import common_testing_methods


class MonsTestCase(common_testing_methods.CommonTestinglMethods):
"""Mons controllers test case,
inherits testing methods from CommonTestingMethods"""

type = "mons"

def test_server_name(self):
"""Testing does response server name equals to response id"""
for j in range(len(self.data['data'])):
data_id = self.data['data'][j]['id']
self.assertEqual(self.data['data'][j]['attributes']['name'], data_id)
self.assertEqual(self.data['data'][j]['attributes']['server'], data_id)


if __name__ == '__main__':
common_testing_methods.main()
24 changes: 24 additions & 0 deletions tests/rest/tests_controllers/test_osds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Osds controller tests
kujira-api must already be running
"""

import common_testing_methods


class OsdsTestCase(common_testing_methods.CommonTestinglMethods):
"""Osds controllers test case,
inherits testing methods from CommonTestingMethods"""

type = "osds"

def test_osds_specific_structure(self):
"""Testing does the response attributes dictionary
structure is the same as expected"""
for i in range(len(self.data['data'])):
self.assertTrue(isinstance(self.data['data'][i]['attributes']['crush-node-ancestry'], list))
self.assertTrue(isinstance(self.data['data'][i]['attributes']['valid-commands'], list))
self.assertTrue(isinstance(self.data['data'][i]['attributes']['pools'], list))


if __name__ == '__main__':
common_testing_methods.main()
22 changes: 22 additions & 0 deletions tests/rest/tests_controllers/test_pools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Pools controller tests
kujira-api must already be running
"""

import common_testing_methods


class PoolsTestCase(common_testing_methods.CommonTestinglMethods):
"""Pools controllers test case,
inherits testing methods from CommonTestingMethods"""

type = "pools"

def test_id_equals(self):
"""Testing does data id equals to data atrributes id"""
for j in range(len(self.data['data'])):
data_id = self.data['data'][j]['id']
self.assertEqual(str(self.data['data'][j]['attributes']['id']), data_id)


if __name__ == '__main__':
common_testing_methods.main()