|
10 | 10 | # License for the specific language governing permissions and limitations |
11 | 11 | # under the License. |
12 | 12 |
|
| 13 | +import json |
| 14 | +import random |
13 | 15 | import uuid |
14 | 16 |
|
15 | 17 | from openstackclient.tests.functional import base |
16 | 18 |
|
17 | 19 |
|
18 | 20 | class SubnetPoolTests(base.TestCase): |
19 | | - """Functional tests for subnet pool. """ |
20 | | - NAME = uuid.uuid4().hex |
21 | | - CREATE_POOL_PREFIX = '10.100.0.0/24' |
22 | | - SET_POOL_PREFIX = '10.100.0.0/16' |
23 | | - HEADERS = ['Name'] |
24 | | - FIELDS = ['name'] |
25 | | - |
26 | | - @classmethod |
27 | | - def setUpClass(cls): |
28 | | - opts = cls.get_opts(cls.FIELDS) |
29 | | - raw_output = cls.openstack('subnet pool create --pool-prefix ' + |
30 | | - cls.CREATE_POOL_PREFIX + ' ' + |
31 | | - cls.NAME + opts) |
32 | | - cls.assertOutput(cls.NAME + '\n', raw_output) |
33 | | - |
34 | | - @classmethod |
35 | | - def tearDownClass(cls): |
36 | | - raw_output = cls.openstack('subnet pool delete ' + cls.NAME) |
37 | | - cls.assertOutput('', raw_output) |
38 | | - |
39 | | - def test_subnet_list(self): |
40 | | - opts = self.get_opts(self.HEADERS) |
41 | | - raw_output = self.openstack('subnet pool list' + opts) |
42 | | - self.assertIn(self.NAME, raw_output) |
43 | | - |
44 | | - def test_subnet_set(self): |
45 | | - self.openstack('subnet pool set --pool-prefix ' + |
46 | | - self.SET_POOL_PREFIX + ' ' + self.NAME) |
47 | | - opts = self.get_opts(['prefixes', 'name']) |
48 | | - raw_output = self.openstack('subnet pool show ' + self.NAME + opts) |
49 | | - self.assertEqual(self.NAME + '\n' + self.SET_POOL_PREFIX + '\n', |
50 | | - raw_output) |
51 | | - |
52 | | - def test_subnet_show(self): |
53 | | - opts = self.get_opts(self.FIELDS) |
54 | | - raw_output = self.openstack('subnet pool show ' + self.NAME + opts) |
55 | | - self.assertEqual(self.NAME + '\n', raw_output) |
| 21 | + """Functional tests for subnet pool""" |
| 22 | + |
| 23 | + def test_subnet_pool_create_delete(self): |
| 24 | + """Test create, delete""" |
| 25 | + name1 = uuid.uuid4().hex |
| 26 | + cmd_output, pool_prefix = self._subnet_pool_create("", name1) |
| 27 | + |
| 28 | + self.assertEqual( |
| 29 | + name1, |
| 30 | + cmd_output["name"] |
| 31 | + ) |
| 32 | + self.assertEqual( |
| 33 | + pool_prefix, |
| 34 | + cmd_output["prefixes"] |
| 35 | + ) |
| 36 | + |
| 37 | + name2 = uuid.uuid4().hex |
| 38 | + cmd_output, pool_prefix = self._subnet_pool_create("", name2) |
| 39 | + |
| 40 | + self.assertEqual( |
| 41 | + name2, |
| 42 | + cmd_output["name"] |
| 43 | + ) |
| 44 | + self.assertEqual( |
| 45 | + pool_prefix, |
| 46 | + cmd_output["prefixes"] |
| 47 | + ) |
| 48 | + |
| 49 | + del_output = self.openstack( |
| 50 | + 'subnet pool delete ' + name1 + ' ' + name2, |
| 51 | + ) |
| 52 | + self.assertOutput('', del_output) |
| 53 | + |
| 54 | + def test_subnet_pool_list(self): |
| 55 | + """Test create, list filter""" |
| 56 | + cmd_output = json.loads(self.openstack('token issue -f json')) |
| 57 | + auth_project_id = cmd_output['project_id'] |
| 58 | + |
| 59 | + cmd_output = json.loads(self.openstack('project list -f json')) |
| 60 | + admin_project_id = None |
| 61 | + demo_project_id = None |
| 62 | + for p in cmd_output: |
| 63 | + if p['Name'] == 'admin': |
| 64 | + admin_project_id = p['ID'] |
| 65 | + if p['Name'] == 'demo': |
| 66 | + demo_project_id = p['ID'] |
| 67 | + |
| 68 | + # Verify assumptions: |
| 69 | + # * admin and demo projects are present |
| 70 | + # * demo and admin are distinct projects |
| 71 | + # * tests run as admin |
| 72 | + self.assertIsNotNone(admin_project_id) |
| 73 | + self.assertIsNotNone(demo_project_id) |
| 74 | + self.assertNotEqual(admin_project_id, demo_project_id) |
| 75 | + self.assertEqual(admin_project_id, auth_project_id) |
| 76 | + |
| 77 | + name1 = uuid.uuid4().hex |
| 78 | + name2 = uuid.uuid4().hex |
| 79 | + |
| 80 | + cmd_output, pool_prefix = self._subnet_pool_create( |
| 81 | + '--project ' + demo_project_id + |
| 82 | + ' --no-share ', |
| 83 | + name1, |
| 84 | + ) |
| 85 | + self.addCleanup(self.openstack, 'subnet pool delete ' + name1) |
| 86 | + self.assertEqual( |
| 87 | + name1, |
| 88 | + cmd_output["name"], |
| 89 | + ) |
| 90 | + self.assertEqual( |
| 91 | + False, |
| 92 | + cmd_output["shared"], |
| 93 | + ) |
| 94 | + self.assertEqual( |
| 95 | + demo_project_id, |
| 96 | + cmd_output["project_id"], |
| 97 | + ) |
| 98 | + self.assertEqual( |
| 99 | + pool_prefix, |
| 100 | + cmd_output["prefixes"], |
| 101 | + ) |
| 102 | + |
| 103 | + cmd_output, pool_prefix = self._subnet_pool_create( |
| 104 | + ' --share ', |
| 105 | + name2, |
| 106 | + ) |
| 107 | + self.addCleanup(self.openstack, 'subnet pool delete ' + name2) |
| 108 | + self.assertEqual( |
| 109 | + name2, |
| 110 | + cmd_output["name"], |
| 111 | + ) |
| 112 | + self.assertEqual( |
| 113 | + True, |
| 114 | + cmd_output["shared"], |
| 115 | + ) |
| 116 | + self.assertEqual( |
| 117 | + admin_project_id, |
| 118 | + cmd_output["project_id"], |
| 119 | + ) |
| 120 | + self.assertEqual( |
| 121 | + pool_prefix, |
| 122 | + cmd_output["prefixes"], |
| 123 | + ) |
| 124 | + |
| 125 | + # Test list --project |
| 126 | + cmd_output = json.loads(self.openstack( |
| 127 | + 'subnet pool list -f json ' + |
| 128 | + '--project ' + demo_project_id |
| 129 | + )) |
| 130 | + names = [x["Name"] for x in cmd_output] |
| 131 | + self.assertIn(name1, names) |
| 132 | + self.assertNotIn(name2, names) |
| 133 | + |
| 134 | + # Test list --share |
| 135 | + cmd_output = json.loads(self.openstack( |
| 136 | + 'subnet pool list -f json ' + |
| 137 | + '--share' |
| 138 | + )) |
| 139 | + names = [x["Name"] for x in cmd_output] |
| 140 | + self.assertNotIn(name1, names) |
| 141 | + self.assertIn(name2, names) |
| 142 | + |
| 143 | + # Test list --name |
| 144 | + cmd_output = json.loads(self.openstack( |
| 145 | + 'subnet pool list -f json ' + |
| 146 | + '--name ' + name1 |
| 147 | + )) |
| 148 | + names = [x["Name"] for x in cmd_output] |
| 149 | + self.assertIn(name1, names) |
| 150 | + self.assertNotIn(name2, names) |
| 151 | + |
| 152 | + # Test list --long |
| 153 | + cmd_output = json.loads(self.openstack( |
| 154 | + 'subnet pool list -f json ' + |
| 155 | + '--long ' |
| 156 | + )) |
| 157 | + names = [x["Name"] for x in cmd_output] |
| 158 | + self.assertIn(name1, names) |
| 159 | + self.assertIn(name2, names) |
| 160 | + |
| 161 | + def test_subnet_pool_set_show(self): |
| 162 | + """Test create, set, show, delete""" |
| 163 | + |
| 164 | + name = uuid.uuid4().hex |
| 165 | + new_name = name + "_" |
| 166 | + cmd_output, pool_prefix = self._subnet_pool_create( |
| 167 | + '--default-prefix-length 16 ' + |
| 168 | + '--min-prefix-length 16 ' + |
| 169 | + '--max-prefix-length 32 ' + |
| 170 | + '--description aaaa ', |
| 171 | + name, |
| 172 | + ) |
| 173 | + |
| 174 | + self.addCleanup(self.openstack, 'subnet pool delete ' + new_name) |
| 175 | + self.assertEqual( |
| 176 | + name, |
| 177 | + cmd_output["name"], |
| 178 | + ) |
| 179 | + self.assertEqual( |
| 180 | + 'aaaa', |
| 181 | + cmd_output["description"], |
| 182 | + ) |
| 183 | + self.assertEqual( |
| 184 | + pool_prefix, |
| 185 | + cmd_output["prefixes"], |
| 186 | + ) |
| 187 | + self.assertEqual( |
| 188 | + 16, |
| 189 | + cmd_output["default_prefixlen"], |
| 190 | + ) |
| 191 | + self.assertEqual( |
| 192 | + 16, |
| 193 | + cmd_output["min_prefixlen"], |
| 194 | + ) |
| 195 | + self.assertEqual( |
| 196 | + 32, |
| 197 | + cmd_output["max_prefixlen"], |
| 198 | + ) |
| 199 | + |
| 200 | + # Test set |
| 201 | + cmd_output = self.openstack( |
| 202 | + 'subnet pool set ' + |
| 203 | + '--name ' + new_name + |
| 204 | + ' --description bbbb ' + |
| 205 | + ' --pool-prefix 10.110.0.0/16 ' + |
| 206 | + '--default-prefix-length 8 ' + |
| 207 | + '--min-prefix-length 8 ' + |
| 208 | + '--max-prefix-length 16 ' + |
| 209 | + name |
| 210 | + ) |
| 211 | + self.assertOutput('', cmd_output) |
| 212 | + |
| 213 | + cmd_output = json.loads(self.openstack( |
| 214 | + 'subnet pool show -f json ' + |
| 215 | + new_name |
| 216 | + )) |
| 217 | + self.assertEqual( |
| 218 | + new_name, |
| 219 | + cmd_output["name"], |
| 220 | + ) |
| 221 | + self.assertEqual( |
| 222 | + 'bbbb', |
| 223 | + cmd_output["description"], |
| 224 | + ) |
| 225 | + self.assertInOutput( |
| 226 | + "10.110.0.0/16", |
| 227 | + cmd_output["prefixes"], |
| 228 | + ) |
| 229 | + self.assertEqual( |
| 230 | + 8, |
| 231 | + cmd_output["default_prefixlen"], |
| 232 | + ) |
| 233 | + self.assertEqual( |
| 234 | + 8, |
| 235 | + cmd_output["min_prefixlen"], |
| 236 | + ) |
| 237 | + self.assertEqual( |
| 238 | + 16, |
| 239 | + cmd_output["max_prefixlen"], |
| 240 | + ) |
| 241 | + |
| 242 | + def _subnet_pool_create(self, cmd, name, is_type_ipv4=True): |
| 243 | + """Make a random subnet pool |
| 244 | +
|
| 245 | + :param string cmd: |
| 246 | + The options for a subnet pool create command, not including |
| 247 | + --pool-prefix and <name> |
| 248 | + :param string name: |
| 249 | + The name of the subnet pool |
| 250 | + :param bool is_type_ipv4: |
| 251 | + Creates an IPv4 pool if True, creates an IPv6 pool otherwise |
| 252 | +
|
| 253 | + Try random subnet ranges because we can not determine ahead of time |
| 254 | + what subnets are already in use, possibly by another test running in |
| 255 | + parallel, try 4 times before failing. |
| 256 | + """ |
| 257 | + for i in range(4): |
| 258 | + # Create a random prefix |
| 259 | + if is_type_ipv4: |
| 260 | + pool_prefix = ".".join(map( |
| 261 | + str, |
| 262 | + (random.randint(0, 223) for _ in range(2)), |
| 263 | + )) + ".0.0/16" |
| 264 | + else: |
| 265 | + pool_prefix = ":".join(map( |
| 266 | + str, |
| 267 | + (hex(random.randint(0, 65535))[2:] for _ in range(6)), |
| 268 | + )) + ":0:0/96" |
| 269 | + |
| 270 | + try: |
| 271 | + cmd_output = json.loads(self.openstack( |
| 272 | + 'subnet pool create -f json ' + |
| 273 | + cmd + ' ' + |
| 274 | + '--pool-prefix ' + pool_prefix + ' ' + |
| 275 | + name |
| 276 | + )) |
| 277 | + except Exception: |
| 278 | + if (i == 3): |
| 279 | + # Raise the exception the last time |
| 280 | + raise |
| 281 | + pass |
| 282 | + else: |
| 283 | + # Break and no longer retry if create is sucessful |
| 284 | + break |
| 285 | + |
| 286 | + return cmd_output, pool_prefix |
0 commit comments