Remove mox from cgtsclient tests

This test converts from mox to mock.
This partially aligns with upstream story 2001546
https://governance.openstack.org/tc/goals/rocky/mox_removal.html

As part of conversion, a mock host-list test was added
to help illustrate how to use mock and to increase code
coverage (which is now up to 35%)

Story: 2004515
Task: 28703
Change-Id: I58b1f2b271dd6dbf4faf07797444d1b51277c7bb
Signed-off-by: Al Bailey <Al.Bailey@windriver.com>
This commit is contained in:
Al Bailey 2018-12-31 13:16:20 -06:00
parent 626a28c4ab
commit 9d8f997fbd
4 changed files with 30 additions and 15 deletions

View File

@ -4,23 +4,23 @@
# SPDX-License-Identifier: Apache-2.0
#
import httplib2
import re
import sys
import fixtures
from testtools import matchers
from keystoneclient.v2_0 import client as ksclient
import httplib2
import mock
import re
from six.moves import cStringIO as StringIO
import sys
from testtools import matchers
from cgtsclient import exc
from cgtsclient import shell as cgts_shell
from cgtsclient.tests import utils
from cgtsclient.v1 import client as v1client
from cgtsclient.v1.ihost import ihost
FAKE_ENV = {'OS_USERNAME': 'username',
'OS_PASSWORD': 'password',
'OS_PROJECT_NAME': 'project',
'OS_REGION_NAME': 'region',
'OS_TENANT_NAME': 'tenant_name',
'OS_AUTH_URL': 'http://no.where'}
@ -35,9 +35,9 @@ class ShellTest(utils.BaseTestCase):
def setUp(self):
super(ShellTest, self).setUp()
self.m.StubOutWithMock(ksclient, 'Client')
self.m.StubOutWithMock(v1client.Client, 'json_request')
self.m.StubOutWithMock(v1client.Client, 'raw_request')
def tearDown(self):
super(ShellTest, self).tearDown()
def shell(self, argstr):
orig = sys.stdout
@ -97,3 +97,21 @@ class ShellTest(utils.BaseTestCase):
def test_auth_param(self):
self.make_env(exclude='OS_USERNAME')
self.test_help()
@mock.patch('cgtsclient.v1.ihost.ihostManager.list')
@mock.patch('cgtsclient.client._get_ksclient')
@mock.patch('cgtsclient.client._get_endpoint')
def test_host_list(self, mock_get_endpoint, mock_get_client, mock_list):
# This unit test mocks returning a single controller-0 host through host-list
mock_get_endpoint.return_value = 'http://fakelocalhost:6385/v1'
fake_controller = {'id': '0',
'hostname': 'controller-0',
'personality': 'controller',
'administrative': 'unlocked',
'operational': 'enabled',
'availability': 'available'}
mock_list.return_value = [ihost(None, fake_controller, True)]
self.make_env()
host_results = self.shell("host-list")
self.assertIn('controller-0', host_results)
self.assertNotIn('controller-1', host_results)

View File

@ -15,7 +15,6 @@
import copy
import fixtures
import mox
import testtools
from cgtsclient.common import http
@ -26,8 +25,6 @@ class BaseTestCase(testtools.TestCase):
def setUp(self):
super(BaseTestCase, self).setUp()
self.m = mox.Mox()
self.addCleanup(self.m.UnsetStubs)
self.useFixture(fixtures.FakeLogger())

View File

@ -8,7 +8,6 @@ bandit>=1.1.0 # Apache-2.0
coverage!=4.4,>=4.0 # Apache-2.0
fixtures>=3.0.0 # Apache-2.0/BSD
mock>=2.0 # BSD
mox
os-testr>=0.8.0 # Apache-2.0
oslotest>=1.10.0 # Apache-2.0
sphinx>=1.6.2 # BSD

View File

@ -49,6 +49,7 @@ commands =
rm -f coverage.xml
python setup.py testr --coverage --testr-args='{posargs}'
coverage xml
coverage report
[flake8]
show-source = true