Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/craton/api/v1/resources/inventory/networks.py b/craton/api/v1/resources/inventory/networks.py
- index 95632e0..cc505a1 100644
- --- a/craton/api/v1/resources/inventory/networks.py
- +++ b/craton/api/v1/resources/inventory/networks.py
- @@ -149,7 +149,7 @@ class NetDevicesVariables(base.Resource):
- def get(self, id):
- """Get variables for the given network."""
- context = request.environ.get('context')
- - obj = dbapi.netdevice_get_by_id(context, id)
- + obj = dbapi.netdevices_get_by_id(context, id)
- resp = {"variables": jsonutils.to_primitive(obj.variables)}
- return resp, 200, None
- @@ -157,7 +157,7 @@ class NetDevicesVariables(base.Resource):
- def put(self, id):
- """"Update device variables, or create if it does not exist."""
- context = request.environ.get('context')
- - obj = dbapi.netdevice_variables_update(context, id, request.json)
- + obj = dbapi.netdevices_variables_update(context, id, request.json)
- resp = {"variables": jsonutils.to_primitive(obj.variables)}
- return resp, 200, None
- @@ -165,7 +165,7 @@ class NetDevicesVariables(base.Resource):
- def delete(self, id):
- """Delete network device variables."""
- context = request.environ.get('context')
- - dbapi.netdevice_variables_delete(context, id, request.json)
- + dbapi.netdevices_variables_delete(context, id, request.json)
- return None, 204, None
- @@ -226,7 +226,7 @@ class NetInterfaceById(base.Resource):
- def get(self, id):
- """Get network interface by given id"""
- context = request.environ.get('context')
- - obj = dbapi.net_interface_get_by_id(context, id)
- + obj = dbapi.net_interfaces_get_by_id(context, id)
- interface = jsonutils.to_primitive(obj)
- interface['variables'] = jsonutils.to_primitive(obj.variables)
- return interface, 200, None
- diff --git a/craton/tests/unit/fake_resources.py b/craton/tests/unit/fake_resources.py
- index c022dd6..7f03d10 100644
- --- a/craton/tests/unit/fake_resources.py
- +++ b/craton/tests/unit/fake_resources.py
- @@ -128,30 +128,44 @@ NETWORKS_LIST = [NETWORK1, NETWORK2]
- class NetDevice():
- - def __init__(self, region_id, ip_address):
- + def __init__(self, hostname, project_id, region_id, device_type,
- + ip_address, variables, labels=None):
- + self.hostname = hostname
- + self.project_id = project_id
- self.region_id = region_id
- + self.device_type = device_type
- self.ip_address = ip_address
- + self.variables = variables
- + self.resolved = copy.copy(variables)
- + self.labels = labels
- def items(self):
- return iter(self.__dict__.items())
- -NETDEVICE1 = NetDevice(1, '10.10.0.1')
- +NETDEVICE1 = NetDevice("NetDevices1", 1, 1, "Server", "10.10.0.1",
- + {"key1": "value1", "key2": "value2"},
- + labels=["a", "b"])
- NETDEVICE_LIST1 = [NETDEVICE1]
- class NetInterface():
- - def __init__(self, name, device_id, interface_type, ip_address):
- + def __init__(self, name, device_id, project_id, interface_type,
- + ip_address, variables):
- self.name = name
- self.device_id = device_id
- + self.project_id = project_id
- self.interface_type = interface_type
- self.ip_address = ip_address
- + self.variables = variables
- def items(self):
- return iter(self.__dict__.items())
- -NETINTERFACE1 = NetInterface(
- - 'NetworkDevice1', 1, 'interface_type1', '10.10.0.1'
- -)
- +NETINTERFACE1 = NetInterface("NetInterface", 1, 1, "interface_type1",
- + "10.10.0.1",
- + {"key1": "value1", "key2": "value2"})
- +
- +NETINTERFACE2 = NetInterface("Sample", 2, 1, "Sample1", "10.0.0.2", {"a": "b"})
- NETINTERFACE_LIST1 = [NETINTERFACE1]
- diff --git a/craton/tests/unit/test_api.py b/craton/tests/unit/test_api.py
- index 33e4747..3fa47de 100644
- --- a/craton/tests/unit/test_api.py
- +++ b/craton/tests/unit/test_api.py
- @@ -1,5 +1,6 @@
- import mock
- import uuid
- +import pdb
- from oslo_serialization import jsonutils
- @@ -538,6 +539,18 @@ class APIV1NetworksTest(APIV1Test):
- resp = self.get('/v1/networks?region_id=1')
- self.assertEqual(len(resp.json), 2)
- + @mock.patch.object(dbapi, 'networks_get_by_id')
- + def test_networks_get_by_id(self, mock_network):
- + mock_network.return_value = fake_resources.NETWORK1
- + resp = self.get('v1/networks/1')
- + self.assertEqual(resp.json["name"], fake_resources.NETWORK1.name)
- +
- + @mock.patch.object(dbapi, 'networks_get_by_id')
- + def test_networks_get_by_bad_id_is_404(self, mock_network):
- + mock_network.side_effect = exceptions.NotFound()
- + resp = self.get('v1/networks/9')
- + self.assertEqual(404, resp.status_code)
- +
- @mock.patch.object(dbapi, 'networks_get_by_region')
- def test_get_networks_by_non_existing_region_raises404(self, fake_network):
- fake_network.side_effect = exceptions.NotFound()
- @@ -561,6 +574,54 @@ class APIV1NetworksTest(APIV1Test):
- resp = self.post('/v1/networks', data=data)
- self.assertEqual(200, resp.status_code)
- + @mock.patch.object(dbapi, 'networks_create')
- + def test_create_networks_with_invalid_data(self, mock_network):
- + mock_network.return_value = None
- + # data is missing entries
- + data = {'region_id': 1}
- + resp = self.post('v1/networks', data=data)
- + self.assertEqual(422, resp.status_code)
- +
- + @mock.patch.object(dbapi, 'networks_update')
- + def test_update_network(self, mock_network):
- + mock_network.return_value = fake_resources.NETWORK1
- + payload = {"name": "Network_New1"}
- + resp = self.put('v1/networks/1', data=payload)
- + self.assertTrue(resp.json['name'], payload['name'])
- + self.assertEqual(resp.status_code, 200)
- +
- + @mock.patch.object(dbapi, 'networks_delete')
- + def test_delete_network(self, mock_network):
- + resp = self.delete('v1/networks/1')
- + self.assertEqual(204, resp.status_code)
- +
- + @mock.patch.object(dbapi, 'networks_variables_update')
- + def test_networks_put_variables(self, mock_network):
- + mock_network.return_value = fake_resources.NETWORK1
- + payload = {"a": "b"}
- + resp = self.put('v1/networks/1/variables', data=payload)
- + self.assertEqual(resp.status_code, 200)
- +
- + @mock.patch.object(dbapi, 'networks_variables_update')
- + def test_network_put_variable(self, mock_network):
- + mock_network.return_value = fake_resources.NETWORK1
- + payload = {"a": "b"}
- + resp = self.put('v1/networks/1/variables', data=payload)
- + self.assertEqual(resp.status_code, 200)
- +
- + @mock.patch.object(dbapi, 'networks_get_by_id')
- + def test_network_get_variables(self, mock_network):
- + mock_network.return_value = fake_resources.NETWORK2
- + resp = self.get('v1/networks/2/variables')
- + expected = {"variables": {"pkey1": "pvalue1"}}
- + self.assertEqual(resp.json, expected)
- +
- + @mock.patch.object(dbapi, 'networks_variables_delete')
- + def test_network_variables_delete(self, mock_network):
- + payload = {"key1": "value1"}
- + resp = self.delete('v1/networks/1/variables', data=payload)
- + self.assertEqual(resp.status_code, 204)
- +
- class APIV1NetDevicesTest(APIV1Test):
- @mock.patch.object(dbapi, 'netdevices_get_by_region')
- @@ -575,10 +636,65 @@ class APIV1NetDevicesTest(APIV1Test):
- resp = self.get(path_query)
- device_resp = fake_resources.NETDEVICE_LIST1
- self.assertEqual(len(resp.json), 1)
- - self.assertEqual(resp.json[0]["ip_address"], device_resp[0].ip_address)
- -
- + self.assertEqual(resp.json[0]["ip_address"],
- + device_resp[0].ip_address)
- fake_devices.assert_called_once_with(mock.ANY, region_id, filters)
- + @mock.patch.object(dbapi, 'netdevices_get_by_region')
- + def test_netdevices_get_by_region(self, mock_devices):
- + mock_devices.return_value = fake_resources.NETDEVICE_LIST1
- + resp = self.get('/v1/netdevices/1')
- + self.assertEqual(len(resp.json), 2)
- +
- + @mock.patch.object(dbapi, 'netdevices_get_by_id')
- + def test_get_netdevices_get_by_id(self, mock_devices):
- + mock_devices.return_value = fake_resources.NETDEVICE1
- + resp = self.get('/v1/netdevices/1')
- + self.assertEqual(resp.json["hostname"],
- + fake_resources.NETDEVICE1.hostname)
- +
- + @mock.patch.object(dbapi, 'netdevices_create')
- + def test_create_netdevices_with_valid_data(self, mock_devices):
- + mock_devices.return_value = None
- + data = {'hostname': 'NewNetDevice1', 'region_id': 1,
- + 'device_type': 'Sample', 'ip_address': '0.0.0.0'}
- + resp = self.post('/v1/netdevices', data=data)
- + self.assertEqual(200, resp.status_code)
- +
- + @mock.patch.object(dbapi, 'netdevices_create')
- + def test_create_netdevices_with_invalid_data(self, mock_devices):
- + mock_devices.return_value = None
- + # data is missing entry
- + data = {'hostname': 'Sample'}
- + resp = self.post('/v1/netdevices', data=data)
- + self.assertEqual(422, resp.status_code)
- +
- + @mock.patch.object(dbapi, 'netdevices_delete')
- + def test_delete_netdevices(self, mock_devices):
- + resp = self.delete('v1/netdevices/1')
- + self.assertEqual(204, resp.status_code)
- +
- + @mock.patch.object(dbapi, 'netdevices_variables_delete')
- + def test_netdevices_variables_delete(self, mock_devices):
- + payload = {"key1": "value1"}
- + resp = self.delete('v1/netdevices/1/variables', data=payload)
- + self.assertEqual(resp.status_code, 204)
- +
- + @mock.patch.object(dbapi, 'netdevices_variables_update')
- + def test_netdevices_variables_update(self, mock_devices):
- + mock_devices.return_value = fake_resources.NETDEVICE1
- + payload = {"a": "b"}
- + resp = self.put('v1/netdevices/1/variables', data=payload)
- + self.assertEqual(resp.status_code, 200)
- +
- + @mock.patch.object(dbapi, 'netdevices_labels_update')
- + def test_netdevices_labels_update(self, mock_devices):
- + payload = {"labels": ["a", "b"]}
- + mock_devices.return_value = fake_resources.NETDEVICE1
- + resp = self.put('v1/netdevices/1/labels', data=payload)
- + self.assertEqual(200, resp.status_code)
- + self.assertEqual(resp.json, payload)
- +
- class APIV1NetInterfacesTest(APIV1Test):
- @mock.patch.object(dbapi, 'net_interfaces_get_by_device')
- @@ -596,3 +712,41 @@ class APIV1NetInterfacesTest(APIV1Test):
- self.assertEqual(resp.json[0]["name"], interface_resp[0].name)
- fake_interfaces.assert_called_once_with(mock.ANY, device_id, filters)
- +
- + @mock.patch.object(dbapi, 'net_interfaces_get_by_device')
- + def test_get_netinterfaces_by_device_id(self, fake_interfaces):
- + pdb.set_trace()
- + fake_interfaces.return_value = fake_resources.NETINTERFACE2
- + resp = self.get('/v1/net_interfaces?device_id=2')
- + print(resp.json)
- + netinterface_resp = fake_resources.NETINTERFACE2
- +# self.assertEqual(len(resp.json), len())
- + self.assertEqual(resp.json[0]["name"], netinterface_resp.name)
- +
- + @mock.patch.object(dbapi, 'net_interfaces_get_by_id')
- + def test_get_netinterfaces_by_id(self, fake_interfaces):
- + fake_interfaces.return_value = fake_resources.NETINTERFACE1
- + resp = self.get('/v1/net_interfaces/1')
- + self.assertEqual(resp.json["name"],
- + fake_resources.NETINTERFACE1.name)
- +
- + @mock.patch.object(dbapi, 'net_interfaces_create')
- + def test_netinterfaces_create_with_Valid_data(self, fake_interfaces):
- + fake_interfaces.return_value = None
- + data = {'name': 'NewNetInterface', 'device_id': 1,
- + 'ip_address': '0.0.0.0', 'interface_type': 'Sample'}
- + resp = self.post('/v1/net_interfaces', data=data)
- + self.assertEqual(200, resp.status_code)
- +
- + @mock.patch.object(dbapi, 'net_interfaces_create')
- + def test_netinterfaces_create_with_Invalid_data(self, fake_interfaces):
- + fake_interfaces.return_value = fake_resources.NETINTERFACE1
- + # data is missing entry
- + data = {'name': 'sample'}
- + resp = self.post('/v1/net_interfaces', data=data)
- + self.assertEqual(422, resp.status_code)
- +
- + @mock.patch.object(dbapi, 'net_interfaces_delete')
- + def test_netinterfaces_update(self, fake_interfaces):
- + resp = self.delete('/v1/net_interfaces/1')
- + self.assertEqual(204, resp.status_code)
- diff --git a/etc/craton-api-conf.sample b/etc/craton-api-conf.sample
- index 3253a95..afd39d7 100644
- --- a/etc/craton-api-conf.sample
- +++ b/etc/craton-api-conf.sample
- @@ -6,11 +6,11 @@ log_file=craton-api.log
- [api]
- # Host IP to use for API service
- -host=0.0.0.0
- +host=127.0.0.1
- # Port to use for API service
- -port=8080
- +port=8081
- # Paste Config file to use
- -api_paste_config=/craton/etc/craton-api-paste.ini
- +api_paste_config=/home/ubuntu/craton/etc/craton-api-paste.ini
- # The name of the Paste pipeline to use for Craton.
- #
- # Pipelines are organized according to authentication scheme. The available
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement