summaryrefslogtreecommitdiff
path: root/nova/tests/functional
diff options
context:
space:
mode:
authorRené Ribaud <rribaud@redhat.com>2022-06-13 15:22:43 +0200
committerRené Ribaud <rribaud@redhat.com>2022-07-22 10:22:34 +0200
commit09239fc2eadcf266b42c640e386c7cebad715eea (patch)
treee9488d10c9e7a12a9f2792dd83ceb9e7cdd23a1d /nova/tests/functional
parenta263fa46f861c091d93782d4796c8302f9c30f4a (diff)
downloadnova-09239fc2eadcf266b42c640e386c7cebad715eea.tar.gz
Allow unshelve to a specific host (REST API part)
This adds support to the REST API, in a new microversion, for specifying a destination host to unshelve server action when the server is shelved offloaded. This patch also supports the ability to unpin the availability_zone of an instance that is bound to it. Note that the functional test changes are due to those tests using the "latest" microversion 2.91. Implements: blueprint unshelve-to-host Change-Id: I9e95428c208582741e6cd99bd3260d6742fcc6b7
Diffstat (limited to 'nova/tests/functional')
-rw-r--r--nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.77/os-unshelve-az.json.tpl5
-rw-r--r--nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.77/os-unshelve.json.tpl4
-rw-r--r--nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-shelve.json.tpl (renamed from nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.77/os-unshelve-null.json.tpl)0
-rw-r--r--nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-az-host.json.tpl6
-rw-r--r--nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-az.json.tpl5
-rw-r--r--nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-host-and-unpin-az.json.tpl6
-rw-r--r--nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-host.json.tpl5
-rw-r--r--nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-unpin-az.json.tpl5
-rw-r--r--nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve.json.tpl3
-rw-r--r--nova/tests/functional/api_sample_tests/test_shelve.py297
-rw-r--r--nova/tests/functional/test_availability_zones.py448
-rw-r--r--nova/tests/functional/test_servers.py51
12 files changed, 811 insertions, 24 deletions
diff --git a/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.77/os-unshelve-az.json.tpl b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.77/os-unshelve-az.json.tpl
new file mode 100644
index 0000000000..9bcd25139a
--- /dev/null
+++ b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.77/os-unshelve-az.json.tpl
@@ -0,0 +1,5 @@
+{
+ "%(action)s": {
+ "availability_zone": "%(availability_zone)s"
+ }
+}
diff --git a/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.77/os-unshelve.json.tpl b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.77/os-unshelve.json.tpl
index 9bcd25139a..d78efa84e1 100644
--- a/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.77/os-unshelve.json.tpl
+++ b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.77/os-unshelve.json.tpl
@@ -1,5 +1,3 @@
{
- "%(action)s": {
- "availability_zone": "%(availability_zone)s"
- }
+ "unshelve": null
}
diff --git a/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.77/os-unshelve-null.json.tpl b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-shelve.json.tpl
index 5a19f85cff..5a19f85cff 100644
--- a/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.77/os-unshelve-null.json.tpl
+++ b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-shelve.json.tpl
diff --git a/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-az-host.json.tpl b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-az-host.json.tpl
new file mode 100644
index 0000000000..eecc4271cb
--- /dev/null
+++ b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-az-host.json.tpl
@@ -0,0 +1,6 @@
+{
+ "%(action)s": {
+ "availability_zone": "%(availability_zone)s",
+ "host": "%(host)s"
+ }
+}
diff --git a/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-az.json.tpl b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-az.json.tpl
new file mode 100644
index 0000000000..9bcd25139a
--- /dev/null
+++ b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-az.json.tpl
@@ -0,0 +1,5 @@
+{
+ "%(action)s": {
+ "availability_zone": "%(availability_zone)s"
+ }
+}
diff --git a/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-host-and-unpin-az.json.tpl b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-host-and-unpin-az.json.tpl
new file mode 100644
index 0000000000..f9d2a2b17a
--- /dev/null
+++ b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-host-and-unpin-az.json.tpl
@@ -0,0 +1,6 @@
+{
+ "%(action)s": {
+ "availability_zone": null,
+ "host": "%(host)s"
+ }
+}
diff --git a/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-host.json.tpl b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-host.json.tpl
new file mode 100644
index 0000000000..3363b524ee
--- /dev/null
+++ b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-host.json.tpl
@@ -0,0 +1,5 @@
+{
+ "%(action)s": {
+ "host": "%(host)s"
+ }
+}
diff --git a/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-unpin-az.json.tpl b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-unpin-az.json.tpl
new file mode 100644
index 0000000000..3815586c5c
--- /dev/null
+++ b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve-unpin-az.json.tpl
@@ -0,0 +1,5 @@
+{
+ "%(action)s": {
+ "availability_zone": null
+ }
+}
diff --git a/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve.json.tpl b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve.json.tpl
new file mode 100644
index 0000000000..d78efa84e1
--- /dev/null
+++ b/nova/tests/functional/api_sample_tests/api_samples/os-shelve/v2.91/os-unshelve.json.tpl
@@ -0,0 +1,3 @@
+{
+ "unshelve": null
+}
diff --git a/nova/tests/functional/api_sample_tests/test_shelve.py b/nova/tests/functional/api_sample_tests/test_shelve.py
index 37d24b6cea..0dfef71055 100644
--- a/nova/tests/functional/api_sample_tests/test_shelve.py
+++ b/nova/tests/functional/api_sample_tests/test_shelve.py
@@ -15,10 +15,25 @@
import nova.conf
+from nova import objects
from nova.tests.functional.api_sample_tests import test_servers
+from oslo_utils.fixture import uuidsentinel
+from unittest import mock
CONF = nova.conf.CONF
+fake_aggregate = {
+ 'deleted': 0,
+ 'deleted_at': None,
+ 'created_at': None,
+ 'updated_at': None,
+ 'id': 123,
+ 'uuid': uuidsentinel.fake_aggregate,
+ 'name': 'us-west',
+ 'hosts': ['host01'],
+ 'metadetails': {'availability_zone': 'us-west'},
+}
+
class ShelveJsonTest(test_servers.ServersSampleBase):
# The 'os_compute_api:os-shelve:shelve_offload' policy is admin-only
@@ -30,9 +45,11 @@ class ShelveJsonTest(test_servers.ServersSampleBase):
# Don't offload instance, so we can test the offload call.
CONF.set_override('shelved_offload_time', -1)
- def _test_server_action(self, uuid, template, action):
+ def _test_server_action(self, uuid, template, action, subs=None):
+ subs = subs or {}
+ subs.update({'action': action})
response = self._do_post('servers/%s/action' % uuid,
- template, {'action': action})
+ template, subs)
self.assertEqual(202, response.status_code)
self.assertEqual("", response.text)
@@ -51,26 +68,288 @@ class ShelveJsonTest(test_servers.ServersSampleBase):
self._test_server_action(uuid, 'os-unshelve', 'unshelve')
-class UnshelveJson277Test(test_servers.ServersSampleBase):
+class UnshelveJson277Test(ShelveJsonTest):
+ ADMIN_API = False
sample_dir = "os-shelve"
microversion = '2.77'
scenarios = [('v2_77', {'api_major_version': 'v2.1'})]
+ def setUp(self):
+ super(UnshelveJson277Test, self).setUp()
+ # Almost all next tests require the instance to be shelve offloaded.
+ # So shelve offload the instance and skip the shelve_offload_test
+ # below.
+ CONF.set_override('shelved_offload_time', 0)
+
+ def test_shelve_offload(self):
+ # Skip this test as the instance is already shelve offloaded.
+ pass
+
+ def test_unshelve_with_az(self):
+ uuid = self._post_server()
+ self._test_server_action(uuid, 'os-shelve', 'shelve')
+ self._test_server_action(
+ uuid,
+ 'os-unshelve-az',
+ 'unshelve',
+ subs={"availability_zone": "us-west"}
+ )
+
+
+class UnshelveJson291Test(UnshelveJson277Test):
+ ADMIN_API = True
+ sample_dir = "os-shelve"
+ microversion = '2.91'
+ scenarios = [('v2_91', {'api_major_version': 'v2.1'})]
+
+ def _test_server_action_invalid(
+ self, uuid, template, action, subs=None, msg=None):
+ subs = subs or {}
+ subs.update({'action': action})
+ response = self._do_post('servers/%s/action' % uuid,
+ template, subs)
+ self.assertEqual(400, response.status_code)
+ self.assertIn(msg, response.text)
+
+ def test_unshelve_with_non_valid_host(self):
+ """Ensure an exception rise if host is invalid and
+ a http 400 error
+ """
+ uuid = self._post_server()
+ self._test_server_action(uuid, 'os-shelve', 'shelve')
+ self._test_server_action_invalid(
+ uuid, 'os-unshelve-host',
+ 'unshelve',
+ subs={'host': 'host01'},
+ msg='Compute host host01 could not be found.')
+
+ @mock.patch('nova.objects.aggregate._get_by_host_from_db')
+ @mock.patch('nova.objects.ComputeNodeList.get_all_by_host')
+ def test_unshelve_with_valid_host(
+ self, compute_node_get_all_by_host, mock_api_get_by_host):
+ """Ensure we can unshelve to a host
+ """
+ # Put compute in the correct az
+ mock_api_get_by_host.return_value = [fake_aggregate]
+
+ uuid = self._post_server()
+ self._test_server_action(uuid, 'os-shelve', 'shelve')
+ fake_computes = objects.ComputeNodeList(
+ objects=[
+ objects.ComputeNode(
+ host='host01',
+ uuid=uuidsentinel.host1,
+ hypervisor_hostname='host01')
+ ]
+ )
+ compute_node_get_all_by_host.return_value = fake_computes
+
+ self._test_server_action(
+ uuid,
+ 'os-unshelve-host',
+ 'unshelve',
+ subs={'host': 'host01'}
+ )
+
+ @mock.patch('nova.objects.aggregate._get_by_host_from_db')
+ @mock.patch('nova.objects.ComputeNodeList.get_all_by_host')
+ def test_unshelve_with_az_and_host(
+ self, compute_node_get_all_by_host, mock_api_get_by_host):
+ """Ensure we can unshelve to a host and az
+ """
+ # Put compute in the correct az
+ mock_api_get_by_host.return_value = [fake_aggregate]
+
+ uuid = self._post_server()
+ self._test_server_action(uuid, 'os-shelve', 'shelve')
+ fake_computes = objects.ComputeNodeList(
+ objects=[
+ objects.ComputeNode(
+ host='host01',
+ uuid=uuidsentinel.host1,
+ hypervisor_hostname='host01')
+ ]
+ )
+ compute_node_get_all_by_host.return_value = fake_computes
+
+ self._test_server_action(
+ uuid,
+ 'os-unshelve-host',
+ 'unshelve',
+ subs={'host': 'host01', 'availability_zone': 'us-west'},
+ )
+
+ @mock.patch('nova.objects.aggregate._get_by_host_from_db')
+ @mock.patch('nova.objects.ComputeNodeList.get_all_by_host')
+ def test_unshelve_with_unpin_az_and_host(
+ self, compute_node_get_all_by_host, mock_api_get_by_host):
+ """Ensure we can unshelve to a host and az
+ """
+ # Put compute in the correct az
+ mock_api_get_by_host.return_value = [fake_aggregate]
+
+ uuid = self._post_server()
+ self._test_server_action(uuid, 'os-shelve', 'shelve')
+ fake_computes = objects.ComputeNodeList(
+ objects=[
+ objects.ComputeNode(
+ host='host01',
+ uuid=uuidsentinel.host1,
+ hypervisor_hostname='host01')
+ ]
+ )
+ compute_node_get_all_by_host.return_value = fake_computes
+
+ self._test_server_action(
+ uuid,
+ 'os-unshelve-host-and-unpin-az',
+ 'unshelve',
+ subs={'host': 'host01'},
+ )
+
+ @mock.patch('nova.objects.aggregate._get_by_host_from_db')
+ @mock.patch('nova.objects.ComputeNodeList.get_all_by_host')
+ def test_unshelve_with_unpin_az(
+ self, compute_node_get_all_by_host, mock_api_get_by_host):
+ """Ensure we can unpin an az
+ """
+ # Put compute in the correct az
+ mock_api_get_by_host.return_value = [fake_aggregate]
+
+ uuid = self._post_server()
+ self._test_server_action(uuid, 'os-shelve', 'shelve')
+ fake_computes = objects.ComputeNodeList(
+ objects=[
+ objects.ComputeNode(
+ host='host01',
+ uuid=uuidsentinel.host1,
+ hypervisor_hostname='host01')
+ ]
+ )
+ compute_node_get_all_by_host.return_value = fake_computes
+
+ self._test_server_action(
+ uuid,
+ 'os-unshelve-unpin-az',
+ 'unshelve',
+ subs={'host': 'host01'},
+ )
+
+
+class UnshelveJson291NonAdminTest(UnshelveJson291Test):
+ # Use non admin api credentials.
+ ADMIN_API = False
+ sample_dir = "os-shelve"
+ microversion = '2.91'
+ scenarios = [('v2_91', {'api_major_version': 'v2.1'})]
+
+ def _test_server_action_invalid(self, uuid, template, action, subs=None):
+ subs = subs or {}
+ subs.update({'action': action})
+ response = self._do_post('servers/%s/action' % uuid,
+ template, subs)
+ self.assertEqual(403, response.status_code)
+ self.assertIn(
+ "Policy doesn\'t allow os_compute_api:os-shelve:unshelve_to_host" +
+ " to be performed.", response.text)
+
def _test_server_action(self, uuid, template, action, subs=None):
subs = subs or {}
subs.update({'action': action})
response = self._do_post('servers/%s/action' % uuid,
template, subs)
self.assertEqual(202, response.status_code)
- self.assertEqual("", response.text)
+ self.assertEqual('', response.text)
+
+ def test_unshelve_with_non_valid_host(self):
+ """Ensure an exception rise if user is not admin.
+ a http 403 error
+ """
+ uuid = self._post_server()
+ self._test_server_action(uuid, 'os-shelve', 'shelve')
+ self._test_server_action_invalid(
+ uuid,
+ 'os-unshelve-host',
+ 'unshelve',
+ subs={'host': 'host01'}
+ )
+
+ @mock.patch('nova.objects.aggregate._get_by_host_from_db')
+ @mock.patch('nova.objects.ComputeNodeList.get_all_by_host')
+ def test_unshelve_with_unpin_az_and_host(
+ self, compute_node_get_all_by_host, mock_api_get_by_host):
+ # Put compute in the correct az
+ mock_api_get_by_host.return_value = [fake_aggregate]
- def test_unshelve_with_az(self):
uuid = self._post_server()
self._test_server_action(uuid, 'os-shelve', 'shelve')
- self._test_server_action(uuid, 'os-unshelve', 'unshelve',
- subs={"availability_zone": "us-west"})
+ fake_computes = objects.ComputeNodeList(
+ objects=[
+ objects.ComputeNode(
+ host='host01',
+ uuid=uuidsentinel.host1,
+ hypervisor_hostname='host01')
+ ]
+ )
+ compute_node_get_all_by_host.return_value = fake_computes
+
+ self._test_server_action_invalid(
+ uuid,
+ 'os-unshelve-host-and-unpin-az',
+ 'unshelve',
+ subs={'host': 'host01'},
+ )
+
+ @mock.patch('nova.objects.aggregate._get_by_host_from_db')
+ @mock.patch('nova.objects.ComputeNodeList.get_all_by_host')
+ def test_unshelve_with_valid_host(
+ self, compute_node_get_all_by_host, mock_api_get_by_host):
+ # Put compute in the correct az
+ mock_api_get_by_host.return_value = [fake_aggregate]
+
+ uuid = self._post_server()
+ self._test_server_action(uuid, 'os-shelve', 'shelve')
+ fake_computes = objects.ComputeNodeList(
+ objects=[
+ objects.ComputeNode(
+ host='host01',
+ uuid=uuidsentinel.host1,
+ hypervisor_hostname='host01')
+ ]
+ )
+ compute_node_get_all_by_host.return_value = fake_computes
+
+ self._test_server_action_invalid(
+ uuid,
+ 'os-unshelve-host',
+ 'unshelve',
+ subs={'host': 'host01'}
+ )
+
+ @mock.patch('nova.objects.aggregate._get_by_host_from_db')
+ @mock.patch('nova.objects.ComputeNodeList.get_all_by_host')
+ def test_unshelve_with_az_and_host(
+ self, compute_node_get_all_by_host, mock_api_get_by_host):
+ """Ensure we can unshelve to a host and az
+ """
+ # Put compute in the correct az
+ mock_api_get_by_host.return_value = [fake_aggregate]
- def test_unshelve_no_az(self):
uuid = self._post_server()
self._test_server_action(uuid, 'os-shelve', 'shelve')
- self._test_server_action(uuid, 'os-unshelve-null', 'unshelve')
+ fake_computes = objects.ComputeNodeList(
+ objects=[
+ objects.ComputeNode(
+ host='host01',
+ uuid=uuidsentinel.host1,
+ hypervisor_hostname='host01')
+ ]
+ )
+ compute_node_get_all_by_host.return_value = fake_computes
+
+ self._test_server_action_invalid(
+ uuid,
+ 'os-unshelve-host',
+ 'unshelve',
+ subs={'host': 'host01', 'availability_zone': 'us-west'},
+ )
diff --git a/nova/tests/functional/test_availability_zones.py b/nova/tests/functional/test_availability_zones.py
index 991f86148d..c376423303 100644
--- a/nova/tests/functional/test_availability_zones.py
+++ b/nova/tests/functional/test_availability_zones.py
@@ -10,12 +10,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+from nova.api.openstack.compute import hosts
+from nova.compute import instance_actions
from nova import context
from nova import objects
from nova import test
from nova.tests import fixtures as nova_fixtures
+from nova.tests.functional.api import client as api_client
from nova.tests.functional import fixtures as func_fixtures
from nova.tests.functional import integrated_helpers
+from nova.tests.unit.api.openstack import fakes
class TestAvailabilityZoneScheduling(
@@ -36,6 +40,9 @@ class TestAvailabilityZoneScheduling(
self.api = api_fixture.admin_api
self.api.microversion = 'latest'
+ self.controller = hosts.HostController()
+ self.req = fakes.HTTPRequest.blank('', use_admin_context=True)
+
self.start_service('conductor')
self.start_service('scheduler')
@@ -68,18 +75,18 @@ class TestAvailabilityZoneScheduling(
self.api.api_post(
'/os-aggregates/%s/action' % aggregate['id'], add_host_body)
- def _create_server(self, name):
+ def _create_server(self, name, zone=None):
# Create a server, it doesn't matter which host it ends up in.
server = super(TestAvailabilityZoneScheduling, self)._create_server(
flavor_id=self.flavor1,
- networks='none',)
- original_host = server['OS-EXT-SRV-ATTR:host']
- # Assert the server has the AZ set (not None or 'nova').
- expected_zone = 'zone1' if original_host == 'host1' else 'zone2'
- self.assertEqual(expected_zone, server['OS-EXT-AZ:availability_zone'])
+ networks='none',
+ az=zone,
+ )
return server
- def _assert_instance_az(self, server, expected_zone):
+ def _assert_instance_az_and_host(
+ self, server, expected_zone, expected_host=None):
+ # Check AZ
# Check the API.
self.assertEqual(expected_zone, server['OS-EXT-AZ:availability_zone'])
# Check the DB.
@@ -88,6 +95,51 @@ class TestAvailabilityZoneScheduling(
ctxt, self.cell_mappings[test.CELL1_NAME]) as cctxt:
instance = objects.Instance.get_by_uuid(cctxt, server['id'])
self.assertEqual(expected_zone, instance.availability_zone)
+ # Check host
+ if expected_host:
+ self.assertEqual(expected_host, server['OS-EXT-SRV-ATTR:host'])
+
+ def _assert_request_spec_az(self, ctxt, server, az):
+ request_spec = objects.RequestSpec.get_by_instance_uuid(
+ ctxt, server['id'])
+ self.assertEqual(request_spec.availability_zone, az)
+
+ def _assert_server_with_az_unshelved_to_specified_az(self, server, az):
+ """Ensure a server with an az constraints is unshelved in the
+ corresponding az.
+ """
+ host_to_disable = 'host1' if az == 'zone1' else 'host2'
+ self._shelve_server(server, expected_state='SHELVED_OFFLOADED')
+ compute_service_id = self.api.get_services(
+ host=host_to_disable, binary='nova-compute')[0]['id']
+ self.api.put_service(compute_service_id, {'status': 'disabled'})
+
+ req = {
+ 'unshelve': None
+ }
+
+ self.api.post_server_action(server['id'], req)
+
+ server = self._wait_for_action_fail_completion(
+ server, instance_actions.UNSHELVE, 'schedule_instances')
+ self.assertIn('Error', server['result'])
+ self.assertIn('No valid host', server['details'])
+
+ def _shelve_unshelve_server(self, ctxt, server, req):
+ self._shelve_server(server, expected_state='SHELVED_OFFLOADED')
+
+ self.api.post_server_action(server['id'], req)
+ server = self._wait_for_server_parameter(
+ server,
+ {'status': 'ACTIVE', },
+ )
+ return self.api.get_server(server['id'])
+
+ def other_az_than(self, az):
+ return 'zone2' if az == 'zone1' else 'zone1'
+
+ def other_host_than(self, host):
+ return 'host2' if host == 'host1' else 'host1'
def test_live_migrate_implicit_az(self):
"""Tests live migration of an instance with an implicit AZ.
@@ -111,7 +163,8 @@ class TestAvailabilityZoneScheduling(
still not restricted to its current zone even if it says it is in one.
"""
server = self._create_server('test_live_migrate_implicit_az')
- original_host = server['OS-EXT-SRV-ATTR:host']
+ original_az = server['OS-EXT-AZ:availability_zone']
+ expected_zone = self.other_az_than(original_az)
# Attempt to live migrate the instance; again, we don't specify a host
# because there are only two hosts so the scheduler would only be able
@@ -132,8 +185,379 @@ class TestAvailabilityZoneScheduling(
# the database because the API will return the AZ from the host
# aggregate if instance.host is not None.
server = self.api.get_server(server['id'])
- expected_zone = 'zone2' if original_host == 'host1' else 'zone1'
- self._assert_instance_az(server, expected_zone)
+ self._assert_instance_az_and_host(server, expected_zone)
+
+ def test_create_server(self):
+ """Create a server without an AZ constraint and make sure asking a new
+ request spec will not have the request_spec.availability_zone set.
+ """
+ ctxt = context.get_admin_context()
+ server = self._create_server('server01')
+ self._assert_request_spec_az(ctxt, server, None)
+
+ def test_create_server_to_zone(self):
+ """Create a server with an AZ constraint and make sure asking a new
+ request spec will have the request_spec.availability_zone to the
+ required zone.
+ """
+ ctxt = context.get_admin_context()
+ server = self._create_server('server01', 'zone2')
+
+ server = self.api.get_server(server['id'])
+ self._assert_instance_az_and_host(server, 'zone2')
+ self._assert_request_spec_az(ctxt, server, 'zone2')
+
+ def test_cold_migrate_cross_az(self):
+ """Test a cold migration cross AZ.
+ """
+ server = self._create_server('server01')
+ original_host = server['OS-EXT-SRV-ATTR:host']
+ original_az = server['OS-EXT-AZ:availability_zone']
+ expected_host = self.other_host_than(original_host)
+ expected_zone = self.other_az_than(original_az)
+
+ self._migrate_server(server)
+ self._confirm_resize(server)
+
+ server = self.api.get_server(server['id'])
+ self._assert_instance_az_and_host(server, expected_zone, expected_host)
+
+# Next tests attempt to check the following behavior
+# +----------+---------------------------+-------+----------------------------+
+# | Boot | Unshelve after offload AZ | Host | Result |
+# +==========+===========================+=======+============================+
+# | No AZ | No AZ or AZ=null | No | Free scheduling, |
+# | | | | reqspec.AZ=None |
+# +----------+---------------------------+-------+----------------------------+
+# | No AZ | No AZ or AZ=null | Host1 | Schedule to host1, |
+# | | | | reqspec.AZ=None |
+# +----------+---------------------------+-------+----------------------------+
+# | No AZ | AZ="AZ1" | No | Schedule to AZ1, |
+# | | | | reqspec.AZ="AZ1" |
+# +----------+---------------------------+-------+----------------------------+
+# | No AZ | AZ="AZ1" | Host1 | Verify that host1 in AZ1, |
+# | | | | or (1). Schedule to |
+# | | | | host1, reqspec.AZ="AZ1" |
+# +----------+---------------------------+-------+----------------------------+
+# | AZ1 | No AZ | No | Schedule to AZ1, |
+# | | | | reqspec.AZ="AZ1" |
+# +----------+---------------------------+-------+----------------------------+
+# | AZ1 | AZ=null | No | Free scheduling, |
+# | | | | reqspec.AZ=None |
+# +----------+---------------------------+-------+----------------------------+
+# | AZ1 | No AZ | Host1 | If host1 is in AZ1, |
+# | | | | then schedule to host1, |
+# | | | | reqspec.AZ="AZ1", otherwise|
+# | | | | reject the request (1) |
+# +----------+---------------------------+-------+----------------------------+
+# | AZ1 | AZ=null | Host1 | Schedule to host1, |
+# | | | | reqspec.AZ=None |
+# +----------+---------------------------+-------+----------------------------+
+# | AZ1 | AZ="AZ2" | No | Schedule to AZ2, |
+# | | | | reqspec.AZ="AZ2" |
+# +----------+---------------------------+-------+----------------------------+
+# | AZ1 | AZ="AZ2" | Host1 | If host1 in AZ2 then |
+# | | | | schedule to host1, |
+# | | | | reqspec.AZ="AZ2", |
+# | | | | otherwise reject (1) |
+# +----------+---------------------------+-------+----------------------------+
+#
+# (1) Check at the api and return an error.
+#
+#
+# +----------+---------------------------+-------+----------------------------+
+# | No AZ | No AZ or AZ=null | No | Free scheduling, |
+# | | | | reqspec.AZ=None |
+# +----------+---------------------------+-------+----------------------------+
+
+ def test_unshelve_server_without_az_contraint(self):
+ ctxt = context.get_admin_context()
+ server = self._create_server('server01')
+
+ req = {
+ 'unshelve': None
+ }
+
+ self._shelve_unshelve_server(ctxt, server, req)
+ self._assert_request_spec_az(ctxt, server, None)
+
+ def test_unshelve_unpin_az_server_without_az_contraint(self):
+ ctxt = context.get_admin_context()
+ server = self._create_server('server01')
+
+ req = {
+ 'unshelve': {'availability_zone': None}
+ }
+
+ self._shelve_unshelve_server(ctxt, server, req)
+ self._assert_request_spec_az(ctxt, server, None)
+
+# +----------+---------------------------+-------+----------------------------+
+# | No AZ | No AZ or AZ=null | Host1 | Schedule to host1, |
+# | | | | reqspec.AZ=None |
+# +----------+---------------------------+-------+----------------------------+
+ def test_unshelve_to_host_server_without_az_contraint(self):
+ ctxt = context.get_admin_context()
+ server = self._create_server('server01')
+ original_host = server['OS-EXT-SRV-ATTR:host']
+ original_az = server['OS-EXT-AZ:availability_zone']
+ dest_hostname = self.other_host_than(original_host)
+ expected_zone = self.other_az_than(original_az)
+
+ req = {
+ 'unshelve': {'host': dest_hostname}
+ }
+
+ server = self._shelve_unshelve_server(ctxt, server, req)
+ self._assert_instance_az_and_host(server, expected_zone, dest_hostname)
+ self._assert_request_spec_az(ctxt, server, None)
+
+ def test_unshelve_to_host_and_unpin_server_without_az_contraint(self):
+ ctxt = context.get_admin_context()
+ server = self._create_server('server01')
+ original_host = server['OS-EXT-SRV-ATTR:host']
+ original_az = server['OS-EXT-AZ:availability_zone']
+ dest_hostname = self.other_host_than(original_host)
+ expected_zone = self.other_az_than(original_az)
+
+ req = {
+ 'unshelve': {
+ 'host': dest_hostname,
+ 'availability_zone': None,
+ }
+ }
+
+ server = self._shelve_unshelve_server(ctxt, server, req)
+ self._assert_instance_az_and_host(server, expected_zone, dest_hostname)
+ self._assert_request_spec_az(ctxt, server, None)
+
+# +----------+---------------------------+-------+----------------------------+
+# | No AZ | AZ="AZ1" | No | Schedule to AZ1, |
+# | | | | reqspec.AZ="AZ1" |
+# +----------+---------------------------+-------+----------------------------+
+ def test_unshelve_to_az_server_without_az_constraint(self):
+ ctxt = context.get_admin_context()
+ server = self._create_server('server01')
+ original_host = server['OS-EXT-SRV-ATTR:host']
+ original_az = server['OS-EXT-AZ:availability_zone']
+ dest_hostname = 'host2' if original_host == 'host1' else 'host1'
+ dest_az = self.other_az_than(original_az)
+
+ req = {
+ 'unshelve': {'availability_zone': dest_az}
+ }
+
+ server = self._shelve_unshelve_server(ctxt, server, req)
+ self._assert_instance_az_and_host(server, dest_az, dest_hostname)
+ self._assert_request_spec_az(ctxt, server, dest_az)
+ self._assert_server_with_az_unshelved_to_specified_az(
+ server, dest_az)
+
+# +----------+---------------------------+-------+----------------------------+
+# | No AZ | AZ="AZ1" | Host1 | Verify that host1 in AZ1, |
+# | | | | or (3). Schedule to |
+# | | | | host1, reqspec.AZ="AZ1" |
+# +----------+---------------------------+-------+----------------------------+
+ def test_unshelve_to_az_and_host_server_without_az_constraint(self):
+ ctxt = context.get_admin_context()
+ server = self._create_server('server01')
+ original_host = server['OS-EXT-SRV-ATTR:host']
+ original_az = server['OS-EXT-AZ:availability_zone']
+ dest_hostname = 'host2' if original_host == 'host1' else 'host1'
+ dest_az = self.other_az_than(original_az)
+
+ req = {
+ 'unshelve': {'host': dest_hostname, 'availability_zone': dest_az}
+ }
+
+ server = self._shelve_unshelve_server(ctxt, server, req)
+ self._assert_instance_az_and_host(server, dest_az, dest_hostname)
+ self._assert_request_spec_az(ctxt, server, dest_az)
+ self._assert_server_with_az_unshelved_to_specified_az(
+ server, dest_az)
+
+ def test_unshelve_to_wrong_az_and_host_server_without_az_constraint(self):
+ server = self._create_server('server01')
+ original_host = server['OS-EXT-SRV-ATTR:host']
+ original_az = server['OS-EXT-AZ:availability_zone']
+ dest_hostname = 'host2' if original_host == 'host1' else 'host1'
+
+ req = {
+ 'unshelve': {'host': dest_hostname,
+ 'availability_zone': original_az}
+ }
+
+ self._shelve_server(server, expected_state='SHELVED_OFFLOADED')
+ exc = self.assertRaises(
+ api_client.OpenStackApiException,
+ self.api.post_server_action,
+ server['id'],
+ req
+ )
+
+ self.assertEqual(409, exc.response.status_code)
+ self.assertIn(
+ 'Host \\\"{}\\\" is not in the availability zone \\\"{}\\\".'
+ .format(dest_hostname, original_az),
+ exc.response.text
+ )
+
+# +----------+---------------------------+-------+----------------------------+
+# | AZ1 | No AZ | No | Schedule to AZ1, |
+# | | | | reqspec.AZ="AZ1" |
+# +----------+---------------------------+-------+----------------------------+
+ def test_unshelve_a_server_with_az_contraint(self):
+ ctxt = context.get_admin_context()
+ server = self._create_server('server01', 'zone2')
+
+ req = {
+ 'unshelve': None
+ }
+
+ self._shelve_unshelve_server(ctxt, server, req)
+ self._assert_request_spec_az(ctxt, server, 'zone2')
+ self._assert_server_with_az_unshelved_to_specified_az(
+ server, 'zone2')
+
+# +----------+---------------------------+-------+----------------------------+
+# | AZ1 | AZ=null | No | Free scheduling, |
+# | | | | reqspec.AZ=None |
+# +----------+---------------------------+-------+----------------------------+
+ def test_unshelve_to_unpin_az_a_server_with_az_constraint(self):
+ ctxt = context.get_admin_context()
+ server = self._create_server('server01', 'zone2')
+
+ req = {
+ 'unshelve': {'availability_zone': None}
+ }
+
+ server = self._shelve_unshelve_server(ctxt, server, req)
+ self._assert_request_spec_az(ctxt, server, None)
+
+# +----------+---------------------------+-------+----------------------------+
+# | AZ1 | No AZ | Host1 | If host1 is in AZ1, |
+# | | | | then schedule to host1, |
+# | | | | reqspec.AZ="AZ1", otherwise|
+# | | | | reject the request (3) |
+# +----------+---------------------------+-------+----------------------------+
+ def test_unshelve_to_host_server_with_az_contraint(self):
+ ctxt = context.get_admin_context()
+ server = self._create_server('server01', 'zone1')
+
+ req = {
+ 'unshelve': {'host': 'host1'}
+ }
+
+ server = self._shelve_unshelve_server(ctxt, server, req)
+ self._assert_instance_az_and_host(server, 'zone1', 'host1')
+ self._assert_request_spec_az(ctxt, server, 'zone1')
+ self._assert_server_with_az_unshelved_to_specified_az(
+ server, 'zone1')
+
+ def test_unshelve_to_host_wrong_az_server_with_az_contraint(self):
+ server = self._create_server('server01', 'zone1')
+
+ req = {
+ 'unshelve': {'host': 'host2'}
+ }
+
+ self._shelve_server(server, expected_state='SHELVED_OFFLOADED')
+ exc = self.assertRaises(
+ api_client.OpenStackApiException,
+ self.api.post_server_action,
+ server['id'],
+ req
+ )
+
+ self.assertEqual(409, exc.response.status_code)
+ self.assertIn(
+ 'Host \\\"host2\\\" is not in the availability '
+ 'zone \\\"zone1\\\".',
+ exc.response.text
+ )
+
+# +----------+---------------------------+-------+----------------------------+
+# | AZ1 | AZ=null | Host1 | Schedule to host1, |
+# | | | | reqspec.AZ=None |
+# +----------+---------------------------+-------+----------------------------+
+ def test_unshelve_to_host_and_unpin_server_with_az_contraint(self):
+ ctxt = context.get_admin_context()
+ server = self._create_server('server01', 'zone1')
+
+ req = {
+ 'unshelve': {'host': 'host2',
+ 'availability_zone': None,
+ }
+ }
+
+ server = self._shelve_unshelve_server(ctxt, server, req)
+ self._assert_instance_az_and_host(server, 'zone2', 'host2')
+ self._assert_request_spec_az(ctxt, server, None)
+
+# +----------+---------------------------+-------+----------------------------+
+# | AZ1 | AZ="AZ2" | No | Schedule to AZ2, |
+# | | | | reqspec.AZ="AZ2" |
+# +----------+---------------------------+-------+----------------------------+
+ def test_unshelve_to_az_a_server_with_az_constraint(self):
+ ctxt = context.get_admin_context()
+ server = self._create_server('server01', 'zone1')
+
+ req = {
+ 'unshelve': {'availability_zone': 'zone2'}
+ }
+
+ server = self._shelve_unshelve_server(ctxt, server, req)
+ self._assert_instance_az_and_host(server, 'zone2', 'host2')
+ self._assert_request_spec_az(ctxt, server, 'zone2')
+ self._assert_server_with_az_unshelved_to_specified_az(
+ server, 'zone2')
+
+# +----------+---------------------------+-------+----------------------------+
+# | AZ1 | AZ="AZ2" | Host1 | If host1 in AZ2 then |
+# | | | | schedule to host1, |
+# | | | | reqspec.AZ="AZ2", |
+# | | | | otherwise reject (3) |
+# +----------+---------------------------+-------+----------------------------+
+ def test_unshelve_to_host_and_az_a_server_with_az_constraint(self):
+ ctxt = context.get_admin_context()
+ server = self._create_server('server01', 'zone1')
+
+ req = {
+ 'unshelve': {'host': 'host2',
+ 'availability_zone': 'zone2',
+ }
+ }
+
+ server = self._shelve_unshelve_server(ctxt, server, req)
+ self._assert_instance_az_and_host(server, 'zone2', 'host2')
+ self._assert_request_spec_az(ctxt, server, 'zone2')
+ self._assert_server_with_az_unshelved_to_specified_az(
+ server, 'zone2')
+
+ def test_unshelve_to_host_and_wrong_az_a_server_with_az_constraint(self):
+ server = self._create_server('server01', 'zone1')
+
+ req = {
+ 'unshelve': {'host': 'host2',
+ 'availability_zone': 'zone1',
+ }
+ }
+
+ self._shelve_server(server, expected_state='SHELVED_OFFLOADED')
+ exc = self.assertRaises(
+ api_client.OpenStackApiException,
+ self.api.post_server_action,
+ server['id'],
+ req
+ )
+
+ self.assertEqual(409, exc.response.status_code)
+ self.assertIn(
+ 'Host \\\"host2\\\" is not in the availability '
+ 'zone \\\"zone1\\\".',
+ exc.response.text
+
+ )
def test_resize_revert_across_azs(self):
"""Creates two compute service hosts in separate AZs. Creates a server
@@ -152,9 +576,9 @@ class TestAvailabilityZoneScheduling(
# Now the server should be in the other AZ.
new_zone = 'zone2' if original_host == 'host1' else 'zone1'
- self._assert_instance_az(server, new_zone)
+ self._assert_instance_az_and_host(server, new_zone)
# Revert the resize and the server should be back in the original AZ.
self.api.post_server_action(server['id'], {'revertResize': None})
server = self._wait_for_state_change(server, 'ACTIVE')
- self._assert_instance_az(server, original_az)
+ self._assert_instance_az_and_host(server, original_az)
diff --git a/nova/tests/functional/test_servers.py b/nova/tests/functional/test_servers.py
index 22939020f0..f65dd59902 100644
--- a/nova/tests/functional/test_servers.py
+++ b/nova/tests/functional/test_servers.py
@@ -2519,6 +2519,57 @@ class ServerMovingTests(integrated_helpers.ProviderUsageBaseTestCase):
self._delete_and_check_allocations(server)
+ def test_shelve_unshelve_to_host(self):
+ source_hostname = self.compute1.host
+ dest_hostname = self.compute2.host
+ source_rp_uuid = self._get_provider_uuid_by_host(source_hostname)
+ dest_rp_uuid = \
+ self._get_provider_uuid_by_host(dest_hostname)
+
+ server = self._boot_then_shelve_and_check_allocations(
+ source_hostname, source_rp_uuid)
+
+ self._shelve_offload_and_check_allocations(server, source_rp_uuid)
+
+ req = {
+ 'unshelve': {'host': dest_hostname}
+ }
+
+ self.api.post_server_action(server['id'], req)
+ self._wait_for_server_parameter(
+ server, {'OS-EXT-SRV-ATTR:host': dest_hostname, 'status': 'ACTIVE'}
+ )
+
+ self.assertFlavorMatchesUsage(dest_rp_uuid, self.flavor1)
+
+ # the server has an allocation on only the dest node
+ self.assertFlavorMatchesAllocation(
+ self.flavor1, server['id'], dest_rp_uuid)
+
+ self._delete_and_check_allocations(server)
+
+ def test_shelve_unshelve_to_host_instance_not_offloaded(self):
+ source_hostname = self.compute1.host
+ dest_hostname = self.compute2.host
+ source_rp_uuid = self._get_provider_uuid_by_host(source_hostname)
+
+ server = self._boot_then_shelve_and_check_allocations(
+ source_hostname, source_rp_uuid)
+
+ req = {
+ 'unshelve': {'host': dest_hostname}
+ }
+
+ ex = self.assertRaises(
+ client.OpenStackApiException,
+ self.api.post_server_action,
+ server['id'], req
+ )
+ self.assertEqual(409, ex.response.status_code)
+ self.assertIn(
+ "The server status must be SHELVED_OFFLOADED",
+ ex.response.text)
+
def _shelve_offload_and_check_allocations(self, server, source_rp_uuid):
req = {
'shelveOffload': {}