def test_create(self): with mock.patch.object(self.dbapi, 'device_create', autospec=True) as mock_device_create: mock_device_create.return_value = self.fake_device device = objects.Device(self.context, **self.fake_device) device.create(self.context) mock_device_create.assert_called_once_with(self.context, self.fake_device) self.assertEqual(self.context, device._context)
def test_device_type(self): for t in ["GPU", "FPGA", "AICHIP"]: device = objects.Device(self.context, type=t) self.assertEqual(self.context, device._context) # Invaild type will raise ValueError self.assertRaises(ValueError, objects.Device, self.context, type='OTHER_TYPE')
def test_get(self): db_device = self.fake_device device = objects.Device(context=self.context, **db_device) device.create(self.context) device_get = objects.Device.get(self.context, device.uuid) db_dpl = self.fake_deployable dpl = objects.Deployable(context=self.context, **db_dpl) dpl.device_id = device_get.id dpl.create(self.context) dpl_get = objects.Deployable.get(self.context, dpl.uuid) self.assertEqual(dpl_get.uuid, dpl.uuid)
def test_get_by_filter_with_attributes(self): db_device = self.fake_device device = objects.Device(context=self.context, **db_device) device.create(self.context) device_get = objects.Device.get(self.context, device.uuid) db_dpl = self.fake_deployable dpl = objects.Deployable(context=self.context, **db_dpl) dpl.device_id = device_get.id dpl.create(self.context) dpl_get = objects.Deployable.get(self.context, dpl.uuid) db_dpl2 = self.fake_deployable2 dpl2 = objects.Deployable(context=self.context, **db_dpl2) dpl2.device_id = device_get.id dpl2.create(self.context) dpl2_get = objects.Deployable.get(self.context, dpl2.uuid) db_attr = self.fake_attribute db_attr2 = self.fake_attribute2 db_attr3 = self.fake_attribute3 dpl.add_attribute(self.context, 'attr_key', 'attr_val') dpl.save(self.context) dpl2.add_attribute(self.context, 'test_key', 'test_val') dpl2.add_attribute(self.context, 'test_key3', 'test_val3') dpl2.save(self.context) query = {"attr_key": "attr_val"} dpl_get_list = objects.Deployable.get_by_filter(self.context, query) self.assertEqual(len(dpl_get_list), 1) self.assertEqual(dpl_get_list[0].uuid, dpl.uuid) query = {"test_key": "test_val"} dpl_get_list = objects.Deployable.get_by_filter(self.context, query) self.assertEqual(len(dpl_get_list), 1) self.assertEqual(dpl_get_list[0].uuid, dpl2.uuid) query = {"test_key": "test_val", "test_key3": "test_val3"} dpl_get_list = objects.Deployable.get_by_filter(self.context, query) self.assertEqual(len(dpl_get_list), 1) self.assertEqual(dpl_get_list[0].uuid, dpl2.uuid) query = {"num_accelerators": 4, "test_key3": "test_val3"} dpl_get_list = objects.Deployable.get_by_filter(self.context, query) self.assertEqual(len(dpl_get_list), 1) self.assertEqual(dpl_get_list[0].uuid, dpl2.uuid)
def test_destroy(self): db_device = self.fake_device device = objects.Device(context=self.context, **db_device) device.create(self.context) device_get = objects.Device.get(self.context, device.uuid) db_dpl = self.fake_deployable dpl = objects.Deployable(context=self.context, **db_dpl) dpl.device_id = device_get.id dpl.create(self.context) self.assertEqual(db_dpl['uuid'], dpl.uuid) dpl.destroy(self.context) self.assertRaises(exception.ResourceNotFound, objects.Deployable.get, self.context, dpl.uuid)
def test_get_by_filter(self): db_device = self.fake_device device = objects.Device(context=self.context, **db_device) device.create(self.context) device_get = objects.Device.get(self.context, device.uuid) db_dpl = self.fake_deployable dpl = objects.Deployable(context=self.context, **db_dpl) dpl.device_id = device_get.id dpl.create(self.context) query = {"uuid": dpl['uuid']} dpl_get_list = objects.Deployable.get_by_filter(self.context, query) self.assertEqual(dpl_get_list[0].uuid, dpl.uuid)
def test_delete_attribute(self): db_device = self.fake_device device = objects.Device(context=self.context, **db_device) device.create(self.context) device_get = objects.Device.get(self.context, device.uuid) db_dpl = self.fake_deployable dpl = objects.Deployable(context=self.context, **db_dpl) dpl.device_id = device_get.id dpl.create(self.context) dpl_get = objects.Deployable.get(self.context, dpl.uuid) db_attr = self.fake_attribute dpl_get.add_attribute(self.context, db_attr['key'], db_attr['value']) dpl_get.save(self.context) dpl_get = objects.Deployable.get(self.context, dpl_get.uuid) self.assertEqual(len(dpl_get.attributes_list), 1) dpl_get.delete_attribute(self.context, dpl_get.attributes_list[0]) self.assertEqual(len(dpl_get.attributes_list), 0)