blob: 44630bcae50fb5285cc9a6512badd749fbe7cd28 [file] [log] [blame]
Soren Hansenbc1d3a02011-09-08 13:33:17 +02001
2import json
3
Soren Hansenbc1d3a02011-09-08 13:33:17 +02004from kong import openstack
Soren Hansen6adacc82011-09-09 13:34:35 +02005from kong import tests
Soren Hansenbc1d3a02011-09-08 13:33:17 +02006
7
Soren Hansen6adacc82011-09-09 13:34:35 +02008class ServersMetadataTest(tests.FunctionalTest):
Soren Hansenbc1d3a02011-09-08 13:33:17 +02009 @classmethod
10 def setUpClass(self):
Soren Hansen6adacc82011-09-09 13:34:35 +020011 super(ServersMetadataTest, self).setUp()
Soren Hansend6b047a2011-09-09 13:39:32 +020012 self.os = openstack.Manager(self.nova)
Soren Hansenbc1d3a02011-09-08 13:33:17 +020013 self.image_ref = self.os.config.env.image_ref
14 self.flavor_ref = self.os.config.env.flavor_ref
15
16 def setUp(self):
17 server = {
18 'name' : 'testserver',
19 'imageRef' : self.image_ref,
20 'flavorRef' : self.flavor_ref,
21 'metadata' : {
22 'testEntry' : 'testValue',
23 },
24 }
25
26 created_server = self.os.nova.create_server(server)
27 self.server_id = created_server['id']
28
29 def tearDown(self):
30 self.os.nova.delete_server(self.server_id)
31
32
33 def test_get_server_metadata(self):
34 """Retrieve metadata for a server"""
35
36 url = '/servers/%s/metadata' % self.server_id
37 response, body = self.os.nova.request('GET', url)
38 self.assertEqual(200, response.status)
39
40 result = json.loads(body)
41 expected = {
42 'metadata' : {
43 'testEntry' : 'testValue',
44 },
45 }
46 self.assertEqual(expected, result)
47
48 def test_post_server_metadata(self):
49 """Create or update metadata for a server"""
50
51 post_metadata = {
52 'metadata' : {
53 'new_entry1' : 'new_value1',
54 'new_entry2' : 'new_value2',
55 },
56 }
57 post_body = json.dumps(post_metadata)
58
59 url = '/servers/%s/metadata' % self.server_id
60 response, body = self.os.nova.request('POST', url, body=post_body)
61 self.assertEqual(200, response.status)
62
63 url = '/servers/%s/metadata' % self.server_id
64 response, body = self.os.nova.request('GET', url)
65 self.assertEqual(200, response.status)
66
67 result = json.loads(body)
68 expected = post_metadata
69 expected['metadata']['testEntry'] = 'testValue'
70 self.assertEqual(expected, result)
71
72 def test_put_server_metadata(self):
73 """Overwrite all metadata for a server"""
74
75 expected = {
76 'metadata' : {
77 'new_entry1' : 'new_value1',
78 'new_entry2' : 'new_value2',
79 },
80 }
81
82 url = '/servers/%s/metadata' % self.server_id
83 post_body = json.dumps(expected)
84 response, body = self.os.nova.request('PUT', url, body=post_body)
85 self.assertEqual(200, response.status)
86
87 url = '/servers/%s/metadata' % self.server_id
88 response, body = self.os.nova.request('GET', url)
89 self.assertEqual(200, response.status)
90
91 result = json.loads(body)
92 # We want to make sure 'testEntry' was removed
93 self.assertEqual(expected, result)
94
95 def test_get_server_metadata_key(self):
96 """Retrieve specific metadata key for a server"""
97
98 url = '/servers/%s/metadata/testEntry' % self.server_id
99 response, body = self.os.nova.request('GET', url)
100 self.assertEqual(200, response.status)
101
102 result = json.loads(body)
103 expected = {
104 'meta':{
105 'testEntry':'testValue',
106 },
107 }
108
109 self.assertDictEqual(expected, result)
110
111 def test_add_server_metadata_key(self):
112 """Set specific metadata key on a server"""
113
114 expected_meta = {
115 'meta' : {
116 'new_meta1' : 'new_value1',
117 },
118 }
119
120 put_body = json.dumps(expected_meta)
121
122 url = '/servers/%s/metadata/new_meta1' % self.server_id
123 response, body = self.os.nova.request('PUT', url, body=put_body)
124 self.assertEqual(200, response.status)
125 result = json.loads(body)
126 self.assertDictEqual(expected_meta, result)
127
128 expected_metadata = {
129 'metadata' : {
130 'testEntry' : 'testValue',
131 'new_meta1' : 'new_value1',
132 },
133 }
134
135 # Now check all metadata to make sure the other values are there
136 url = '/servers/%s/metadata' % self.server_id
137 response, body = self.os.nova.request('GET', url)
138 result = json.loads(body)
139 self.assertDictEqual(expected_metadata, result)
140
141 def test_update_server_metadata_key(self):
142 """Update specific metadata key for a server"""
143
144 expected_meta = {
145 'meta' : {
146 'testEntry' : 'testValue2',
147 },
148 }
149 put_body = json.dumps(expected_meta)
150
151 url = '/servers/%s/metadata/testEntry' % self.server_id
152 response, body = self.os.nova.request('PUT', url, body=put_body)
153 self.assertEqual(200, response.status)
154 result = json.loads(body)
155 self.assertEqual(expected_meta, result)
156
157 def test_delete_server_metadata_key(self):
158 """Delete metadata for a server"""
159
160 url = '/servers/%s/metadata/testEntry' % self.server_id
161 response, body = self.os.nova.request('DELETE', url)
162 self.assertEquals(204, response.status)
163
164 url = '/servers/%s/metadata/testEntry' % self.server_id
165 response, body = self.os.nova.request('GET', url)
166 self.assertEquals(404, response.status)
167
168 url = '/servers/%s/metadata' % self.server_id
169 response, body = self.os.nova.request('GET', url)
170 self.assertEquals(200, response.status)
171 result = json.loads(body)
172 self.assertDictEqual({'metadata':{}}, result)