|
10 | 10 | # License for the specific language governing permissions and limitations |
11 | 11 | # under the License. |
12 | 12 |
|
| 13 | +import random |
| 14 | +import re |
13 | 15 | import uuid |
14 | 16 |
|
15 | 17 | from openstackclient.tests.functional import base |
16 | 18 |
|
17 | 19 |
|
18 | 20 | class FloatingIpTests(base.TestCase): |
19 | | - """Functional tests for floating ip. """ |
| 21 | + """Functional tests for floating ip""" |
20 | 22 | SUBNET_NAME = uuid.uuid4().hex |
21 | 23 | NETWORK_NAME = uuid.uuid4().hex |
22 | | - ID = None |
23 | | - HEADERS = ['ID'] |
24 | | - FIELDS = ['id'] |
25 | 24 |
|
26 | 25 | @classmethod |
27 | 26 | def setUpClass(cls): |
28 | | - # Create a network for the floating ip. |
29 | | - cls.openstack('network create --external ' + cls.NETWORK_NAME) |
30 | | - # Create a subnet for the network. |
31 | | - cls.openstack( |
32 | | - 'subnet create --network ' + cls.NETWORK_NAME + |
33 | | - ' --subnet-range 10.10.10.0/24 ' + |
34 | | - cls.SUBNET_NAME |
| 27 | + # Set up some regex for matching below |
| 28 | + cls.re_id = re.compile("id\s+\|\s+(\S+)") |
| 29 | + cls.re_floating_ip = re.compile("floating_ip_address\s+\|\s+(\S+)") |
| 30 | + cls.re_fixed_ip = re.compile("fixed_ip_address\s+\|\s+(\S+)") |
| 31 | + cls.re_description = re.compile("description\s+\|\s+([^|]+?)\s+\|") |
| 32 | + cls.re_network_id = re.compile("floating_network_id\s+\|\s+(\S+)") |
| 33 | + |
| 34 | + # Make a random subnet |
| 35 | + cls.subnet = ".".join(map( |
| 36 | + str, |
| 37 | + (random.randint(0, 255) for _ in range(3)) |
| 38 | + )) + ".0/26" |
| 39 | + |
| 40 | + # Create a network for the floating ip |
| 41 | + raw_output = cls.openstack( |
| 42 | + 'network create --external ' + cls.NETWORK_NAME |
35 | 43 | ) |
36 | | - opts = cls.get_opts(cls.FIELDS) |
| 44 | + cls.network_id = re.search(cls.re_id, raw_output).group(1) |
| 45 | + |
| 46 | + # Create a subnet for the network |
37 | 47 | raw_output = cls.openstack( |
38 | | - 'floating ip create ' + cls.NETWORK_NAME + opts) |
39 | | - cls.ID = raw_output.strip('\n') |
| 48 | + 'subnet create ' + |
| 49 | + '--network ' + cls.NETWORK_NAME + ' ' + |
| 50 | + '--subnet-range ' + cls.subnet + ' ' + |
| 51 | + cls.SUBNET_NAME |
| 52 | + ) |
| 53 | + cls.subnet_id = re.search(cls.re_id, raw_output).group(1) |
40 | 54 |
|
41 | 55 | @classmethod |
42 | 56 | def tearDownClass(cls): |
43 | | - raw_output = cls.openstack('floating ip delete ' + cls.ID) |
44 | | - cls.assertOutput('', raw_output) |
45 | 57 | raw_output = cls.openstack('subnet delete ' + cls.SUBNET_NAME) |
46 | 58 | cls.assertOutput('', raw_output) |
47 | 59 | raw_output = cls.openstack('network delete ' + cls.NETWORK_NAME) |
48 | 60 | cls.assertOutput('', raw_output) |
49 | 61 |
|
| 62 | + def test_floating_ip_delete(self): |
| 63 | + """Test create, delete multiple""" |
| 64 | + raw_output = self.openstack( |
| 65 | + 'floating ip create ' + |
| 66 | + '--description aaaa ' + |
| 67 | + self.NETWORK_NAME |
| 68 | + ) |
| 69 | + re_ip = re.search(self.re_floating_ip, raw_output) |
| 70 | + self.assertIsNotNone(re_ip) |
| 71 | + ip1 = re_ip.group(1) |
| 72 | + self.assertEqual( |
| 73 | + 'aaaa', |
| 74 | + re.search(self.re_description, raw_output).group(1), |
| 75 | + ) |
| 76 | + |
| 77 | + raw_output = self.openstack( |
| 78 | + 'floating ip create ' + |
| 79 | + '--description bbbb ' + |
| 80 | + self.NETWORK_NAME |
| 81 | + ) |
| 82 | + ip2 = re.search(self.re_floating_ip, raw_output).group(1) |
| 83 | + self.assertEqual( |
| 84 | + 'bbbb', |
| 85 | + re.search(self.re_description, raw_output).group(1), |
| 86 | + ) |
| 87 | + |
| 88 | + # Clean up after ourselves |
| 89 | + raw_output = self.openstack('floating ip delete ' + ip1 + ' ' + ip2) |
| 90 | + self.assertOutput('', raw_output) |
| 91 | + |
50 | 92 | def test_floating_ip_list(self): |
51 | | - opts = self.get_opts(self.HEADERS) |
52 | | - raw_output = self.openstack('floating ip list' + opts) |
53 | | - self.assertIn(self.ID, raw_output) |
| 93 | + """Test create defaults, list filters, delete""" |
| 94 | + raw_output = self.openstack( |
| 95 | + 'floating ip create ' + |
| 96 | + '--description aaaa ' + |
| 97 | + self.NETWORK_NAME |
| 98 | + ) |
| 99 | + re_ip = re.search(self.re_floating_ip, raw_output) |
| 100 | + self.assertIsNotNone(re_ip) |
| 101 | + ip1 = re_ip.group(1) |
| 102 | + self.addCleanup(self.openstack, 'floating ip delete ' + ip1) |
| 103 | + self.assertEqual( |
| 104 | + 'aaaa', |
| 105 | + re.search(self.re_description, raw_output).group(1), |
| 106 | + ) |
| 107 | + self.assertIsNotNone(re.search(self.re_network_id, raw_output)) |
| 108 | + |
| 109 | + raw_output = self.openstack( |
| 110 | + 'floating ip create ' + |
| 111 | + '--description bbbb ' + |
| 112 | + self.NETWORK_NAME |
| 113 | + ) |
| 114 | + ip2 = re.search(self.re_floating_ip, raw_output).group(1) |
| 115 | + self.addCleanup(self.openstack, 'floating ip delete ' + ip2) |
| 116 | + self.assertEqual( |
| 117 | + 'bbbb', |
| 118 | + re.search(self.re_description, raw_output).group(1), |
| 119 | + ) |
| 120 | + |
| 121 | + # Test list |
| 122 | + raw_output = self.openstack('floating ip list') |
| 123 | + self.assertIsNotNone(re.search("\|\s+" + ip1 + "\s+\|", raw_output)) |
| 124 | + self.assertIsNotNone(re.search("\|\s+" + ip2 + "\s+\|", raw_output)) |
| 125 | + |
| 126 | + # Test list --long |
| 127 | + raw_output = self.openstack('floating ip list --long') |
| 128 | + self.assertIsNotNone(re.search("\|\s+" + ip1 + "\s+\|", raw_output)) |
| 129 | + self.assertIsNotNone(re.search("\|\s+" + ip2 + "\s+\|", raw_output)) |
| 130 | + |
| 131 | + # TODO(dtroyer): add more filter tests |
54 | 132 |
|
55 | 133 | def test_floating_ip_show(self): |
56 | | - opts = self.get_opts(self.FIELDS) |
57 | | - raw_output = self.openstack('floating ip show ' + self.ID + opts) |
58 | | - self.assertEqual(self.ID + "\n", raw_output) |
| 134 | + """Test show""" |
| 135 | + raw_output = self.openstack( |
| 136 | + 'floating ip create ' + |
| 137 | + '--description shosho ' + |
| 138 | + # '--fixed-ip-address 1.2.3.4 ' + |
| 139 | + self.NETWORK_NAME |
| 140 | + ) |
| 141 | + re_ip = re.search(self.re_floating_ip, raw_output) |
| 142 | + self.assertIsNotNone(re_ip) |
| 143 | + ip = re_ip.group(1) |
| 144 | + |
| 145 | + raw_output = self.openstack('floating ip show ' + ip) |
| 146 | + self.addCleanup(self.openstack, 'floating ip delete ' + ip) |
| 147 | + |
| 148 | + self.assertEqual( |
| 149 | + 'shosho', |
| 150 | + re.search(self.re_description, raw_output).group(1), |
| 151 | + ) |
| 152 | + # TODO(dtroyer): not working??? |
| 153 | + # self.assertEqual( |
| 154 | + # '1.2.3.4', |
| 155 | + # re.search(self.re_floating_ip, raw_output).group(1), |
| 156 | + # ) |
| 157 | + self.assertIsNotNone(re.search(self.re_network_id, raw_output)) |
0 commit comments