Merge "Add hide server address tests in test_serversV21.py"

This commit is contained in:
Zuul 2018-10-02 08:57:15 +00:00 committed by Gerrit Code Review
commit 4aaef75925
2 changed files with 100 additions and 138 deletions

View File

@ -1,137 +0,0 @@
# Copyright 2012 OpenStack Foundation
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_serialization import jsonutils
import six
from nova.compute import vm_states
from nova import exception
from nova import objects
from nova.objects import instance as instance_obj
from nova import test
from nova.tests.unit.api.openstack import fakes
from nova.tests.unit import fake_instance
SENTINEL = object()
def fake_compute_get(*args, **kwargs):
def _return_server(*_args, **_kwargs):
inst = fakes.stub_instance(*args, **kwargs)
return fake_instance.fake_instance_obj(_args[1], **inst)
return _return_server
class HideServerAddressesTestV21(test.TestCase):
content_type = 'application/json'
base_url = '/v2/fake/servers'
def _setup_wsgi(self):
self.wsgi_app = fakes.wsgi_app_v21()
def setUp(self):
super(HideServerAddressesTestV21, self).setUp()
fakes.stub_out_nw_api(self)
fakes.stub_out_secgroup_api(self)
return_server = fakes.fake_instance_get()
self.stub_out('nova.db.api.instance_get_by_uuid', return_server)
self._setup_wsgi()
def _make_request(self, url):
req = fakes.HTTPRequest.blank(url)
req.headers['Accept'] = self.content_type
res = req.get_response(self.wsgi_app)
return res
@staticmethod
def _get_server(body):
return jsonutils.loads(body).get('server')
@staticmethod
def _get_servers(body):
return jsonutils.loads(body).get('servers')
@staticmethod
def _get_addresses(server):
return server.get('addresses', SENTINEL)
def _check_addresses(self, addresses, exists):
self.assertIsNot(addresses, SENTINEL)
if exists:
self.assertTrue(addresses)
else:
self.assertFalse(addresses)
def test_show_hides_in_building(self):
instance_id = 1
uuid = fakes.get_fake_uuid(instance_id)
self.stub_out('nova.compute.api.API.get',
fake_compute_get(instance_id, uuid=uuid,
vm_state=vm_states.BUILDING))
res = self._make_request(self.base_url + '/%s' % uuid)
self.assertEqual(200, res.status_int)
server = self._get_server(res.body)
addresses = self._get_addresses(server)
self._check_addresses(addresses, exists=False)
def test_show(self):
instance_id = 1
uuid = fakes.get_fake_uuid(instance_id)
self.stub_out('nova.compute.api.API.get',
fake_compute_get(instance_id, uuid=uuid,
vm_state=vm_states.ACTIVE))
res = self._make_request(self.base_url + '/%s' % uuid)
self.assertEqual(200, res.status_int)
server = self._get_server(res.body)
addresses = self._get_addresses(server)
self._check_addresses(addresses, exists=True)
def test_detail_hides_building_server_addresses(self):
instance_0 = fakes.stub_instance(0, uuid=fakes.get_fake_uuid(0),
vm_state=vm_states.ACTIVE)
instance_1 = fakes.stub_instance(1, uuid=fakes.get_fake_uuid(1),
vm_state=vm_states.BUILDING)
instances = [instance_0, instance_1]
def get_all(*args, **kwargs):
fields = instance_obj.INSTANCE_DEFAULT_FIELDS
return instance_obj._make_instance_list(
args[1], objects.InstanceList(), instances, fields)
self.stub_out('nova.compute.api.API.get_all', get_all)
res = self._make_request(self.base_url + '/detail')
self.assertEqual(200, res.status_int)
servers = self._get_servers(res.body)
self.assertEqual(len(servers), len(instances))
for instance, server in six.moves.zip(instances, servers):
addresses = self._get_addresses(server)
exists = (instance['vm_state'] == vm_states.ACTIVE)
self._check_addresses(addresses, exists=exists)
def test_no_instance_passthrough_404(self):
def fake_compute_get(*args, **kwargs):
raise exception.InstanceNotFound(instance_id='fake')
self.stub_out('nova.compute.api.API.get', fake_compute_get)
res = self._make_request(self.base_url + '/' + fakes.get_fake_uuid())
self.assertEqual(404, res.status_int)

View File

