blob: 18491cac93b4912ceee5af2d0f0585ac213dac43 [file] [log] [blame]
sapan-kona17d7f062011-12-22 17:35:00 +05301from nose.plugins.attrib import attr
Daryl Wallecked8bef32011-12-05 23:02:08 -06002from tempest import openstack
3from tempest.common.utils.data_utils import rand_name
Daryl Walleck73a9e7a2011-11-15 17:43:31 -06004import unittest2 as unittest
Daryl Walleck73a9e7a2011-11-15 17:43:31 -06005
6
7class ServerMetadataTest(unittest.TestCase):
8
9 @classmethod
10 def setUpClass(cls):
11 cls.os = openstack.Manager()
12 cls.client = cls.os.servers_client
Jay Pipes7f757632011-12-02 15:53:32 -050013 cls.config = cls.os.config
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060014 cls.image_ref = cls.config.env.image_ref
15 cls.flavor_ref = cls.config.env.flavor_ref
16
17 #Create a server to be used for all read only tests
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060018 name = rand_name('server')
Brian Waldon738cd632011-12-12 18:45:09 -050019 resp, server = cls.client.create_server(name, cls.image_ref,
20 cls.flavor_ref, meta={})
21 cls.server_id = server['id']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060022
23 #Wait for the server to become active
Brian Waldon738cd632011-12-12 18:45:09 -050024 cls.client.wait_for_server_status(cls.server_id, 'ACTIVE')
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060025
26 @classmethod
27 def tearDownClass(cls):
Brian Waldon738cd632011-12-12 18:45:09 -050028 cls.client.delete_server(cls.server_id)
29
30 def setUp(self):
31 meta = {'key1': 'value1', 'key2': 'value2'}
32 resp, _ = self.client.set_server_metadata(self.server_id, meta)
33 self.assertEqual(resp.status, 200)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060034
35 def test_list_server_metadata(self):
36 """All metadata key/value pairs for a server should be returned"""
Brian Waldon738cd632011-12-12 18:45:09 -050037 resp, resp_metadata = self.client.list_server_metadata(self.server_id)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060038
39 #Verify the expected metadata items are in the list
40 self.assertEqual(200, resp.status)
Brian Waldon738cd632011-12-12 18:45:09 -050041 expected = {'key1': 'value1', 'key2': 'value2'}
42 self.assertEqual(expected, resp_metadata)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060043
44 def test_set_server_metadata(self):
45 """The server's metadata should be replaced with the provided values"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060046 #Create a new set of metadata for the server
Brian Waldon738cd632011-12-12 18:45:09 -050047 req_metadata = {'meta2': 'data2', 'meta3': 'data3'}
48 resp, metadata = self.client.set_server_metadata(self.server_id,
49 req_metadata)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060050 self.assertEqual(200, resp.status)
51
52 #Verify the expected values are correct, and that the
53 #previous values have been removed
Brian Waldon738cd632011-12-12 18:45:09 -050054 resp, resp_metadata = self.client.list_server_metadata(self.server_id)
55 self.assertEqual(resp_metadata, req_metadata)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060056
David Kranz6e977a72012-02-13 09:34:05 -050057 @attr(type='negative')
gavin-brebner-orange9cc23342011-12-02 12:49:58 +010058 def test_server_create_metadata_key_too_long(self):
59 """
60 Attempt to start a server with a meta-data key that is > 255 characters
61 Try a few values
62 """
63 for sz in [256, 257, 511, 1023]:
64 key = "k" * sz
65 meta = {key: 'data1'}
66 name = rand_name('server')
David Kranz6e977a72012-02-13 09:34:05 -050067 try:
68 resp, server = self.client.create_server(name, self.image_ref,
69 self.flavor_ref,
70 meta=meta)
71 except:
72 pass
73 else:
74 self.fail('Metadata should have been too long')
gavin-brebner-orange9cc23342011-12-02 12:49:58 +010075 # no teardown - all creates should fail
76
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060077 def test_update_server_metadata(self):
78 """
79 The server's metadata values should be updated to the
80 provided values
81 """
Brian Waldon738cd632011-12-12 18:45:09 -050082 meta = {'key1': 'alt1', 'key3': 'value3'}
83 resp, metadata = self.client.update_server_metadata(self.server_id,
84 meta)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060085 self.assertEqual(200, resp.status)
86
87 #Verify the values have been updated to the proper values
Brian Waldon738cd632011-12-12 18:45:09 -050088 resp, resp_metadata = self.client.list_server_metadata(self.server_id)
89 expected = {'key1': 'alt1', 'key2': 'value2', 'key3': 'value3'}
90 self.assertEqual(expected, resp_metadata)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060091
92 def test_get_server_metadata_item(self):
93 """ The value for a specic metadata key should be returned """
Brian Waldon738cd632011-12-12 18:45:09 -050094 resp, meta = self.client.get_server_metadata_item(self.server_id,
95 'key2')
96 self.assertTrue('value2', meta['key2'])
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060097
98 def test_set_server_metadata_item(self):
99 """The item's value should be updated to the provided value"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600100 #Update the metadata value
101 meta = {'nova': 'alt'}
Brian Waldon738cd632011-12-12 18:45:09 -0500102 resp, body = self.client.set_server_metadata_item(self.server_id,
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600103 'nova', meta)
104 self.assertEqual(200, resp.status)
105
106 #Verify the meta item's value has been updated
Brian Waldon738cd632011-12-12 18:45:09 -0500107 resp, resp_metadata = self.client.list_server_metadata(self.server_id)
108 expected = {'key1': 'value1', 'key2': 'value2', 'nova': 'alt'}
109 self.assertEqual(expected, resp_metadata)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600110
111 def test_delete_server_metadata_item(self):
112 """The metadata value/key pair should be deleted from the server"""
Brian Waldon738cd632011-12-12 18:45:09 -0500113 resp, meta = self.client.delete_server_metadata_item(self.server_id,
114 'key1')
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600115 self.assertEqual(204, resp.status)
116
117 #Verify the metadata item has been removed
Brian Waldon738cd632011-12-12 18:45:09 -0500118 resp, resp_metadata = self.client.list_server_metadata(self.server_id)
119 expected = {'key2': 'value2'}
120 self.assertEqual(expected, resp_metadata)
sapan-kona17d7f062011-12-22 17:35:00 +0530121
122 @attr(type='negative')
123 def test_get_nonexistant_server_metadata_item(self):
124 """Negative test: GET on nonexistant server should not succeed"""
125 try:
126 resp, meta = self.client.get_server_metadata_item(999, 'test2')
127 except:
128 pass
129 else:
130 self.fail('GET on nonexistant server should not succeed')
131
132 @attr(type='negative')
133 def test_list_nonexistant_server_metadata(self):
134 """
135 Negative test:List metadata on a non existant server should not succeed
136 """
137 try:
138 resp, metadata = self.client.list_server_metadata(999)
139 except:
140 pass
141 else:
142 self.fail('List metadata on a non existant server should'
143 'not succeed')
144
145 @attr(type='negative')
146 def test_set_nonexistant_server_metadata(self):
147 """
148 Negative test: Set metadata on a non existant server should not succeed
149 """
150 meta = {'meta1': 'data1'}
151 try:
152 resp, metadata = self.client.set_server_metadata(999, meta)
153 except:
154 pass
155 else:
156 self.fail('Set metadata on a non existant server should'
157 'not succeed')
158
159 @attr(type='negative')
160 def test_update_nonexistant_server_metadata(self):
161 """
162 Negative test: An update should not happen for a nonexistant image
163 """
164 meta = {'key1': 'value1', 'key2': 'value2'}
165 try:
166 resp, metadata = self.client.update_server_metadata(999, meta)
167 except:
168 pass
169 else:
170 self.fail('An update should not happen for a nonexistant image')
171
172 @attr(type='negative')
173 def test_delete_nonexistant_server_metadata_item(self):
174 """
175 Negative test: Should not be able to delete metadata item from a
176 nonexistant server
177 """
David Kranz6e977a72012-02-13 09:34:05 -0500178 meta = {'d': 'delvalue'}
sapan-kona17d7f062011-12-22 17:35:00 +0530179
180 #Delete the metadata item
David Kranz6e977a72012-02-13 09:34:05 -0500181 try:
182 resp, metadata = self.client.delete_server_metadata_item(999, 'd')
183 except:
184 pass
185 else:
186 self.fail('A delete should not happen for a nonexistant image')