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