blob: 4ac7e29f0437424d01ea1ec858403f967a698488 [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
Mh Raiesa9bb79d2014-04-17 16:20:17 +053025 _, self.chassis = self.create_chassis()
26 _, self.node = self.create_node(self.chassis['uuid'])
27 _, self.port = self.create_port(self.node['uuid'],
28 data_utils.rand_mac_address())
29
30 def _assertExpected(self, expected, actual):
31 # Check if not expected keys/values exists in actual response body
32 for key, value in expected.iteritems():
33 if key not in ('created_at', 'updated_at'):
34 self.assertIn(key, actual)
35 self.assertEqual(value, actual[key])
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030036
37 @test.attr(type='smoke')
38 def test_create_port(self):
39 node_id = self.node['uuid']
40 address = data_utils.rand_mac_address()
41
Mh Raiesa9bb79d2014-04-17 16:20:17 +053042 resp, port = self.create_port(node_id=node_id, address=address)
43 self.assertEqual(201, resp.status)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040044
45 resp, body = self.client.show_port(port['uuid'])
46
47 self.assertEqual(200, resp.status)
Mh Raiesa9bb79d2014-04-17 16:20:17 +053048 self._assertExpected(port, body)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040049
50 @test.attr(type='smoke')
51 def test_create_port_specifying_uuid(self):
52 node_id = self.node['uuid']
53 address = data_utils.rand_mac_address()
54 uuid = data_utils.rand_uuid()
55
Mh Raiesa9bb79d2014-04-17 16:20:17 +053056 resp, port = self.create_port(node_id=node_id,
57 address=address, uuid=uuid)
58 self.assertEqual(201, resp.status)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040059
60 resp, body = self.client.show_port(uuid)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040061 self.assertEqual(200, resp.status)
Mh Raiesa9bb79d2014-04-17 16:20:17 +053062 self._assertExpected(port, body)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040063
64 @test.attr(type='smoke')
65 def test_create_port_with_extra(self):
66 node_id = self.node['uuid']
67 address = data_utils.rand_mac_address()
68 extra = {'key': 'value'}
69
Mh Raiesa9bb79d2014-04-17 16:20:17 +053070 resp, port = self.create_port(node_id=node_id, address=address,
71 extra=extra)
72 self.assertEqual(201, resp.status)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040073
74 resp, body = self.client.show_port(port['uuid'])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040075 self.assertEqual(200, resp.status)
Mh Raiesa9bb79d2014-04-17 16:20:17 +053076 self._assertExpected(port, body)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030077
78 @test.attr(type='smoke')
79 def test_delete_port(self):
80 node_id = self.node['uuid']
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040081 address = data_utils.rand_mac_address()
Mh Raiesa9bb79d2014-04-17 16:20:17 +053082 resp, port = self.create_port(node_id=node_id, address=address)
83 self.assertEqual(201, resp.status)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030084
Mh Raiesa9bb79d2014-04-17 16:20:17 +053085 resp = self.delete_port(port['uuid'])
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030086
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040087 self.assertEqual(204, resp.status)
Mh Raiesa9bb79d2014-04-17 16:20:17 +053088 self.assertRaises(exc.NotFound, self.client.show_port, port['uuid'])
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030089
90 @test.attr(type='smoke')
91 def test_show_port(self):
Mh Raiesa9bb79d2014-04-17 16:20:17 +053092 resp, port = self.client.show_port(self.port['uuid'])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040093 self.assertEqual(200, resp.status)
Mh Raiesa9bb79d2014-04-17 16:20:17 +053094 self._assertExpected(self.port, port)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040095
96 @test.attr(type='smoke')
97 def test_show_port_with_links(self):
Mh Raiesa9bb79d2014-04-17 16:20:17 +053098 resp, port = self.client.show_port(self.port['uuid'])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040099 self.assertEqual(200, resp.status)
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530100 self.assertIn('links', port.keys())
101 self.assertEqual(2, len(port['links']))
102 self.assertIn(port['uuid'], port['links'][0]['href'])
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300103
104 @test.attr(type='smoke')
105 def test_list_ports(self):
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300106 resp, body = self.client.list_ports()
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400107 self.assertEqual(200, resp.status)
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530108 self.assertIn(self.port['uuid'],
109 [i['uuid'] for i in body['ports']])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400110 # Verify self links.
111 for port in body['ports']:
112 self.validate_self_link('ports', port['uuid'],
113 port['links'][0]['href'])
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300114
115 @test.attr(type='smoke')
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400116 def test_list_with_limit(self):
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400117 resp, body = self.client.list_ports(limit=3)
118 self.assertEqual(200, resp.status)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400119
120 next_marker = body['ports'][-1]['uuid']
121 self.assertIn(next_marker, body['next'])
122
123 def test_list_ports_details(self):
124 node_id = self.node['uuid']
125
126 uuids = [
127 self.create_port(node_id=node_id,
128 address=data_utils.rand_mac_address())
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530129 [1]['uuid'] for i in range(0, 5)]
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400130
131 resp, body = self.client.list_ports_detail()
132 self.assertEqual(200, resp.status)
133
Dirk Mueller483c6f72014-05-23 12:23:50 +0200134 ports_dict = dict((port['uuid'], port) for port in body['ports']
135 if port['uuid'] in uuids)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400136
137 for uuid in uuids:
138 self.assertIn(uuid, ports_dict)
139 port = ports_dict[uuid]
140 self.assertIn('extra', port)
141 self.assertIn('node_uuid', port)
142 # never expose the node_id
143 self.assertNotIn('node_id', port)
144 # Verify self link.
145 self.validate_self_link('ports', port['uuid'],
146 port['links'][0]['href'])
147
Yuiko Takada3083fca2014-04-25 11:52:33 +0000148 def test_list_ports_details_with_address(self):
149 node_id = self.node['uuid']
150 address = data_utils.rand_mac_address()
151 self.create_port(node_id=node_id, address=address)
152 for i in range(0, 5):
153 self.create_port(node_id=node_id,
154 address=data_utils.rand_mac_address())
155
156 resp, body = self.client.list_ports_detail(address=address)
157 self.assertEqual(200, resp.status)
158 self.assertEqual(1, len(body['ports']))
159 self.assertEqual(address, body['ports'][0]['address'])
160
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400161 @test.attr(type='smoke')
162 def test_update_port_replace(self):
163 node_id = self.node['uuid']
164 address = data_utils.rand_mac_address()
165 extra = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
166
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530167 resp, port = self.create_port(node_id=node_id, address=address,
168 extra=extra)
169 self.assertEqual(201, resp.status)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300170
171 new_address = data_utils.rand_mac_address()
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400172 new_extra = {'key1': 'new-value1', 'key2': 'new-value2',
173 'key3': 'new-value3'}
174
175 patch = [{'path': '/address',
176 'op': 'replace',
177 'value': new_address},
178 {'path': '/extra/key1',
179 'op': 'replace',
180 'value': new_extra['key1']},
181 {'path': '/extra/key2',
182 'op': 'replace',
183 'value': new_extra['key2']},
184 {'path': '/extra/key3',
185 'op': 'replace',
186 'value': new_extra['key3']}]
187
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530188 resp, _ = self.client.update_port(port['uuid'], patch)
189 self.assertEqual(200, resp.status)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300190
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530191 resp, body = self.client.show_port(port['uuid'])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400192 self.assertEqual(200, resp.status)
193 self.assertEqual(new_address, body['address'])
194 self.assertEqual(new_extra, body['extra'])
195
196 @test.attr(type='smoke')
197 def test_update_port_remove(self):
198 node_id = self.node['uuid']
199 address = data_utils.rand_mac_address()
200 extra = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
201
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530202 resp, port = self.create_port(node_id=node_id, address=address,
203 extra=extra)
204 self.assertEqual(201, resp.status)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400205
206 # Removing one item from the collection
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530207 resp, _ = self.client.update_port(port['uuid'],
208 [{'path': '/extra/key2',
209 'op': 'remove'}])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400210 self.assertEqual(200, resp.status)
211 extra.pop('key2')
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530212 resp, body = self.client.show_port(port['uuid'])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400213 self.assertEqual(200, resp.status)
214 self.assertEqual(extra, body['extra'])
215
216 # Removing the collection
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530217 resp, _ = self.client.update_port(port['uuid'], [{'path': '/extra',
218 'op': 'remove'}])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400219 self.assertEqual(200, resp.status)
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530220 resp, body = self.client.show_port(port['uuid'])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400221 self.assertEqual(200, resp.status)
222 self.assertEqual({}, body['extra'])
223
224 # Assert nothing else was changed
225 self.assertEqual(node_id, body['node_uuid'])
226 self.assertEqual(address, body['address'])
227
228 @test.attr(type='smoke')
229 def test_update_port_add(self):
230 node_id = self.node['uuid']
231 address = data_utils.rand_mac_address()
232
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530233 resp, port = self.create_port(node_id=node_id, address=address)
234 self.assertEqual(201, resp.status)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400235
236 extra = {'key1': 'value1', 'key2': 'value2'}
237
238 patch = [{'path': '/extra/key1',
239 'op': 'add',
240 'value': extra['key1']},
241 {'path': '/extra/key2',
242 'op': 'add',
243 'value': extra['key2']}]
244
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530245 resp, _ = self.client.update_port(port['uuid'], patch)
246 self.assertEqual(200, resp.status)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400247
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530248 resp, body = self.client.show_port(port['uuid'])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400249 self.assertEqual(200, resp.status)
250 self.assertEqual(extra, body['extra'])
251
252 @test.attr(type='smoke')
253 def test_update_port_mixed_ops(self):
254 node_id = self.node['uuid']
255 address = data_utils.rand_mac_address()
256 extra = {'key1': 'value1', 'key2': 'value2'}
257
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530258 resp, port = self.create_port(node_id=node_id, address=address,
259 extra=extra)
260 self.assertEqual(201, resp.status)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400261
262 new_address = data_utils.rand_mac_address()
263 new_extra = {'key1': 'new-value1', 'key3': 'new-value3'}
264
265 patch = [{'path': '/address',
266 'op': 'replace',
267 'value': new_address},
268 {'path': '/extra/key1',
269 'op': 'replace',
270 'value': new_extra['key1']},
271 {'path': '/extra/key2',
272 'op': 'remove'},
273 {'path': '/extra/key3',
274 'op': 'add',
275 'value': new_extra['key3']}]
276
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530277 resp, _ = self.client.update_port(port['uuid'], patch)
278 self.assertEqual(200, resp.status)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400279
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530280 resp, body = self.client.show_port(port['uuid'])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400281 self.assertEqual(200, resp.status)
282 self.assertEqual(new_address, body['address'])
283 self.assertEqual(new_extra, body['extra'])