Skip to content

Instantly share code, notes, and snippets.

@skamithi
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save skamithi/aa57667544c305e8044d to your computer and use it in GitHub Desktop.

Select an option

Save skamithi/aa57667544c305e8044d to your computer and use it in GitHub Desktop.
ansible module exit functionality tester
@mock.patch('library.prefix_check.check_if_route_exists')
@mock.patch('library.prefix_check.AnsibleModule')
def test_main_exit_functionality_success(mock_module,
mock_route_check):
"""
prefix_check - test_main_exit_functionality - success
"""
# This creates a instance of the AnsibleModule mock.
instance = mock_module.return_value
# What happens the route is found. Check that correct
# exit function is called
mock_route_check.return_value = True
prefix_check.main()
# AnsibleModule.exit_json should be called
# "changed" var should be false
instance.exit_json.assert_called_with(
msg='Route Found', changed=False)
# AnsibleModule.fail_json should not be called
assert_equals(instance.fail_json.call_count, 0)
@mock.patch('library.prefix_check.check_if_route_exists')
@mock.patch('library.prefix_check.AnsibleModule')
def test_main_exit_functionality_failure(mock_module,
mock_loop_route_check):
"""
prefix_check - test_main_exit_functionality - failure
"""
instance = mock_module.return_value
# What happens when the check_if_route_exists returns False
# that is route is not found
mock_loop_route_check.return_value = False
prefix_check.main()
# AnsibleModule.exit_json should be activated
assert_equals(instance.exit_json.call_count, 0)
# AnsibleModule.fail_json should be called
instance.fail_json.assert_called_with(
msg='Route not Found. Check Routing Configuration')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment