def test_network_devices_get_all(self): dbapi.network_devices_create(self.context, device1) dbapi.network_devices_create(self.context, device2) filters = {} res, _ = dbapi.network_devices_get_all(self.context, filters, default_pagination) self.assertEqual(len(res), 2)
def test_network_devices_update_sets_parent_id(self): parent = dbapi.network_devices_create( self.context, { 'project_id': project_id1, 'cloud_id': cloud_id1, 'region_id': 1, 'name': '1.www.example.com', 'ip_address': '10.1.2.102', 'device_type': 'switch', } ) child = dbapi.network_devices_create( self.context, { 'project_id': project_id1, 'cloud_id': cloud_id1, 'region_id': 1, 'name': '2.www.example.com', 'ip_address': '10.1.2.102', 'device_type': 'switch', 'parent_id': None, } ) self.assertIsNone(child.parent_id) child_update = dbapi.network_devices_update( self.context, child.id, { 'parent_id': parent.id, } ) self.assertEqual(parent.id, child_update.parent_id)
def test_network_device_get_all_filter_region(self): dbapi.network_devices_create(self.context, device1) dbapi.network_devices_create(self.context, device2) filters = { 'region_id': device1['region_id'], } res, _ = dbapi.network_devices_get_all(self.context, filters, default_pagination) self.assertEqual(len(res), 1) self.assertEqual(res[0]['hostname'], 'switch1')
def create_network_device( self, name, project_id, cloud_id, region_id, ip_address, network_device_type, cell_id=None, parent_id=None, labels=None, variables=None, ): network_device = { 'name': name, 'project_id': project_id, 'cloud_id': cloud_id, 'region_id': region_id, 'cell_id': cell_id, 'ip_address': ip_address, 'parent_id': parent_id, 'device_type': network_device_type, 'active': True, 'labels': labels or (), 'variables': variables or {}, } network_device = dbapi.network_devices_create(self.context, network_device) self.assertEqual(variables, network_device.variables) return network_device.id
def make_network_device(self, project_id, cloud_id, region_id, name, ip_address, device_type, cell_id=None, parent_id=None, **variables): network_device_data = { 'name': name, 'project_id': project_id, 'cloud_id': cloud_id, 'region_id': region_id, 'cell_id': cell_id, 'ip_address': ip_address, 'parent_id': parent_id, 'device_type': device_type, 'active': True, 'variables': variables, } network_device = dbapi.network_devices_create(self.context, network_device_data) return network_device.id
def test_network_devices_update_fails_when_parent_set_to_descendant(self): parent = dbapi.network_devices_create( self.context, { 'project_id': project_id1, 'cloud_id': cloud_id1, 'region_id': 1, 'name': '1.www.example.com', 'ip_address': '10.1.2.101', 'device_type': 'switch', 'parent_id': None, } ) child = dbapi.network_devices_create( self.context, { 'project_id': project_id1, 'cloud_id': cloud_id1, 'region_id': 1, 'name': '2.www.example.com', 'ip_address': '10.1.2.102', 'device_type': 'switch', 'parent_id': parent.id, } ) grandchild = dbapi.network_devices_create( self.context, { 'project_id': project_id1, 'cloud_id': cloud_id1, 'region_id': 1, 'name': '3.www.example.com', 'ip_address': '10.1.2.103', 'device_type': 'switch', 'parent_id': child.id, } ) self.assertRaises( exceptions.BadRequest, dbapi.network_devices_update, self.context, parent.id, { 'parent_id': grandchild.id, } )
def test_network_devices_update(self): device = dbapi.network_devices_create(self.context, device1) res = dbapi.network_devices_get_by_id(self.context, device.id) self.assertEqual(res.hostname, 'switch1') new_name = 'switch2' res = dbapi.network_devices_update(self.context, res.id, {'name': 'switch2'}) self.assertEqual(res.name, new_name)
def test_network_devices_delete(self): device = dbapi.network_devices_create(self.context, device1) # First make sure we have the device res = dbapi.network_devices_get_by_id(self.context, device.id) self.assertEqual(res.id, device.id) # Delete the device dbapi.network_devices_delete(self.context, res.id) self.assertRaises(exceptions.NotFound, dbapi.network_devices_get_by_id, self.context, res.id)
def test_network_device_get_all_filter_ip_address(self): dbapi.network_devices_create(self.context, device1) dbapi.network_devices_create(self.context, device3) ip = device1['ip_address'] setup_res, _ = dbapi.network_devices_get_all(self.context, {}, default_pagination) self.assertEqual(len(setup_res), 2) matches = [dev for dev in setup_res if str(dev['ip_address']) == ip] self.assertEqual(len(matches), 1) filters = { 'ip_address': ip, } res, _ = dbapi.network_devices_get_all(self.context, filters, default_pagination) self.assertEqual(len(res), 1) self.assertEqual(str(res[0]['ip_address']), ip)
def test_network_devices_labels_delete(self): device = dbapi.network_devices_create(self.context, device1) _labels = {"labels": ["tom", "jerry"]} dbapi.network_devices_labels_update(self.context, device.id, _labels) ndevice = dbapi.network_devices_get_by_id(self.context, device.id) self.assertEqual(sorted(ndevice.labels), sorted(_labels["labels"])) _dlabels = {"labels": ["tom"]} dbapi.network_devices_labels_delete(self.context, ndevice.id, _dlabels) ndevice = dbapi.network_devices_get_by_id(self.context, ndevice.id) self.assertEqual(ndevice.labels, {"jerry"})
def test_network_device_get_all_filter_name(self): dbapi.network_devices_create(self.context, device1) dbapi.network_devices_create(self.context, device2) name = device1['hostname'] setup_res, _ = dbapi.network_devices_get_all(self.context, {}, default_pagination) self.assertEqual(len(setup_res), 2) matches = [dev for dev in setup_res if dev['hostname'] == name] self.assertEqual(len(matches), 1) filters = { 'name': name, } res, _ = dbapi.network_devices_get_all(self.context, filters, default_pagination) self.assertEqual(len(res), 1) self.assertEqual(res[0]['hostname'], name)
def test_network_device_get_all_filter_id(self): dbapi.network_devices_create(self.context, device1) dbapi.network_devices_create(self.context, device2) setup_res, _ = dbapi.network_devices_get_all(self.context, {}, default_pagination) self.assertEqual(len(setup_res), 2) dev_id = setup_res[0]['id'] self.assertNotEqual(dev_id, setup_res[1]['id']) filters = { 'id': dev_id } res, _ = dbapi.network_devices_get_all(self.context, filters, default_pagination) self.assertEqual(len(res), 1) self.assertEqual(res[0]['id'], dev_id)
def test_network_device_get_all_filter_device_type(self): dbapi.network_devices_create(self.context, device1) dbapi.network_devices_create(self.context, device3) dev_type = device1['device_type'] setup_res, _ = dbapi.network_devices_get_all(self.context, {}, default_pagination) self.assertEqual(len(setup_res), 2) matches = [dev for dev in setup_res if dev['device_type'] == dev_type] self.assertEqual(len(matches), 1) filters = { 'device_type': dev_type, } res, _ = dbapi.network_devices_get_all(self.context, filters, default_pagination) self.assertEqual(len(res), 1) self.assertEqual(res[0]['device_type'], dev_type)
def test_network_device_get_all_filter_cell_id(self): region_id = 1 cell1 = dbapi.cells_create( self.context, { 'name': 'cell1', 'project_id': project_id1, 'cloud_id': cloud_id1, 'region_id': region_id, } ) cell2 = dbapi.cells_create( self.context, { 'name': 'cell2', 'project_id': project_id1, 'cloud_id': cloud_id1, 'region_id': region_id, } ) dbapi.network_devices_create( self.context, dict(cell_id=cell1.id, **device1) ) dbapi.network_devices_create( self.context, dict(cell_id=cell2.id, **device2) ) setup_res, _ = dbapi.network_devices_get_all(self.context, {}, default_pagination) self.assertEqual(len(setup_res), 2) matches = [dev for dev in setup_res if dev['cell_id'] == cell1.id] self.assertEqual(len(matches), 1) filters = { 'cell_id': cell1.id, } res, _ = dbapi.network_devices_get_all(self.context, filters, default_pagination) self.assertEqual(len(res), 1) self.assertEqual(res[0]['cell_id'], cell1.id)
def test_network_devices_update_fails_when_parent_id_set_to_own_id(self): network_device1 = dbapi.network_devices_create( self.context, { 'project_id': project_id1, 'cloud_id': cloud_id1, 'region_id': 1, 'name': '1.www.example.com', 'ip_address': '10.1.2.101', 'device_type': 'switch', 'parent_id': None, } ) self.assertRaises( exceptions.BadRequest, dbapi.network_devices_update, self.context, network_device1.id, { 'parent_id': network_device1.id, } )
def test_network_devices_create(self): try: dbapi.network_devices_create(self.context, device1) except Exception: self.fail("Network device create raised unexpected exception")
def test_network_devices_labels_create(self): device = dbapi.network_devices_create(self.context, device1) labels = {"labels": ["tom", "jerry"]} dbapi.network_devices_labels_update(self.context, device.id, labels)
def test_network_devices_get_by_filter_no_exit(self): dbapi.network_devices_create(self.context, device1) filters = {"hostname": "foo"} res, _ = dbapi.networks_get_all(self.context, filters, default_pagination) self.assertEqual(res, [])
def test_network_devices_get_by_id(self): device = dbapi.network_devices_create(self.context, device1) res = dbapi.network_devices_get_by_id(self.context, device.id) self.assertEqual(res.hostname, 'switch1')