def test_flattening(self): # Verify that we get the same result whether creating from a # dictionary, individual fields, or a nested data element txn_id = util.randomhex() target = util.randomword() op = util.randomword() d = util.randomdict() req1 = BllRequest( dict(target=target, foo="baz", txn_id=txn_id, operation=op, bar=d)) req2 = BllRequest(target=target, foo="baz", txn_id=txn_id, operation=op, bar=d) req3 = BllRequest(target=target, txn_id=txn_id, data={ 'operation': op, 'foo': 'baz', 'bar': d }) self.assertDictEqual(req1, req2) self.assertDictEqual(req2, req3) self.assertIn("operation", req1['data']) self.assertIn("foo", req1['data']) self.assertIn("bar", req1['data']) self.assertNotIn("target", req1['data']) self.assertNotIn("txn_id", req1['data'])
def test_data_remains_gone_when_none_supplied(self): # Verify that when neither 'operation' nor 'data' are supplied, that # the resulting request has no 'data' key req1 = BllRequest(target=util.randomword(), action=util.randomword()) self.assertFalse(req1.get('data'))
def test_generic_get(self, _mock_endpoints, _mock_get_func): mock_client = mock.MagicMock() mock_client.node = mock.create_autospec(NodeManager) _mock_endpoints.return_value = [{ 'region': randomword(), 'url': randomurl() }] nodeid = randomhex() res = self.MockResource({ 'instance_uuid': randomhex(), 'uuid': nodeid, 'driver': 'agent_ilo', 'name': randomword(), 'power_state': 'power on', 'provision_state': 'active', }) mock_client.node.get.return_value = res _mock_get_func.return_value = mock_client svc = IronicSvc( BllRequest(operation='node.get', auth_token=get_mock_token(), data={'node_id': randomhex})) data = svc.handle()[api.DATA] self.assertIsInstance(data, dict) self.assertEqual(data['driver'], 'agent_ilo') self.assertEqual(data['uuid'], nodeid)
def get_resource_list(self): return [{ "username": randomword(), "name": randomword(), "ip_address": randomip(), "resource_mgr_id": vcenter_id, "id": randomidentifier(), "state": "activated", "password": randomword(), "type": "esxcluster", "port": str(random.randint(1, 1024)) }]
class MockEonClient(object): resource_mgr = { "username": randomword(), "name": vcenter_name, "ip_address": vcenter_ip, "id": vcenter_id, "state": "Registered", "activated_clusters": 0, "password": randomword(), "type": "vcenter", "port": str(random.randint(1, 1024)) } def get_resource_mgr_list(self): return [self.resource_mgr] def get_resource_mgr(self, id): if id == self.resource_mgr['id']: return self.resource_mgr return None def add_resource_mgr(self, vcenter_data): return {"id": vcenter_id, "name:": vcenter_name} def update_resource_mgr(self, vcenter_id, vcenter_data): return { "id": vcenter_id, "name": vcenter_name, "vcenter_meta": { "name:": vcenter_name } } def get_resource_list(self): return [{ "username": randomword(), "name": randomword(), "ip_address": randomip(), "resource_mgr_id": vcenter_id, "id": randomidentifier(), "state": "activated", "password": randomword(), "type": "esxcluster", "port": str(random.randint(1, 1024)) }] def delete_resource_mgr(self, id): return id def delete_resource(self, id): return id
def test_overrides(self): # Test that explicitly supplied values override those in the # request parameter of the BllRequest constructor req1 = BllRequest(target=util.randomword(), auth_token=util.randomword(), operation=util.randomword(), action=util.randomword(), data=util.randomdict()) target = util.randomword() operation = util.randomword() action = util.randomword() auth_token = util.randomword() req2 = BllRequest(request=req1, target=target, operation=operation, action=action, auth_token=auth_token) self.assertEquals(req2['action'], action) self.assertEquals(req2['target'], target) self.assertEquals(req2['auth_token'], auth_token) self.assertEquals(req2['data']['operation'], operation)
def test_crud_operations(self): user = randomword() prefs = self.create_random_dict() # GET preferences for a non-existent user with log_level(logging.CRITICAL, 'bll.plugins.service'): reply = self.handle('GET', user) self.assertEqual('error', reply[api.STATUS]) try: reply = self.handle('POST', user, prefs) self.assertEqual('complete', reply[api.STATUS]) reply = self.handle('GET', user) self.assertEqual('complete', reply[api.STATUS]) self.assertEqual(prefs, reply[api.DATA]) prefs = self.create_random_dict() reply = self.handle('PUT', user, prefs) self.assertEqual('complete', reply[api.STATUS]) reply = self.handle('GET', user) self.assertEqual('complete', reply[api.STATUS]) self.assertEqual(prefs, reply[api.DATA]) finally: reply = self.handle('DELETE', user) self.assertEqual('complete', reply[api.STATUS]) with log_level(logging.CRITICAL, 'bll.plugins.service'): reply = self.handle('GET', user) self.assertEqual('error', reply[api.STATUS])
def create_random_dict(self): # build a complicated, nested dictionary my_dict = {} my_dict[randomidentifier()] = randomword() nested_dict = {} nested_dict[randomidentifier()] = randomword() my_dict['dict'] = nested_dict nested_array = [] nested_array.append(randomword()) nested_array.append(random.random()) nested_array.append(random.randint(0, 1000)) my_dict['array'] = nested_array return my_dict
def bar(self): msg = randomword() reply = self.call_service_async(target="general", operation="echo_slow", message=msg, polling_interval=0.1) if reply != msg: self.response.error("Did not receive data")
def test_doubly_nested_data(self): target = util.randomword() d = util.randomdict() req = BllRequest(target=target, data={'data': d}) # Make sure that the doubly nested data got populated correctly self.assertDictEqual(d, req['data']['data'])
def test_get_data(self): # Verify that get_data returns all non reserved fields correctly req = BllRequest(target=util.randomword(), action="GET", foo=util.randomword(), txn_id=util.randomhex(), auth_token=util.randomhex(), operation=util.randomword(), version="1") data = req.get_data() self.assertNotIn("action", data) self.assertNotIn("target", data) self.assertNotIn("txn_id", data) self.assertNotIn("auth_token", data) self.assertNotIn("region", data) self.assertNotIn("data", data) self.assertNotIn(api.VERSION, data) self.assertNotIn("operation", data) self.assertIn("foo", data)
def test_baremetal_list(self, _mock_get_func, _mock_endpoints, call_service): # this will also indirectly test node.list (generic_list) mock_client = mock.MagicMock() mock_client.node = mock.create_autospec(NodeManager) _mock_endpoints.return_value = [{ 'region': randomword(), 'url': randomurl() }] the_list = [ self.MockResource({ 'instance_uuid': 'instance_1', 'uuid': randomhex(), 'name': randomword(), 'power_state': 'power on' }), self.MockResource({ 'instance_uuid': 'instance_2', 'uuid': randomhex(), 'name': randomword(), 'power_state': 'power on' }) ] mock_client.node.list.return_value = the_list _mock_get_func.return_value = mock_client svc = IronicSvc( BllRequest(operation='baremetal-list', auth_token=get_mock_token())) data = svc.handle()[api.DATA] self.assertEqual(len(data), 2) for inst in data: self.assertTrue('baremetal', 'compute' in inst.keys()) self.assertTrue( inst['baremetal']['instance_uuid'] == inst['compute']['id'])
def __init__(self, name=None, image_id=None, power_state=None, flavor_id=None, metadata=None, tenant_id=None): self.name = name or randomword() self.id = randomword() self.metadata = metadata self.status = randomword() self._info = { 'OS-EXT-SRV-ATTR:host': randomword(), 'OS-EXT-STS:power_state': power_state, 'OS-EXT-AZ:availability_zone': randomword(), 'OS-EXT-STS:task_state': randomword()} self.image = {'id': image_id} self.addresses = randomword() self.created = None self.key_name = randomword() self.flavor = {'id': flavor_id} self.tenant_id = tenant_id
def __init__(self): self.vcpus_used = randint(0, 4) self.vcpus = randint(0, 4) self.memory_mb_used = randint(1024, 2048) self.memory_mb = randint(4096, 8192) self.local_gb_used = randint(1, 5) self.local_gb = randint(10, 20) self.running_vms = randint(0, 4) self.NAME_ATTR = 'hypervisor_hostname' self.hypervisor_hostname = 'testhost-%s' % randomword() self.host_ip = randomip() self.id = randint(1, 10000) self.status = 'enabled' self.state = 'up' self.hypervisor_type = 'QEMU' self.service = {'host': self.hypervisor_hostname}
def test_get_vcenter_update_no_id(self, *_): svc = IntegratedToolsSvc( BllRequest(action='PUT', operation='edit_vcenter', auth_token=get_mock_token(), data={ 'data': { 'name': vcenter_name, 'username': randomidentifier(), 'password': randomword(), 'ip_address': randomip(), 'type': 'cluster' } })) with self.assertRaisesRegexp(InvalidBllRequestException, 'Invalid.*vCenter id'): svc.handle()
def test_get_vcenter_update_complete(self, *_): svc = IntegratedToolsSvc( BllRequest(action='PUT', operation='edit_vcenter', auth_token=get_mock_token(), data={ 'data': { 'id': vcenter_id, 'name': vcenter_name, 'username': randomidentifier(), 'password': randomword(), 'ip_address': randomip(), 'type': 'cluster' } })) output = svc.complete() self.assertEqual('complete', output['status'])
def test_register_vcenter(self, *_): svc = IntegratedToolsSvc( BllRequest(action='POST', operation='vcenters', auth_token=get_mock_token(), data={ 'data': { 'name': vcenter_name, 'username': randomidentifier(), 'password': randomword(), 'ip_address': randomip(), 'port': '443', 'type': 'vcenter' } })) svc.handle() output = svc.complete() self.assertEqual('complete', output['status'])
def test_get_vcenter_update_exception(self): attrs = {'update_resource_mgr.side_effect': Exception()} with patch.object(IntegratedToolsSvc, '_get_eon_client', return_value=Mock(**attrs)): svc = IntegratedToolsSvc( BllRequest(action='PUT', operation='edit_vcenter', auth_token=get_mock_token(), data={ 'data': { 'id': vcenter_id, 'name': vcenter_name, 'username': randomidentifier(), 'password': randomword(), 'ip_address': randomip(), 'type': 'cluster' } })) output = svc.complete() self.assertEqual('error', output['status']) self.assertEqual(REGISTERED_STATE, get_vcenter_state(id))
# (c) Copyright 2016-2017 Hewlett Packard Enterprise Development LP # (c) Copyright 2017 SUSE LLC import random from bll.api.request import BllRequest from bll.common.exception import InvalidBllRequestException from bll.plugins.integratedtools_service import IntegratedToolsSvc, \ get_vcenter_state, REGISTERED_STATE from mock import patch, Mock from tests.util import TestCase, randomword, get_mock_token, \ randomidentifier, randomip vcenter_name = randomword() vcenter_id = randomidentifier() vcenter_ip = randomip() class MockEonClient(object): resource_mgr = { "username": randomword(), "name": vcenter_name, "ip_address": vcenter_ip, "id": vcenter_id, "state": "Registered", "activated_clusters": 0, "password": randomword(), "type": "vcenter", "port": str(random.randint(1, 1024)) }
def test_chained_creation(self): req1 = BllRequest(target=util.randomword(), operation=util.randomword()) req2 = BllRequest(req1) self.assertEquals(req1, req2)
def test_put_to_missing_user(self): # PUT preferences for a non-existent user with log_level(logging.CRITICAL, 'bll.plugins.service'): reply = self.handle('PUT', randomword(), randomword()) self.assertEqual('error', reply[api.STATUS])
def __init__(self): self.id = randomword() self.name = randomword() self.vcpus = 2**randint(0, 3) self.ram = 1024 * randint(1, 16) self.disk = 1000 * randint(1, 8)
def setUp(self): self.flavor_list = [] for x in range(5): self.flavor_list.append(Flavor()) self.image_list = [Struct({'id': '1', 'name': 'cirros'})] self.project_list = [ Struct({ 'id': '1', 'name': 'default_project' }), Struct({ 'id': '2', 'name': 'admin_project' }) ] self.hyp_list = [] for x in range(4): self.hyp_list.append(Hypervisor()) self.mock_novaclient = MagicMock(spec=nclient.Client) self.mock_novaclient.flavors = Mock( **{'list.return_value': self.flavor_list}) self.mock_novaclient.images = Mock( **{'list.return_value': self.image_list}) self.mock_novaclient.hypervisors = Mock( **{'list.return_value': self.hyp_list}) self.server_list = [] self.server_list.append( Server(name=randomword(), flavor_id=random.choice(self.flavor_list).id, image_id=random.choice(self.image_list).id, power_state=random.choice(power_states.keys()), metadata={'monitor': 'true'}, tenant_id=self.project_list[1].id)) # project node self.server_list.append( Server(name=randomword(), flavor_id=random.choice(self.flavor_list).id, image_id=random.choice(self.image_list).id, power_state=random.choice(power_states.keys()), metadata={}, tenant_id=self.project_list[0].id)) self.services_list = [Struct({'id': '1', 'host': 'myhost1'})] self.mock_novaclient.servers = Mock( **{'list.return_value': self.server_list}) self.mock_novaclient.services = Mock(**{ 'list.return_value': self.services_list, 'delete.return_value': 'Pass' }) self.mock_ksclient = MagicMock(spec=ksclient.Client) self.mock_ksclient.projects = Mock( **{'list.return_value': self.project_list}) self.mock_monasca_client = MagicMock(spec=msclient.Client) self.mock_monasca_client.metrics = Mock( **{ 'list_statistics.return_value': [{ "name": "some.metric", "statistics": [ ["2016-07-12T19:16:00.000Z", 31], ["2016-07-12T19:17:00.000Z", 31], ], "dimensions": {}, "columns": ["timestamp", "avg"], "id": "682bec4c92f43a52fd4f3a2855c2a026b27a063d" }], 'list_measurements.return_value': [{ "name": "some.metric", "measurements": [ ["2016-07-12T19:16:00.000Z", 10], ["2016-07-12T19:17:00.000Z", 10], ], "dimensions": {}, "columns": ["timestamp", "avg"], "id": "682bec4c92f43a52fd4f3a2855c2a026b27a063d" }] }) self.mock_get_endpoints = [{'region': None, 'url': None}]
def test_delete_of_missing_user(self): # DELETE preferences for a non-existent user should be ok reply = self.handle('DELETE', randomword()) self.assertEqual('complete', reply[api.STATUS])
def test_creation_from_dict(self): req1 = dict(target=util.randomword(), operation=util.randomword()) req2 = BllRequest(req1) req3 = BllRequest(req2) self.assertEquals(req2, req3)