From a1524348952c9c3f4f234130bd856b4200c963fd Mon Sep 17 00:00:00 2001 From: Maho Koshiya Date: Mon, 31 Oct 2016 15:29:01 +0900 Subject: [PATCH] Add metadata functional negative tests The negative tests of metadata API are not covered enough in functional tests. We want to add the conflict tests of when the update/delete metadata API runs in the unexpected state (e.g. not ACTIVE) of server. Change-Id: Ic74d584bdce8a0d878e358211d69c7e7f629e457 --- nova/tests/functional/api/client.py | 4 +++ nova/tests/functional/test_servers.py | 41 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/nova/tests/functional/api/client.py b/nova/tests/functional/api/client.py index ff5c629c44a4..a8b047dcbaa3 100644 --- a/nova/tests/functional/api/client.py +++ b/nova/tests/functional/api/client.py @@ -364,6 +364,10 @@ class TestOpenStackClient(object): return self.api_post('/servers/%s/metadata' % server_id, post_body).body['metadata'] + def delete_server_metadata(self, server_id, key): + return self.api_delete('/servers/%s/metadata/%s' % + (server_id, key)) + def get_server_groups(self, all_projects=None): if all_projects: return self.api_get( diff --git a/nova/tests/functional/test_servers.py b/nova/tests/functional/test_servers.py index 94cea9acff9a..57fd3d744e64 100644 --- a/nova/tests/functional/test_servers.py +++ b/nova/tests/functional/test_servers.py @@ -346,6 +346,47 @@ class ServersTest(ServersTestBase): # Cleanup self._delete_server(created_server_id) + def test_server_metadata_actions_negative_invalid_state(self): + # Create server with metadata + server = self._build_minimal_create_server_request() + + metadata = {'key_1': 'value_1'} + + server['metadata'] = metadata + + post = {'server': server} + created_server = self.api.post_server(post) + + found_server = self._wait_for_state_change(created_server, 'BUILD') + self.assertEqual('ACTIVE', found_server['status']) + self.assertEqual(metadata, found_server.get('metadata')) + server_id = found_server['id'] + + # Change status from ACTIVE to SHELVED for negative test + self.flags(shelved_offload_time = -1) + self.api.post_server_action(server_id, {'shelve': {}}) + found_server = self._wait_for_state_change(found_server, 'ACTIVE') + self.assertEqual('SHELVED', found_server['status']) + + metadata = {'key_2': 'value_2'} + + # Update Metadata item in SHELVED (not ACTIVE, etc.) + ex = self.assertRaises(client.OpenStackApiException, + self.api.post_server_metadata, + server_id, metadata) + self.assertEqual(409, ex.response.status_code) + self.assertEqual('SHELVED', found_server['status']) + + # Delete Metadata item in SHELVED (not ACTIVE, etc.) + ex = self.assertRaises(client.OpenStackApiException, + self.api.delete_server_metadata, + server_id, 'key_1') + self.assertEqual(409, ex.response.status_code) + self.assertEqual('SHELVED', found_server['status']) + + # Cleanup + self._delete_server(server_id) + def test_create_and_rebuild_server(self): # Rebuild a server with metadata.