|
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.network.v2 import common |
16 | 17 |
|
17 | 18 |
|
18 | 19 | class SecurityGroupRuleTests(common.NetworkTests): |
19 | 20 | """Functional tests for security group rule""" |
20 | | - SECURITY_GROUP_RULE_ID = None |
21 | | - NAME_FIELD = ['name'] |
22 | | - ID_FIELD = ['id'] |
23 | | - ID_HEADER = ['ID'] |
24 | | - |
25 | | - @classmethod |
26 | | - def setUpClass(cls): |
27 | | - common.NetworkTests.setUpClass() |
28 | | - if cls.haz_network: |
29 | | - cls.SECURITY_GROUP_NAME = uuid.uuid4().hex |
30 | | - |
31 | | - # Create the security group to hold the rule |
32 | | - opts = cls.get_opts(cls.NAME_FIELD) |
33 | | - raw_output = cls.openstack( |
34 | | - 'security group create ' + |
35 | | - cls.SECURITY_GROUP_NAME + |
36 | | - opts |
37 | | - ) |
38 | | - expected = cls.SECURITY_GROUP_NAME + '\n' |
39 | | - cls.assertOutput(expected, raw_output) |
40 | | - |
41 | | - # Create the security group rule. |
42 | | - opts = cls.get_opts(cls.ID_FIELD) |
43 | | - raw_output = cls.openstack( |
44 | | - 'security group rule create ' + |
45 | | - cls.SECURITY_GROUP_NAME + ' ' + |
46 | | - '--protocol tcp --dst-port 80:80 ' + |
47 | | - '--ingress --ethertype IPv4 ' + |
48 | | - opts |
49 | | - ) |
50 | | - cls.SECURITY_GROUP_RULE_ID = raw_output.strip('\n') |
51 | | - |
52 | | - @classmethod |
53 | | - def tearDownClass(cls): |
54 | | - try: |
55 | | - if cls.haz_network: |
56 | | - raw_output = cls.openstack( |
57 | | - 'security group rule delete ' + |
58 | | - cls.SECURITY_GROUP_RULE_ID |
59 | | - ) |
60 | | - cls.assertOutput('', raw_output) |
61 | | - raw_output = cls.openstack( |
62 | | - 'security group delete ' + |
63 | | - cls.SECURITY_GROUP_NAME |
64 | | - ) |
65 | | - cls.assertOutput('', raw_output) |
66 | | - finally: |
67 | | - super(SecurityGroupRuleTests, cls).tearDownClass() |
68 | 21 |
|
69 | 22 | def setUp(self): |
70 | 23 | super(SecurityGroupRuleTests, self).setUp() |
71 | 24 | # Nothing in this class works with Nova Network |
72 | 25 | if not self.haz_network: |
73 | 26 | self.skipTest("No Network service present") |
74 | 27 |
|
| 28 | + self.SECURITY_GROUP_NAME = uuid.uuid4().hex |
| 29 | + |
| 30 | + # Create the security group to hold the rule |
| 31 | + cmd_output = json.loads(self.openstack( |
| 32 | + 'security group create -f json ' + |
| 33 | + self.SECURITY_GROUP_NAME |
| 34 | + )) |
| 35 | + self.addCleanup(self.openstack, |
| 36 | + 'security group delete ' + self.SECURITY_GROUP_NAME) |
| 37 | + self.assertEqual(self.SECURITY_GROUP_NAME, cmd_output['name']) |
| 38 | + |
| 39 | + # Create the security group rule. |
| 40 | + cmd_output = json.loads(self.openstack( |
| 41 | + 'security group rule create -f json ' + |
| 42 | + self.SECURITY_GROUP_NAME + ' ' + |
| 43 | + '--protocol tcp --dst-port 80:80 ' + |
| 44 | + '--ingress --ethertype IPv4 ' |
| 45 | + )) |
| 46 | + self.addCleanup(self.openstack, |
| 47 | + 'security group rule delete ' + cmd_output['id']) |
| 48 | + self.SECURITY_GROUP_RULE_ID = cmd_output['id'] |
| 49 | + |
75 | 50 | def test_security_group_rule_list(self): |
76 | | - opts = self.get_opts(self.ID_HEADER) |
77 | | - raw_output = self.openstack('security group rule list ' + |
78 | | - self.SECURITY_GROUP_NAME + |
79 | | - opts) |
80 | | - self.assertIn(self.SECURITY_GROUP_RULE_ID, raw_output) |
| 51 | + cmd_output = json.loads(self.openstack( |
| 52 | + 'security group rule list -f json ' + self.SECURITY_GROUP_NAME)) |
| 53 | + self.assertIn(self.SECURITY_GROUP_RULE_ID, |
| 54 | + [rule['ID'] for rule in cmd_output]) |
81 | 55 |
|
82 | 56 | def test_security_group_rule_show(self): |
83 | | - opts = self.get_opts(self.ID_FIELD) |
84 | | - raw_output = self.openstack('security group rule show ' + |
85 | | - self.SECURITY_GROUP_RULE_ID + |
86 | | - opts) |
87 | | - self.assertEqual(self.SECURITY_GROUP_RULE_ID + "\n", raw_output) |
| 57 | + cmd_output = json.loads(self.openstack( |
| 58 | + 'security group rule show -f json ' + self.SECURITY_GROUP_RULE_ID)) |
| 59 | + self.assertEqual(self.SECURITY_GROUP_RULE_ID, cmd_output['id']) |
0 commit comments