blob: 8b76811ba020e97f7ec92a2778fbba406b255315 [file] [log] [blame]
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +03001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13from tempest.api.baremetal import base
14from tempest.common.utils import data_utils
15from tempest import exceptions as exc
16from tempest import test
17
18
19class TestPorts(base.BaseBaremetalTest):
20 """Tests for ports."""
21
22 def setUp(self):
23 super(TestPorts, self).setUp()
24
25 chassis = self.create_chassis()['chassis']
26 self.node = self.create_node(chassis['uuid'])['node']
27
28 @test.attr(type='smoke')
29 def test_create_port(self):
30 node_id = self.node['uuid']
31 address = data_utils.rand_mac_address()
32
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040033 result = self.create_port(node_id=node_id, address=address)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030034
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040035 port = result['port']
36
37 resp, body = self.client.show_port(port['uuid'])
38
39 self.assertEqual(200, resp.status)
40 self.assertEqual(port['uuid'], body['uuid'])
41 self.assertEqual(address, body['address'])
42 self.assertEqual({}, body['extra'])
43 self.assertEqual(node_id, body['node_uuid'])
44
45 @test.attr(type='smoke')
46 def test_create_port_specifying_uuid(self):
47 node_id = self.node['uuid']
48 address = data_utils.rand_mac_address()
49 uuid = data_utils.rand_uuid()
50
51 self.create_port(node_id=node_id, address=address, uuid=uuid)
52
53 resp, body = self.client.show_port(uuid)
54
55 self.assertEqual(200, resp.status)
56 self.assertEqual(uuid, body['uuid'])
57 self.assertEqual(address, body['address'])
58 self.assertEqual({}, body['extra'])
59 self.assertEqual(node_id, body['node_uuid'])
60
61 @test.attr(type='smoke')
62 def test_create_port_with_extra(self):
63 node_id = self.node['uuid']
64 address = data_utils.rand_mac_address()
65 extra = {'key': 'value'}
66
67 result = self.create_port(node_id=node_id, address=address,
68 extra=extra)
69 port = result['port']
70
71 resp, body = self.client.show_port(port['uuid'])
72
73 self.assertEqual(200, resp.status)
74 self.assertEqual(port['uuid'], body['uuid'])
75 self.assertEqual(address, body['address'])
76 self.assertEqual(extra, body['extra'])
77 self.assertEqual(node_id, body['node_uuid'])
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030078
79 @test.attr(type='smoke')
80 def test_delete_port(self):
81 node_id = self.node['uuid']
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040082 address = data_utils.rand_mac_address()
83 port_id = self.create_port(node_id=node_id, address=address)['port'][
84 'uuid']
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030085
86 resp = self.delete_port(port_id)
87
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040088 self.assertEqual(204, resp.status)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030089 self.assertRaises(exc.NotFound, self.client.show_port, port_id)
90
91 @test.attr(type='smoke')
92 def test_show_port(self):
93 node_id = self.node['uuid']
94 address = data_utils.rand_mac_address()
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040095 extra = {'key': 'value'}
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030096
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040097 port_id = self.create_port(node_id=node_id, address=address,
98 extra=extra)['port']['uuid']
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030099
100 resp, port = self.client.show_port(port_id)
101
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400102 self.assertEqual(200, resp.status)
103 self.assertEqual(port_id, port['uuid'])
104 self.assertEqual(address, port['address'])
105 self.assertEqual(extra, port['extra'])
106
107 @test.attr(type='smoke')
108 def test_show_port_with_links(self):
109 node_id = self.node['uuid']
110 address = data_utils.rand_mac_address()
111
112 port_id = self.create_port(node_id=node_id, address=address)['port'][
113 'uuid']
114
115 resp, body = self.client.show_port(port_id)
116
117 self.assertEqual(200, resp.status)
118 self.assertIn('links', body.keys())
119 self.assertEqual(2, len(body['links']))
120 self.assertIn(port_id, body['links'][0]['href'])
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300121
122 @test.attr(type='smoke')
123 def test_list_ports(self):
124 node_id = self.node['uuid']
125
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400126 uuids = [self.create_port(node_id=node_id,
127 address=data_utils.rand_mac_address())
128 ['port']['uuid'] for i in xrange(5)]
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300129
130 resp, body = self.client.list_ports()
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400131 self.assertEqual(200, resp.status)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300132 loaded_uuids = [p['uuid'] for p in body['ports']]
133
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400134 for uuid in uuids:
135 self.assertIn(uuid, loaded_uuids)
136
137 # Verify self links.
138 for port in body['ports']:
139 self.validate_self_link('ports', port['uuid'],
140 port['links'][0]['href'])
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300141
142 @test.attr(type='smoke')
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400143 def test_list_with_limit(self):
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300144 node_id = self.node['uuid']
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400145
146 for i in xrange(5):
147 self.create_port(node_id=node_id,
148 address=data_utils.rand_mac_address())
149
150 resp, body = self.client.list_ports(limit=3)
151 self.assertEqual(200, resp.status)
152 self.assertEqual(3, len(body['ports']))
153
154 next_marker = body['ports'][-1]['uuid']
155 self.assertIn(next_marker, body['next'])
156
157 def test_list_ports_details(self):
158 node_id = self.node['uuid']
159
160 uuids = [
161 self.create_port(node_id=node_id,
162 address=data_utils.rand_mac_address())
163 ['port']['uuid'] for i in range(0, 5)]
164
165 resp, body = self.client.list_ports_detail()
166 self.assertEqual(200, resp.status)
167
168 ports_dict = {port['uuid']: port for port in body['ports']
169 if port['uuid'] in uuids}
170
171 for uuid in uuids:
172 self.assertIn(uuid, ports_dict)
173 port = ports_dict[uuid]
174 self.assertIn('extra', port)
175 self.assertIn('node_uuid', port)
176 # never expose the node_id
177 self.assertNotIn('node_id', port)
178 # Verify self link.
179 self.validate_self_link('ports', port['uuid'],
180 port['links'][0]['href'])
181
182 @test.attr(type='smoke')
183 def test_update_port_replace(self):
184 node_id = self.node['uuid']
185 address = data_utils.rand_mac_address()
186 extra = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
187
188 port_id = self.create_port(node_id=node_id, address=address,
189 extra=extra)['port']['uuid']
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300190
191 new_address = data_utils.rand_mac_address()
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400192 new_extra = {'key1': 'new-value1', 'key2': 'new-value2',
193 'key3': 'new-value3'}
194
195 patch = [{'path': '/address',
196 'op': 'replace',
197 'value': new_address},
198 {'path': '/extra/key1',
199 'op': 'replace',
200 'value': new_extra['key1']},
201 {'path': '/extra/key2',
202 'op': 'replace',
203 'value': new_extra['key2']},
204 {'path': '/extra/key3',
205 'op': 'replace',
206 'value': new_extra['key3']}]
207
208 self.client.update_port(port_id, patch)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300209
210 resp, body = self.client.show_port(port_id)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400211 self.assertEqual(200, resp.status)
212 self.assertEqual(new_address, body['address'])
213 self.assertEqual(new_extra, body['extra'])
214
215 @test.attr(type='smoke')
216 def test_update_port_remove(self):
217 node_id = self.node['uuid']
218 address = data_utils.rand_mac_address()
219 extra = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
220
221 port_id = self.create_port(node_id=node_id, address=address,
222 extra=extra)['port']['uuid']
223
224 # Removing one item from the collection
225 resp, _ = self.client.update_port(port_id, [{'path': '/extra/key2',
226 'op': 'remove'}])
227 self.assertEqual(200, resp.status)
228 extra.pop('key2')
229 resp, body = self.client.show_port(port_id)
230 self.assertEqual(200, resp.status)
231 self.assertEqual(extra, body['extra'])
232
233 # Removing the collection
234 resp, _ = self.client.update_port(port_id, [{'path': '/extra',
235 'op': 'remove'}])
236 self.assertEqual(200, resp.status)
237 resp, body = self.client.show_port(port_id)
238 self.assertEqual(200, resp.status)
239 self.assertEqual({}, body['extra'])
240
241 # Assert nothing else was changed
242 self.assertEqual(node_id, body['node_uuid'])
243 self.assertEqual(address, body['address'])
244
245 @test.attr(type='smoke')
246 def test_update_port_add(self):
247 node_id = self.node['uuid']
248 address = data_utils.rand_mac_address()
249
250 port_id = self.create_port(node_id=node_id, address=address)['port'][
251 'uuid']
252
253 extra = {'key1': 'value1', 'key2': 'value2'}
254
255 patch = [{'path': '/extra/key1',
256 'op': 'add',
257 'value': extra['key1']},
258 {'path': '/extra/key2',
259 'op': 'add',
260 'value': extra['key2']}]
261
262 self.client.update_port(port_id, patch)
263
264 resp, body = self.client.show_port(port_id)
265 self.assertEqual(200, resp.status)
266 self.assertEqual(extra, body['extra'])
267
268 @test.attr(type='smoke')
269 def test_update_port_mixed_ops(self):
270 node_id = self.node['uuid']
271 address = data_utils.rand_mac_address()
272 extra = {'key1': 'value1', 'key2': 'value2'}
273
274 port_id = self.create_port(node_id=node_id, address=address,
275 extra=extra)['port']['uuid']
276
277 new_address = data_utils.rand_mac_address()
278 new_extra = {'key1': 'new-value1', 'key3': 'new-value3'}
279
280 patch = [{'path': '/address',
281 'op': 'replace',
282 'value': new_address},
283 {'path': '/extra/key1',
284 'op': 'replace',
285 'value': new_extra['key1']},
286 {'path': '/extra/key2',
287 'op': 'remove'},
288 {'path': '/extra/key3',
289 'op': 'add',
290 'value': new_extra['key3']}]
291
292 self.client.update_port(port_id, patch)
293
294 resp, body = self.client.show_port(port_id)
295 self.assertEqual(200, resp.status)
296 self.assertEqual(new_address, body['address'])
297 self.assertEqual(new_extra, body['extra'])