Functional test for router

Refactor functional tests for testing more command options.

Change-Id: I6200045c6228e245fc48a4d48d4b3796dede61b5
This commit is contained in:
jiahui.qiang 2017-01-18 21:27:35 +08:00
parent 4b4194755a
commit f96cff1a6d

View File

@ -10,6 +10,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import json
import uuid import uuid
from openstackclient.tests.functional import base from openstackclient.tests.functional import base
@ -17,34 +18,210 @@ from openstackclient.tests.functional import base
class RouterTests(base.TestCase): class RouterTests(base.TestCase):
"""Functional tests for router. """ """Functional tests for router. """
NAME = uuid.uuid4().hex
HEADERS = ['Name']
FIELDS = ['name']
@classmethod def test_router_create_and_delete(self):
def setUpClass(cls): """Test create options, delete"""
opts = cls.get_opts(cls.FIELDS) name1 = uuid.uuid4().hex
raw_output = cls.openstack('router create ' + cls.NAME + opts) name2 = uuid.uuid4().hex
expected = cls.NAME + '\n' cmd_output = json.loads(self.openstack(
cls.assertOutput(expected, raw_output) 'router create -f json ' +
name1
))
self.assertEqual(
name1,
cmd_output["name"],
)
cmd_output = json.loads(self.openstack(
'router create -f json ' +
name2
))
self.assertEqual(
name2,
cmd_output["name"],
)
@classmethod del_output = self.openstack(
def tearDownClass(cls): 'router delete ' + name1 + ' ' + name2)
raw_output = cls.openstack('router delete ' + cls.NAME) self.assertOutput('', del_output)
cls.assertOutput('', raw_output)
def test_router_list(self): def test_router_list(self):
opts = self.get_opts(self.HEADERS) """Test create, list filter"""
raw_output = self.openstack('router list' + opts) # Get project IDs
self.assertIn(self.NAME, raw_output) cmd_output = json.loads(self.openstack('token issue -f json '))
auth_project_id = cmd_output['project_id']
def test_router_set(self): cmd_output = json.loads(self.openstack('project list -f json '))
self.openstack('router set --disable ' + self.NAME) admin_project_id = None
opts = self.get_opts(['name', 'admin_state_up']) demo_project_id = None
raw_output = self.openstack('router show ' + self.NAME + opts) for p in cmd_output:
self.assertEqual("DOWN\n" + self.NAME + "\n", raw_output) if p['Name'] == 'admin':
admin_project_id = p['ID']
if p['Name'] == 'demo':
demo_project_id = p['ID']
def test_router_show(self): # Verify assumptions:
opts = self.get_opts(self.FIELDS) # * admin and demo projects are present
raw_output = self.openstack('router show ' + self.NAME + opts) # * demo and admin are distinct projects
self.assertEqual(self.NAME + "\n", raw_output) # * tests run as admin
self.assertIsNotNone(admin_project_id)
self.assertIsNotNone(demo_project_id)
self.assertNotEqual(admin_project_id, demo_project_id)
self.assertEqual(admin_project_id, auth_project_id)
name1 = uuid.uuid4().hex
name2 = uuid.uuid4().hex
cmd_output = json.loads(self.openstack(
'router create -f json ' +
'--disable ' +
name1
))
self.assertEqual(
name1,
cmd_output["name"],
)
self.assertEqual(
"DOWN",
cmd_output["admin_state_up"],
)
self.assertEqual(
admin_project_id,
cmd_output["project_id"],
)
cmd_output = json.loads(self.openstack(
'router create -f json ' +
'--project ' + demo_project_id +
' ' + name2
))
self.assertEqual(
name2,
cmd_output["name"],
)
self.assertEqual(
"UP",
cmd_output["admin_state_up"],
)
self.assertEqual(
demo_project_id,
cmd_output["project_id"],
)
# Test list --project
cmd_output = json.loads(self.openstack(
'router list -f json ' +
'--project ' + demo_project_id
))
names = [x["Name"] for x in cmd_output]
self.assertNotIn(name1, names)
self.assertIn(name2, names)
# Test list --disable
cmd_output = json.loads(self.openstack(
'router list -f json ' +
'--disable '
))
names = [x["Name"] for x in cmd_output]
self.assertIn(name1, names)
self.assertNotIn(name2, names)
# Test list --name
cmd_output = json.loads(self.openstack(
'router list -f json ' +
'--name ' + name1
))
names = [x["Name"] for x in cmd_output]
self.assertIn(name1, names)
self.assertNotIn(name2, names)
# Test list --long
cmd_output = json.loads(self.openstack(
'router list -f json ' +
'--long '
))
names = [x["Name"] for x in cmd_output]
self.assertIn(name1, names)
self.assertIn(name2, names)
del_output = self.openstack(
'router delete ' + name1 + ' ' + name2)
self.assertOutput('', del_output)
def test_router_set_show_unset(self):
"""Tests create router, set, unset, show, delete"""
name = uuid.uuid4().hex
new_name = name + "_"
cmd_output = json.loads(self.openstack(
'router create -f json ' +
'--description aaaa ' +
name
))
self.assertEqual(
name,
cmd_output["name"],
)
self.assertEqual(
'aaaa',
cmd_output["description"],
)
# Test set --disable
cmd_output = self.openstack(
'router set ' +
'--name ' + new_name +
' --description bbbb ' +
'--disable ' +
name
)
self.assertOutput('', cmd_output)
cmd_output = json.loads(self.openstack(
'router show -f json ' +
new_name
))
self.assertEqual(
new_name,
cmd_output["name"],
)
self.assertEqual(
'bbbb',
cmd_output["description"],
)
self.assertEqual(
'DOWN',
cmd_output["admin_state_up"],
)
# Test set --ha --distributed
cmd_output = self.openstack(
'router set ' +
'--distributed ' +
'--external-gateway public ' +
new_name
)
self.assertOutput('', cmd_output)
cmd_output = json.loads(self.openstack(
'router show -f json ' +
new_name
))
self.assertEqual(
True,
cmd_output["distributed"],
)
self.assertIsNotNone(cmd_output["external_gateway_info"])
# Test unset
cmd_output = self.openstack(
'router unset ' +
'--external-gateway ' +
new_name
)
cmd_output = json.loads(self.openstack(
'router show -f json ' +
new_name
))
self.assertIsNone(cmd_output["external_gateway_info"])
del_output = self.openstack(
'router delete ' + new_name)
self.assertOutput('', del_output)