class TestFloatingIPPool(base.TestCase): mock_pools = [ {'id': 'pool1_id', 'name': 'pool1'}, {'id': 'pool2_id', 'name': 'pool2'}] def setUp(self): super(TestFloatingIPPool, self).setUp() self.client = OpenStackCloud('cloud', {}) @patch.object(OpenStackCloud, '_has_nova_extension') @patch.object(OpenStackCloud, 'nova_client') def test_list_floating_ip_pools( self, mock_nova_client, mock__has_nova_extension): mock_nova_client.floating_ip_pools.list.return_value = [ FakeFloatingIPPool(**p) for p in self.mock_pools ] mock__has_nova_extension.return_value = True floating_ip_pools = self.client.list_floating_ip_pools() self.assertItemsEqual(floating_ip_pools, self.mock_pools) @patch.object(OpenStackCloud, '_has_nova_extension') @patch.object(OpenStackCloud, 'nova_client') def test_list_floating_ip_pools_exception( self, mock_nova_client, mock__has_nova_extension): mock_nova_client.floating_ip_pools.list.side_effect = \ Exception('whatever') mock__has_nova_extension.return_value = True self.assertRaises( OpenStackCloudException, self.client.list_floating_ip_pools)
class TestFloatingIPPool(base.TestCase): mock_pools = [{"id": "pool1_id", "name": "pool1"}, {"id": "pool2_id", "name": "pool2"}] def setUp(self): super(TestFloatingIPPool, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud(cloud_config=config.get_one_cloud()) @patch.object(OpenStackCloud, "_has_nova_extension") @patch.object(OpenStackCloud, "nova_client") def test_list_floating_ip_pools(self, mock_nova_client, mock__has_nova_extension): mock_nova_client.floating_ip_pools.list.return_value = [FakeFloatingIPPool(**p) for p in self.mock_pools] mock__has_nova_extension.return_value = True floating_ip_pools = self.client.list_floating_ip_pools() self.assertItemsEqual(floating_ip_pools, self.mock_pools) @patch.object(OpenStackCloud, "_has_nova_extension") @patch.object(OpenStackCloud, "nova_client") def test_list_floating_ip_pools_exception(self, mock_nova_client, mock__has_nova_extension): mock_nova_client.floating_ip_pools.list.side_effect = Exception("whatever") mock__has_nova_extension.return_value = True self.assertRaises(OpenStackCloudException, self.client.list_floating_ip_pools)
class TestShade(base.TestCase): def setUp(self): super(TestShade, self).setUp() self.cloud = OpenStackCloud("cloud", {}) @mock.patch.object(swift_client, "Connection") @mock.patch.object(shade.OpenStackCloud, "auth_token", new_callable=mock.PropertyMock) @mock.patch.object(shade.OpenStackCloud, "get_session_endpoint") def test_swift_client(self, endpoint_mock, auth_mock, swift_mock): endpoint_mock.return_value = "danzig" auth_mock.return_value = "yankee" self.cloud.swift_client swift_mock.assert_called_with( preauthurl="danzig", preauthtoken="yankee", auth_version="2", os_options=dict(object_storage_url="danzig", auth_token="yankee", region_name=""), ) @mock.patch.object(shade.OpenStackCloud, "auth_token", new_callable=mock.PropertyMock) @mock.patch.object(shade.OpenStackCloud, "get_session_endpoint") def test_swift_client_no_endpoint(self, endpoint_mock, auth_mock): endpoint_mock.side_effect = KeyError auth_mock.return_value = "quebec" e = self.assertRaises(exc.OpenStackCloudException, lambda: self.cloud.swift_client) self.assertIn("Error constructing swift client", str(e)) @mock.patch.object(shade.OpenStackCloud, "auth_token") @mock.patch.object(shade.OpenStackCloud, "get_session_endpoint") def test_swift_service(self, endpoint_mock, auth_mock): endpoint_mock.return_value = "slayer" auth_mock.return_value = "zulu" self.assertIsInstance(self.cloud.swift_service, swift_service.SwiftService) endpoint_mock.assert_called_with(service_key="object-store") @mock.patch.object(shade.OpenStackCloud, "get_session_endpoint") def test_swift_service_no_endpoint(self, endpoint_mock): endpoint_mock.side_effect = KeyError e = self.assertRaises(exc.OpenStackCloudException, lambda: self.cloud.swift_service) self.assertIn("Error constructing swift client", str(e)) @mock.patch.object(shade.OpenStackCloud, "swift_client") def test_get_object_segment_size(self, swift_mock): swift_mock.get_capabilities.return_value = {"swift": {"max_file_size": 1000}} self.assertEqual(900, self.cloud.get_object_segment_size(900)) self.assertEqual(1000, self.cloud.get_object_segment_size(1000)) self.assertEqual(1000, self.cloud.get_object_segment_size(1100))
class TestDeleteServer(base.TestCase): def setUp(self): super(TestDeleteServer, self).setUp() self.cloud = OpenStackCloud("cloud", {}) @mock.patch('shade.OpenStackCloud.nova_client') def test_delete_server(self, nova_mock): """ Test that novaclient server delete is called when wait=False """ server = mock.MagicMock(id='1234', status='ACTIVE') server.name = 'daffy' nova_mock.servers.list.return_value = [server] self.cloud.delete_server('daffy', wait=False) nova_mock.servers.delete.assert_called_with(server=server) @mock.patch('shade.OpenStackCloud.nova_client') def test_delete_server_already_gone(self, nova_mock): """ Test that we return immediately when server is already gone """ nova_mock.servers.list.return_value = [] self.cloud.delete_server('tweety', wait=False) self.assertFalse(nova_mock.servers.delete.called) @mock.patch('shade.OpenStackCloud.nova_client') def test_delete_server_already_gone_wait(self, nova_mock): self.cloud.delete_server('speedy', wait=True) self.assertFalse(nova_mock.servers.delete.called) @mock.patch('shade.OpenStackCloud.nova_client') def test_delete_server_wait_for_notfound(self, nova_mock): """ Test that delete_server waits for NotFound from novaclient """ server = mock.MagicMock(id='9999', status='ACTIVE') server.name = 'wily' nova_mock.servers.list.return_value = [server] def _delete_wily(*args, **kwargs): self.assertIn('server', kwargs) self.assertEqual('9999', kwargs['server'].id) nova_mock.servers.list.return_value = [] def _raise_notfound(*args, **kwargs): self.assertIn('server', kwargs) self.assertEqual('9999', kwargs['server'].id) raise nova_exc.NotFound(code='404') nova_mock.servers.get.side_effect = _raise_notfound nova_mock.servers.delete.side_effect = _delete_wily self.cloud.delete_server('wily', wait=True) nova_mock.servers.delete.assert_called_with(server=server)
def setUp(self): super(TestFloatingIP, self).setUp() # floating_ip_source='neutron' is default for OpenStackCloud() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud( cloud_config=config.get_one_cloud(validate=False)) self.fake_server = meta.obj_to_dict( fakes.FakeServer( 'server-id', '', 'ACTIVE', addresses={u'test_pnztt_net': [{ u'OS-EXT-IPS:type': u'fixed', u'addr': '192.0.2.129', u'version': 4, u'OS-EXT-IPS-MAC:mac_addr': u'fa:16:3e:ae:7d:42'}]})) self.floating_ip = _utils.normalize_neutron_floating_ips( self.mock_floating_ip_list_rep['floatingips'])[0]
def setUp(self): super(TestObject, self).setUp() config = os_client_config.OpenStackConfig() self.cloud = OpenStackCloud( cloud_config=config.get_one_cloud(validate=False))
class TestCreateVolumeSnapshot(base.TestCase): def setUp(self): super(TestCreateVolumeSnapshot, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud(cloud_config=config.get_one_cloud(validate=False)) @patch.object(OpenStackCloud, "cinder_client") def test_create_volume_snapshot_wait(self, mock_cinder): """ Test that create_volume_snapshot with a wait returns the volume snapshot when its status changes to "available". """ build_snapshot = fakes.FakeVolumeSnapshot("1234", "creating", "foo", "derpysnapshot") fake_snapshot = fakes.FakeVolumeSnapshot("1234", "available", "foo", "derpysnapshot") mock_cinder.volume_snapshots.create.return_value = build_snapshot mock_cinder.volume_snapshots.get.return_value = fake_snapshot mock_cinder.volume_snapshots.list.return_value = [build_snapshot, fake_snapshot] self.assertEqual( _utils.normalize_volumes([meta.obj_to_dict(fake_snapshot)])[0], self.client.create_volume_snapshot(volume_id="1234", wait=True), ) mock_cinder.volume_snapshots.create.assert_called_with(force=False, volume_id="1234") mock_cinder.volume_snapshots.get.assert_called_with(snapshot_id=meta.obj_to_dict(build_snapshot)["id"]) @patch.object(OpenStackCloud, "cinder_client") def test_create_volume_snapshot_with_timeout(self, mock_cinder): """ Test that a timeout while waiting for the volume snapshot to create raises an exception in create_volume_snapshot. """ build_snapshot = fakes.FakeVolumeSnapshot("1234", "creating", "foo", "derpysnapshot") mock_cinder.volume_snapshots.create.return_value = build_snapshot mock_cinder.volume_snapshots.get.return_value = build_snapshot mock_cinder.volume_snapshots.list.return_value = [build_snapshot] self.assertRaises( OpenStackCloudTimeout, self.client.create_volume_snapshot, volume_id="1234", wait=True, timeout=1 ) mock_cinder.volume_snapshots.create.assert_called_with(force=False, volume_id="1234") mock_cinder.volume_snapshots.get.assert_called_with(snapshot_id=meta.obj_to_dict(build_snapshot)["id"]) @patch.object(OpenStackCloud, "cinder_client") def test_create_volume_snapshot_with_error(self, mock_cinder): """ Test that a error status while waiting for the volume snapshot to create raises an exception in create_volume_snapshot. """ build_snapshot = fakes.FakeVolumeSnapshot("1234", "creating", "bar", "derpysnapshot") error_snapshot = fakes.FakeVolumeSnapshot("1234", "error", "blah", "derpysnapshot") mock_cinder.volume_snapshots.create.return_value = build_snapshot mock_cinder.volume_snapshots.get.return_value = error_snapshot mock_cinder.volume_snapshots.list.return_value = [error_snapshot] self.assertRaises( OpenStackCloudException, self.client.create_volume_snapshot, volume_id="1234", wait=True, timeout=5 ) mock_cinder.volume_snapshots.create.assert_called_with(force=False, volume_id="1234") mock_cinder.volume_snapshots.get.assert_called_with(snapshot_id=meta.obj_to_dict(build_snapshot)["id"])
class TestFloatingIP(base.TestCase): def setUp(self): super(TestFloatingIP, self).setUp() self.client = OpenStackCloud("cloud", {}) @patch.object(OpenStackCloud, 'get_floating_ip') @patch.object(OpenStackCloud, 'attach_ip_to_server') @patch.object(OpenStackCloud, 'available_floating_ip') def test_add_auto_ip( self, mock_available_floating_ip, mock_attach_ip_to_server, mock_get_floating_ip): server = FakeServer( id='server-id', name='test-server', status="ACTIVE", addresses={} ) server_dict = meta.obj_to_dict(server) mock_available_floating_ip.return_value = { "id": "this-is-a-floating-ip-id", "fixed_ip_address": None, "floating_ip_address": "203.0.113.29", "network": "this-is-a-net-or-pool-id", "attached": False, "status": "ACTIVE" } self.client.add_auto_ip(server=server_dict) mock_attach_ip_to_server.assert_called_with( timeout=60, wait=False, server_id='server-id', floating_ip_id='this-is-a-floating-ip-id') @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'add_ip_from_pool') def test_add_ips_to_server_pool( self, mock_add_ip_from_pool, mock_nova_client): server = FakeServer( id='server-id', name='test-server', status="ACTIVE", addresses={} ) server_dict = meta.obj_to_dict(server) pool = 'nova' mock_nova_client.servers.get.return_value = server self.client.add_ips_to_server(server_dict, ip_pool=pool) mock_add_ip_from_pool.assert_called_with(server_dict, pool) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'add_ip_list') def test_add_ips_to_server_ip_list( self, mock_add_ip_list, mock_nova_client): server = FakeServer( id='server-id', name='test-server', status="ACTIVE", addresses={} ) server_dict = meta.obj_to_dict(server) ips = ['203.0.113.29', '172.24.4.229'] mock_nova_client.servers.get.return_value = server self.client.add_ips_to_server(server_dict, ips=ips) mock_add_ip_list.assert_called_with(server_dict, ips) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'add_auto_ip') def test_add_ips_to_server_auto_ip( self, mock_add_auto_ip, mock_nova_client): server = FakeServer( id='server-id', name='test-server', status="ACTIVE", addresses={} ) server_dict = meta.obj_to_dict(server) mock_nova_client.servers.get.return_value = server self.client.add_ips_to_server(server_dict) mock_add_auto_ip.assert_called_with(server_dict)
class TestObject(base.TestCase): def setUp(self): super(TestObject, self).setUp() config = os_client_config.OpenStackConfig() self.cloud = OpenStackCloud(cloud_config=config.get_one_cloud( validate=False)) @mock.patch.object(swift_client, 'Connection') @mock.patch.object(shade.OpenStackCloud, 'keystone_session', new_callable=mock.PropertyMock) @mock.patch.object(shade.OpenStackCloud, 'get_session_endpoint') def test_swift_client(self, endpoint_mock, session_mock, swift_mock): endpoint_mock.return_value = 'danzig' session = mock.MagicMock() session.get_token = mock.MagicMock() session.get_token.return_value = 'yankee' session_mock.return_value = session self.cloud.swift_client swift_mock.assert_called_with(preauthurl='danzig', preauthtoken='yankee', auth_version=mock.ANY, timeout=None, os_options=dict( object_storage_url='danzig', auth_token='yankee', region_name='')) @mock.patch.object(shade.OpenStackCloud, 'keystone_session', new_callable=mock.PropertyMock) @mock.patch.object(shade.OpenStackCloud, 'get_session_endpoint') def test_swift_client_no_endpoint(self, endpoint_mock, session_mock): endpoint_mock.side_effect = KeyError e = self.assertRaises(exc.OpenStackCloudException, lambda: self.cloud.swift_client) self.assertIn('Error constructing swift client', str(e)) @mock.patch.object(shade.OpenStackCloud, 'auth_token') @mock.patch.object(shade.OpenStackCloud, 'get_session_endpoint') def test_swift_service(self, endpoint_mock, auth_mock): endpoint_mock.return_value = 'slayer' auth_mock.return_value = 'zulu' self.assertIsInstance(self.cloud.swift_service, swift_service.SwiftService) endpoint_mock.assert_called_with(service_key='object-store') @mock.patch.object(shade.OpenStackCloud, 'get_session_endpoint') def test_swift_service_no_endpoint(self, endpoint_mock): endpoint_mock.side_effect = KeyError e = self.assertRaises(exc.OpenStackCloudException, lambda: self.cloud.swift_service) self.assertIn('Error constructing swift client', str(e)) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_object_segment_size(self, swift_mock): swift_mock.get_capabilities.return_value = { 'swift': { 'max_file_size': 1000 } } self.assertEqual(900, self.cloud.get_object_segment_size(900)) self.assertEqual(1000, self.cloud.get_object_segment_size(1000)) self.assertEqual(1000, self.cloud.get_object_segment_size(1100)) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_object_segment_size_http_412(self, swift_mock): swift_mock.get_capabilities.side_effect = swift_exc.ClientException( "Precondition failed", http_status=412) self.assertEqual(shade.openstackcloud.DEFAULT_OBJECT_SEGMENT_SIZE, self.cloud.get_object_segment_size(None))
def setUp(self): super(TestFloatingIP, self).setUp() self.client = OpenStackCloud("cloud", {})
def setUp(self): super(TestPort, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud(cloud_config=config.get_one_cloud())
def setUp(self): super(TestPort, self).setUp() self.client = OpenStackCloud('cloud', {})
def setUp(self): super(TestFloatingIP, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud(cloud_config=config.get_one_cloud())
class TestFloatingIP(base.TestCase): mock_floating_ip_list_rep = [ { 'fixed_ip': None, 'id': 1, 'instance_id': None, 'ip': '203.0.113.1', 'pool': 'nova' }, { 'fixed_ip': None, 'id': 2, 'instance_id': None, 'ip': '203.0.113.2', 'pool': 'nova' }, { 'fixed_ip': '192.0.2.3', 'id': 29, 'instance_id': 'myself', 'ip': '198.51.100.29', 'pool': 'black_hole' } ] mock_floating_ip_pools = [ {'id': 'pool1_id', 'name': 'nova'}, {'id': 'pool2_id', 'name': 'pool2'}] def assertAreInstances(self, elements, elem_type): for e in elements: self.assertIsInstance(e, elem_type) def setUp(self): super(TestFloatingIP, self).setUp() self.client = OpenStackCloud("cloud", {}) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_list_floating_ips(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = [ FakeFloatingIP(**ip) for ip in self.mock_floating_ip_list_rep ] floating_ips = self.client.list_floating_ips() mock_nova_client.floating_ips.list.assert_called_with() self.assertIsInstance(floating_ips, list) self.assertEqual(3, len(floating_ips)) self.assertAreInstances(floating_ips, dict) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_search_floating_ips(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = [ FakeFloatingIP(**ip) for ip in self.mock_floating_ip_list_rep ] floating_ips = self.client.search_floating_ips( filters={'attached': False}) mock_nova_client.floating_ips.list.assert_called_with() self.assertIsInstance(floating_ips, list) self.assertEqual(2, len(floating_ips)) self.assertAreInstances(floating_ips, dict) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_get_floating_ip(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = [ FakeFloatingIP(**ip) for ip in self.mock_floating_ip_list_rep ] floating_ip = self.client.get_floating_ip(id='29') mock_nova_client.floating_ips.list.assert_called_with() self.assertIsInstance(floating_ip, dict) self.assertEqual('198.51.100.29', floating_ip['floating_ip_address']) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_get_floating_ip_not_found( self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = [ FakeFloatingIP(**ip) for ip in self.mock_floating_ip_list_rep ] floating_ip = self.client.get_floating_ip(id='666') self.assertIsNone(floating_ip) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_create_floating_ip(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.create.return_value = FakeFloatingIP( **self.mock_floating_ip_list_rep[1]) self.client.create_floating_ip(network='nova') mock_nova_client.floating_ips.create.assert_called_with(pool='nova') @patch.object(OpenStackCloud, '_nova_list_floating_ips') @patch.object(OpenStackCloud, 'has_service') def test_available_floating_ip_existing( self, mock_has_service, mock__nova_list_floating_ips): mock_has_service.side_effect = has_service_side_effect mock__nova_list_floating_ips.return_value = \ self.mock_floating_ip_list_rep[:1] ip = self.client.available_floating_ip(network='nova') self.assertEqual(self.mock_floating_ip_list_rep[0]['ip'], ip['floating_ip_address']) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, '_nova_list_floating_ips') @patch.object(OpenStackCloud, 'has_service') def test_available_floating_ip_new( self, mock_has_service, mock__nova_list_floating_ips, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock__nova_list_floating_ips.return_value = [] mock_nova_client.floating_ips.create.return_value = \ FakeFloatingIP(**self.mock_floating_ip_list_rep[0]) ip = self.client.available_floating_ip(network='nova') self.assertEqual(self.mock_floating_ip_list_rep[0]['ip'], ip['floating_ip_address']) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_delete_floating_ip_existing( self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.delete.return_value = None ret = self.client.delete_floating_ip( floating_ip_id='a-wild-id-appears') mock_nova_client.floating_ips.delete.assert_called_with( floating_ip='a-wild-id-appears') self.assertTrue(ret) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_delete_floating_ip_not_found( self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.delete.side_effect = n_exc.NotFound( code=404) ret = self.client.delete_floating_ip( floating_ip_id='a-wild-id-appears') self.assertFalse(ret) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_attach_ip_to_server(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = [ FakeFloatingIP(**ip) for ip in self.mock_floating_ip_list_rep ] self.client.attach_ip_to_server( server_id='server-id', floating_ip_id=1, fixed_address='192.0.2.129') mock_nova_client.servers.add_floating_ip.assert_called_with( server='server-id', address='203.0.113.1', fixed_address='192.0.2.129') @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_detach_ip_from_server(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = [ FakeFloatingIP(**ip) for ip in self.mock_floating_ip_list_rep ] self.client.detach_ip_from_server( server_id='server-id', floating_ip_id=1) mock_nova_client.servers.remove_floating_ip.assert_called_with( server='server-id', address='203.0.113.1') @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_add_ip_from_pool(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = [ FakeFloatingIP(**ip) for ip in self.mock_floating_ip_list_rep ] ip = self.client.add_ip_from_pool( server_id='server-id', network='nova', fixed_address='192.0.2.129') self.assertEqual('203.0.113.1', ip['floating_ip_address'])
class TestCreateServer(base.TestCase): def setUp(self): super(TestCreateServer, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud( cloud_config=config.get_one_cloud(validate=False)) self.client._SERVER_AGE = 0 def test_create_server_with_create_exception(self): """ Test that an exception in the novaclient create raises an exception in create_server. """ with patch("shade.OpenStackCloud"): config = { "servers.create.side_effect": Exception("exception"), } OpenStackCloud.nova_client = Mock(**config) self.assertRaises( OpenStackCloudException, self.client.create_server, 'server-name', 'image-id', 'flavor-id') def test_create_server_with_get_exception(self): """ Test that an exception when attempting to get the server instance via the novaclient raises an exception in create_server. """ with patch("shade.OpenStackCloud"): config = { "servers.create.return_value": Mock(status="BUILD"), "servers.get.side_effect": Exception("exception") } OpenStackCloud.nova_client = Mock(**config) self.assertRaises( OpenStackCloudException, self.client.create_server, 'server-name', 'image-id', 'flavor-id') def test_create_server_with_server_error(self): """ Test that a server error before we return or begin waiting for the server instance spawn raises an exception in create_server. """ build_server = fakes.FakeServer('1234', '', 'BUILD') error_server = fakes.FakeServer('1234', '', 'ERROR') with patch("shade.OpenStackCloud"): config = { "servers.create.return_value": build_server, "servers.get.return_value": error_server, } OpenStackCloud.nova_client = Mock(**config) self.assertRaises( OpenStackCloudException, self.client.create_server, 'server-name', 'image-id', 'flavor-id') def test_create_server_wait_server_error(self): """ Test that a server error while waiting for the server to spawn raises an exception in create_server. """ with patch("shade.OpenStackCloud"): build_server = fakes.FakeServer('1234', '', 'BUILD') error_server = fakes.FakeServer('1234', '', 'ERROR') config = { "servers.create.return_value": build_server, "servers.get.return_value": build_server, "servers.list.side_effect": [ [build_server], [error_server]] } OpenStackCloud.nova_client = Mock(**config) self.assertRaises( OpenStackCloudException, self.client.create_server, 'server-name', 'image-id', 'flavor-id', wait=True) def test_create_server_with_timeout(self): """ Test that a timeout while waiting for the server to spawn raises an exception in create_server. """ with patch("shade.OpenStackCloud"): fake_server = fakes.FakeServer('1234', '', 'BUILD') config = { "servers.create.return_value": fake_server, "servers.get.return_value": fake_server, "servers.list.return_value": [fake_server], } OpenStackCloud.nova_client = Mock(**config) self.assertRaises( OpenStackCloudTimeout, self.client.create_server, 'server-name', 'image-id', 'flavor-id', wait=True, timeout=1) def test_create_server_no_wait(self): """ Test that create_server with no wait and no exception in the novaclient create call returns the server instance. """ with patch("shade.OpenStackCloud"): fake_server = fakes.FakeServer('1234', '', 'BUILD') config = { "servers.create.return_value": fake_server, "servers.get.return_value": fake_server } OpenStackCloud.nova_client = Mock(**config) self.assertEqual( _utils.normalize_server( meta.obj_to_dict(fake_server), cloud_name=self.client.name, region_name=self.client.region_name), self.client.create_server( name='server-name', image='image=id', flavor='flavor-id')) def test_create_server_with_admin_pass_no_wait(self): """ Test that a server with an admin_pass passed returns the password """ with patch("shade.OpenStackCloud"): fake_server = fakes.FakeServer('1234', '', 'BUILD') fake_create_server = fakes.FakeServer('1234', '', 'BUILD', adminPass='******') config = { "servers.create.return_value": fake_create_server, "servers.get.return_value": fake_server } OpenStackCloud.nova_client = Mock(**config) self.assertEqual( _utils.normalize_server( meta.obj_to_dict(fake_create_server), cloud_name=self.client.name, region_name=self.client.region_name), self.client.create_server( name='server-name', image='image=id', flavor='flavor-id', admin_pass='******')) @patch.object(OpenStackCloud, "wait_for_server") @patch.object(OpenStackCloud, "nova_client") def test_create_server_with_admin_pass_wait(self, mock_nova, mock_wait): """ Test that a server with an admin_pass passed returns the password """ fake_server = fakes.FakeServer('1234', '', 'BUILD') fake_server_with_pass = fakes.FakeServer('1234', '', 'BUILD', adminPass='******') mock_nova.servers.create.return_value = fake_server mock_nova.servers.get.return_value = fake_server # The wait returns non-password server mock_wait.return_value = _utils.normalize_server( meta.obj_to_dict(fake_server), None, None) server = self.client.create_server( name='server-name', image='image-id', flavor='flavor-id', admin_pass='******', wait=True) # Assert that we did wait self.assertTrue(mock_wait.called) # Even with the wait, we should still get back a passworded server self.assertEqual( server, _utils.normalize_server(meta.obj_to_dict(fake_server_with_pass), None, None) ) @patch.object(OpenStackCloud, "get_active_server") @patch.object(OpenStackCloud, "get_server") def test_wait_for_server(self, mock_get_server, mock_get_active_server): """ Test that waiting for a server returns the server instance when its status changes to "ACTIVE". """ building_server = {'id': 'fake_server_id', 'status': 'BUILDING'} active_server = {'id': 'fake_server_id', 'status': 'ACTIVE'} mock_get_server.side_effect = iter([building_server, active_server]) mock_get_active_server.side_effect = iter([ building_server, active_server]) server = self.client.wait_for_server(building_server) self.assertEqual(2, mock_get_server.call_count) mock_get_server.assert_has_calls([ mock.call(building_server['id']), mock.call(active_server['id']), ]) self.assertEqual(2, mock_get_active_server.call_count) mock_get_active_server.assert_has_calls([ mock.call(server=building_server, reuse=True, auto_ip=True, ips=None, ip_pool=None, wait=True, timeout=mock.ANY), mock.call(server=active_server, reuse=True, auto_ip=True, ips=None, ip_pool=None, wait=True, timeout=mock.ANY), ]) self.assertEqual('ACTIVE', server['status']) @patch.object(OpenStackCloud, 'wait_for_server') @patch.object(OpenStackCloud, 'nova_client') def test_create_server_wait(self, mock_nova, mock_wait): """ Test that create_server with a wait actually does the wait. """ fake_server = {'id': 'fake_server_id', 'status': 'BUILDING'} mock_nova.servers.create.return_value = fake_server self.client.create_server( 'server-name', 'image-id', 'flavor-id', wait=True), mock_wait.assert_called_once_with( fake_server, auto_ip=True, ips=None, ip_pool=None, reuse=True, timeout=180 ) @patch('time.sleep') def test_create_server_no_addresses(self, mock_sleep): """ Test that create_server with a wait throws an exception if the server doesn't have addresses. """ with patch("shade.OpenStackCloud"): build_server = fakes.FakeServer('1234', '', 'BUILD') fake_server = fakes.FakeServer('1234', '', 'ACTIVE') config = { "servers.create.return_value": build_server, "servers.get.return_value": [build_server, None], "servers.list.side_effect": [ [build_server], [fake_server]], "servers.delete.return_value": None, } OpenStackCloud.nova_client = Mock(**config) self.client._SERVER_AGE = 0 with patch.object(OpenStackCloud, "add_ips_to_server", return_value=fake_server): self.assertRaises( OpenStackCloudException, self.client.create_server, 'server-name', 'image-id', 'flavor-id', wait=True) @patch('shade.OpenStackCloud.nova_client') @patch('shade.OpenStackCloud.get_network') def test_create_server_network_with_no_nics(self, mock_get_network, mock_nova): """ Verify that if 'network' is supplied, and 'nics' is not, that we attempt to get the network for the server. """ self.client.create_server('server-name', 'image-id', 'flavor-id', network='network-name') mock_get_network.assert_called_once_with(name_or_id='network-name') @patch('shade.OpenStackCloud.nova_client') @patch('shade.OpenStackCloud.get_network') def test_create_server_network_with_empty_nics(self, mock_get_network, mock_nova): """ Verify that if 'network' is supplied, along with an empty 'nics' list, it's treated the same as if 'nics' were not included. """ self.client.create_server('server-name', 'image-id', 'flavor-id', network='network-name', nics=[]) mock_get_network.assert_called_once_with(name_or_id='network-name')
class TestRebuildServer(base.TestCase): def setUp(self): super(TestRebuildServer, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud(cloud_config=config.get_one_cloud()) def test_rebuild_server_rebuild_exception(self): """ Test that an exception in the novaclient rebuild raises an exception in rebuild_server. """ with patch("shade.OpenStackCloud"): config = { "servers.rebuild.side_effect": Exception("exception"), } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudException, self.client.rebuild_server, "a", "b") def test_rebuild_server_server_error(self): """ Test that a server error while waiting for the server to rebuild raises an exception in rebuild_server. """ with patch("shade.OpenStackCloud"): config = { "servers.rebuild.return_value": Mock(status="REBUILD"), "servers.get.return_value": Mock(status="ERROR") } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudException, self.client.rebuild_server, "a", "b", wait=True) def test_rebuild_server_timeout(self): """ Test that a timeout while waiting for the server to rebuild raises an exception in rebuild_server. """ with patch("shade.OpenStackCloud"): config = { "servers.rebuild.return_value": Mock(status="REBUILD"), "servers.get.return_value": Mock(status="REBUILD") } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudTimeout, self.client.rebuild_server, "a", "b", wait=True, timeout=0.001) def test_rebuild_server_no_wait(self): """ Test that rebuild_server with no wait and no exception in the novaclient rebuild call returns the server instance. """ with patch("shade.OpenStackCloud"): mock_server = Mock(status="ACTIVE") config = {"servers.rebuild.return_value": mock_server} OpenStackCloud.nova_client = Mock(**config) self.assertEqual(meta.obj_to_dict(mock_server), self.client.rebuild_server("a", "b")) def test_rebuild_server_wait(self): """ Test that rebuild_server with a wait returns the server instance when its status changes to "ACTIVE". """ with patch("shade.OpenStackCloud"): mock_server = Mock(status="ACTIVE") config = { "servers.rebuild.return_value": Mock(status="REBUILD"), "servers.get.return_value": mock_server } OpenStackCloud.nova_client = Mock(**config) self.assertEqual(meta.obj_to_dict(mock_server), self.client.rebuild_server("a", "b", wait=True))
class TestFloatingIP(base.TestCase): mock_floating_ip_list_rep = { 'floatingips': [{ 'router_id': 'd23abc8d-2991-4a55-ba98-2aaea84cc72f', 'tenant_id': '4969c491a3c74ee4af974e6d800c62de', 'floating_network_id': '376da547-b977-4cfe-9cba-275c80debf57', 'fixed_ip_address': '192.0.2.29', 'floating_ip_address': '203.0.113.29', 'port_id': 'ce705c24-c1ef-408a-bda3-7bbd946164ab', 'id': '2f245a7b-796b-4f26-9cf9-9e82d248fda7', 'status': 'ACTIVE' }, { 'router_id': None, 'tenant_id': '4969c491a3c74ee4af974e6d800c62de', 'floating_network_id': '376da547-b977-4cfe-9cba-275c80debf57', 'fixed_ip_address': None, 'floating_ip_address': '203.0.113.30', 'port_id': None, 'id': '61cea855-49cb-4846-997d-801b70c71bdd', 'status': 'DOWN' }] } mock_floating_ip_new_rep = { 'floatingip': { 'fixed_ip_address': '10.0.0.4', 'floating_ip_address': '172.24.4.229', 'floating_network_id': 'my-network-id', 'id': '2f245a7b-796b-4f26-9cf9-9e82d248fda8', 'port_id': None, 'router_id': None, 'status': 'ACTIVE', 'tenant_id': '4969c491a3c74ee4af974e6d800c62df' } } mock_get_network_rep = { 'status': 'ACTIVE', 'subnets': ['54d6f61d-db07-451c-9ab3-b9609b6b6f0b'], 'name': 'my-network', 'provider:physical_network': None, 'admin_state_up': True, 'tenant_id': '4fd44f30292945e481c7b8a0c8908869', 'provider:network_type': 'local', 'router:external': True, 'shared': True, 'id': 'my-network-id', 'provider:segmentation_id': None } mock_search_ports_rep = [{ 'status': 'ACTIVE', 'binding:host_id': 'devstack', 'name': 'first-port', 'allowed_address_pairs': [], 'admin_state_up': True, 'network_id': '70c1db1f-b701-45bd-96e0-a313ee3430b3', 'tenant_id': '', 'extra_dhcp_opts': [], 'binding:vif_details': { 'port_filter': True, 'ovs_hybrid_plug': True }, 'binding:vif_type': 'ovs', 'device_owner': 'compute:None', 'mac_address': 'fa:16:3e:58:42:ed', 'binding:profile': {}, 'binding:vnic_type': 'normal', 'fixed_ips': [{ 'subnet_id': '008ba151-0b8c-4a67-98b5-0d2b87666062', 'ip_address': u'172.24.4.2' }], 'id': 'ce705c24-c1ef-408a-bda3-7bbd946164ac', 'security_groups': [], 'device_id': 'server_id' }] def assertAreInstances(self, elements, elem_type): for e in elements: self.assertIsInstance(e, elem_type) def setUp(self): super(TestFloatingIP, self).setUp() # floating_ip_source='neutron' is default for OpenStackCloud() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud(cloud_config=config.get_one_cloud( validate=False)) self.fake_server = meta.obj_to_dict( fakes.FakeServer('server-id', '', 'ACTIVE', addresses={ u'test_pnztt_net': [{ u'OS-EXT-IPS:type': u'fixed', u'addr': '192.0.2.129', u'version': 4, u'OS-EXT-IPS-MAC:mac_addr': u'fa:16:3e:ae:7d:42' }] })) self.floating_ip = _utils.normalize_neutron_floating_ips( self.mock_floating_ip_list_rep['floatingips'])[0] @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'has_service') def test_list_floating_ips(self, mock_has_service, mock_neutron_client): mock_has_service.return_value = True mock_neutron_client.list_floatingips.return_value = \ self.mock_floating_ip_list_rep floating_ips = self.client.list_floating_ips() mock_neutron_client.list_floatingips.assert_called_with() self.assertIsInstance(floating_ips, list) self.assertAreInstances(floating_ips, dict) self.assertEqual(2, len(floating_ips)) @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'has_service') def test_search_floating_ips(self, mock_has_service, mock_neutron_client): mock_has_service.return_value = True mock_neutron_client.list_floatingips.return_value = \ self.mock_floating_ip_list_rep floating_ips = self.client.search_floating_ips( filters={'attached': False}) mock_neutron_client.list_floatingips.assert_called_with() self.assertIsInstance(floating_ips, list) self.assertAreInstances(floating_ips, dict) self.assertEqual(1, len(floating_ips)) @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'has_service') def test_get_floating_ip(self, mock_has_service, mock_neutron_client): mock_has_service.return_value = True mock_neutron_client.list_floatingips.return_value = \ self.mock_floating_ip_list_rep floating_ip = self.client.get_floating_ip( id='2f245a7b-796b-4f26-9cf9-9e82d248fda7') mock_neutron_client.list_floatingips.assert_called_with() self.assertIsInstance(floating_ip, dict) self.assertEqual('203.0.113.29', floating_ip['floating_ip_address']) @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'has_service') def test_get_floating_ip_not_found(self, mock_has_service, mock_neutron_client): mock_has_service.return_value = True mock_neutron_client.list_floatingips.return_value = \ self.mock_floating_ip_list_rep floating_ip = self.client.get_floating_ip(id='non-existent') self.assertIsNone(floating_ip) @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'search_networks') @patch.object(OpenStackCloud, 'has_service') def test_create_floating_ip(self, mock_has_service, mock_search_networks, mock_neutron_client): mock_has_service.return_value = True mock_search_networks.return_value = [self.mock_get_network_rep] mock_neutron_client.create_floatingip.return_value = \ self.mock_floating_ip_new_rep ip = self.client.create_floating_ip(network='my-network') mock_neutron_client.create_floatingip.assert_called_with( body={'floatingip': { 'floating_network_id': 'my-network-id' }}) self.assertEqual( self.mock_floating_ip_new_rep['floatingip']['floating_ip_address'], ip['floating_ip_address']) @patch.object(OpenStackCloud, 'keystone_session') @patch.object(OpenStackCloud, '_neutron_list_floating_ips') @patch.object(OpenStackCloud, 'search_networks') @patch.object(OpenStackCloud, 'has_service') def test_available_floating_ip_existing(self, mock_has_service, mock_search_networks, mock__neutron_list_floating_ips, mock_keystone_session): mock_has_service.return_value = True mock_search_networks.return_value = [self.mock_get_network_rep] mock__neutron_list_floating_ips.return_value = \ [self.mock_floating_ip_new_rep['floatingip']] mock_keystone_session.get_project_id.return_value = \ '4969c491a3c74ee4af974e6d800c62df' ip = self.client.available_floating_ip(network='my-network') self.assertEqual( self.mock_floating_ip_new_rep['floatingip']['floating_ip_address'], ip['floating_ip_address']) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'keystone_session') @patch.object(OpenStackCloud, '_neutron_create_floating_ip') @patch.object(OpenStackCloud, '_attach_ip_to_server') @patch.object(OpenStackCloud, 'has_service') def test_auto_ip_pool_no_reuse(self, mock_has_service, mock_attach_ip_to_server, mock__neutron_create_floating_ip, mock_keystone_session, mock_nova_client): mock_has_service.return_value = True mock__neutron_create_floating_ip.return_value = \ self.mock_floating_ip_list_rep['floatingips'][0] mock_keystone_session.get_project_id.return_value = \ '4969c491a3c74ee4af974e6d800c62df' self.client.add_ips_to_server(dict(id='1234'), ip_pool='my-network', reuse=False) mock__neutron_create_floating_ip.assert_called_once_with( network_name_or_id='my-network', server=None) mock_attach_ip_to_server.assert_called_once_with( server={'id': '1234'}, fixed_address=None, floating_ip=self.floating_ip) @patch.object(OpenStackCloud, 'keystone_session') @patch.object(OpenStackCloud, '_neutron_create_floating_ip') @patch.object(OpenStackCloud, '_neutron_list_floating_ips') @patch.object(OpenStackCloud, 'search_networks') @patch.object(OpenStackCloud, 'has_service') def test_available_floating_ip_new(self, mock_has_service, mock_search_networks, mock__neutron_list_floating_ips, mock__neutron_create_floating_ip, mock_keystone_session): mock_has_service.return_value = True mock_search_networks.return_value = [self.mock_get_network_rep] mock__neutron_list_floating_ips.return_value = [] mock__neutron_create_floating_ip.return_value = \ self.mock_floating_ip_new_rep['floatingip'] mock_keystone_session.get_project_id.return_value = \ '4969c491a3c74ee4af974e6d800c62df' ip = self.client.available_floating_ip(network='my-network') self.assertEqual( self.mock_floating_ip_new_rep['floatingip']['floating_ip_address'], ip['floating_ip_address']) @patch.object(OpenStackCloud, 'get_floating_ip') @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'has_service') def test_delete_floating_ip_existing(self, mock_has_service, mock_neutron_client, mock_get_floating_ip): mock_has_service.return_value = True mock_get_floating_ip.return_value = { 'id': '2f245a7b-796b-4f26-9cf9-9e82d248fda7', } mock_neutron_client.delete_floatingip.return_value = None ret = self.client.delete_floating_ip( floating_ip_id='2f245a7b-796b-4f26-9cf9-9e82d248fda7') mock_neutron_client.delete_floatingip.assert_called_with( floatingip='2f245a7b-796b-4f26-9cf9-9e82d248fda7') self.assertTrue(ret) @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'has_service') def test_delete_floating_ip_not_found(self, mock_has_service, mock_neutron_client): mock_has_service.return_value = True mock_neutron_client.delete_floatingip.side_effect = \ n_exc.NotFound() ret = self.client.delete_floating_ip( floating_ip_id='a-wild-id-appears') self.assertFalse(ret) @patch.object(OpenStackCloud, 'search_ports') @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'has_service') def test_attach_ip_to_server(self, mock_has_service, mock_neutron_client, mock_search_ports): mock_has_service.return_value = True mock_search_ports.return_value = self.mock_search_ports_rep mock_neutron_client.list_floatingips.return_value = \ self.mock_floating_ip_list_rep self.client._attach_ip_to_server(server=self.fake_server, floating_ip=self.floating_ip) mock_neutron_client.update_floatingip.assert_called_with( floatingip=self.mock_floating_ip_list_rep['floatingips'][0]['id'], body={ 'floatingip': { 'port_id': self.mock_search_ports_rep[0]['id'], 'fixed_ip_address': self.mock_search_ports_rep[0]['fixed_ips'][0]['ip_address'] } }) @patch.object(OpenStackCloud, 'get_floating_ip') @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'has_service') def test_detach_ip_from_server(self, mock_has_service, mock_neutron_client, mock_get_floating_ip): mock_has_service.return_value = True mock_get_floating_ip.return_value = \ _utils.normalize_neutron_floating_ips( self.mock_floating_ip_list_rep['floatingips'])[0] self.client.detach_ip_from_server( server_id='server-id', floating_ip_id='2f245a7b-796b-4f26-9cf9-9e82d248fda7') mock_neutron_client.update_floatingip.assert_called_with( floatingip='2f245a7b-796b-4f26-9cf9-9e82d248fda7', body={'floatingip': { 'port_id': None }}) @patch.object(OpenStackCloud, '_attach_ip_to_server') @patch.object(OpenStackCloud, 'available_floating_ip') @patch.object(OpenStackCloud, 'has_service') def test_add_ip_from_pool(self, mock_has_service, mock_available_floating_ip, mock_attach_ip_to_server): mock_has_service.return_value = True mock_available_floating_ip.return_value = \ _utils.normalize_neutron_floating_ips([ self.mock_floating_ip_new_rep['floatingip']])[0] mock_attach_ip_to_server.return_value = None ip = self.client._add_ip_from_pool(server=self.fake_server, network='network-name', fixed_address='192.0.2.129') self.assertEqual( self.mock_floating_ip_new_rep['floatingip']['floating_ip_address'], ip['floating_ip_address'])
def setUp(self): super(TestFloatingIP, self).setUp() # floating_ip_source='neutron' is default for OpenStackCloud() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud(cloud_config=config.get_one_cloud())
def setUp(self): super(TestShade, self).setUp() self.cloud = OpenStackCloud('cloud', {})
def setUp(self): super(TestDeleteVolumeSnapshot, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud( cloud_config=config.get_one_cloud(validate=False))
def setUp(self): super(TestFloatingIP, self).setUp() self.client = OpenStackCloud("cloud", {})
class TestFloatingIP(base.TestCase): mock_floating_ip_list_rep = { 'floatingips': [ { 'router_id': 'd23abc8d-2991-4a55-ba98-2aaea84cc72f', 'tenant_id': '4969c491a3c74ee4af974e6d800c62de', 'floating_network_id': '376da547-b977-4cfe-9cba-275c80debf57', 'fixed_ip_address': '192.0.2.29', 'floating_ip_address': '203.0.113.29', 'port_id': 'ce705c24-c1ef-408a-bda3-7bbd946164ab', 'id': '2f245a7b-796b-4f26-9cf9-9e82d248fda7', 'status': 'ACTIVE' }, { 'router_id': None, 'tenant_id': '4969c491a3c74ee4af974e6d800c62de', 'floating_network_id': '376da547-b977-4cfe-9cba-275c80debf57', 'fixed_ip_address': None, 'floating_ip_address': '203.0.113.30', 'port_id': None, 'id': '61cea855-49cb-4846-997d-801b70c71bdd', 'status': 'DOWN' } ] } mock_floating_ip_new_rep = { 'floatingip': { 'fixed_ip_address': '10.0.0.4', 'floating_ip_address': '172.24.4.229', 'floating_network_id': 'my-network-id', 'id': '2f245a7b-796b-4f26-9cf9-9e82d248fda8', 'port_id': None, 'router_id': None, 'status': 'ACTIVE', 'tenant_id': '4969c491a3c74ee4af974e6d800c62df' } } mock_get_network_rep = { 'status': 'ACTIVE', 'subnets': [ '54d6f61d-db07-451c-9ab3-b9609b6b6f0b' ], 'name': 'my-network', 'provider:physical_network': None, 'admin_state_up': True, 'tenant_id': '4fd44f30292945e481c7b8a0c8908869', 'provider:network_type': 'local', 'router:external': True, 'shared': True, 'id': 'my-network-id', 'provider:segmentation_id': None } mock_search_ports_rep = [ { 'status': 'ACTIVE', 'binding:host_id': 'devstack', 'name': 'first-port', 'allowed_address_pairs': [], 'admin_state_up': True, 'network_id': '70c1db1f-b701-45bd-96e0-a313ee3430b3', 'tenant_id': '', 'extra_dhcp_opts': [], 'binding:vif_details': { 'port_filter': True, 'ovs_hybrid_plug': True }, 'binding:vif_type': 'ovs', 'device_owner': 'compute:None', 'mac_address': 'fa:16:3e:58:42:ed', 'binding:profile': {}, 'binding:vnic_type': 'normal', 'fixed_ips': [ { 'subnet_id': '008ba151-0b8c-4a67-98b5-0d2b87666062', 'ip_address': u'172.24.4.2' } ], 'id': 'ce705c24-c1ef-408a-bda3-7bbd946164ac', 'security_groups': [], 'device_id': 'server_id' } ] def assertAreInstances(self, elements, elem_type): for e in elements: self.assertIsInstance(e, elem_type) def setUp(self): super(TestFloatingIP, self).setUp() # floating_ip_source='neutron' is default for OpenStackCloud() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud( cloud_config=config.get_one_cloud(validate=False)) self.fake_server = meta.obj_to_dict( fakes.FakeServer( 'server-id', '', 'ACTIVE', addresses={u'test_pnztt_net': [{ u'OS-EXT-IPS:type': u'fixed', u'addr': '192.0.2.129', u'version': 4, u'OS-EXT-IPS-MAC:mac_addr': u'fa:16:3e:ae:7d:42'}]})) self.floating_ip = _utils.normalize_neutron_floating_ips( self.mock_floating_ip_list_rep['floatingips'])[0] @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'has_service') def test_list_floating_ips(self, mock_has_service, mock_neutron_client): mock_has_service.return_value = True mock_neutron_client.list_floatingips.return_value = \ self.mock_floating_ip_list_rep floating_ips = self.client.list_floating_ips() mock_neutron_client.list_floatingips.assert_called_with() self.assertIsInstance(floating_ips, list) self.assertAreInstances(floating_ips, dict) self.assertEqual(2, len(floating_ips)) @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'has_service') def test_search_floating_ips(self, mock_has_service, mock_neutron_client): mock_has_service.return_value = True mock_neutron_client.list_floatingips.return_value = \ self.mock_floating_ip_list_rep floating_ips = self.client.search_floating_ips( filters={'attached': False}) mock_neutron_client.list_floatingips.assert_called_with() self.assertIsInstance(floating_ips, list) self.assertAreInstances(floating_ips, dict) self.assertEqual(1, len(floating_ips)) @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'has_service') def test_get_floating_ip(self, mock_has_service, mock_neutron_client): mock_has_service.return_value = True mock_neutron_client.list_floatingips.return_value = \ self.mock_floating_ip_list_rep floating_ip = self.client.get_floating_ip( id='2f245a7b-796b-4f26-9cf9-9e82d248fda7') mock_neutron_client.list_floatingips.assert_called_with() self.assertIsInstance(floating_ip, dict) self.assertEqual('203.0.113.29', floating_ip['floating_ip_address']) @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'has_service') def test_get_floating_ip_not_found( self, mock_has_service, mock_neutron_client): mock_has_service.return_value = True mock_neutron_client.list_floatingips.return_value = \ self.mock_floating_ip_list_rep floating_ip = self.client.get_floating_ip(id='non-existent') self.assertIsNone(floating_ip) @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'search_networks') @patch.object(OpenStackCloud, 'has_service') def test_create_floating_ip( self, mock_has_service, mock_search_networks, mock_neutron_client): mock_has_service.return_value = True mock_search_networks.return_value = [self.mock_get_network_rep] mock_neutron_client.create_floatingip.return_value = \ self.mock_floating_ip_new_rep ip = self.client.create_floating_ip(network='my-network') mock_neutron_client.create_floatingip.assert_called_with( body={'floatingip': {'floating_network_id': 'my-network-id'}} ) self.assertEqual( self.mock_floating_ip_new_rep['floatingip']['floating_ip_address'], ip['floating_ip_address']) @patch.object(OpenStackCloud, 'keystone_session') @patch.object(OpenStackCloud, '_neutron_list_floating_ips') @patch.object(OpenStackCloud, 'search_networks') @patch.object(OpenStackCloud, 'has_service') def test_available_floating_ip_existing( self, mock_has_service, mock_search_networks, mock__neutron_list_floating_ips, mock_keystone_session): mock_has_service.return_value = True mock_search_networks.return_value = [self.mock_get_network_rep] mock__neutron_list_floating_ips.return_value = \ [self.mock_floating_ip_new_rep['floatingip']] mock_keystone_session.get_project_id.return_value = \ '4969c491a3c74ee4af974e6d800c62df' ip = self.client.available_floating_ip(network='my-network') self.assertEqual( self.mock_floating_ip_new_rep['floatingip']['floating_ip_address'], ip['floating_ip_address']) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'keystone_session') @patch.object(OpenStackCloud, '_neutron_create_floating_ip') @patch.object(OpenStackCloud, '_attach_ip_to_server') @patch.object(OpenStackCloud, 'has_service') def test_auto_ip_pool_no_reuse( self, mock_has_service, mock_attach_ip_to_server, mock__neutron_create_floating_ip, mock_keystone_session, mock_nova_client): mock_has_service.return_value = True mock__neutron_create_floating_ip.return_value = \ self.mock_floating_ip_list_rep['floatingips'][0] mock_keystone_session.get_project_id.return_value = \ '4969c491a3c74ee4af974e6d800c62df' self.client.add_ips_to_server( dict(id='1234'), ip_pool='my-network', reuse=False) mock__neutron_create_floating_ip.assert_called_once_with( network_name_or_id='my-network', server=None) mock_attach_ip_to_server.assert_called_once_with( server={'id': '1234'}, fixed_address=None, floating_ip=self.floating_ip, wait=False, timeout=60) @patch.object(OpenStackCloud, 'keystone_session') @patch.object(OpenStackCloud, '_neutron_create_floating_ip') @patch.object(OpenStackCloud, '_neutron_list_floating_ips') @patch.object(OpenStackCloud, 'search_networks') @patch.object(OpenStackCloud, 'has_service') def test_available_floating_ip_new( self, mock_has_service, mock_search_networks, mock__neutron_list_floating_ips, mock__neutron_create_floating_ip, mock_keystone_session): mock_has_service.return_value = True mock_search_networks.return_value = [self.mock_get_network_rep] mock__neutron_list_floating_ips.return_value = [] mock__neutron_create_floating_ip.return_value = \ self.mock_floating_ip_new_rep['floatingip'] mock_keystone_session.get_project_id.return_value = \ '4969c491a3c74ee4af974e6d800c62df' ip = self.client.available_floating_ip(network='my-network') self.assertEqual( self.mock_floating_ip_new_rep['floatingip']['floating_ip_address'], ip['floating_ip_address']) @patch.object(OpenStackCloud, 'get_floating_ip') @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'has_service') def test_delete_floating_ip_existing( self, mock_has_service, mock_neutron_client, mock_get_floating_ip): mock_has_service.return_value = True mock_get_floating_ip.return_value = { 'id': '2f245a7b-796b-4f26-9cf9-9e82d248fda7', } mock_neutron_client.delete_floatingip.return_value = None ret = self.client.delete_floating_ip( floating_ip_id='2f245a7b-796b-4f26-9cf9-9e82d248fda7') mock_neutron_client.delete_floatingip.assert_called_with( floatingip='2f245a7b-796b-4f26-9cf9-9e82d248fda7' ) self.assertTrue(ret) @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'has_service') def test_delete_floating_ip_not_found( self, mock_has_service, mock_neutron_client): mock_has_service.return_value = True mock_neutron_client.delete_floatingip.side_effect = \ n_exc.NotFound() ret = self.client.delete_floating_ip( floating_ip_id='a-wild-id-appears') self.assertFalse(ret) @patch.object(OpenStackCloud, 'search_ports') @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'has_service') def test_attach_ip_to_server( self, mock_has_service, mock_neutron_client, mock_search_ports): mock_has_service.return_value = True mock_search_ports.return_value = self.mock_search_ports_rep mock_neutron_client.list_floatingips.return_value = \ self.mock_floating_ip_list_rep self.client._attach_ip_to_server( server=self.fake_server, floating_ip=self.floating_ip) mock_neutron_client.update_floatingip.assert_called_with( floatingip=self.mock_floating_ip_list_rep['floatingips'][0]['id'], body={ 'floatingip': { 'port_id': self.mock_search_ports_rep[0]['id'], 'fixed_ip_address': self.mock_search_ports_rep[0][ 'fixed_ips'][0]['ip_address'] } } ) @patch.object(OpenStackCloud, 'get_floating_ip') @patch.object(OpenStackCloud, 'neutron_client') @patch.object(OpenStackCloud, 'has_service') def test_detach_ip_from_server( self, mock_has_service, mock_neutron_client, mock_get_floating_ip): mock_has_service.return_value = True mock_get_floating_ip.return_value = \ _utils.normalize_neutron_floating_ips( self.mock_floating_ip_list_rep['floatingips'])[0] self.client.detach_ip_from_server( server_id='server-id', floating_ip_id='2f245a7b-796b-4f26-9cf9-9e82d248fda7') mock_neutron_client.update_floatingip.assert_called_with( floatingip='2f245a7b-796b-4f26-9cf9-9e82d248fda7', body={ 'floatingip': { 'port_id': None } } ) @patch.object(OpenStackCloud, '_attach_ip_to_server') @patch.object(OpenStackCloud, 'available_floating_ip') @patch.object(OpenStackCloud, 'has_service') def test_add_ip_from_pool( self, mock_has_service, mock_available_floating_ip, mock_attach_ip_to_server): mock_has_service.return_value = True mock_available_floating_ip.return_value = \ _utils.normalize_neutron_floating_ips([ self.mock_floating_ip_new_rep['floatingip']])[0] mock_attach_ip_to_server.return_value = self.fake_server server = self.client._add_ip_from_pool( server=self.fake_server, network='network-name', fixed_address='192.0.2.129') self.assertEqual(server, self.fake_server)
class TestCreateVolumeSnapshot(base.TestCase): def setUp(self): super(TestCreateVolumeSnapshot, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud( cloud_config=config.get_one_cloud(validate=False)) @patch.object(OpenStackCloud, 'cinder_client') def test_create_volume_snapshot_wait(self, mock_cinder): """ Test that create_volume_snapshot with a wait returns the volume snapshot when its status changes to "available". """ build_snapshot = fakes.FakeVolumeSnapshot('1234', 'creating', 'foo', 'derpysnapshot') fake_snapshot = fakes.FakeVolumeSnapshot('1234', 'available', 'foo', 'derpysnapshot') mock_cinder.volume_snapshots.create.return_value = build_snapshot mock_cinder.volume_snapshots.get.return_value = fake_snapshot mock_cinder.volume_snapshots.list.return_value = [ build_snapshot, fake_snapshot] self.assertEqual( meta.obj_to_dict(fake_snapshot), self.client.create_volume_snapshot(volume_id='1234', wait=True) ) mock_cinder.volume_snapshots.create.assert_called_with( display_description=None, display_name=None, force=False, volume_id='1234' ) mock_cinder.volume_snapshots.get.assert_called_with( snapshot_id=meta.obj_to_dict(build_snapshot)['id'] ) @patch.object(OpenStackCloud, 'cinder_client') def test_create_volume_snapshot_with_timeout(self, mock_cinder): """ Test that a timeout while waiting for the volume snapshot to create raises an exception in create_volume_snapshot. """ build_snapshot = fakes.FakeVolumeSnapshot('1234', 'creating', 'foo', 'derpysnapshot') mock_cinder.volume_snapshots.create.return_value = build_snapshot mock_cinder.volume_snapshots.get.return_value = build_snapshot mock_cinder.volume_snapshots.list.return_value = [build_snapshot] self.assertRaises( OpenStackCloudTimeout, self.client.create_volume_snapshot, volume_id='1234', wait=True, timeout=1) mock_cinder.volume_snapshots.create.assert_called_with( display_description=None, display_name=None, force=False, volume_id='1234' ) mock_cinder.volume_snapshots.get.assert_called_with( snapshot_id=meta.obj_to_dict(build_snapshot)['id'] ) @patch.object(OpenStackCloud, 'cinder_client') def test_create_volume_snapshot_with_error(self, mock_cinder): """ Test that a error status while waiting for the volume snapshot to create raises an exception in create_volume_snapshot. """ build_snapshot = fakes.FakeVolumeSnapshot('1234', 'creating', 'bar', 'derpysnapshot') error_snapshot = fakes.FakeVolumeSnapshot('1234', 'error', 'blah', 'derpysnapshot') mock_cinder.volume_snapshots.create.return_value = build_snapshot mock_cinder.volume_snapshots.get.return_value = error_snapshot mock_cinder.volume_snapshots.list.return_value = [error_snapshot] self.assertRaises( OpenStackCloudException, self.client.create_volume_snapshot, volume_id='1234', wait=True, timeout=5) mock_cinder.volume_snapshots.create.assert_called_with( display_description=None, display_name=None, force=False, volume_id='1234' ) mock_cinder.volume_snapshots.get.assert_called_with( snapshot_id=meta.obj_to_dict(build_snapshot)['id'] )
class TestDeleteServer(base.TestCase): novaclient_exceptions = (nova_exc.BadRequest, nova_exc.Unauthorized, nova_exc.Forbidden, nova_exc.MethodNotAllowed, nova_exc.Conflict, nova_exc.OverLimit, nova_exc.RateLimit, nova_exc.HTTPNotImplemented) def setUp(self): super(TestDeleteServer, self).setUp() self.cloud = OpenStackCloud("cloud", {}) @mock.patch('shade.OpenStackCloud.nova_client') def test_delete_server(self, nova_mock): """ Test that novaclient server delete is called when wait=False """ server = fakes.FakeServer('1234', 'daffy', 'ACTIVE') nova_mock.servers.list.return_value = [server] self.cloud.delete_server('daffy', wait=False) nova_mock.servers.delete.assert_called_with(server=server.id) @mock.patch('shade.OpenStackCloud.nova_client') def test_delete_server_already_gone(self, nova_mock): """ Test that we return immediately when server is already gone """ nova_mock.servers.list.return_value = [] self.cloud.delete_server('tweety', wait=False) self.assertFalse(nova_mock.servers.delete.called) @mock.patch('shade.OpenStackCloud.nova_client') def test_delete_server_already_gone_wait(self, nova_mock): self.cloud.delete_server('speedy', wait=True) self.assertFalse(nova_mock.servers.delete.called) @mock.patch('shade.OpenStackCloud.nova_client') def test_delete_server_wait_for_notfound(self, nova_mock): """ Test that delete_server waits for NotFound from novaclient """ server = fakes.FakeServer('9999', 'wily', 'ACTIVE') nova_mock.servers.list.return_value = [server] def _delete_wily(*args, **kwargs): self.assertIn('server', kwargs) self.assertEqual('9999', kwargs['server']) nova_mock.servers.list.return_value = [] def _raise_notfound(*args, **kwargs): self.assertIn('server', kwargs) self.assertEqual('9999', kwargs['server']) raise nova_exc.NotFound(code='404') nova_mock.servers.get.side_effect = _raise_notfound nova_mock.servers.delete.side_effect = _delete_wily self.cloud.delete_server('wily', wait=True) nova_mock.servers.delete.assert_called_with(server=server.id) @mock.patch('shade.OpenStackCloud.nova_client') def test_delete_server_fails(self, nova_mock): """ Test that delete_server wraps novaclient exceptions """ nova_mock.servers.list.return_value = [fakes.FakeServer('1212', 'speedy', 'ACTIVE')] for fail in self.novaclient_exceptions: def _raise_fail(server): raise fail(code=fail.http_status) nova_mock.servers.delete.side_effect = _raise_fail exc = self.assertRaises(shade_exc.OpenStackCloudException, self.cloud.delete_server, 'speedy', wait=False) # Note that message is deprecated from Exception, but not in # the novaclient exceptions. self.assertIn(fail.message, str(exc)) @mock.patch('shade.OpenStackCloud.nova_client') def test_delete_server_get_fails(self, nova_mock): """ Test that delete_server wraps novaclient exceptions on wait fails """ nova_mock.servers.list.return_value = [fakes.FakeServer('2000', 'yosemite', 'ACTIVE')] for fail in self.novaclient_exceptions: def _raise_fail(server): raise fail(code=fail.http_status) nova_mock.servers.get.side_effect = _raise_fail exc = self.assertRaises(shade_exc.OpenStackCloudException, self.cloud.delete_server, 'yosemite', wait=True) # Note that message is deprecated from Exception, but not in # the novaclient exceptions. self.assertIn(fail.message, str(exc))
class TestRebuildServer(base.TestCase): def setUp(self): super(TestRebuildServer, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud(cloud_config=config.get_one_cloud( validate=False)) def test_rebuild_server_rebuild_exception(self): """ Test that an exception in the novaclient rebuild raises an exception in rebuild_server. """ with patch("shade.OpenStackCloud"): config = { "servers.rebuild.side_effect": Exception("exception"), } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudException, self.client.rebuild_server, "a", "b") def test_rebuild_server_server_error(self): """ Test that a server error while waiting for the server to rebuild raises an exception in rebuild_server. """ rebuild_server = fakes.FakeServer('1234', '', 'REBUILD') error_server = fakes.FakeServer('1234', '', 'ERROR') with patch("shade.OpenStackCloud"): config = { "servers.rebuild.return_value": rebuild_server, "servers.get.return_value": error_server, } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudException, self.client.rebuild_server, "a", "b", wait=True) def test_rebuild_server_timeout(self): """ Test that a timeout while waiting for the server to rebuild raises an exception in rebuild_server. """ rebuild_server = fakes.FakeServer('1234', '', 'REBUILD') with patch("shade.OpenStackCloud"): config = { "servers.rebuild.return_value": rebuild_server, "servers.get.return_value": rebuild_server, } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudTimeout, self.client.rebuild_server, "a", "b", wait=True, timeout=0.001) def test_rebuild_server_no_wait(self): """ Test that rebuild_server with no wait and no exception in the novaclient rebuild call returns the server instance. """ with patch("shade.OpenStackCloud"): rebuild_server = fakes.FakeServer('1234', '', 'REBUILD') config = {"servers.rebuild.return_value": rebuild_server} OpenStackCloud.nova_client = Mock(**config) self.assertEqual(meta.obj_to_dict(rebuild_server), self.client.rebuild_server("a", "b")) def test_rebuild_server_with_admin_pass_no_wait(self): """ Test that a server with an admin_pass passed returns the password """ with patch("shade.OpenStackCloud"): rebuild_server = fakes.FakeServer('1234', '', 'REBUILD', adminPass='******') config = { "servers.rebuild.return_value": rebuild_server, } OpenStackCloud.nova_client = Mock(**config) self.assertEqual( meta.obj_to_dict(rebuild_server), self.client.rebuild_server('a', 'b', admin_pass='******')) def test_rebuild_server_with_admin_pass_wait(self): """ Test that a server with an admin_pass passed returns the password """ with patch("shade.OpenStackCloud"): rebuild_server = fakes.FakeServer('1234', '', 'REBUILD', adminPass='******') active_server = fakes.FakeServer('1234', '', 'ACTIVE') ret_active_server = fakes.FakeServer('1234', '', 'ACTIVE', adminPass='******') config = { "servers.rebuild.return_value": rebuild_server, "servers.get.return_value": active_server, } OpenStackCloud.nova_client = Mock(**config) self.client.name = 'cloud-name' self.assertEqual( _utils.normalize_server(meta.obj_to_dict(ret_active_server), cloud_name='cloud-name', region_name=''), self.client.rebuild_server("a", "b", wait=True, admin_pass='******')) def test_rebuild_server_wait(self): """ Test that rebuild_server with a wait returns the server instance when its status changes to "ACTIVE". """ with patch("shade.OpenStackCloud"): rebuild_server = fakes.FakeServer('1234', '', 'REBUILD') active_server = fakes.FakeServer('1234', '', 'ACTIVE') config = { "servers.rebuild.return_value": rebuild_server, "servers.get.return_value": active_server } OpenStackCloud.nova_client = Mock(**config) self.client.name = 'cloud-name' self.assertEqual( _utils.normalize_server(meta.obj_to_dict(active_server), cloud_name='cloud-name', region_name=''), self.client.rebuild_server("a", "b", wait=True))
class TestRebuildServer(base.TestCase): def setUp(self): super(TestRebuildServer, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud( cloud_config=config.get_one_cloud(validate=False)) def test_rebuild_server_rebuild_exception(self): """ Test that an exception in the novaclient rebuild raises an exception in rebuild_server. """ with patch("shade.OpenStackCloud"): config = { "servers.rebuild.side_effect": Exception("exception"), } OpenStackCloud.nova_client = Mock(**config) self.assertRaises( OpenStackCloudException, self.client.rebuild_server, "a", "b") def test_rebuild_server_server_error(self): """ Test that a server error while waiting for the server to rebuild raises an exception in rebuild_server. """ rebuild_server = fakes.FakeServer('1234', '', 'REBUILD') error_server = fakes.FakeServer('1234', '', 'ERROR') with patch("shade.OpenStackCloud"): config = { "servers.rebuild.return_value": rebuild_server, "servers.get.return_value": error_server, } OpenStackCloud.nova_client = Mock(**config) self.assertRaises( OpenStackCloudException, self.client.rebuild_server, "a", "b", wait=True) def test_rebuild_server_timeout(self): """ Test that a timeout while waiting for the server to rebuild raises an exception in rebuild_server. """ rebuild_server = fakes.FakeServer('1234', '', 'REBUILD') with patch("shade.OpenStackCloud"): config = { "servers.rebuild.return_value": rebuild_server, "servers.get.return_value": rebuild_server, } OpenStackCloud.nova_client = Mock(**config) self.assertRaises( OpenStackCloudTimeout, self.client.rebuild_server, "a", "b", wait=True, timeout=0.001) def test_rebuild_server_no_wait(self): """ Test that rebuild_server with no wait and no exception in the novaclient rebuild call returns the server instance. """ with patch("shade.OpenStackCloud"): rebuild_server = fakes.FakeServer('1234', '', 'REBUILD') config = { "servers.rebuild.return_value": rebuild_server } OpenStackCloud.nova_client = Mock(**config) self.assertEqual(meta.obj_to_dict(rebuild_server), self.client.rebuild_server("a", "b")) def test_rebuild_server_with_admin_pass_no_wait(self): """ Test that a server with an admin_pass passed returns the password """ with patch("shade.OpenStackCloud"): rebuild_server = fakes.FakeServer('1234', '', 'REBUILD', adminPass='******') config = { "servers.rebuild.return_value": rebuild_server, } OpenStackCloud.nova_client = Mock(**config) self.assertEqual( meta.obj_to_dict(rebuild_server), self.client.rebuild_server('a', 'b', admin_pass='******')) def test_rebuild_server_with_admin_pass_wait(self): """ Test that a server with an admin_pass passed returns the password """ with patch("shade.OpenStackCloud"): rebuild_server = fakes.FakeServer('1234', '', 'REBUILD', adminPass='******') active_server = fakes.FakeServer('1234', '', 'ACTIVE') ret_active_server = fakes.FakeServer('1234', '', 'ACTIVE', adminPass='******') config = { "servers.rebuild.return_value": rebuild_server, "servers.get.return_value": active_server, } OpenStackCloud.nova_client = Mock(**config) self.client.name = 'cloud-name' self.assertEqual( _utils.normalize_server( meta.obj_to_dict(ret_active_server), cloud_name='cloud-name', region_name=''), self.client.rebuild_server("a", "b", wait=True, admin_pass='******')) def test_rebuild_server_wait(self): """ Test that rebuild_server with a wait returns the server instance when its status changes to "ACTIVE". """ with patch("shade.OpenStackCloud"): rebuild_server = fakes.FakeServer('1234', '', 'REBUILD') active_server = fakes.FakeServer('1234', '', 'ACTIVE') config = { "servers.rebuild.return_value": rebuild_server, "servers.get.return_value": active_server } OpenStackCloud.nova_client = Mock(**config) self.client.name = 'cloud-name' self.assertEqual( _utils.normalize_server( meta.obj_to_dict(active_server), cloud_name='cloud-name', region_name=''), self.client.rebuild_server("a", "b", wait=True))
def setUp(self): super(TestFloatingIP, self).setUp() # floating_ip_source='neutron' is default for OpenStackCloud() self.client = OpenStackCloud("cloud", {})
class TestFloatingIP(base.TestCase): mock_floating_ip_list_rep = [ { 'fixed_ip': None, 'id': 1, 'instance_id': None, 'ip': '203.0.113.1', 'pool': 'nova' }, { 'fixed_ip': None, 'id': 2, 'instance_id': None, 'ip': '203.0.113.2', 'pool': 'nova' }, { 'fixed_ip': '192.0.2.3', 'id': 29, 'instance_id': 'myself', 'ip': '198.51.100.29', 'pool': 'black_hole' } ] def assertAreInstances(self, elements, elem_type): for e in elements: self.assertIsInstance(e, elem_type) def setUp(self): super(TestFloatingIP, self).setUp() self.client = OpenStackCloud("cloud", {}) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_list_floating_ips(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = [ FakeFloatingIP(**ip) for ip in self.mock_floating_ip_list_rep ] floating_ips = self.client.list_floating_ips() mock_nova_client.floating_ips.list.assert_called_with() self.assertIsInstance(floating_ips, list) self.assertEqual(3, len(floating_ips)) self.assertAreInstances(floating_ips, dict) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_search_floating_ips(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = [ FakeFloatingIP(**ip) for ip in self.mock_floating_ip_list_rep ] floating_ips = self.client.search_floating_ips( filters={'attached': False}) mock_nova_client.floating_ips.list.assert_called_with() self.assertIsInstance(floating_ips, list) self.assertEqual(2, len(floating_ips)) self.assertAreInstances(floating_ips, dict) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_get_floating_ip(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = [ FakeFloatingIP(**ip) for ip in self.mock_floating_ip_list_rep ] floating_ip = self.client.get_floating_ip(id='29') mock_nova_client.floating_ips.list.assert_called_with() self.assertIsInstance(floating_ip, dict) self.assertEqual('198.51.100.29', floating_ip['floating_ip_address']) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_get_floating_ip_not_found( self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = [ FakeFloatingIP(**ip) for ip in self.mock_floating_ip_list_rep ] floating_ip = self.client.get_floating_ip(id='666') self.assertIsNone(floating_ip)
class TestDeleteServer(base.TestCase): novaclient_exceptions = ( nova_exc.BadRequest, nova_exc.Unauthorized, nova_exc.Forbidden, nova_exc.MethodNotAllowed, nova_exc.Conflict, nova_exc.OverLimit, nova_exc.RateLimit, nova_exc.HTTPNotImplemented, ) def setUp(self): super(TestDeleteServer, self).setUp() config = os_client_config.OpenStackConfig() self.cloud = OpenStackCloud(cloud_config=config.get_one_cloud(validate=False)) @mock.patch("shade.OpenStackCloud.nova_client") def test_delete_server(self, nova_mock): """ Test that novaclient server delete is called when wait=False """ server = fakes.FakeServer("1234", "daffy", "ACTIVE") nova_mock.servers.list.return_value = [server] self.assertTrue(self.cloud.delete_server("daffy", wait=False)) nova_mock.servers.delete.assert_called_with(server=server.id) @mock.patch("shade.OpenStackCloud.nova_client") def test_delete_server_already_gone(self, nova_mock): """ Test that we return immediately when server is already gone """ nova_mock.servers.list.return_value = [] self.assertFalse(self.cloud.delete_server("tweety", wait=False)) self.assertFalse(nova_mock.servers.delete.called) @mock.patch("shade.OpenStackCloud.nova_client") def test_delete_server_already_gone_wait(self, nova_mock): self.assertFalse(self.cloud.delete_server("speedy", wait=True)) self.assertFalse(nova_mock.servers.delete.called) @mock.patch("shade.OpenStackCloud.nova_client") def test_delete_server_wait_for_notfound(self, nova_mock): """ Test that delete_server waits for NotFound from novaclient """ server = fakes.FakeServer("9999", "wily", "ACTIVE") nova_mock.servers.list.return_value = [server] def _delete_wily(*args, **kwargs): self.assertIn("server", kwargs) self.assertEqual("9999", kwargs["server"]) nova_mock.servers.list.return_value = [] def _raise_notfound(*args, **kwargs): self.assertIn("server", kwargs) self.assertEqual("9999", kwargs["server"]) raise nova_exc.NotFound(code="404") nova_mock.servers.get.side_effect = _raise_notfound nova_mock.servers.delete.side_effect = _delete_wily self.assertTrue(self.cloud.delete_server("wily", wait=True)) nova_mock.servers.delete.assert_called_with(server=server.id) @mock.patch("shade.OpenStackCloud.nova_client") def test_delete_server_fails(self, nova_mock): """ Test that delete_server wraps novaclient exceptions """ nova_mock.servers.list.return_value = [fakes.FakeServer("1212", "speedy", "ACTIVE")] for fail in self.novaclient_exceptions: def _raise_fail(server): raise fail(code=fail.http_status) nova_mock.servers.delete.side_effect = _raise_fail exc = self.assertRaises(shade_exc.OpenStackCloudException, self.cloud.delete_server, "speedy", wait=False) # Note that message is deprecated from Exception, but not in # the novaclient exceptions. self.assertIn(fail.message, str(exc)) @mock.patch("shade.OpenStackCloud.nova_client") def test_delete_server_get_fails(self, nova_mock): """ Test that delete_server wraps novaclient exceptions on wait fails """ nova_mock.servers.list.return_value = [fakes.FakeServer("2000", "yosemite", "ACTIVE")] for fail in self.novaclient_exceptions: def _raise_fail(server): raise fail(code=fail.http_status) nova_mock.servers.get.side_effect = _raise_fail exc = self.assertRaises(shade_exc.OpenStackCloudException, self.cloud.delete_server, "yosemite", wait=True) # Note that message is deprecated from Exception, but not in # the novaclient exceptions. self.assertIn(fail.message, str(exc)) @mock.patch("shade.OpenStackCloud.get_volume") @mock.patch("shade.OpenStackCloud.nova_client") def test_delete_server_no_cinder(self, nova_mock, cinder_mock): """ Test that novaclient server delete is called when wait=False """ server = fakes.FakeServer("1234", "porky", "ACTIVE") nova_mock.servers.list.return_value = [server] with mock.patch("shade.OpenStackCloud.has_service", return_value=False): self.assertTrue(self.cloud.delete_server("porky", wait=False)) nova_mock.servers.delete.assert_called_with(server=server.id) self.assertFalse(cinder_mock.called)
class TestObject(base.TestCase): def setUp(self): super(TestObject, self).setUp() config = os_client_config.OpenStackConfig() self.cloud = OpenStackCloud(cloud_config=config.get_one_cloud( validate=False)) @mock.patch.object(cloud_config.CloudConfig, 'get_session') def test_swift_client_no_endpoint(self, get_session_mock): session_mock = mock.Mock() session_mock.get_endpoint.return_value = None get_session_mock.return_value = session_mock e = self.assertRaises(exc.OpenStackCloudException, lambda: self.cloud.swift_client) self.assertIn('Failed to instantiate object-store client.', str(e)) @mock.patch.object(shade.OpenStackCloud, 'auth_token') @mock.patch.object(shade.OpenStackCloud, 'get_session_endpoint') def test_swift_service(self, endpoint_mock, auth_mock): endpoint_mock.return_value = 'slayer' auth_mock.return_value = 'zulu' self.assertIsInstance(self.cloud.swift_service, swift_service.SwiftService) endpoint_mock.assert_called_with(service_key='object-store') @mock.patch.object(shade.OpenStackCloud, 'get_session_endpoint') def test_swift_service_no_endpoint(self, endpoint_mock): endpoint_mock.side_effect = KeyError e = self.assertRaises(exc.OpenStackCloudException, lambda: self.cloud.swift_service) self.assertIn('Error constructing swift client', str(e)) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_object_segment_size(self, swift_mock): swift_mock.get_capabilities.return_value = { 'swift': { 'max_file_size': 1000 } } self.assertEqual(900, self.cloud.get_object_segment_size(900)) self.assertEqual(1000, self.cloud.get_object_segment_size(1000)) self.assertEqual(1000, self.cloud.get_object_segment_size(1100)) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_object_segment_size_http_412(self, swift_mock): swift_mock.get_capabilities.side_effect = swift_exc.ClientException( "Precondition failed", http_status=412) self.assertEqual(shade.openstackcloud.DEFAULT_OBJECT_SEGMENT_SIZE, self.cloud.get_object_segment_size(None)) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_create_container(self, mock_swift): """Test creating a (private) container""" name = 'test_container' mock_swift.head_container.return_value = None self.cloud.create_container(name) expected_head_container_calls = [ # once for exist test mock.call(container=name), # once for the final return mock.call(container=name, skip_cache=True) ] self.assertTrue(expected_head_container_calls, mock_swift.head_container.call_args_list) mock_swift.put_container.assert_called_once_with(container=name) # Because the default is 'private', we shouldn't be calling update self.assertFalse(mock_swift.post_container.called) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_create_container_public(self, mock_swift): """Test creating a public container""" name = 'test_container' mock_swift.head_container.return_value = None self.cloud.create_container(name, public=True) expected_head_container_calls = [ # once for exist test mock.call(container=name), # once for the final return mock.call(container=name, skip_cache=True) ] self.assertTrue(expected_head_container_calls, mock_swift.head_container.call_args_list) mock_swift.put_container.assert_called_once_with(container=name) mock_swift.post_container.assert_called_once_with( container=name, headers={ 'x-container-read': shade.openstackcloud.OBJECT_CONTAINER_ACLS['public'] }) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_create_container_exists(self, mock_swift): """Test creating a container that already exists""" name = 'test_container' fake_container = dict(id='1', name='name') mock_swift.head_container.return_value = fake_container container = self.cloud.create_container(name) mock_swift.head_container.assert_called_once_with(container=name) self.assertEqual(fake_container, container) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_delete_container(self, mock_swift): name = 'test_container' self.cloud.delete_container(name) mock_swift.delete_container.assert_called_once_with(container=name) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_delete_container_404(self, mock_swift): """No exception when deleting a container that does not exist""" name = 'test_container' mock_swift.delete_container.side_effect = swift_exc.ClientException( 'ERROR', http_status=404) self.cloud.delete_container(name) mock_swift.delete_container.assert_called_once_with(container=name) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_delete_container_error(self, mock_swift): """Non-404 swift error re-raised as OSCE""" mock_swift.delete_container.side_effect = swift_exc.ClientException( 'ERROR') self.assertRaises(shade.OpenStackCloudException, self.cloud.delete_container, '') @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_update_container(self, mock_swift): name = 'test_container' headers = { 'x-container-read': shade.openstackcloud.OBJECT_CONTAINER_ACLS['public'] } self.cloud.update_container(name, headers) mock_swift.post_container.assert_called_once_with(container=name, headers=headers) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_update_container_error(self, mock_swift): """Swift error re-raised as OSCE""" mock_swift.post_container.side_effect = swift_exc.ClientException( 'ERROR') self.assertRaises(shade.OpenStackCloudException, self.cloud.update_container, '', '') @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_set_container_access_public(self, mock_swift): name = 'test_container' self.cloud.set_container_access(name, 'public') mock_swift.post_container.assert_called_once_with( container=name, headers={ 'x-container-read': shade.openstackcloud.OBJECT_CONTAINER_ACLS['public'] }) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_set_container_access_private(self, mock_swift): name = 'test_container' self.cloud.set_container_access(name, 'private') mock_swift.post_container.assert_called_once_with( container=name, headers={ 'x-container-read': shade.openstackcloud.OBJECT_CONTAINER_ACLS['private'] }) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_set_container_access_invalid(self, mock_swift): self.assertRaises(shade.OpenStackCloudException, self.cloud.set_container_access, '', 'invalid') @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_container(self, mock_swift): fake_container = { 'x-container-read': shade.openstackcloud.OBJECT_CONTAINER_ACLS['public'] } mock_swift.head_container.return_value = fake_container access = self.cloud.get_container_access('foo') self.assertEqual('public', access) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_container_invalid(self, mock_swift): fake_container = {'x-container-read': 'invalid'} mock_swift.head_container.return_value = fake_container with testtools.ExpectedException( exc.OpenStackCloudException, "Could not determine container access for ACL: invalid"): self.cloud.get_container_access('foo') @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_container_access_not_found(self, mock_swift): name = 'invalid_container' mock_swift.head_container.return_value = None with testtools.ExpectedException(exc.OpenStackCloudException, "Container not found: %s" % name): self.cloud.get_container_access(name) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_list_containers(self, mock_swift): containers = [dict(id='1', name='containter1')] mock_swift.get_account.return_value = ('response_headers', containers) ret = self.cloud.list_containers() mock_swift.get_account.assert_called_once_with(full_listing=True) self.assertEqual(containers, ret) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_list_containers_not_full(self, mock_swift): containers = [dict(id='1', name='containter1')] mock_swift.get_account.return_value = ('response_headers', containers) ret = self.cloud.list_containers(full_listing=False) mock_swift.get_account.assert_called_once_with(full_listing=False) self.assertEqual(containers, ret) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_list_containers_exception(self, mock_swift): mock_swift.get_account.side_effect = swift_exc.ClientException("ERROR") self.assertRaises(exc.OpenStackCloudException, self.cloud.list_containers) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_list_objects(self, mock_swift): objects = [dict(id='1', name='object1')] mock_swift.get_container.return_value = ('response_headers', objects) ret = self.cloud.list_objects('container_name') mock_swift.get_container.assert_called_once_with( container='container_name', full_listing=True) self.assertEqual(objects, ret) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_list_objects_not_full(self, mock_swift): objects = [dict(id='1', name='object1')] mock_swift.get_container.return_value = ('response_headers', objects) ret = self.cloud.list_objects('container_name', full_listing=False) mock_swift.get_container.assert_called_once_with( container='container_name', full_listing=False) self.assertEqual(objects, ret) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_list_objects_exception(self, mock_swift): mock_swift.get_container.side_effect = swift_exc.ClientException( "ERROR") self.assertRaises(exc.OpenStackCloudException, self.cloud.list_objects, 'container_name') @mock.patch.object(shade.OpenStackCloud, 'get_object_metadata') @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_delete_object(self, mock_swift, mock_get_meta): container_name = 'container_name' object_name = 'object_name' mock_get_meta.return_value = {'object': object_name} self.assertTrue(self.cloud.delete_object(container_name, object_name)) mock_get_meta.assert_called_once_with(container_name, object_name) mock_swift.delete_object.assert_called_once_with( container=container_name, obj=object_name) @mock.patch.object(shade.OpenStackCloud, 'get_object_metadata') @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_delete_object_not_found(self, mock_swift, mock_get_meta): container_name = 'container_name' object_name = 'object_name' mock_get_meta.return_value = None self.assertFalse(self.cloud.delete_object(container_name, object_name)) mock_get_meta.assert_called_once_with(container_name, object_name) self.assertFalse(mock_swift.delete_object.called) @mock.patch.object(shade.OpenStackCloud, 'get_object_metadata') @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_delete_object_exception(self, mock_swift, mock_get_meta): container_name = 'container_name' object_name = 'object_name' mock_get_meta.return_value = {'object': object_name} mock_swift.delete_object.side_effect = swift_exc.ClientException( "ERROR") self.assertRaises(shade.OpenStackCloudException, self.cloud.delete_object, container_name, object_name) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_object(self, mock_swift): fake_resp = ({'headers': 'yup'}, 'test body') mock_swift.get_object.return_value = fake_resp container_name = 'container_name' object_name = 'object_name' resp = self.cloud.get_object(container_name, object_name) self.assertEqual(fake_resp, resp) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_object_not_found(self, mock_swift): mock_swift.get_object.side_effect = swift_exc.ClientException( 'ERROR', http_status=404) container_name = 'container_name' object_name = 'object_name' self.assertIsNone(self.cloud.get_object(container_name, object_name)) mock_swift.get_object.assert_called_once_with(container=container_name, obj=object_name, query_string=None, resp_chunk_size=None) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_object_exception(self, mock_swift): mock_swift.get_object.side_effect = swift_exc.ClientException("ERROR") container_name = 'container_name' object_name = 'object_name' self.assertRaises(shade.OpenStackCloudException, self.cloud.get_object, container_name, object_name)
def setUp(self): super(TestRebuildServer, self).setUp() self.client = OpenStackCloud("cloud", {})
class TestDeleteServer(base.TestCase): novaclient_exceptions = (nova_exc.BadRequest, nova_exc.Unauthorized, nova_exc.Forbidden, nova_exc.MethodNotAllowed, nova_exc.Conflict, nova_exc.OverLimit, nova_exc.RateLimit, nova_exc.HTTPNotImplemented) def setUp(self): super(TestDeleteServer, self).setUp() config = os_client_config.OpenStackConfig() self.cloud = OpenStackCloud(cloud_config=config.get_one_cloud()) @mock.patch('shade.OpenStackCloud.nova_client') def test_delete_server(self, nova_mock): """ Test that novaclient server delete is called when wait=False """ server = fakes.FakeServer('1234', 'daffy', 'ACTIVE') nova_mock.servers.list.return_value = [server] self.cloud.delete_server('daffy', wait=False) nova_mock.servers.delete.assert_called_with(server=server.id) @mock.patch('shade.OpenStackCloud.nova_client') def test_delete_server_already_gone(self, nova_mock): """ Test that we return immediately when server is already gone """ nova_mock.servers.list.return_value = [] self.cloud.delete_server('tweety', wait=False) self.assertFalse(nova_mock.servers.delete.called) @mock.patch('shade.OpenStackCloud.nova_client') def test_delete_server_already_gone_wait(self, nova_mock): self.cloud.delete_server('speedy', wait=True) self.assertFalse(nova_mock.servers.delete.called) @mock.patch('shade.OpenStackCloud.nova_client') def test_delete_server_wait_for_notfound(self, nova_mock): """ Test that delete_server waits for NotFound from novaclient """ server = fakes.FakeServer('9999', 'wily', 'ACTIVE') nova_mock.servers.list.return_value = [server] def _delete_wily(*args, **kwargs): self.assertIn('server', kwargs) self.assertEqual('9999', kwargs['server']) nova_mock.servers.list.return_value = [] def _raise_notfound(*args, **kwargs): self.assertIn('server', kwargs) self.assertEqual('9999', kwargs['server']) raise nova_exc.NotFound(code='404') nova_mock.servers.get.side_effect = _raise_notfound nova_mock.servers.delete.side_effect = _delete_wily self.cloud.delete_server('wily', wait=True) nova_mock.servers.delete.assert_called_with(server=server.id) @mock.patch('shade.OpenStackCloud.nova_client') def test_delete_server_fails(self, nova_mock): """ Test that delete_server wraps novaclient exceptions """ nova_mock.servers.list.return_value = [ fakes.FakeServer('1212', 'speedy', 'ACTIVE') ] for fail in self.novaclient_exceptions: def _raise_fail(server): raise fail(code=fail.http_status) nova_mock.servers.delete.side_effect = _raise_fail exc = self.assertRaises(shade_exc.OpenStackCloudException, self.cloud.delete_server, 'speedy', wait=False) # Note that message is deprecated from Exception, but not in # the novaclient exceptions. self.assertIn(fail.message, str(exc)) @mock.patch('shade.OpenStackCloud.nova_client') def test_delete_server_get_fails(self, nova_mock): """ Test that delete_server wraps novaclient exceptions on wait fails """ nova_mock.servers.list.return_value = [ fakes.FakeServer('2000', 'yosemite', 'ACTIVE') ] for fail in self.novaclient_exceptions: def _raise_fail(server): raise fail(code=fail.http_status) nova_mock.servers.get.side_effect = _raise_fail exc = self.assertRaises(shade_exc.OpenStackCloudException, self.cloud.delete_server, 'yosemite', wait=True) # Note that message is deprecated from Exception, but not in # the novaclient exceptions. self.assertIn(fail.message, str(exc))
def setUp(self): super(TestDeleteServer, self).setUp() self.cloud = OpenStackCloud("cloud", {})
def setUp(self): super(TestFloatingIPPool, self).setUp() self.client = OpenStackCloud('cloud', {})
def setUp(self): super(TestDeleteServer, self).setUp() config = os_client_config.OpenStackConfig() self.cloud = OpenStackCloud(cloud_config=config.get_one_cloud())
class TestFloatingIP(base.TestCase): def setUp(self): super(TestFloatingIP, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud(cloud_config=config.get_one_cloud(validate=False)) @patch.object(OpenStackCloud, "get_floating_ip") @patch.object(OpenStackCloud, "_attach_ip_to_server") @patch.object(OpenStackCloud, "available_floating_ip") def test_add_auto_ip(self, mock_available_floating_ip, mock_attach_ip_to_server, mock_get_floating_ip): server = FakeServer(id="server-id", name="test-server", status="ACTIVE", addresses={}) server_dict = meta.obj_to_dict(server) floating_ip_dict = { "id": "this-is-a-floating-ip-id", "fixed_ip_address": None, "floating_ip_address": "203.0.113.29", "network": "this-is-a-net-or-pool-id", "attached": False, "status": "ACTIVE", } mock_available_floating_ip.return_value = floating_ip_dict self.client.add_auto_ip(server=server_dict) mock_attach_ip_to_server.assert_called_with( timeout=60, wait=False, server=server_dict, floating_ip=floating_ip_dict, skip_attach=False ) @patch.object(OpenStackCloud, "nova_client") @patch.object(OpenStackCloud, "_add_ip_from_pool") def test_add_ips_to_server_pool(self, mock_add_ip_from_pool, mock_nova_client): server = FakeServer(id="romeo", name="test-server", status="ACTIVE", addresses={}) server_dict = meta.obj_to_dict(server) pool = "nova" mock_nova_client.servers.get.return_value = server self.client.add_ips_to_server(server_dict, ip_pool=pool) mock_add_ip_from_pool.assert_called_with(server_dict, pool, reuse=True) @patch.object(OpenStackCloud, "nova_client") @patch.object(OpenStackCloud, "add_ip_list") def test_add_ips_to_server_ip_list(self, mock_add_ip_list, mock_nova_client): server = FakeServer(id="server-id", name="test-server", status="ACTIVE", addresses={}) server_dict = meta.obj_to_dict(server) ips = ["203.0.113.29", "172.24.4.229"] mock_nova_client.servers.get.return_value = server self.client.add_ips_to_server(server_dict, ips=ips) mock_add_ip_list.assert_called_with(server_dict, ips) @patch.object(OpenStackCloud, "nova_client") @patch.object(OpenStackCloud, "add_auto_ip") def test_add_ips_to_server_auto_ip(self, mock_add_auto_ip, mock_nova_client): server = FakeServer(id="server-id", name="test-server", status="ACTIVE", addresses={}) server_dict = meta.obj_to_dict(server) mock_nova_client.servers.get.return_value = server self.client.add_ips_to_server(server_dict) mock_add_auto_ip.assert_called_with(server_dict, wait=False, timeout=60, reuse=True)
class TestFloatingIP(base.TestCase): mock_floating_ip_list_rep = [{ 'fixed_ip': None, 'id': 1, 'instance_id': None, 'ip': '203.0.113.1', 'pool': 'nova' }, { 'fixed_ip': None, 'id': 2, 'instance_id': None, 'ip': '203.0.113.2', 'pool': 'nova' }, { 'fixed_ip': '192.0.2.3', 'id': 29, 'instance_id': 'myself', 'ip': '198.51.100.29', 'pool': 'black_hole' }] mock_floating_ip_pools = [{ 'id': 'pool1_id', 'name': 'nova' }, { 'id': 'pool2_id', 'name': 'pool2' }] def assertAreInstances(self, elements, elem_type): for e in elements: self.assertIsInstance(e, elem_type) def setUp(self): super(TestFloatingIP, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud(cloud_config=config.get_one_cloud( validate=False)) self.floating_ips = [ fakes.FakeFloatingIP(**ip) for ip in self.mock_floating_ip_list_rep ] self.fake_server = meta.obj_to_dict( fakes.FakeServer('server-id', '', 'ACTIVE', addresses={ u'test_pnztt_net': [{ u'OS-EXT-IPS:type': u'fixed', u'addr': '192.0.2.129', u'version': 4, u'OS-EXT-IPS-MAC:mac_addr': u'fa:16:3e:ae:7d:42' }] })) self.floating_ip = _utils.normalize_nova_floating_ips( meta.obj_list_to_dict(self.floating_ips))[0] @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_list_floating_ips(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = self.floating_ips floating_ips = self.client.list_floating_ips() mock_nova_client.floating_ips.list.assert_called_with() self.assertIsInstance(floating_ips, list) self.assertEqual(3, len(floating_ips)) self.assertAreInstances(floating_ips, dict) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_search_floating_ips(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = self.floating_ips floating_ips = self.client.search_floating_ips( filters={'attached': False}) mock_nova_client.floating_ips.list.assert_called_with() self.assertIsInstance(floating_ips, list) self.assertEqual(2, len(floating_ips)) self.assertAreInstances(floating_ips, dict) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_get_floating_ip(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = self.floating_ips floating_ip = self.client.get_floating_ip(id='29') mock_nova_client.floating_ips.list.assert_called_with() self.assertIsInstance(floating_ip, dict) self.assertEqual('198.51.100.29', floating_ip['floating_ip_address']) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_get_floating_ip_not_found(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = self.floating_ips floating_ip = self.client.get_floating_ip(id='666') self.assertIsNone(floating_ip) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_create_floating_ip(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.create.return_value =\ fakes.FakeFloatingIP(**self.mock_floating_ip_list_rep[1]) self.client.create_floating_ip(network='nova') mock_nova_client.floating_ips.create.assert_called_with(pool='nova') @patch.object(OpenStackCloud, '_nova_list_floating_ips') @patch.object(OpenStackCloud, 'has_service') def test_available_floating_ip_existing(self, mock_has_service, mock__nova_list_floating_ips): mock_has_service.side_effect = has_service_side_effect mock__nova_list_floating_ips.return_value = \ self.mock_floating_ip_list_rep[:1] ip = self.client.available_floating_ip(network='nova') self.assertEqual(self.mock_floating_ip_list_rep[0]['ip'], ip['floating_ip_address']) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, '_nova_list_floating_ips') @patch.object(OpenStackCloud, 'has_service') def test_available_floating_ip_new(self, mock_has_service, mock__nova_list_floating_ips, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock__nova_list_floating_ips.return_value = [] mock_nova_client.floating_ips.create.return_value = \ fakes.FakeFloatingIP(**self.mock_floating_ip_list_rep[0]) ip = self.client.available_floating_ip(network='nova') self.assertEqual(self.mock_floating_ip_list_rep[0]['ip'], ip['floating_ip_address']) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_delete_floating_ip_existing(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.delete.return_value = None ret = self.client.delete_floating_ip( floating_ip_id='a-wild-id-appears') mock_nova_client.floating_ips.delete.assert_called_with( floating_ip='a-wild-id-appears') self.assertTrue(ret) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'get_floating_ip') def test_delete_floating_ip_not_found(self, mock_get_floating_ip, mock_nova_client): mock_get_floating_ip.return_value = None mock_nova_client.floating_ips.delete.side_effect = n_exc.NotFound( code=404) ret = self.client.delete_floating_ip( floating_ip_id='a-wild-id-appears') self.assertFalse(ret) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_attach_ip_to_server(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = self.floating_ips self.client._attach_ip_to_server(server=self.fake_server, floating_ip=self.floating_ip, fixed_address='192.0.2.129') mock_nova_client.servers.add_floating_ip.assert_called_with( server='server-id', address='203.0.113.1', fixed_address='192.0.2.129') @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_detach_ip_from_server(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = [ fakes.FakeFloatingIP(**ip) for ip in self.mock_floating_ip_list_rep ] self.client.detach_ip_from_server(server_id='server-id', floating_ip_id=1) mock_nova_client.servers.remove_floating_ip.assert_called_with( server='server-id', address='203.0.113.1') @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_add_ip_from_pool(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = self.floating_ips ip = self.client._add_ip_from_pool(server=self.fake_server, network='nova', fixed_address='192.0.2.129') self.assertEqual('203.0.113.1', ip['floating_ip_address'])
class TestRebuildServer(base.TestCase): def setUp(self): super(TestRebuildServer, self).setUp() self.client = OpenStackCloud("cloud", {}) def test_rebuild_server_rebuild_exception(self): """ Test that an exception in the novaclient rebuild raises an exception in rebuild_server. """ with patch("shade.OpenStackCloud"): config = { "servers.rebuild.side_effect": Exception("exception"), } OpenStackCloud.nova_client = Mock(**config) self.assertRaises( OpenStackCloudException, self.client.rebuild_server, "a", "b") def test_rebuild_server_server_error(self): """ Test that a server error while waiting for the server to rebuild raises an exception in rebuild_server. """ with patch("shade.OpenStackCloud"): config = { "servers.rebuild.return_value": Mock(status="REBUILD"), "servers.get.return_value": Mock(status="ERROR") } OpenStackCloud.nova_client = Mock(**config) self.assertRaises( OpenStackCloudException, self.client.rebuild_server, "a", "b", wait=True) def test_rebuild_server_timeout(self): """ Test that a timeout while waiting for the server to rebuild raises an exception in rebuild_server. """ with patch("shade.OpenStackCloud"): config = { "servers.rebuild.return_value": Mock(status="REBUILD"), "servers.get.return_value": Mock(status="REBUILD") } OpenStackCloud.nova_client = Mock(**config) self.assertRaises( OpenStackCloudTimeout, self.client.rebuild_server, "a", "b", wait=True, timeout=0.001) def test_rebuild_server_no_wait(self): """ Test that rebuild_server with no wait and no exception in the novaclient rebuild call returns the server instance. """ with patch("shade.OpenStackCloud"): mock_server = Mock(status="ACTIVE") config = { "servers.rebuild.return_value": mock_server } OpenStackCloud.nova_client = Mock(**config) self.assertEqual( self.client.rebuild_server("a", "b"), mock_server) def test_rebuild_server_wait(self): """ Test that rebuild_server with a wait returns the server instance when its status changes to "ACTIVE". """ with patch("shade.OpenStackCloud"): mock_server = Mock(status="ACTIVE") config = { "servers.rebuild.return_value": Mock(status="REBUILD"), "servers.get.return_value": mock_server } OpenStackCloud.nova_client = Mock(**config) self.assertEqual( self.client.rebuild_server("a", "b", wait=True), mock_server)
class TestCreateServer(base.TestCase): def setUp(self): super(TestCreateServer, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud(cloud_config=config.get_one_cloud( validate=False)) self.client._SERVER_AGE = 0 def test_create_server_with_create_exception(self): """ Test that an exception in the novaclient create raises an exception in create_server. """ with patch("shade.OpenStackCloud"): config = { "servers.create.side_effect": Exception("exception"), } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudException, self.client.create_server, 'server-name', 'image-id', 'flavor-id') def test_create_server_with_get_exception(self): """ Test that an exception when attempting to get the server instance via the novaclient raises an exception in create_server. """ with patch("shade.OpenStackCloud"): config = { "servers.create.return_value": Mock(status="BUILD"), "servers.get.side_effect": Exception("exception") } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudException, self.client.create_server, 'server-name', 'image-id', 'flavor-id') def test_create_server_with_server_error(self): """ Test that a server error before we return or begin waiting for the server instance spawn raises an exception in create_server. """ build_server = fakes.FakeServer('1234', '', 'BUILD') error_server = fakes.FakeServer('1234', '', 'ERROR') with patch("shade.OpenStackCloud"): config = { "servers.create.return_value": build_server, "servers.get.return_value": error_server, } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudException, self.client.create_server, 'server-name', 'image-id', 'flavor-id') def test_create_server_wait_server_error(self): """ Test that a server error while waiting for the server to spawn raises an exception in create_server. """ with patch("shade.OpenStackCloud"): build_server = fakes.FakeServer('1234', '', 'BUILD') error_server = fakes.FakeServer('1234', '', 'ERROR') config = { "servers.create.return_value": build_server, "servers.get.return_value": build_server, "servers.list.side_effect": [[build_server], [error_server]] } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudException, self.client.create_server, 'server-name', 'image-id', 'flavor-id', wait=True) def test_create_server_with_timeout(self): """ Test that a timeout while waiting for the server to spawn raises an exception in create_server. """ with patch("shade.OpenStackCloud"): fake_server = fakes.FakeServer('1234', '', 'BUILD') config = { "servers.create.return_value": fake_server, "servers.get.return_value": fake_server, "servers.list.return_value": [fake_server], } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudTimeout, self.client.create_server, 'server-name', 'image-id', 'flavor-id', wait=True, timeout=1) def test_create_server_no_wait(self): """ Test that create_server with no wait and no exception in the novaclient create call returns the server instance. """ with patch("shade.OpenStackCloud"): fake_server = fakes.FakeServer('1234', '', 'BUILD') config = { "servers.create.return_value": fake_server, "servers.get.return_value": fake_server } OpenStackCloud.nova_client = Mock(**config) self.assertEqual( _utils.normalize_server(meta.obj_to_dict(fake_server), cloud_name=self.client.name, region_name=self.client.region_name), self.client.create_server(name='server-name', image='image=id', flavor='flavor-id')) def test_create_server_with_admin_pass_no_wait(self): """ Test that a server with an admin_pass passed returns the password """ with patch("shade.OpenStackCloud"): fake_server = fakes.FakeServer('1234', '', 'BUILD') fake_create_server = fakes.FakeServer('1234', '', 'BUILD', adminPass='******') config = { "servers.create.return_value": fake_create_server, "servers.get.return_value": fake_server } OpenStackCloud.nova_client = Mock(**config) self.assertEqual( _utils.normalize_server(meta.obj_to_dict(fake_create_server), cloud_name=self.client.name, region_name=self.client.region_name), self.client.create_server(name='server-name', image='image=id', flavor='flavor-id', admin_pass='******')) def test_create_server_with_admin_pass_wait(self): """ Test that a server with an admin_pass passed returns the password """ with patch("shade.OpenStackCloud"): build_server = fakes.FakeServer('1234', '', 'BUILD', addresses=dict(public='1.1.1.1'), adminPass='******') next_server = fakes.FakeServer('1234', '', 'BUILD', addresses=dict(public='1.1.1.1')) fake_server = fakes.FakeServer('1234', '', 'ACTIVE', addresses=dict(public='1.1.1.1')) ret_fake_server = fakes.FakeServer( '1234', '', 'ACTIVE', addresses=dict(public='1.1.1.1'), adminPass='******') config = { "servers.create.return_value": build_server, "servers.get.return_value": next_server, "servers.list.side_effect": [[next_server], [fake_server]] } OpenStackCloud.nova_client = Mock(**config) with patch.object(OpenStackCloud, "add_ips_to_server", return_value=fake_server): self.assertEqual( _utils.normalize_server( meta.obj_to_dict(ret_fake_server), cloud_name=self.client.name, region_name=self.client.region_name), _utils.normalize_server( meta.obj_to_dict( self.client.create_server( 'server-name', 'image-id', 'flavor-id', wait=True, admin_pass='******')), cloud_name=self.client.name, region_name=self.client.region_name)) def test_create_server_wait(self): """ Test that create_server with a wait returns the server instance when its status changes to "ACTIVE". """ with patch("shade.OpenStackCloud"): build_server = fakes.FakeServer('1234', '', 'ACTIVE', addresses=dict(public='1.1.1.1')) fake_server = fakes.FakeServer('1234', '', 'ACTIVE', addresses=dict(public='1.1.1.1')) config = { "servers.create.return_value": build_server, "servers.get.return_value": build_server, "servers.list.side_effect": [[build_server], [fake_server]] } OpenStackCloud.nova_client = Mock(**config) with patch.object(OpenStackCloud, "add_ips_to_server", return_value=fake_server): self.assertEqual( self.client.create_server('server-name', 'image-id', 'flavor-id', wait=True), fake_server) @patch('time.sleep') def test_create_server_no_addresses(self, mock_sleep): """ Test that create_server with a wait throws an exception if the server doesn't have addresses. """ with patch("shade.OpenStackCloud"): build_server = fakes.FakeServer('1234', '', 'BUILD') fake_server = fakes.FakeServer('1234', '', 'ACTIVE') config = { "servers.create.return_value": build_server, "servers.get.return_value": [build_server, None], "servers.list.side_effect": [[build_server], [fake_server]], "servers.delete.return_value": None, } OpenStackCloud.nova_client = Mock(**config) self.client._SERVER_AGE = 0 with patch.object(OpenStackCloud, "add_ips_to_server", return_value=fake_server): self.assertRaises(OpenStackCloudException, self.client.create_server, 'server-name', 'image-id', 'flavor-id', wait=True) @patch('shade.OpenStackCloud.nova_client') @patch('shade.OpenStackCloud.get_network') def test_create_server_network_with_no_nics(self, mock_get_network, mock_nova): """ Verify that if 'network' is supplied, and 'nics' is not, that we attempt to get the network for the server. """ self.client.create_server('server-name', 'image-id', 'flavor-id', network='network-name') mock_get_network.assert_called_once_with(name_or_id='network-name') @patch('shade.OpenStackCloud.nova_client') @patch('shade.OpenStackCloud.get_network') def test_create_server_network_with_empty_nics(self, mock_get_network, mock_nova): """ Verify that if 'network' is supplied, along with an empty 'nics' list, it's treated the same as if 'nics' were not included. """ self.client.create_server('server-name', 'image-id', 'flavor-id', network='network-name', nics=[]) mock_get_network.assert_called_once_with(name_or_id='network-name')
class TestObject(base.TestCase): def setUp(self): super(TestObject, self).setUp() config = os_client_config.OpenStackConfig() self.cloud = OpenStackCloud( cloud_config=config.get_one_cloud(validate=False)) @mock.patch.object(swift_client, 'Connection') @mock.patch.object(cloud_config.CloudConfig, 'get_session') def test_swift_client(self, get_session_mock, swift_mock): session_mock = mock.Mock() session_mock.get_endpoint.return_value = 'danzig' session_mock.get_token.return_value = 'yankee' get_session_mock.return_value = session_mock self.cloud.swift_client swift_mock.assert_called_with( preauthurl='danzig', preauthtoken='yankee', auth_version=mock.ANY, os_options=dict( object_storage_url='danzig', auth_token='yankee', region_name='')) @mock.patch.object(cloud_config.CloudConfig, 'get_session') def test_swift_client_no_endpoint(self, get_session_mock): session_mock = mock.Mock() session_mock.get_endpoint.return_value = None get_session_mock.return_value = session_mock e = self.assertRaises( exc.OpenStackCloudException, lambda: self.cloud.swift_client) self.assertIn( 'Failed to instantiate object-store client.', str(e)) @mock.patch.object(shade.OpenStackCloud, 'auth_token') @mock.patch.object(shade.OpenStackCloud, 'get_session_endpoint') def test_swift_service(self, endpoint_mock, auth_mock): endpoint_mock.return_value = 'slayer' auth_mock.return_value = 'zulu' self.assertIsInstance(self.cloud.swift_service, swift_service.SwiftService) endpoint_mock.assert_called_with(service_key='object-store') @mock.patch.object(shade.OpenStackCloud, 'get_session_endpoint') def test_swift_service_no_endpoint(self, endpoint_mock): endpoint_mock.side_effect = KeyError e = self.assertRaises(exc.OpenStackCloudException, lambda: self.cloud.swift_service) self.assertIn( 'Error constructing swift client', str(e)) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_object_segment_size(self, swift_mock): swift_mock.get_capabilities.return_value = {'swift': {'max_file_size': 1000}} self.assertEqual(900, self.cloud.get_object_segment_size(900)) self.assertEqual(1000, self.cloud.get_object_segment_size(1000)) self.assertEqual(1000, self.cloud.get_object_segment_size(1100)) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_object_segment_size_http_412(self, swift_mock): swift_mock.get_capabilities.side_effect = swift_exc.ClientException( "Precondition failed", http_status=412) self.assertEqual(shade.openstackcloud.DEFAULT_OBJECT_SEGMENT_SIZE, self.cloud.get_object_segment_size(None)) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_create_container(self, mock_swift): """Test creating a (private) container""" name = 'test_container' mock_swift.head_container.return_value = None self.cloud.create_container(name) expected_head_container_calls = [ # once for exist test mock.call(container=name), # once for the final return mock.call(container=name, skip_cache=True) ] self.assertTrue(expected_head_container_calls, mock_swift.head_container.call_args_list) mock_swift.put_container.assert_called_once_with(container=name) # Because the default is 'private', we shouldn't be calling update self.assertFalse(mock_swift.post_container.called) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_create_container_public(self, mock_swift): """Test creating a public container""" name = 'test_container' mock_swift.head_container.return_value = None self.cloud.create_container(name, public=True) expected_head_container_calls = [ # once for exist test mock.call(container=name), # once for the final return mock.call(container=name, skip_cache=True) ] self.assertTrue(expected_head_container_calls, mock_swift.head_container.call_args_list) mock_swift.put_container.assert_called_once_with(container=name) mock_swift.post_container.assert_called_once_with( container=name, headers={'x-container-read': shade.openstackcloud.OBJECT_CONTAINER_ACLS['public']} ) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_create_container_exists(self, mock_swift): """Test creating a container that already exists""" name = 'test_container' fake_container = dict(id='1', name='name') mock_swift.head_container.return_value = fake_container container = self.cloud.create_container(name) mock_swift.head_container.assert_called_once_with(container=name) self.assertEqual(fake_container, container) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_delete_container(self, mock_swift): name = 'test_container' self.cloud.delete_container(name) mock_swift.delete_container.assert_called_once_with(container=name) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_delete_container_404(self, mock_swift): """No exception when deleting a container that does not exist""" name = 'test_container' mock_swift.delete_container.side_effect = swift_exc.ClientException( 'ERROR', http_status=404) self.cloud.delete_container(name) mock_swift.delete_container.assert_called_once_with(container=name) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_delete_container_error(self, mock_swift): """Non-404 swift error re-raised as OSCE""" mock_swift.delete_container.side_effect = swift_exc.ClientException( 'ERROR') self.assertRaises(shade.OpenStackCloudException, self.cloud.delete_container, '') @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_update_container(self, mock_swift): name = 'test_container' headers = {'x-container-read': shade.openstackcloud.OBJECT_CONTAINER_ACLS['public']} self.cloud.update_container(name, headers) mock_swift.post_container.assert_called_once_with( container=name, headers=headers) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_update_container_error(self, mock_swift): """Swift error re-raised as OSCE""" mock_swift.post_container.side_effect = swift_exc.ClientException( 'ERROR') self.assertRaises(shade.OpenStackCloudException, self.cloud.update_container, '', '') @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_set_container_access_public(self, mock_swift): name = 'test_container' self.cloud.set_container_access(name, 'public') mock_swift.post_container.assert_called_once_with( container=name, headers={'x-container-read': shade.openstackcloud.OBJECT_CONTAINER_ACLS['public']}) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_set_container_access_private(self, mock_swift): name = 'test_container' self.cloud.set_container_access(name, 'private') mock_swift.post_container.assert_called_once_with( container=name, headers={'x-container-read': shade.openstackcloud.OBJECT_CONTAINER_ACLS['private']}) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_set_container_access_invalid(self, mock_swift): self.assertRaises(shade.OpenStackCloudException, self.cloud.set_container_access, '', 'invalid') @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_container(self, mock_swift): fake_container = { 'x-container-read': shade.openstackcloud.OBJECT_CONTAINER_ACLS['public'] } mock_swift.head_container.return_value = fake_container access = self.cloud.get_container_access('foo') self.assertEqual('public', access) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_container_invalid(self, mock_swift): fake_container = {'x-container-read': 'invalid'} mock_swift.head_container.return_value = fake_container with testtools.ExpectedException( exc.OpenStackCloudException, "Could not determine container access for ACL: invalid" ): self.cloud.get_container_access('foo') @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_container_access_not_found(self, mock_swift): name = 'invalid_container' mock_swift.head_container.return_value = None with testtools.ExpectedException( exc.OpenStackCloudException, "Container not found: %s" % name ): self.cloud.get_container_access(name) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_list_containers(self, mock_swift): containers = [dict(id='1', name='containter1')] mock_swift.get_account.return_value = ('response_headers', containers) ret = self.cloud.list_containers() mock_swift.get_account.assert_called_once_with(full_listing=True) self.assertEqual(containers, ret) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_list_containers_not_full(self, mock_swift): containers = [dict(id='1', name='containter1')] mock_swift.get_account.return_value = ('response_headers', containers) ret = self.cloud.list_containers(full_listing=False) mock_swift.get_account.assert_called_once_with(full_listing=False) self.assertEqual(containers, ret) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_list_containers_exception(self, mock_swift): mock_swift.get_account.side_effect = swift_exc.ClientException("ERROR") self.assertRaises(exc.OpenStackCloudException, self.cloud.list_containers) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_list_objects(self, mock_swift): objects = [dict(id='1', name='object1')] mock_swift.get_container.return_value = ('response_headers', objects) ret = self.cloud.list_objects('container_name') mock_swift.get_container.assert_called_once_with( container='container_name', full_listing=True) self.assertEqual(objects, ret) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_list_objects_not_full(self, mock_swift): objects = [dict(id='1', name='object1')] mock_swift.get_container.return_value = ('response_headers', objects) ret = self.cloud.list_objects('container_name', full_listing=False) mock_swift.get_container.assert_called_once_with( container='container_name', full_listing=False) self.assertEqual(objects, ret) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_list_objects_exception(self, mock_swift): mock_swift.get_container.side_effect = swift_exc.ClientException( "ERROR") self.assertRaises(exc.OpenStackCloudException, self.cloud.list_objects, 'container_name') @mock.patch.object(shade.OpenStackCloud, 'get_object_metadata') @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_delete_object(self, mock_swift, mock_get_meta): container_name = 'container_name' object_name = 'object_name' mock_get_meta.return_value = {'object': object_name} self.assertTrue(self.cloud.delete_object(container_name, object_name)) mock_get_meta.assert_called_once_with(container_name, object_name) mock_swift.delete_object.assert_called_once_with( container=container_name, obj=object_name ) @mock.patch.object(shade.OpenStackCloud, 'get_object_metadata') @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_delete_object_not_found(self, mock_swift, mock_get_meta): container_name = 'container_name' object_name = 'object_name' mock_get_meta.return_value = None self.assertFalse(self.cloud.delete_object(container_name, object_name)) mock_get_meta.assert_called_once_with(container_name, object_name) self.assertFalse(mock_swift.delete_object.called) @mock.patch.object(shade.OpenStackCloud, 'get_object_metadata') @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_delete_object_exception(self, mock_swift, mock_get_meta): container_name = 'container_name' object_name = 'object_name' mock_get_meta.return_value = {'object': object_name} mock_swift.delete_object.side_effect = swift_exc.ClientException( "ERROR") self.assertRaises(shade.OpenStackCloudException, self.cloud.delete_object, container_name, object_name) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_object(self, mock_swift): fake_resp = ({'headers': 'yup'}, 'test body') mock_swift.get_object.return_value = fake_resp container_name = 'container_name' object_name = 'object_name' resp = self.cloud.get_object(container_name, object_name) self.assertEqual(fake_resp, resp) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_object_not_found(self, mock_swift): mock_swift.get_object.side_effect = swift_exc.ClientException( 'ERROR', http_status=404) container_name = 'container_name' object_name = 'object_name' self.assertIsNone(self.cloud.get_object(container_name, object_name)) mock_swift.get_object.assert_called_once_with( container=container_name, obj=object_name, query_string=None, resp_chunk_size=None) @mock.patch.object(shade.OpenStackCloud, 'swift_client') def test_get_object_exception(self, mock_swift): mock_swift.get_object.side_effect = swift_exc.ClientException("ERROR") container_name = 'container_name' object_name = 'object_name' self.assertRaises(shade.OpenStackCloudException, self.cloud.get_object, container_name, object_name)
def setUp(self): super(TestCreateServer, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud(cloud_config=config.get_one_cloud( validate=False)) self.client._SERVER_AGE = 0
class TestDeleteVolumeSnapshot(base.TestCase): def setUp(self): super(TestDeleteVolumeSnapshot, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud( cloud_config=config.get_one_cloud(validate=False)) @patch.object(OpenStackCloud, 'cinder_client') def test_delete_volume_snapshot(self, mock_cinder): """ Test that delete_volume_snapshot without a wait returns True instance when the volume snapshot deletes. """ fake_snapshot = fakes.FakeVolumeSnapshot('1234', 'available', 'foo', 'derpysnapshot') mock_cinder.volume_snapshots.list.return_value = [fake_snapshot] self.assertEqual( True, self.client.delete_volume_snapshot(name_or_id='1234', wait=False) ) mock_cinder.volume_snapshots.list.assert_called_with(detailed=True, search_opts=None) @patch.object(OpenStackCloud, 'cinder_client') def test_delete_volume_snapshot_with_error(self, mock_cinder): """ Test that a exception while deleting a volume snapshot will cause an OpenStackCloudException. """ fake_snapshot = fakes.FakeVolumeSnapshot('1234', 'available', 'foo', 'derpysnapshot') mock_cinder.volume_snapshots.delete.side_effect = Exception( "exception") mock_cinder.volume_snapshots.list.return_value = [fake_snapshot] self.assertRaises( OpenStackCloudException, self.client.delete_volume_snapshot, name_or_id='1234', wait=True, timeout=1) mock_cinder.volume_snapshots.delete.assert_called_with( snapshot='1234') @patch.object(OpenStackCloud, 'cinder_client') def test_delete_volume_snapshot_with_timeout(self, mock_cinder): """ Test that a timeout while waiting for the volume snapshot to delete raises an exception in delete_volume_snapshot. """ fake_snapshot = fakes.FakeVolumeSnapshot('1234', 'available', 'foo', 'derpysnapshot') mock_cinder.volume_snapshots.list.return_value = [fake_snapshot] self.assertRaises( OpenStackCloudTimeout, self.client.delete_volume_snapshot, name_or_id='1234', wait=True, timeout=1) mock_cinder.volume_snapshots.list.assert_called_with(detailed=True, search_opts=None)
class TestFloatingIP(base.TestCase): def setUp(self): super(TestFloatingIP, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud(cloud_config=config.get_one_cloud( validate=False)) @patch.object(OpenStackCloud, 'get_floating_ip') @patch.object(OpenStackCloud, '_attach_ip_to_server') @patch.object(OpenStackCloud, 'available_floating_ip') def test_add_auto_ip(self, mock_available_floating_ip, mock_attach_ip_to_server, mock_get_floating_ip): server = FakeServer(id='server-id', name='test-server', status="ACTIVE", addresses={}) server_dict = meta.obj_to_dict(server) floating_ip_dict = { "id": "this-is-a-floating-ip-id", "fixed_ip_address": None, "internal_network": None, "floating_ip_address": "203.0.113.29", "network": "this-is-a-net-or-pool-id", "attached": False, "status": "ACTIVE" } mock_available_floating_ip.return_value = floating_ip_dict self.client.add_auto_ip(server=server_dict) mock_attach_ip_to_server.assert_called_with( timeout=60, wait=False, server=server_dict, floating_ip=floating_ip_dict, skip_attach=False) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, '_add_ip_from_pool') def test_add_ips_to_server_pool(self, mock_add_ip_from_pool, mock_nova_client): server = FakeServer(id='romeo', name='test-server', status="ACTIVE", addresses={}) server_dict = meta.obj_to_dict(server) pool = 'nova' mock_nova_client.servers.get.return_value = server self.client.add_ips_to_server(server_dict, ip_pool=pool) mock_add_ip_from_pool.assert_called_with(server_dict, pool, reuse=True, wait=False, timeout=60, fixed_address=None, nat_destination=None) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'add_ip_list') def test_add_ips_to_server_ip_list(self, mock_add_ip_list, mock_nova_client): server = FakeServer(id='server-id', name='test-server', status="ACTIVE", addresses={}) server_dict = meta.obj_to_dict(server) ips = ['203.0.113.29', '172.24.4.229'] mock_nova_client.servers.get.return_value = server self.client.add_ips_to_server(server_dict, ips=ips) mock_add_ip_list.assert_called_with(server_dict, ips, wait=False, timeout=60, fixed_address=None) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, '_add_auto_ip') def test_add_ips_to_server_auto_ip(self, mock_add_auto_ip, mock_nova_client): server = FakeServer(id='server-id', name='test-server', status="ACTIVE", addresses={}) server_dict = meta.obj_to_dict(server) mock_nova_client.servers.get.return_value = server self.client.add_ips_to_server(server_dict) mock_add_auto_ip.assert_called_with(server_dict, wait=False, timeout=60, reuse=True)
class TestCreateServer(base.TestCase): def setUp(self): super(TestCreateServer, self).setUp() self.client = OpenStackCloud("cloud", {}) def test_create_server_with_create_exception(self): """ Test that an exception in the novaclient create raises an exception in create_server. """ with patch("shade.OpenStackCloud"): config = { "servers.create.side_effect": Exception("exception"), } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudException, self.client.create_server) def test_create_server_with_get_exception(self): """ Test that an exception when attempting to get the server instance via the novaclient raises an exception in create_server. """ with patch("shade.OpenStackCloud"): config = { "servers.create.return_value": Mock(status="BUILD"), "servers.get.side_effect": Exception("exception") } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudException, self.client.create_server) def test_create_server_with_server_error(self): """ Test that a server error before we return or begin waiting for the server instance spawn raises an exception in create_server. """ with patch("shade.OpenStackCloud"): config = { "servers.create.return_value": Mock(status="BUILD"), "servers.get.return_value": Mock(status="ERROR") } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudException, self.client.create_server) def test_create_server_wait_server_error(self): """ Test that a server error while waiting for the server to spawn raises an exception in create_server. """ with patch("shade.OpenStackCloud"): config = { "servers.create.return_value": Mock(status="BUILD"), "servers.get.side_effect": [Mock(status="BUILD"), Mock(status="ERROR")] } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudException, self.client.create_server, wait=True) def test_create_server_with_timeout(self): """ Test that a timeout while waiting for the server to spawn raises an exception in create_server. """ with patch("shade.OpenStackCloud"): config = { "servers.create.return_value": Mock(status="BUILD"), "servers.get.return_value": Mock(status="BUILD") } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudTimeout, self.client.create_server, wait=True, timeout=1) def test_create_server_no_wait(self): """ Test that create_server with no wait and no exception in the novaclient create call returns the server instance. """ with patch("shade.OpenStackCloud"): mock_server = Mock(status="BUILD") config = { "servers.create.return_value": mock_server, "servers.get.return_value": mock_server } OpenStackCloud.nova_client = Mock(**config) self.assertEqual(self.client.create_server(), mock_server) def test_create_server_wait(self): """ Test that create_server with a wait returns the server instance when its status changes to "ACTIVE". """ with patch("shade.OpenStackCloud"): mock_server = Mock(status="ACTIVE") config = { "servers.create.return_value": Mock(status="BUILD"), "servers.get.side_effect": [Mock(status="BUILD"), mock_server] } OpenStackCloud.nova_client = Mock(**config) with patch.object(OpenStackCloud, "add_ips_to_server", return_value=mock_server): self.assertEqual(self.client.create_server(wait=True), mock_server)
def setUp(self): super(TestCreateServer, self).setUp() self.client = OpenStackCloud("cloud", {})
class TestCreateServer(base.TestCase): def setUp(self): super(TestCreateServer, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud(cloud_config=config.get_one_cloud()) def test_create_server_with_create_exception(self): """ Test that an exception in the novaclient create raises an exception in create_server. """ with patch("shade.OpenStackCloud"): config = { "servers.create.side_effect": Exception("exception"), } OpenStackCloud.nova_client = Mock(**config) self.assertRaises( OpenStackCloudException, self.client.create_server) def test_create_server_with_get_exception(self): """ Test that an exception when attempting to get the server instance via the novaclient raises an exception in create_server. """ with patch("shade.OpenStackCloud"): config = { "servers.create.return_value": Mock(status="BUILD"), "servers.get.side_effect": Exception("exception") } OpenStackCloud.nova_client = Mock(**config) self.assertRaises( OpenStackCloudException, self.client.create_server) def test_create_server_with_server_error(self): """ Test that a server error before we return or begin waiting for the server instance spawn raises an exception in create_server. """ with patch("shade.OpenStackCloud"): config = { "servers.create.return_value": Mock(status="BUILD"), "servers.get.return_value": Mock(status="ERROR") } OpenStackCloud.nova_client = Mock(**config) self.assertRaises( OpenStackCloudException, self.client.create_server) def test_create_server_wait_server_error(self): """ Test that a server error while waiting for the server to spawn raises an exception in create_server. """ with patch("shade.OpenStackCloud"): config = { "servers.create.return_value": Mock(status="BUILD"), "servers.get.side_effect": [ Mock(status="BUILD"), Mock(status="ERROR")] } OpenStackCloud.nova_client = Mock(**config) self.assertRaises( OpenStackCloudException, self.client.create_server, wait=True) def test_create_server_with_timeout(self): """ Test that a timeout while waiting for the server to spawn raises an exception in create_server. """ with patch("shade.OpenStackCloud"): config = { "servers.create.return_value": Mock(status="BUILD"), "servers.get.return_value": Mock(status="BUILD") } OpenStackCloud.nova_client = Mock(**config) self.assertRaises( OpenStackCloudTimeout, self.client.create_server, wait=True, timeout=1) def test_create_server_no_wait(self): """ Test that create_server with no wait and no exception in the novaclient create call returns the server instance. """ with patch("shade.OpenStackCloud"): fake_server = fakes.FakeServer('', '', 'BUILD') config = { "servers.create.return_value": fake_server, "servers.get.return_value": fake_server } OpenStackCloud.nova_client = Mock(**config) self.assertEqual(meta.obj_to_dict(fake_server), self.client.create_server()) def test_create_server_wait(self): """ Test that create_server with a wait returns the server instance when its status changes to "ACTIVE". """ with patch("shade.OpenStackCloud"): fake_server = fakes.FakeServer( '', '', 'ACTIVE', addresses=dict(public='1.1.1.1')) config = { "servers.create.return_value": fakes.FakeServer('', '', 'ACTIVE'), "servers.get.side_effect": [ Mock(status="BUILD"), fake_server] } OpenStackCloud.nova_client = Mock(**config) with patch.object(OpenStackCloud, "add_ips_to_server", return_value=fake_server): self.assertEqual( self.client.create_server(wait=True), fake_server) def test_create_server_no_addresses(self): """ Test that create_server with a wait throws an exception if the server doesn't have addresses. """ with patch("shade.OpenStackCloud"): fake_server = fakes.FakeServer('', '', 'ACTIVE') config = { "servers.create.return_value": fakes.FakeServer('', '', 'ACTIVE'), "servers.get.side_effect": [ Mock(status="BUILD"), fake_server] } OpenStackCloud.nova_client = Mock(**config) with patch.object(OpenStackCloud, "add_ips_to_server", return_value=fake_server): self.assertRaises( OpenStackCloudException, self.client.create_server, wait=True)
def setUp(self): super(TestPort, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud( cloud_config=config.get_one_cloud(validate=False))
class TestFloatingIP(base.TestCase): mock_floating_ip_list_rep = [ { 'fixed_ip': None, 'id': 1, 'instance_id': None, 'ip': '203.0.113.1', 'pool': 'nova' }, { 'fixed_ip': None, 'id': 2, 'instance_id': None, 'ip': '203.0.113.2', 'pool': 'nova' }, { 'fixed_ip': '192.0.2.3', 'id': 29, 'instance_id': 'myself', 'ip': '198.51.100.29', 'pool': 'black_hole' } ] mock_floating_ip_pools = [ {'id': 'pool1_id', 'name': 'nova'}, {'id': 'pool2_id', 'name': 'pool2'}] def assertAreInstances(self, elements, elem_type): for e in elements: self.assertIsInstance(e, elem_type) def setUp(self): super(TestFloatingIP, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud( cloud_config=config.get_one_cloud(validate=False)) self.floating_ips = [ fakes.FakeFloatingIP(**ip) for ip in self.mock_floating_ip_list_rep ] self.fake_server = meta.obj_to_dict( fakes.FakeServer( 'server-id', '', 'ACTIVE', addresses={u'test_pnztt_net': [{ u'OS-EXT-IPS:type': u'fixed', u'addr': '192.0.2.129', u'version': 4, u'OS-EXT-IPS-MAC:mac_addr': u'fa:16:3e:ae:7d:42'}]})) self.floating_ip = _utils.normalize_nova_floating_ips( meta.obj_list_to_dict(self.floating_ips))[0] @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_list_floating_ips(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = self.floating_ips floating_ips = self.client.list_floating_ips() mock_nova_client.floating_ips.list.assert_called_with() self.assertIsInstance(floating_ips, list) self.assertEqual(3, len(floating_ips)) self.assertAreInstances(floating_ips, dict) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_search_floating_ips(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = self.floating_ips floating_ips = self.client.search_floating_ips( filters={'attached': False}) mock_nova_client.floating_ips.list.assert_called_with() self.assertIsInstance(floating_ips, list) self.assertEqual(2, len(floating_ips)) self.assertAreInstances(floating_ips, dict) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_get_floating_ip(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = self.floating_ips floating_ip = self.client.get_floating_ip(id='29') mock_nova_client.floating_ips.list.assert_called_with() self.assertIsInstance(floating_ip, dict) self.assertEqual('198.51.100.29', floating_ip['floating_ip_address']) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_get_floating_ip_not_found( self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = self.floating_ips floating_ip = self.client.get_floating_ip(id='666') self.assertIsNone(floating_ip) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_create_floating_ip(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.create.return_value =\ fakes.FakeFloatingIP(**self.mock_floating_ip_list_rep[1]) self.client.create_floating_ip(network='nova') mock_nova_client.floating_ips.create.assert_called_with(pool='nova') @patch.object(OpenStackCloud, '_nova_list_floating_ips') @patch.object(OpenStackCloud, 'has_service') def test_available_floating_ip_existing( self, mock_has_service, mock__nova_list_floating_ips): mock_has_service.side_effect = has_service_side_effect mock__nova_list_floating_ips.return_value = \ self.mock_floating_ip_list_rep[:1] ip = self.client.available_floating_ip(network='nova') self.assertEqual(self.mock_floating_ip_list_rep[0]['ip'], ip['floating_ip_address']) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, '_nova_list_floating_ips') @patch.object(OpenStackCloud, 'has_service') def test_available_floating_ip_new( self, mock_has_service, mock__nova_list_floating_ips, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock__nova_list_floating_ips.return_value = [] mock_nova_client.floating_ips.create.return_value = \ fakes.FakeFloatingIP(**self.mock_floating_ip_list_rep[0]) ip = self.client.available_floating_ip(network='nova') self.assertEqual(self.mock_floating_ip_list_rep[0]['ip'], ip['floating_ip_address']) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_delete_floating_ip_existing( self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.delete.return_value = None ret = self.client.delete_floating_ip( floating_ip_id='a-wild-id-appears') mock_nova_client.floating_ips.delete.assert_called_with( floating_ip='a-wild-id-appears') self.assertTrue(ret) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'get_floating_ip') def test_delete_floating_ip_not_found( self, mock_get_floating_ip, mock_nova_client): mock_get_floating_ip.return_value = None mock_nova_client.floating_ips.delete.side_effect = n_exc.NotFound( code=404) ret = self.client.delete_floating_ip( floating_ip_id='a-wild-id-appears') self.assertFalse(ret) @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_attach_ip_to_server(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = self.floating_ips self.client._attach_ip_to_server( server=self.fake_server, floating_ip=self.floating_ip, fixed_address='192.0.2.129') mock_nova_client.servers.add_floating_ip.assert_called_with( server='server-id', address='203.0.113.1', fixed_address='192.0.2.129') @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_detach_ip_from_server(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = [ fakes.FakeFloatingIP(**ip) for ip in self.mock_floating_ip_list_rep ] self.client.detach_ip_from_server( server_id='server-id', floating_ip_id=1) mock_nova_client.servers.remove_floating_ip.assert_called_with( server='server-id', address='203.0.113.1') @patch.object(OpenStackCloud, 'nova_client') @patch.object(OpenStackCloud, 'has_service') def test_add_ip_from_pool(self, mock_has_service, mock_nova_client): mock_has_service.side_effect = has_service_side_effect mock_nova_client.floating_ips.list.return_value = self.floating_ips server = self.client._add_ip_from_pool( server=self.fake_server, network='nova', fixed_address='192.0.2.129') self.assertEqual(server, self.fake_server)
class TestPort(base.TestCase): mock_neutron_port_create_rep = { 'port': { 'status': 'DOWN', 'binding:host_id': '', 'name': 'test-port-name', 'allowed_address_pairs': [], 'admin_state_up': True, 'network_id': 'test-net-id', 'tenant_id': 'test-tenant-id', 'binding:vif_details': {}, 'binding:vnic_type': 'normal', 'binding:vif_type': 'unbound', 'device_owner': '', 'mac_address': '50:1c:0d:e4:f0:0d', 'binding:profile': {}, 'fixed_ips': [ { 'subnet_id': 'test-subnet-id', 'ip_address': '29.29.29.29' } ], 'id': 'test-port-id', 'security_groups': [], 'device_id': '' } } mock_neutron_port_update_rep = { 'port': { 'status': 'DOWN', 'binding:host_id': '', 'name': 'test-port-name-updated', 'allowed_address_pairs': [], 'admin_state_up': True, 'network_id': 'test-net-id', 'tenant_id': 'test-tenant-id', 'binding:vif_details': {}, 'binding:vnic_type': 'normal', 'binding:vif_type': 'unbound', 'device_owner': '', 'mac_address': '50:1c:0d:e4:f0:0d', 'binding:profile': {}, 'fixed_ips': [ { 'subnet_id': 'test-subnet-id', 'ip_address': '29.29.29.29' } ], 'id': 'test-port-id', 'security_groups': [], 'device_id': '' } } mock_neutron_port_list_rep = { 'ports': [ { 'status': 'ACTIVE', 'binding:host_id': 'devstack', 'name': 'first-port', 'allowed_address_pairs': [], 'admin_state_up': True, 'network_id': '70c1db1f-b701-45bd-96e0-a313ee3430b3', 'tenant_id': '', 'extra_dhcp_opts': [], 'binding:vif_details': { 'port_filter': True, 'ovs_hybrid_plug': True }, 'binding:vif_type': 'ovs', 'device_owner': 'network:router_gateway', 'mac_address': 'fa:16:3e:58:42:ed', 'binding:profile': {}, 'binding:vnic_type': 'normal', 'fixed_ips': [ { 'subnet_id': '008ba151-0b8c-4a67-98b5-0d2b87666062', 'ip_address': '172.24.4.2' } ], 'id': 'd80b1a3b-4fc1-49f3-952e-1e2ab7081d8b', 'security_groups': [], 'device_id': '9ae135f4-b6e0-4dad-9e91-3c223e385824' }, { 'status': 'ACTIVE', 'binding:host_id': 'devstack', 'name': '', 'allowed_address_pairs': [], 'admin_state_up': True, 'network_id': 'f27aa545-cbdd-4907-b0c6-c9e8b039dcc2', 'tenant_id': 'd397de8a63f341818f198abb0966f6f3', 'extra_dhcp_opts': [], 'binding:vif_details': { 'port_filter': True, 'ovs_hybrid_plug': True }, 'binding:vif_type': 'ovs', 'device_owner': 'network:router_interface', 'mac_address': 'fa:16:3e:bb:3c:e4', 'binding:profile': {}, 'binding:vnic_type': 'normal', 'fixed_ips': [ { 'subnet_id': '288bf4a1-51ba-43b6-9d0a-520e9005db17', 'ip_address': '10.0.0.1' } ], 'id': 'f71a6703-d6de-4be1-a91a-a570ede1d159', 'security_groups': [], 'device_id': '9ae135f4-b6e0-4dad-9e91-3c223e385824' } ] } def setUp(self): super(TestPort, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud( cloud_config=config.get_one_cloud(validate=False)) @patch.object(OpenStackCloud, 'neutron_client') def test_create_port(self, mock_neutron_client): mock_neutron_client.create_port.return_value = \ self.mock_neutron_port_create_rep port = self.client.create_port( network_id='test-net-id', name='test-port-name', admin_state_up=True) mock_neutron_client.create_port.assert_called_with( body={'port': dict(network_id='test-net-id', name='test-port-name', admin_state_up=True)}) self.assertEqual(self.mock_neutron_port_create_rep['port'], port) def test_create_port_parameters(self): """Test that we detect invalid arguments passed to create_port""" self.assertRaises( TypeError, self.client.create_port, network_id='test-net-id', nome='test-port-name', stato_amministrativo_porta=True) @patch.object(OpenStackCloud, 'neutron_client') def test_create_port_exception(self, mock_neutron_client): mock_neutron_client.create_port.side_effect = Exception('blah') self.assertRaises( OpenStackCloudException, self.client.create_port, network_id='test-net-id', name='test-port-name', admin_state_up=True) @patch.object(OpenStackCloud, 'neutron_client') def test_update_port(self, mock_neutron_client): mock_neutron_client.list_ports.return_value = \ self.mock_neutron_port_list_rep mock_neutron_client.update_port.return_value = \ self.mock_neutron_port_update_rep port = self.client.update_port( name_or_id='d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b', name='test-port-name-updated') mock_neutron_client.update_port.assert_called_with( port='d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b', body={'port': dict(name='test-port-name-updated')}) self.assertEqual(self.mock_neutron_port_update_rep['port'], port) def test_update_port_parameters(self): """Test that we detect invalid arguments passed to update_port""" self.assertRaises( TypeError, self.client.update_port, name_or_id='test-port-id', nome='test-port-name-updated') @patch.object(OpenStackCloud, 'neutron_client') def test_update_port_exception(self, mock_neutron_client): mock_neutron_client.list_ports.return_value = \ self.mock_neutron_port_list_rep mock_neutron_client.update_port.side_effect = Exception('blah') self.assertRaises( OpenStackCloudException, self.client.update_port, name_or_id='d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b', name='test-port-name-updated') @patch.object(OpenStackCloud, 'neutron_client') def test_list_ports(self, mock_neutron_client): mock_neutron_client.list_ports.return_value = \ self.mock_neutron_port_list_rep ports = self.client.list_ports() mock_neutron_client.list_ports.assert_called_with() self.assertItemsEqual(self.mock_neutron_port_list_rep['ports'], ports) @patch.object(OpenStackCloud, 'neutron_client') def test_list_ports_exception(self, mock_neutron_client): mock_neutron_client.list_ports.side_effect = Exception('blah') self.assertRaises(OpenStackCloudException, self.client.list_ports) @patch.object(OpenStackCloud, 'neutron_client') def test_search_ports_by_id(self, mock_neutron_client): mock_neutron_client.list_ports.return_value = \ self.mock_neutron_port_list_rep ports = self.client.search_ports( name_or_id='f71a6703-d6de-4be1-a91a-a570ede1d159') mock_neutron_client.list_ports.assert_called_with() self.assertEquals(1, len(ports)) self.assertEquals('fa:16:3e:bb:3c:e4', ports[0]['mac_address']) @patch.object(OpenStackCloud, 'neutron_client') def test_search_ports_by_name(self, mock_neutron_client): mock_neutron_client.list_ports.return_value = \ self.mock_neutron_port_list_rep ports = self.client.search_ports(name_or_id='first-port') mock_neutron_client.list_ports.assert_called_with() self.assertEquals(1, len(ports)) self.assertEquals('fa:16:3e:58:42:ed', ports[0]['mac_address']) @patch.object(OpenStackCloud, 'neutron_client') def test_search_ports_not_found(self, mock_neutron_client): mock_neutron_client.list_ports.return_value = \ self.mock_neutron_port_list_rep ports = self.client.search_ports(name_or_id='non-existent') mock_neutron_client.list_ports.assert_called_with() self.assertEquals(0, len(ports)) @patch.object(OpenStackCloud, 'neutron_client') def test_delete_port(self, mock_neutron_client): mock_neutron_client.list_ports.return_value = \ self.mock_neutron_port_list_rep self.client.delete_port(name_or_id='first-port') mock_neutron_client.delete_port.assert_called_with( port='d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b')
def setUp(self): super(TestDeleteServer, self).setUp() self.cloud = OpenStackCloud("cloud", {})
def setUp(self): super(TestCreateServer, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud( cloud_config=config.get_one_cloud(validate=False)) self.client._SERVER_AGE = 0
class TestPort(base.TestCase): mock_neutron_port_create_rep = { 'port': { 'status': 'DOWN', 'binding:host_id': '', 'name': 'test-port-name', 'allowed_address_pairs': [], 'admin_state_up': True, 'network_id': 'test-net-id', 'tenant_id': 'test-tenant-id', 'binding:vif_details': {}, 'binding:vnic_type': 'normal', 'binding:vif_type': 'unbound', 'device_owner': '', 'mac_address': '50:1c:0d:e4:f0:0d', 'binding:profile': {}, 'fixed_ips': [ { 'subnet_id': 'test-subnet-id', 'ip_address': '29.29.29.29' } ], 'id': 'test-port-id', 'security_groups': [], 'device_id': '' } } mock_neutron_port_update_rep = { 'port': { 'status': 'DOWN', 'binding:host_id': '', 'name': 'test-port-name-updated', 'allowed_address_pairs': [], 'admin_state_up': True, 'network_id': 'test-net-id', 'tenant_id': 'test-tenant-id', 'binding:vif_details': {}, 'binding:vnic_type': 'normal', 'binding:vif_type': 'unbound', 'device_owner': '', 'mac_address': '50:1c:0d:e4:f0:0d', 'binding:profile': {}, 'fixed_ips': [ { 'subnet_id': 'test-subnet-id', 'ip_address': '29.29.29.29' } ], 'id': 'test-port-id', 'security_groups': [], 'device_id': '' } } mock_neutron_port_list_rep = { 'ports': [ { 'status': 'ACTIVE', 'binding:host_id': 'devstack', 'name': 'first-port', 'allowed_address_pairs': [], 'admin_state_up': True, 'network_id': '70c1db1f-b701-45bd-96e0-a313ee3430b3', 'tenant_id': '', 'extra_dhcp_opts': [], 'binding:vif_details': { 'port_filter': True, 'ovs_hybrid_plug': True }, 'binding:vif_type': 'ovs', 'device_owner': 'network:router_gateway', 'mac_address': 'fa:16:3e:58:42:ed', 'binding:profile': {}, 'binding:vnic_type': 'normal', 'fixed_ips': [ { 'subnet_id': '008ba151-0b8c-4a67-98b5-0d2b87666062', 'ip_address': '172.24.4.2' } ], 'id': 'd80b1a3b-4fc1-49f3-952e-1e2ab7081d8b', 'security_groups': [], 'device_id': '9ae135f4-b6e0-4dad-9e91-3c223e385824' }, { 'status': 'ACTIVE', 'binding:host_id': 'devstack', 'name': '', 'allowed_address_pairs': [], 'admin_state_up': True, 'network_id': 'f27aa545-cbdd-4907-b0c6-c9e8b039dcc2', 'tenant_id': 'd397de8a63f341818f198abb0966f6f3', 'extra_dhcp_opts': [], 'binding:vif_details': { 'port_filter': True, 'ovs_hybrid_plug': True }, 'binding:vif_type': 'ovs', 'device_owner': 'network:router_interface', 'mac_address': 'fa:16:3e:bb:3c:e4', 'binding:profile': {}, 'binding:vnic_type': 'normal', 'fixed_ips': [ { 'subnet_id': '288bf4a1-51ba-43b6-9d0a-520e9005db17', 'ip_address': '10.0.0.1' } ], 'id': 'f71a6703-d6de-4be1-a91a-a570ede1d159', 'security_groups': [], 'device_id': '9ae135f4-b6e0-4dad-9e91-3c223e385824' } ] } def setUp(self): super(TestPort, self).setUp() self.client = OpenStackCloud('cloud', {}) @patch.object(OpenStackCloud, 'neutron_client') def test_create_port(self, mock_neutron_client): mock_neutron_client.create_port.return_value = \ self.mock_neutron_port_create_rep port = self.client.create_port( network_id='test-net-id', name='test-port-name', admin_state_up=True) mock_neutron_client.create_port.assert_called_with( body={'port': dict(network_id='test-net-id', name='test-port-name', admin_state_up=True)}) self.assertEqual(self.mock_neutron_port_create_rep['port'], port) def test_create_port_parameters(self): """Test that we detect invalid arguments passed to create_port""" self.assertRaises( TypeError, self.client.create_port, network_id='test-net-id', nome='test-port-name', stato_amministrativo_porta=True) @patch.object(OpenStackCloud, 'neutron_client') def test_create_port_exception(self, mock_neutron_client): mock_neutron_client.create_port.side_effect = Exception('blah') self.assertRaises( OpenStackCloudException, self.client.create_port, network_id='test-net-id', name='test-port-name', admin_state_up=True) @patch.object(OpenStackCloud, 'neutron_client') def test_update_port(self, mock_neutron_client): mock_neutron_client.list_ports.return_value = \ self.mock_neutron_port_list_rep mock_neutron_client.update_port.return_value = \ self.mock_neutron_port_update_rep port = self.client.update_port( name_or_id='d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b', name='test-port-name-updated') mock_neutron_client.update_port.assert_called_with( port='d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b', body={'port': dict(name='test-port-name-updated')}) self.assertEqual(self.mock_neutron_port_update_rep['port'], port) def test_update_port_parameters(self): """Test that we detect invalid arguments passed to update_port""" self.assertRaises( TypeError, self.client.update_port, name_or_id='test-port-id', nome='test-port-name-updated') @patch.object(OpenStackCloud, 'neutron_client') def test_update_port_exception(self, mock_neutron_client): mock_neutron_client.list_ports.return_value = \ self.mock_neutron_port_list_rep mock_neutron_client.update_port.side_effect = Exception('blah') self.assertRaises( OpenStackCloudException, self.client.update_port, name_or_id='d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b', name='test-port-name-updated') @patch.object(OpenStackCloud, 'neutron_client') def test_list_ports(self, mock_neutron_client): mock_neutron_client.list_ports.return_value = \ self.mock_neutron_port_list_rep ports = self.client.list_ports() mock_neutron_client.list_ports.assert_called_with() self.assertItemsEqual(self.mock_neutron_port_list_rep['ports'], ports) @patch.object(OpenStackCloud, 'neutron_client') def test_list_ports_exception(self, mock_neutron_client): mock_neutron_client.list_ports.side_effect = Exception('blah') self.assertRaises(OpenStackCloudException, self.client.list_ports) @patch.object(OpenStackCloud, 'neutron_client') def test_search_ports_by_id(self, mock_neutron_client): mock_neutron_client.list_ports.return_value = \ self.mock_neutron_port_list_rep ports = self.client.search_ports( name_or_id='f71a6703-d6de-4be1-a91a-a570ede1d159') mock_neutron_client.list_ports.assert_called_with() self.assertEquals(1, len(ports)) self.assertEquals('fa:16:3e:bb:3c:e4', ports[0]['mac_address']) @patch.object(OpenStackCloud, 'neutron_client') def test_search_ports_by_name(self, mock_neutron_client): mock_neutron_client.list_ports.return_value = \ self.mock_neutron_port_list_rep ports = self.client.search_ports(name_or_id='first-port') mock_neutron_client.list_ports.assert_called_with() self.assertEquals(1, len(ports)) self.assertEquals('fa:16:3e:58:42:ed', ports[0]['mac_address']) @patch.object(OpenStackCloud, 'neutron_client') def test_search_ports_not_found(self, mock_neutron_client): mock_neutron_client.list_ports.return_value = \ self.mock_neutron_port_list_rep ports = self.client.search_ports(name_or_id='non-existent') mock_neutron_client.list_ports.assert_called_with() self.assertEquals(0, len(ports)) @patch.object(OpenStackCloud, 'neutron_client') def test_delete_port(self, mock_neutron_client): mock_neutron_client.list_ports.return_value = \ self.mock_neutron_port_list_rep self.client.delete_port(name_or_id='first-port') mock_neutron_client.delete_port.assert_called_with( port='d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b')
def setUp(self): super(TestDeleteServer, self).setUp() config = os_client_config.OpenStackConfig() self.cloud = OpenStackCloud(cloud_config=config.get_one_cloud())
def setUp(self): super(TestRebuildServer, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud( cloud_config=config.get_one_cloud(validate=False))
class TestCreateServer(base.TestCase): def setUp(self): super(TestCreateServer, self).setUp() config = os_client_config.OpenStackConfig() self.client = OpenStackCloud(cloud_config=config.get_one_cloud( validate=False)) def test_create_server_with_create_exception(self): """ Test that an exception in the novaclient create raises an exception in create_server. """ with patch("shade.OpenStackCloud"): config = { "servers.create.side_effect": Exception("exception"), } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudException, self.client.create_server, 'server-name', 'image-id', 'flavor-id') def test_create_server_with_get_exception(self): """ Test that an exception when attempting to get the server instance via the novaclient raises an exception in create_server. """ with patch("shade.OpenStackCloud"): config = { "servers.create.return_value": Mock(status="BUILD"), "servers.get.side_effect": Exception("exception") } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudException, self.client.create_server, 'server-name', 'image-id', 'flavor-id') def test_create_server_with_server_error(self): """ Test that a server error before we return or begin waiting for the server instance spawn raises an exception in create_server. """ build_server = fakes.FakeServer('1234', '', 'BUILD') error_server = fakes.FakeServer('1234', '', 'ERROR') with patch("shade.OpenStackCloud"): config = { "servers.create.return_value": build_server, "servers.get.return_value": error_server, } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudException, self.client.create_server, 'server-name', 'image-id', 'flavor-id') def test_create_server_wait_server_error(self): """ Test that a server error while waiting for the server to spawn raises an exception in create_server. """ with patch("shade.OpenStackCloud"): build_server = fakes.FakeServer('1234', '', 'BUILD') error_server = fakes.FakeServer('1234', '', 'ERROR') config = { "servers.create.return_value": build_server, "servers.get.return_value": build_server, "servers.list.side_effect": [[build_server], [error_server]] } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudException, self.client.create_server, 'server-name', 'image-id', 'flavor-id', wait=True) def test_create_server_with_timeout(self): """ Test that a timeout while waiting for the server to spawn raises an exception in create_server. """ with patch("shade.OpenStackCloud"): fake_server = fakes.FakeServer('1234', '', 'BUILD') config = { "servers.create.return_value": fake_server, "servers.get.return_value": fake_server, "servers.list.return_value": [fake_server], } OpenStackCloud.nova_client = Mock(**config) self.assertRaises(OpenStackCloudTimeout, self.client.create_server, 'server-name', 'image-id', 'flavor-id', wait=True, timeout=1) def test_create_server_no_wait(self): """ Test that create_server with no wait and no exception in the novaclient create call returns the server instance. """ with patch("shade.OpenStackCloud"): fake_server = fakes.FakeServer('1234', '', 'BUILD') config = { "servers.create.return_value": fake_server, "servers.get.return_value": fake_server } OpenStackCloud.nova_client = Mock(**config) self.assertEqual( meta.obj_to_dict(fake_server), self.client.create_server(name='server-name', image='image=id', flavor='flavor-id')) def test_create_server_wait(self): """ Test that create_server with a wait returns the server instance when its status changes to "ACTIVE". """ with patch("shade.OpenStackCloud"): build_server = fakes.FakeServer('1234', '', 'ACTIVE', addresses=dict(public='1.1.1.1')) fake_server = fakes.FakeServer('1234', '', 'ACTIVE', addresses=dict(public='1.1.1.1')) config = { "servers.create.return_value": build_server, "servers.get.return_value": build_server, "servers.list.side_effect": [[build_server], [fake_server]] } OpenStackCloud.nova_client = Mock(**config) with patch.object(OpenStackCloud, "add_ips_to_server", return_value=fake_server): self.assertEqual( self.client.create_server('server-name', 'image-id', 'flavor-id', wait=True), fake_server) def test_create_server_no_addresses(self): """ Test that create_server with a wait throws an exception if the server doesn't have addresses. """ with patch("shade.OpenStackCloud"): build_server = fakes.FakeServer('1234', '', 'BUILD') fake_server = fakes.FakeServer('1234', '', 'ACTIVE') config = { "servers.create.return_value": build_server, "servers.get.return_value": build_server, "servers.list.side_effect": [[build_server], [fake_server]] } OpenStackCloud.nova_client = Mock(**config) with patch.object(OpenStackCloud, "add_ips_to_server", return_value=fake_server): self.assertRaises(OpenStackCloudException, self.client.create_server, 'server-name', 'image-id', 'flavor-id', wait=True)