Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing mock for locate_oc_binary method #3944

Merged
merged 2 commits into from
Apr 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion roles/lib_openshift/src/test/unit/test_oc_adm_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,11 @@ class RegistryTest(unittest.TestCase):
}
]}'''

@mock.patch('oc_adm_registry.locate_oc_binary')
@mock.patch('oc_adm_registry.Utils._write')
@mock.patch('oc_adm_registry.Utils.create_tmpfile_copy')
@mock.patch('oc_adm_registry.Registry._run')
def test_state_present(self, mock_cmd, mock_tmpfile_copy, mock_write):
def test_state_present(self, mock_cmd, mock_tmpfile_copy, mock_write, mock_oc_binary):
''' Testing state present '''
params = {'state': 'present',
'debug': False,
Expand Down Expand Up @@ -245,6 +246,11 @@ def test_state_present(self, mock_cmd, mock_tmpfile_copy, mock_write):
'/tmp/mocked_kubeconfig',
]

mock_oc_binary.side_effect = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For these mock_oc_binarys I think it's safer to simply assign oc to their return_value property. Can you swap these to use return_value instead? That will save us from any pain in the future if the code base changes again and the number of calls changes. return_value will always return just that one value. side_effect on an iterator will run out of values (as we saw before).

    mock_oc_binary.return_value = 'oc'

side_effect is good at what it does, returning items from an iterable or anything from a function, but it's a little too much for this use-case.

'oc',
'oc',
]

results = Registry.run_ansible(params, False)

self.assertTrue(results['changed'])
Expand Down
7 changes: 6 additions & 1 deletion roles/lib_openshift/src/test/unit/test_oc_adm_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,11 @@ class RouterTest(unittest.TestCase):
]
}'''

@mock.patch('oc_adm_router.locate_oc_binary')
@mock.patch('oc_adm_router.Utils._write')
@mock.patch('oc_adm_router.Utils.create_tmpfile_copy')
@mock.patch('oc_adm_router.Router._run')
def test_state_present(self, mock_cmd, mock_tmpfile_copy, mock_write):
def test_state_present(self, mock_cmd, mock_tmpfile_copy, mock_write, mock_oc_binary):
''' Testing a create '''
params = {'state': 'present',
'debug': False,
Expand Down Expand Up @@ -345,6 +346,10 @@ def test_state_present(self, mock_cmd, mock_tmpfile_copy, mock_write):
'/tmp/mocked_kubeconfig',
]

mock_oc_binary.side_effect = [
'oc',
]

results = Router.run_ansible(params, False)

self.assertTrue(results['changed'])
Expand Down
28 changes: 24 additions & 4 deletions roles/lib_openshift/src/test/unit/test_oc_objectvalidator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ class OCObjectValidatorTest(unittest.TestCase):

maxDiff = None

@mock.patch('oc_objectvalidator.locate_oc_binary')
@mock.patch('oc_objectvalidator.Utils.create_tmpfile_copy')
@mock.patch('oc_objectvalidator.OCObjectValidator._run')
def test_no_data(self, mock_cmd, mock_tmpfile_copy):
def test_no_data(self, mock_cmd, mock_tmpfile_copy, mock_oc_binary):
''' Testing when both all objects are empty '''

# Arrange
Expand Down Expand Up @@ -62,6 +63,10 @@ def test_no_data(self, mock_cmd, mock_tmpfile_copy):
'/tmp/mocked_kubeconfig',
]

mock_oc_binary.side_effect = [
'oc',
]

# Act
results = OCObjectValidator.run_ansible(params)

Expand All @@ -76,9 +81,10 @@ def test_no_data(self, mock_cmd, mock_tmpfile_copy):
mock.call(['oc', 'get', 'namespace', '-o', 'json', '-n', 'default'], None),
])

@mock.patch('oc_objectvalidator.locate_oc_binary')
@mock.patch('oc_objectvalidator.Utils.create_tmpfile_copy')
@mock.patch('oc_objectvalidator.OCObjectValidator._run')
def test_error_code(self, mock_cmd, mock_tmpfile_copy):
def test_error_code(self, mock_cmd, mock_tmpfile_copy, mock_oc_binary):
''' Testing when we fail to get objects '''

# Arrange
Expand All @@ -98,6 +104,10 @@ def test_error_code(self, mock_cmd, mock_tmpfile_copy):
'/tmp/mocked_kubeconfig',
]

mock_oc_binary.side_effect = [
'oc'
]

error_results = {
'returncode': 1,
'stderr': 'Error.',
Expand All @@ -120,9 +130,10 @@ def test_error_code(self, mock_cmd, mock_tmpfile_copy):
mock.call(['oc', 'get', 'hostsubnet', '-o', 'json', '-n', 'default'], None),
])

@mock.patch('oc_objectvalidator.locate_oc_binary')
@mock.patch('oc_objectvalidator.Utils.create_tmpfile_copy')
@mock.patch('oc_objectvalidator.OCObjectValidator._run')
def test_valid_both(self, mock_cmd, mock_tmpfile_copy):
def test_valid_both(self, mock_cmd, mock_tmpfile_copy, mock_oc_binary):
''' Testing when both all objects are valid '''

# Arrange
Expand Down Expand Up @@ -427,6 +438,10 @@ def test_valid_both(self, mock_cmd, mock_tmpfile_copy):
'/tmp/mocked_kubeconfig',
]

mock_oc_binary.side_effect = [
'oc'
]

# Act
results = OCObjectValidator.run_ansible(params)

Expand All @@ -441,9 +456,10 @@ def test_valid_both(self, mock_cmd, mock_tmpfile_copy):
mock.call(['oc', 'get', 'namespace', '-o', 'json', '-n', 'default'], None),
])

@mock.patch('oc_objectvalidator.locate_oc_binary')
@mock.patch('oc_objectvalidator.Utils.create_tmpfile_copy')
@mock.patch('oc_objectvalidator.OCObjectValidator._run')
def test_invalid_both(self, mock_cmd, mock_tmpfile_copy):
def test_invalid_both(self, mock_cmd, mock_tmpfile_copy, mock_oc_binary):
''' Testing when all objects are invalid '''

# Arrange
Expand Down Expand Up @@ -886,6 +902,10 @@ def test_invalid_both(self, mock_cmd, mock_tmpfile_copy):
'/tmp/mocked_kubeconfig',
]

mock_oc_binary.side_effect = [
'oc'
]

# Act
results = OCObjectValidator.run_ansible(params)

Expand Down