From 956555f5ee072a019f7882f52923efd38df0713f Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Tue, 1 Oct 2019 11:15:19 +0100 Subject: [PATCH] tests: Correctly mock out security groups in NeutronFixture For our immediate purposes, this should always return something so we modify the mock to do just that. That requires some modifications to existing tests but is otherwise pretty simple. A future change will address the TODO contained within to properly mock the neutron API. Change-Id: Ibbee7fd11c1aa254e399d302adbae69126e98262 Signed-off-by: Stephen Finucane --- .../servers/v2.69/servers-details-resp.json | 7 ++++++- nova/tests/fixtures.py | 11 ++++++++++- .../servers/v2.69/servers-details-resp.json.tpl | 7 ++++++- nova/tests/functional/test_servers.py | 10 +++++----- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/doc/api_samples/servers/v2.69/servers-details-resp.json b/doc/api_samples/servers/v2.69/servers-details-resp.json index 251e16963e..458b7e846d 100644 --- a/doc/api_samples/servers/v2.69/servers-details-resp.json +++ b/doc/api_samples/servers/v2.69/servers-details-resp.json @@ -5,6 +5,11 @@ "id": "b6b0410f-b65f-4473-855e-5d82a71759e0", "status": "UNKNOWN", "tenant_id": "6f70656e737461636b20342065766572", + "security_groups": [ + { + "name": "default" + } + ], "links": [ { "href": "http://openstack.example.com/v2.1/6f70656e737461636b20342065766572/servers/b6b0410f-b65f-4473-855e-5d82a71759e0", @@ -17,4 +22,4 @@ ] } ] -} \ No newline at end of file +} diff --git a/nova/tests/fixtures.py b/nova/tests/fixtures.py index dfd77c2974..580679153a 100644 --- a/nova/tests/fixtures.py +++ b/nova/tests/fixtures.py @@ -1499,10 +1499,12 @@ class NeutronFixture(fixtures.Fixture): 'nova.network.neutronv2.api.API.remove_fixed_ip_from_instance', lambda *args, **kwargs: network_model.NetworkInfo.hydrate( NeutronFixture.nw_info)) + # TODO(stephenfin): This is a rubbish mock. We should instead mock the + # methods for the neutron client, like 'list_security_groups' self.test.stub_out( 'nova.network.security_group.neutron_driver.SecurityGroupAPI.' 'get_instances_security_groups_bindings', - lambda *args, **kwargs: {}) + self.fake_get_instance_security_group_bindings) # Stub out port binding APIs which go through a KSA client Adapter # rather than python-neutronclient. @@ -1532,6 +1534,13 @@ class NeutronFixture(fixtures.Fixture): # per port so we can reflect the status accurately. return fake_requests.FakeResponse(204) + @staticmethod + def fake_get_instance_security_group_bindings( + _, context, servers, detailed=False): + if detailed: + raise Exception('We do not support detailed view') + return {server['id']: [{'name': 'default'}] for server in servers} + def _get_first_id_match(self, id, list): filtered_list = [p for p in list if p['id'] == id] if len(filtered_list) > 0: diff --git a/nova/tests/functional/api_sample_tests/api_samples/servers/v2.69/servers-details-resp.json.tpl b/nova/tests/functional/api_sample_tests/api_samples/servers/v2.69/servers-details-resp.json.tpl index e634abc7d8..ca7dffc85d 100644 --- a/nova/tests/functional/api_sample_tests/api_samples/servers/v2.69/servers-details-resp.json.tpl +++ b/nova/tests/functional/api_sample_tests/api_samples/servers/v2.69/servers-details-resp.json.tpl @@ -5,6 +5,11 @@ "id": "%(uuid)s", "status": "UNKNOWN", "tenant_id": "6f70656e737461636b20342065766572", + "security_groups": [ + { + "name": "default" + } + ], "links": [ { "href": "%(versioned_compute_endpoint)s/servers/%(uuid)s", @@ -17,4 +22,4 @@ ] } ] -} \ No newline at end of file +} diff --git a/nova/tests/functional/test_servers.py b/nova/tests/functional/test_servers.py index 7b6310a215..f1ff6b540e 100644 --- a/nova/tests/functional/test_servers.py +++ b/nova/tests/functional/test_servers.py @@ -1261,9 +1261,9 @@ class ServerTestV269(ServersTestBase): # server is in the down cell. self.assertEqual('UNKNOWN', server['status']) self.assertIn(server['id'], self.down_cell_insts) - # the partial construct will have only 5 keys: - # created, tenant_id, status, id and links. - self.assertEqual(5, len(server)) + # the partial construct will have only 6 keys: + # created, tenant_id, security_groups, status, id and links. + self.assertEqual(6, len(server)) else: # server in up cell self.assertIn(server['id'], self.up_cell_insts) @@ -1367,8 +1367,8 @@ class ServerTestV269(ServersTestBase): if server['tenant_id'] != 'faker': self.assertIn(server['id'], self.down_cell_insts) # the partial construct will have only 5 keys: - # created, tenant_id, status, id and links - self.assertEqual(5, len(server)) + # created, tenant_id, security_groups, status, id and links + self.assertEqual(6, len(server)) else: # server in up cell if server['tenant_id'] != 'faker':