blob: b3f9b7f48bdc63c3b4d7d9971a6e64a62f946202 [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
Adam Gandelman2a86f1c2014-06-18 11:34:42 -070013from tempest.api.baremetal.admin import base
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030014from 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
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +000042 _, port = self.create_port(node_id=node_id, address=address)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040043
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +000044 _, body = self.client.show_port(port['uuid'])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040045
Mh Raiesa9bb79d2014-04-17 16:20:17 +053046 self._assertExpected(port, body)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040047
48 @test.attr(type='smoke')
49 def test_create_port_specifying_uuid(self):
50 node_id = self.node['uuid']
51 address = data_utils.rand_mac_address()
52 uuid = data_utils.rand_uuid()
53
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +000054 _, port = self.create_port(node_id=node_id,
55 address=address, uuid=uuid)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040056
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +000057 _, body = self.client.show_port(uuid)
Mh Raiesa9bb79d2014-04-17 16:20:17 +053058 self._assertExpected(port, body)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040059
60 @test.attr(type='smoke')
61 def test_create_port_with_extra(self):
62 node_id = self.node['uuid']
63 address = data_utils.rand_mac_address()
64 extra = {'key': 'value'}
65
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +000066 _, port = self.create_port(node_id=node_id, address=address,
67 extra=extra)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040068
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +000069 _, body = self.client.show_port(port['uuid'])
Mh Raiesa9bb79d2014-04-17 16:20:17 +053070 self._assertExpected(port, body)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030071
72 @test.attr(type='smoke')
73 def test_delete_port(self):
74 node_id = self.node['uuid']
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040075 address = data_utils.rand_mac_address()
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +000076 _, port = self.create_port(node_id=node_id, address=address)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030077
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +000078 self.delete_port(port['uuid'])
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030079
Mh Raiesa9bb79d2014-04-17 16:20:17 +053080 self.assertRaises(exc.NotFound, self.client.show_port, port['uuid'])
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030081
82 @test.attr(type='smoke')
83 def test_show_port(self):
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +000084 _, port = self.client.show_port(self.port['uuid'])
Mh Raiesa9bb79d2014-04-17 16:20:17 +053085 self._assertExpected(self.port, port)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040086
87 @test.attr(type='smoke')
88 def test_show_port_with_links(self):
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +000089 _, port = self.client.show_port(self.port['uuid'])
Mh Raiesa9bb79d2014-04-17 16:20:17 +053090 self.assertIn('links', port.keys())
91 self.assertEqual(2, len(port['links']))
92 self.assertIn(port['uuid'], port['links'][0]['href'])
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030093
94 @test.attr(type='smoke')
95 def test_list_ports(self):
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +000096 _, body = self.client.list_ports()
Mh Raiesa9bb79d2014-04-17 16:20:17 +053097 self.assertIn(self.port['uuid'],
98 [i['uuid'] for i in body['ports']])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +040099 # Verify self links.
100 for port in body['ports']:
101 self.validate_self_link('ports', port['uuid'],
102 port['links'][0]['href'])
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300103
104 @test.attr(type='smoke')
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400105 def test_list_with_limit(self):
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +0000106 _, body = self.client.list_ports(limit=3)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400107
108 next_marker = body['ports'][-1]['uuid']
109 self.assertIn(next_marker, body['next'])
110
111 def test_list_ports_details(self):
112 node_id = self.node['uuid']
113
114 uuids = [
115 self.create_port(node_id=node_id,
116 address=data_utils.rand_mac_address())
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530117 [1]['uuid'] for i in range(0, 5)]
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400118
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +0000119 _, body = self.client.list_ports_detail()
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400120
Dirk Mueller483c6f72014-05-23 12:23:50 +0200121 ports_dict = dict((port['uuid'], port) for port in body['ports']
122 if port['uuid'] in uuids)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400123
124 for uuid in uuids:
125 self.assertIn(uuid, ports_dict)
126 port = ports_dict[uuid]
127 self.assertIn('extra', port)
128 self.assertIn('node_uuid', port)
129 # never expose the node_id
130 self.assertNotIn('node_id', port)
131 # Verify self link.
132 self.validate_self_link('ports', port['uuid'],
133 port['links'][0]['href'])
134
Yuiko Takada3083fca2014-04-25 11:52:33 +0000135 def test_list_ports_details_with_address(self):
136 node_id = self.node['uuid']
137 address = data_utils.rand_mac_address()
138 self.create_port(node_id=node_id, address=address)
139 for i in range(0, 5):
140 self.create_port(node_id=node_id,
141 address=data_utils.rand_mac_address())
142
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +0000143 _, body = self.client.list_ports_detail(address=address)
Yuiko Takada3083fca2014-04-25 11:52:33 +0000144 self.assertEqual(1, len(body['ports']))
145 self.assertEqual(address, body['ports'][0]['address'])
146
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400147 @test.attr(type='smoke')
148 def test_update_port_replace(self):
149 node_id = self.node['uuid']
150 address = data_utils.rand_mac_address()
151 extra = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
152
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +0000153 _, port = self.create_port(node_id=node_id, address=address,
154 extra=extra)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300155
156 new_address = data_utils.rand_mac_address()
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400157 new_extra = {'key1': 'new-value1', 'key2': 'new-value2',
158 'key3': 'new-value3'}
159
160 patch = [{'path': '/address',
161 'op': 'replace',
162 'value': new_address},
163 {'path': '/extra/key1',
164 'op': 'replace',
165 'value': new_extra['key1']},
166 {'path': '/extra/key2',
167 'op': 'replace',
168 'value': new_extra['key2']},
169 {'path': '/extra/key3',
170 'op': 'replace',
171 'value': new_extra['key3']}]
172
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +0000173 self.client.update_port(port['uuid'], patch)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300174
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +0000175 _, body = self.client.show_port(port['uuid'])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400176 self.assertEqual(new_address, body['address'])
177 self.assertEqual(new_extra, body['extra'])
178
179 @test.attr(type='smoke')
180 def test_update_port_remove(self):
181 node_id = self.node['uuid']
182 address = data_utils.rand_mac_address()
183 extra = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
184
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +0000185 _, port = self.create_port(node_id=node_id, address=address,
186 extra=extra)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400187
188 # Removing one item from the collection
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +0000189 self.client.update_port(port['uuid'],
190 [{'path': '/extra/key2',
191 'op': 'remove'}])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400192 extra.pop('key2')
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +0000193 _, body = self.client.show_port(port['uuid'])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400194 self.assertEqual(extra, body['extra'])
195
196 # Removing the collection
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +0000197 self.client.update_port(port['uuid'], [{'path': '/extra',
198 'op': 'remove'}])
199 _, body = self.client.show_port(port['uuid'])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400200 self.assertEqual({}, body['extra'])
201
202 # Assert nothing else was changed
203 self.assertEqual(node_id, body['node_uuid'])
204 self.assertEqual(address, body['address'])
205
206 @test.attr(type='smoke')
207 def test_update_port_add(self):
208 node_id = self.node['uuid']
209 address = data_utils.rand_mac_address()
210
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +0000211 _, port = self.create_port(node_id=node_id, address=address)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400212
213 extra = {'key1': 'value1', 'key2': 'value2'}
214
215 patch = [{'path': '/extra/key1',
216 'op': 'add',
217 'value': extra['key1']},
218 {'path': '/extra/key2',
219 'op': 'add',
220 'value': extra['key2']}]
221
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +0000222 self.client.update_port(port['uuid'], patch)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400223
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +0000224 _, body = self.client.show_port(port['uuid'])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400225 self.assertEqual(extra, body['extra'])
226
227 @test.attr(type='smoke')
228 def test_update_port_mixed_ops(self):
229 node_id = self.node['uuid']
230 address = data_utils.rand_mac_address()
231 extra = {'key1': 'value1', 'key2': 'value2'}
232
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +0000233 _, port = self.create_port(node_id=node_id, address=address,
234 extra=extra)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400235
236 new_address = data_utils.rand_mac_address()
237 new_extra = {'key1': 'new-value1', 'key3': 'new-value3'}
238
239 patch = [{'path': '/address',
240 'op': 'replace',
241 'value': new_address},
242 {'path': '/extra/key1',
243 'op': 'replace',
244 'value': new_extra['key1']},
245 {'path': '/extra/key2',
246 'op': 'remove'},
247 {'path': '/extra/key3',
248 'op': 'add',
249 'value': new_extra['key3']}]
250
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +0000251 self.client.update_port(port['uuid'], patch)
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400252
Swapnil Kulkarniaa57d6e2014-08-19 10:40:35 +0000253 _, body = self.client.show_port(port['uuid'])
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400254 self.assertEqual(new_address, body['address'])
255 self.assertEqual(new_extra, body['extra'])