def test_delete_device(self, mock_etcd_read, mock_etcd_delete): device = utils.get_test_device_db_info() mock_etcd_read.return_value = utils.get_etcd_read_result( device['uuid'], json.dumps(device)) db_api.Connection.delete_device(device['uuid']) mock_etcd_delete.assert_called_with('/devices/' + device['uuid'])
def test_get_device_by_uuid(self, mock_etcd_read): device = utils.get_test_device_db_info() mock_etcd_read.return_value = utils.get_etcd_read_result( device['uuid'], json.dumps(device)) result = db_api.Connection.get_device_by_uuid(device['uuid']) self.assertEqual(device, result.as_dict()) mock_etcd_read.assert_called_once_with('/devices/' + device['uuid'])
def test_list_devices_with_filters(self, mock_etcd_read): device = utils.get_test_device_db_info() device2 = copy.deepcopy(device) device2['uuid'] = 'ea8e2a25-2901-438d-8157-de7ffd68d051' device2['podm_id'] = 'mk8elki9-2901-j8j7-8157-de7ffdk8gt7u' mock_etcd_read.return_value = utils.get_etcd_read_list( '/devices', json.dumps(device), json.dumps(device2)) result = db_api.Connection.list_devices( filters={'podm_id': 'mk8elki9-2901-j8j7-8157-de7ffdk8gt7u'}) result = [dev.as_dict() for dev in result] self.assertEqual([device2], result) mock_etcd_read.assert_called_once_with('/devices')
def test_add_device(self, mock_etcd_read, mock_etcd_write): device = utils.get_test_device_db_info() fake_utcnow = '2017-01-01 00:00:00 UTC' device['created_at'] = fake_utcnow device['updated_at'] = fake_utcnow # Mark this uuid don't exist in etcd db mock_etcd_read.side_effect = etcd.EtcdKeyNotFound result = db_api.Connection.add_device(device) self.assertEqual(device, result.as_dict()) mock_etcd_read.assert_called_once_with('/devices/' + device['uuid']) mock_etcd_write.assert_called_once_with('/devices/' + device['uuid'], json.dumps(result.as_dict()))
def test_update_device(self, mock_etcd_read, mock_etcd_write): device = utils.get_test_device_db_info() mock_etcd_read.return_value = utils.get_etcd_read_result( device['uuid'], json.dumps(device)) fake_utcnow = '2017-01-01 00:00:00 UTC' device['updated_at'] = fake_utcnow device.update({'resource_uri': 'new_uri'}) result = db_api.Connection.update_device(device['uuid'], {'resource_uri': 'new_uri'}) self.assertEqual(device, result.as_dict()) mock_etcd_read.assert_called_with('/devices/' + device['uuid']) mock_etcd_write.assert_called_with('/devices/' + device['uuid'], json.dumps(result.as_dict()))