Skip to content

Commit 95e2791

Browse files
committed
Convert network security group functional tests to JSON
Change-Id: Icb63aa0dfbce9016fb824f97915a660cf130d120
1 parent 5cc4d5b commit 95e2791

File tree

2 files changed

+52
-106
lines changed

2 files changed

+52
-106
lines changed

openstackclient/tests/functional/network/v2/test_security_group.py

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,74 +10,48 @@
1010
# License for the specific language governing permissions and limitations
1111
# under the License.
1212

13+
import json
1314
import uuid
1415

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

1718

1819
class SecurityGroupTests(common.NetworkTests):
1920
"""Functional tests for security group"""
20-
HEADERS = ['Name']
21-
FIELDS = ['name']
22-
23-
@classmethod
24-
def setUpClass(cls):
25-
common.NetworkTests.setUpClass()
26-
if cls.haz_network:
27-
cls.NAME = uuid.uuid4().hex
28-
cls.OTHER_NAME = uuid.uuid4().hex
29-
30-
opts = cls.get_opts(cls.FIELDS)
31-
raw_output = cls.openstack(
32-
'security group create ' +
33-
cls.NAME +
34-
opts
35-
)
36-
expected = cls.NAME + '\n'
37-
cls.assertOutput(expected, raw_output)
38-
39-
@classmethod
40-
def tearDownClass(cls):
41-
try:
42-
if cls.haz_network:
43-
# Rename test
44-
raw_output = cls.openstack(
45-
'security group set --name ' +
46-
cls.OTHER_NAME + ' ' +
47-
cls.NAME
48-
)
49-
cls.assertOutput('', raw_output)
50-
# Delete test
51-
raw_output = cls.openstack(
52-
'security group delete ' +
53-
cls.OTHER_NAME
54-
)
55-
cls.assertOutput('', raw_output)
56-
finally:
57-
super(SecurityGroupTests, cls).tearDownClass()
5821

5922
def setUp(self):
6023
super(SecurityGroupTests, self).setUp()
6124
# Nothing in this class works with Nova Network
6225
if not self.haz_network:
6326
self.skipTest("No Network service present")
6427

28+
self.NAME = uuid.uuid4().hex
29+
self.OTHER_NAME = uuid.uuid4().hex
30+
cmd_output = json.loads(self.openstack(
31+
'security group create -f json ' +
32+
self.NAME
33+
))
34+
self.addCleanup(self.openstack,
35+
'security group delete ' + cmd_output['id'])
36+
self.assertEqual(self.NAME, cmd_output['name'])
37+
6538
def test_security_group_list(self):
66-
opts = self.get_opts(self.HEADERS)
67-
raw_output = self.openstack('security group list' + opts)
68-
self.assertIn(self.NAME, raw_output)
39+
cmd_output = json.loads(self.openstack('security group list -f json'))
40+
self.assertIn(self.NAME, [sg['Name'] for sg in cmd_output])
6941

7042
def test_security_group_set(self):
43+
other_name = uuid.uuid4().hex
7144
raw_output = self.openstack(
72-
'security group set --description NSA ' + self.NAME
45+
'security group set --description NSA --name ' +
46+
other_name + ' ' + self.NAME
7347
)
7448
self.assertEqual('', raw_output)
7549

76-
opts = self.get_opts(['description'])
77-
raw_output = self.openstack('security group show ' + self.NAME + opts)
78-
self.assertEqual("NSA\n", raw_output)
50+
cmd_output = json.loads(self.openstack(
51+
'security group show -f json ' + other_name))
52+
self.assertEqual('NSA', cmd_output['description'])
7953

8054
def test_security_group_show(self):
81-
opts = self.get_opts(self.FIELDS)
82-
raw_output = self.openstack('security group show ' + self.NAME + opts)
83-
self.assertEqual(self.NAME + "\n", raw_output)
55+
cmd_output = json.loads(self.openstack(
56+
'security group show -f json ' + self.NAME))
57+
self.assertEqual(self.NAME, cmd_output['name'])

openstackclient/tests/functional/network/v2/test_security_group_rule.py

Lines changed: 30 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,78 +10,50 @@
1010
# License for the specific language governing permissions and limitations
1111
# under the License.
1212

13+
import json
1314
import uuid
1415

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

1718

1819
class SecurityGroupRuleTests(common.NetworkTests):
1920
"""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()
6821

6922
def setUp(self):
7023
super(SecurityGroupRuleTests, self).setUp()
7124
# Nothing in this class works with Nova Network
7225
if not self.haz_network:
7326
self.skipTest("No Network service present")
7427

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+
7550
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])
8155

8256
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

Comments
 (0)