@ -516,7 +516,7 @@ class ServersControllerTest(ControllerTest):
expected_attrs=['flavor', 'info_cache', 'metadata',
'numa_topology'])
def test_get_server_addresses_from_cache(self):
def _generate_nw_cache_info(self):
pub0 = ('172.19.0.1', '172.19.0.2',)
pub1 = ('1.2.3.4',)
pub2 = ('b33f::fdee:ddff:fecc:bbaa',)
@ -544,7 +544,10 @@ class ServersControllerTest(ControllerTest):
'label': 'private',
'subnets': [{'cidr': '192.168.0.0/24',
'ips': [_ip(ip) for ip in priv0]}]}}]
return nw_cache
def test_get_server_addresses_from_cache(self):
nw_cache = self._generate_nw_cache_info()
self.mock_get.side_effect = fakes.fake_compute_get(nw_cache=nw_cache,
availability_zone='nova')
@ -590,6 +593,102 @@ class ServersControllerTest(ControllerTest):
self.mock_get.assert_called_once_with(
req.environ['nova.context'], uuids.fake, expected_attrs=None)
def test_show_server_hide_addresses_in_building(self):
uuid = FAKE_UUID
self.mock_get.side_effect = fakes.fake_compute_get(
uuid=uuid, vm_state=vm_states.BUILDING)
req = self.req('/v2/fake/servers/%s' % uuid)
res_dict = self.controller.show(req, uuid)
self.assertEqual({}, res_dict['server']['addresses'])
def test_show_server_addresses_in_non_building(self):
uuid = FAKE_UUID
nw_cache = self._generate_nw_cache_info()
expected = {
'addresses': {
'private': [
{'version': 4, 'addr': '192.168.0.3',
'OS-EXT-IPS:type': 'fixed',
'OS-EXT-IPS-MAC:mac_addr': 'bb:bb:bb:bb:bb:bb'},
{'version': 4, 'addr': '192.168.0.4',
'OS-EXT-IPS:type': 'fixed',
'OS-EXT-IPS-MAC:mac_addr': 'bb:bb:bb:bb:bb:bb'},
],
'public': [
{'version': 4, 'addr': '172.19.0.1',
'OS-EXT-IPS:type': 'fixed',
'OS-EXT-IPS-MAC:mac_addr': 'aa:aa:aa:aa:aa:aa'},
{'version': 4, 'addr': '172.19.0.2',
'OS-EXT-IPS:type': 'fixed',
'OS-EXT-IPS-MAC:mac_addr': 'aa:aa:aa:aa:aa:aa'},
{'version': 4, 'addr': '1.2.3.4',
'OS-EXT-IPS:type': 'fixed',
'OS-EXT-IPS-MAC:mac_addr': 'aa:aa:aa:aa:aa:aa'},
{'version': 6, 'addr': 'b33f::fdee:ddff:fecc:bbaa',
'OS-EXT-IPS:type': 'fixed',
'OS-EXT-IPS-MAC:mac_addr': 'aa:aa:aa:aa:aa:aa'},
],
},
}
self.mock_get.side_effect = fakes.fake_compute_get(
nw_cache=nw_cache, uuid=uuid, vm_state=vm_states.ACTIVE)
req = self.req('/v2/fake/servers/%s' % uuid)
res_dict = self.controller.show(req, uuid)
self.assertThat(res_dict['server']['addresses'],
matchers.DictMatches(expected['addresses']))
def test_detail_server_hide_addresses(self):
nw_cache = self._generate_nw_cache_info()
expected = {
'addresses': {
'private': [
{'version': 4, 'addr': '192.168.0.3',
'OS-EXT-IPS:type': 'fixed',
'OS-EXT-IPS-MAC:mac_addr': 'bb:bb:bb:bb:bb:bb'},
{'version': 4, 'addr': '192.168.0.4',
'OS-EXT-IPS:type': 'fixed',
'OS-EXT-IPS-MAC:mac_addr': 'bb:bb:bb:bb:bb:bb'},
],
'public': [
{'version': 4, 'addr': '172.19.0.1',
'OS-EXT-IPS:type': 'fixed',
'OS-EXT-IPS-MAC:mac_addr': 'aa:aa:aa:aa:aa:aa'},
{'version': 4, 'addr': '172.19.0.2',
'OS-EXT-IPS:type': 'fixed',
'OS-EXT-IPS-MAC:mac_addr': 'aa:aa:aa:aa:aa:aa'},
{'version': 4, 'addr': '1.2.3.4',
'OS-EXT-IPS:type': 'fixed',
'OS-EXT-IPS-MAC:mac_addr': 'aa:aa:aa:aa:aa:aa'},
{'version': 6, 'addr': 'b33f::fdee:ddff:fecc:bbaa',
'OS-EXT-IPS:type': 'fixed',
'OS-EXT-IPS-MAC:mac_addr': 'aa:aa:aa:aa:aa:aa'},
],
},
}
def fake_get_all(context, **kwargs):
return objects.InstanceList(
objects=[fakes.stub_instance_obj(1,
vm_state=vm_states.BUILDING,
uuid=uuids.fake,
nw_cache=nw_cache),
fakes.stub_instance_obj(2,
vm_state=vm_states.ACTIVE,
uuid=uuids.fake2,
nw_cache=nw_cache)])
self.mock_get_all.side_effect = fake_get_all
req = self.req('/fake/servers?deleted=true',
use_admin_context=True)
servers = self.controller.detail(req)['servers']
for server in servers:
if server['OS-EXT-STS:vm_state'] == 'building':
self.assertEqual({}, server['addresses'])
else:
self.assertThat(server['addresses'],
matchers.DictMatches(expected['addresses']))
def test_get_server_list_empty(self):
self.mock_get_all.side_effect = None
self.mock_get_all.return_value = objects.InstanceList(objects=[])