|
10 | 10 | # License for the specific language governing permissions and limitations |
11 | 11 | # under the License. |
12 | 12 |
|
| 13 | +import json |
13 | 14 | import uuid |
14 | 15 |
|
15 | 16 | from openstackclient.tests.functional import base |
16 | 17 |
|
17 | 18 |
|
18 | 19 | class RouterTests(base.TestCase): |
19 | 20 | """Functional tests for router. """ |
20 | | - NAME = uuid.uuid4().hex |
21 | | - HEADERS = ['Name'] |
22 | | - FIELDS = ['name'] |
23 | | - |
24 | | - @classmethod |
25 | | - def setUpClass(cls): |
26 | | - opts = cls.get_opts(cls.FIELDS) |
27 | | - raw_output = cls.openstack('router create ' + cls.NAME + opts) |
28 | | - expected = cls.NAME + '\n' |
29 | | - cls.assertOutput(expected, raw_output) |
30 | | - |
31 | | - @classmethod |
32 | | - def tearDownClass(cls): |
33 | | - raw_output = cls.openstack('router delete ' + cls.NAME) |
34 | | - cls.assertOutput('', raw_output) |
| 21 | + |
| 22 | + def test_router_create_and_delete(self): |
| 23 | + """Test create options, delete""" |
| 24 | + name1 = uuid.uuid4().hex |
| 25 | + name2 = uuid.uuid4().hex |
| 26 | + cmd_output = json.loads(self.openstack( |
| 27 | + 'router create -f json ' + |
| 28 | + name1 |
| 29 | + )) |
| 30 | + self.assertEqual( |
| 31 | + name1, |
| 32 | + cmd_output["name"], |
| 33 | + ) |
| 34 | + cmd_output = json.loads(self.openstack( |
| 35 | + 'router create -f json ' + |
| 36 | + name2 |
| 37 | + )) |
| 38 | + self.assertEqual( |
| 39 | + name2, |
| 40 | + cmd_output["name"], |
| 41 | + ) |
| 42 | + |
| 43 | + del_output = self.openstack( |
| 44 | + 'router delete ' + name1 + ' ' + name2) |
| 45 | + self.assertOutput('', del_output) |
35 | 46 |
|
36 | 47 | def test_router_list(self): |
37 | | - opts = self.get_opts(self.HEADERS) |
38 | | - raw_output = self.openstack('router list' + opts) |
39 | | - self.assertIn(self.NAME, raw_output) |
40 | | - |
41 | | - def test_router_set(self): |
42 | | - self.openstack('router set --disable ' + self.NAME) |
43 | | - opts = self.get_opts(['name', 'admin_state_up']) |
44 | | - raw_output = self.openstack('router show ' + self.NAME + opts) |
45 | | - self.assertEqual("DOWN\n" + self.NAME + "\n", raw_output) |
46 | | - |
47 | | - def test_router_show(self): |
48 | | - opts = self.get_opts(self.FIELDS) |
49 | | - raw_output = self.openstack('router show ' + self.NAME + opts) |
50 | | - self.assertEqual(self.NAME + "\n", raw_output) |
| 48 | + """Test create, list filter""" |
| 49 | + # Get project IDs |
| 50 | + cmd_output = json.loads(self.openstack('token issue -f json ')) |
| 51 | + auth_project_id = cmd_output['project_id'] |
| 52 | + |
| 53 | + cmd_output = json.loads(self.openstack('project list -f json ')) |
| 54 | + admin_project_id = None |
| 55 | + demo_project_id = None |
| 56 | + for p in cmd_output: |
| 57 | + if p['Name'] == 'admin': |
| 58 | + admin_project_id = p['ID'] |
| 59 | + if p['Name'] == 'demo': |
| 60 | + demo_project_id = p['ID'] |
| 61 | + |
| 62 | + # Verify assumptions: |
| 63 | + # * admin and demo projects are present |
| 64 | + # * demo and admin are distinct projects |
| 65 | + # * tests run as admin |
| 66 | + self.assertIsNotNone(admin_project_id) |
| 67 | + self.assertIsNotNone(demo_project_id) |
| 68 | + self.assertNotEqual(admin_project_id, demo_project_id) |
| 69 | + self.assertEqual(admin_project_id, auth_project_id) |
| 70 | + |
| 71 | + name1 = uuid.uuid4().hex |
| 72 | + name2 = uuid.uuid4().hex |
| 73 | + cmd_output = json.loads(self.openstack( |
| 74 | + 'router create -f json ' + |
| 75 | + '--disable ' + |
| 76 | + name1 |
| 77 | + )) |
| 78 | + self.assertEqual( |
| 79 | + name1, |
| 80 | + cmd_output["name"], |
| 81 | + ) |
| 82 | + self.assertEqual( |
| 83 | + "DOWN", |
| 84 | + cmd_output["admin_state_up"], |
| 85 | + ) |
| 86 | + self.assertEqual( |
| 87 | + admin_project_id, |
| 88 | + cmd_output["project_id"], |
| 89 | + ) |
| 90 | + cmd_output = json.loads(self.openstack( |
| 91 | + 'router create -f json ' + |
| 92 | + '--project ' + demo_project_id + |
| 93 | + ' ' + name2 |
| 94 | + )) |
| 95 | + self.assertEqual( |
| 96 | + name2, |
| 97 | + cmd_output["name"], |
| 98 | + ) |
| 99 | + self.assertEqual( |
| 100 | + "UP", |
| 101 | + cmd_output["admin_state_up"], |
| 102 | + ) |
| 103 | + self.assertEqual( |
| 104 | + demo_project_id, |
| 105 | + cmd_output["project_id"], |
| 106 | + ) |
| 107 | + |
| 108 | + # Test list --project |
| 109 | + cmd_output = json.loads(self.openstack( |
| 110 | + 'router list -f json ' + |
| 111 | + '--project ' + demo_project_id |
| 112 | + )) |
| 113 | + names = [x["Name"] for x in cmd_output] |
| 114 | + self.assertNotIn(name1, names) |
| 115 | + self.assertIn(name2, names) |
| 116 | + |
| 117 | + # Test list --disable |
| 118 | + cmd_output = json.loads(self.openstack( |
| 119 | + 'router list -f json ' + |
| 120 | + '--disable ' |
| 121 | + )) |
| 122 | + names = [x["Name"] for x in cmd_output] |
| 123 | + self.assertIn(name1, names) |
| 124 | + self.assertNotIn(name2, names) |
| 125 | + |
| 126 | + # Test list --name |
| 127 | + cmd_output = json.loads(self.openstack( |
| 128 | + 'router list -f json ' + |
| 129 | + '--name ' + name1 |
| 130 | + )) |
| 131 | + names = [x["Name"] for x in cmd_output] |
| 132 | + self.assertIn(name1, names) |
| 133 | + self.assertNotIn(name2, names) |
| 134 | + |
| 135 | + # Test list --long |
| 136 | + cmd_output = json.loads(self.openstack( |
| 137 | + 'router list -f json ' + |
| 138 | + '--long ' |
| 139 | + )) |
| 140 | + names = [x["Name"] for x in cmd_output] |
| 141 | + self.assertIn(name1, names) |
| 142 | + self.assertIn(name2, names) |
| 143 | + |
| 144 | + del_output = self.openstack( |
| 145 | + 'router delete ' + name1 + ' ' + name2) |
| 146 | + self.assertOutput('', del_output) |
| 147 | + |
| 148 | + def test_router_set_show_unset(self): |
| 149 | + """Tests create router, set, unset, show, delete""" |
| 150 | + |
| 151 | + name = uuid.uuid4().hex |
| 152 | + new_name = name + "_" |
| 153 | + cmd_output = json.loads(self.openstack( |
| 154 | + 'router create -f json ' + |
| 155 | + '--description aaaa ' + |
| 156 | + name |
| 157 | + )) |
| 158 | + self.assertEqual( |
| 159 | + name, |
| 160 | + cmd_output["name"], |
| 161 | + ) |
| 162 | + self.assertEqual( |
| 163 | + 'aaaa', |
| 164 | + cmd_output["description"], |
| 165 | + ) |
| 166 | + |
| 167 | + # Test set --disable |
| 168 | + cmd_output = self.openstack( |
| 169 | + 'router set ' + |
| 170 | + '--name ' + new_name + |
| 171 | + ' --description bbbb ' + |
| 172 | + '--disable ' + |
| 173 | + name |
| 174 | + ) |
| 175 | + self.assertOutput('', cmd_output) |
| 176 | + |
| 177 | + cmd_output = json.loads(self.openstack( |
| 178 | + 'router show -f json ' + |
| 179 | + new_name |
| 180 | + )) |
| 181 | + self.assertEqual( |
| 182 | + new_name, |
| 183 | + cmd_output["name"], |
| 184 | + ) |
| 185 | + self.assertEqual( |
| 186 | + 'bbbb', |
| 187 | + cmd_output["description"], |
| 188 | + ) |
| 189 | + self.assertEqual( |
| 190 | + 'DOWN', |
| 191 | + cmd_output["admin_state_up"], |
| 192 | + ) |
| 193 | + |
| 194 | + # Test set --ha --distributed |
| 195 | + cmd_output = self.openstack( |
| 196 | + 'router set ' + |
| 197 | + '--distributed ' + |
| 198 | + '--external-gateway public ' + |
| 199 | + new_name |
| 200 | + ) |
| 201 | + self.assertOutput('', cmd_output) |
| 202 | + |
| 203 | + cmd_output = json.loads(self.openstack( |
| 204 | + 'router show -f json ' + |
| 205 | + new_name |
| 206 | + )) |
| 207 | + self.assertEqual( |
| 208 | + True, |
| 209 | + cmd_output["distributed"], |
| 210 | + ) |
| 211 | + self.assertIsNotNone(cmd_output["external_gateway_info"]) |
| 212 | + |
| 213 | + # Test unset |
| 214 | + cmd_output = self.openstack( |
| 215 | + 'router unset ' + |
| 216 | + '--external-gateway ' + |
| 217 | + new_name |
| 218 | + ) |
| 219 | + cmd_output = json.loads(self.openstack( |
| 220 | + 'router show -f json ' + |
| 221 | + new_name |
| 222 | + )) |
| 223 | + self.assertIsNone(cmd_output["external_gateway_info"]) |
| 224 | + |
| 225 | + del_output = self.openstack( |
| 226 | + 'router delete ' + new_name) |
| 227 | + self.assertOutput('', del_output) |
0 commit comments