summaryrefslogtreecommitdiff
path: root/openstackclient/tests/functional/network/v2/test_subnet_pool.py
blob: f7cb1d74268ba2cee90a80acc9a54e839078375f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import random
import uuid

from openstackclient.tests.functional.network.v2 import common


class SubnetPoolTests(common.NetworkTagTests):
    """Functional tests for subnet pool"""

    base_command = 'subnet pool'

    def setUp(self):
        super(SubnetPoolTests, self).setUp()
        # Nothing in this class works with Nova Network
        if not self.haz_network:
            self.skipTest("No Network service present")

    def test_subnet_pool_create_delete(self):
        """Test create, delete"""
        name1 = uuid.uuid4().hex
        cmd_output, pool_prefix = self._subnet_pool_create("", name1)

        self.assertEqual(name1, cmd_output["name"])
        self.assertEqual([pool_prefix], cmd_output["prefixes"])

        name2 = uuid.uuid4().hex
        cmd_output, pool_prefix = self._subnet_pool_create("", name2)

        self.assertEqual(name2, cmd_output["name"])
        self.assertEqual([pool_prefix], cmd_output["prefixes"])

        del_output = self.openstack(
            'subnet pool delete ' + name1 + ' ' + name2,
        )
        self.assertOutput('', del_output)

    def test_subnet_pool_list(self):
        """Test create, list filter"""
        cmd_output = self.openstack('token issue', parse_output=True)
        auth_project_id = cmd_output['project_id']

        cmd_output = self.openstack('project list', parse_output=True)
        admin_project_id = None
        demo_project_id = None
        for p in cmd_output:
            if p['Name'] == 'admin':
                admin_project_id = p['ID']
            if p['Name'] == 'demo':
                demo_project_id = p['ID']

        # Verify assumptions:
        # * admin and demo projects are present
        # * demo and admin are distinct projects
        # * tests run as admin
        self.assertIsNotNone(admin_project_id)
        self.assertIsNotNone(demo_project_id)
        self.assertNotEqual(admin_project_id, demo_project_id)
        self.assertEqual(admin_project_id, auth_project_id)

        name1 = uuid.uuid4().hex
        name2 = uuid.uuid4().hex

        cmd_output, pool_prefix = self._subnet_pool_create(
            '--project ' + demo_project_id + ' --no-share ',
            name1,
        )
        self.addCleanup(self.openstack, 'subnet pool delete ' + name1)
        self.assertEqual(
            name1,
            cmd_output["name"],
        )
        self.assertEqual(
            False,
            cmd_output["shared"],
        )
        self.assertEqual(
            demo_project_id,
            cmd_output["project_id"],
        )
        self.assertEqual(
            [pool_prefix],
            cmd_output["prefixes"],
        )

        cmd_output, pool_prefix = self._subnet_pool_create(
            ' --share ',
            name2,
        )
        self.addCleanup(self.openstack, 'subnet pool delete ' + name2)
        self.assertEqual(
            name2,
            cmd_output["name"],
        )
        self.assertEqual(
            True,
            cmd_output["shared"],
        )
        self.assertEqual(
            admin_project_id,
            cmd_output["project_id"],
        )
        self.assertEqual(
            [pool_prefix],
            cmd_output["prefixes"],
        )

        # Test list --project
        cmd_output = self.openstack(
            'subnet pool list ' + '--project ' + demo_project_id,
            parse_output=True,
        )
        names = [x["Name"] for x in cmd_output]
        self.assertIn(name1, names)
        self.assertNotIn(name2, names)

        # Test list --share
        cmd_output = self.openstack(
            'subnet pool list ' + '--share',
            parse_output=True,
        )
        names = [x["Name"] for x in cmd_output]
        self.assertNotIn(name1, names)
        self.assertIn(name2, names)

        # Test list --name
        cmd_output = self.openstack(
            'subnet pool list ' + '--name ' + name1,
            parse_output=True,
        )
        names = [x["Name"] for x in cmd_output]
        self.assertIn(name1, names)
        self.assertNotIn(name2, names)

        # Test list --long
        cmd_output = self.openstack(
            'subnet pool list ' + '--long ',
            parse_output=True,
        )
        names = [x["Name"] for x in cmd_output]
        self.assertIn(name1, names)
        self.assertIn(name2, names)

    def test_subnet_pool_set_show(self):
        """Test create, delete, set, show, unset"""

        name = uuid.uuid4().hex
        new_name = name + "_"
        cmd_output, pool_prefix = self._subnet_pool_create(
            '--default-prefix-length 16 '
            + '--min-prefix-length 16 '
            + '--max-prefix-length 32 '
            + '--description aaaa '
            + '--default-quota 10 ',
            name,
        )

        self.addCleanup(
            self.openstack,
            'subnet pool delete ' + cmd_output['id'],
        )
        self.assertEqual(
            name,
            cmd_output["name"],
        )
        self.assertEqual(
            'aaaa',
            cmd_output["description"],
        )
        self.assertEqual(
            [pool_prefix],
            cmd_output["prefixes"],
        )
        self.assertEqual(
            16,
            cmd_output["default_prefixlen"],
        )
        self.assertEqual(
            16,
            cmd_output["min_prefixlen"],
        )
        self.assertEqual(
            32,
            cmd_output["max_prefixlen"],
        )
        self.assertEqual(
            10,
            cmd_output["default_quota"],
        )

        # Test set
        cmd_output = self.openstack(
            'subnet pool set '
            + '--name '
            + new_name
            + ' --description bbbb '
            + ' --pool-prefix 10.110.0.0/16 '
            + '--default-prefix-length 8 '
            + '--min-prefix-length 8 '
            + '--max-prefix-length 16 '
            + '--default-quota 20 '
            + name,
        )
        self.assertOutput('', cmd_output)

        cmd_output = self.openstack(
            'subnet pool show ' + new_name,
            parse_output=True,
        )
        self.assertEqual(
            new_name,
            cmd_output["name"],
        )
        self.assertEqual(
            'bbbb',
            cmd_output["description"],
        )
        self.assertEqual(
            sorted(["10.110.0.0/16", pool_prefix]),
            sorted(cmd_output["prefixes"]),
        )
        self.assertEqual(
            8,
            cmd_output["default_prefixlen"],
        )
        self.assertEqual(
            8,
            cmd_output["min_prefixlen"],
        )
        self.assertEqual(
            16,
            cmd_output["max_prefixlen"],
        )
        self.assertEqual(
            20,
            cmd_output["default_quota"],
        )

        # Test unset
        # NOTE(dtroyer): The unset command --default-quota option DOES NOT
        #                WORK after a default quota has been set once on a
        #                pool.  The error appears to be in a lower layer,
        #                once that is fixed add a test for subnet pool unset
        #                --default-quota.
        #                The unset command of --pool-prefixes also doesn't work
        #                right now. It would be fixed in a separate patch once
        #                the lower layer is fixed.
        # cmd_output = self.openstack(
        #    '--debug ' +
        #    'subnet pool unset ' +
        #    ' --pool-prefix 10.110.0.0/16 ' +
        #    new_name,
        # )
        # self.assertOutput('', cmd_output)
        # self.assertNone(cmd_output["prefixes"])

    def _subnet_pool_create(self, cmd, name, is_type_ipv4=True):
        """Make a random subnet pool

        :param string cmd:
            The options for a subnet pool create command, not including
            --pool-prefix and <name>
        :param string name:
            The name of the subnet pool
        :param bool is_type_ipv4:
            Creates an IPv4 pool if True, creates an IPv6 pool otherwise

        Try random subnet ranges because we can not determine ahead of time
        what subnets are already in use, possibly by another test running in
        parallel, try 4 times before failing.
        """
        for i in range(4):
            # Create a random prefix
            if is_type_ipv4:
                pool_prefix = (
                    ".".join(
                        map(
                            str,
                            (random.randint(0, 223) for _ in range(2)),
                        )
                    )
                    + ".0.0/16"
                )
            else:
                pool_prefix = (
                    ":".join(
                        map(
                            str,
                            (
                                hex(random.randint(0, 65535))[2:]
                                for _ in range(6)
                            ),
                        )
                    )
                    + ":0:0/96"
                )

            try:
                cmd_output = self.openstack(
                    'subnet pool create '
                    + cmd
                    + ' '
                    + '--pool-prefix '
                    + pool_prefix
                    + ' '
                    + name,
                    parse_output=True,
                )
            except Exception:
                if i == 3:
                    # Raise the exception the last time
                    raise
                pass
            else:
                # Break and no longer retry if create is successful
                break

        return cmd_output, pool_prefix

    def _create_resource_for_tag_test(self, name, args):
        cmd_output, _pool_prefix = self._subnet_pool_create(args, name)
        return cmd_output