Skip to content

Instantly share code, notes, and snippets.

@skamithi
Last active August 29, 2015 14:15
Show Gist options
  • Save skamithi/afc31a5286f5e1a30f17 to your computer and use it in GitHub Desktop.
Save skamithi/afc31a5286f5e1a30f17 to your computer and use it in GitHub Desktop.
ansible unit testing post - test ip route show functionality
@mock.patch('library.prefix_check.single_route_check_run')
@mock.patch('library.prefix_check.AnsibleModule')
def test_route_check_route_found(mock_module, mock_single_run):
"""
prefix_check - test action when prefix is found
"""
# on success, single_route_check_run() will return true
mock_single_run.return_value = True
result = prefix_check.check_if_route_exists(mock_module)
assert_equals(result, True)
@mock.patch('library.prefix_check.single_route_check_run')
@mock.patch('library.prefix_check.AnsibleModule')
def test_route_check_route_found(mock_module, mock_single_run):
"""
prefix_check - test action when prefix is found
"""
# on success, single_route_check_run() will return true
mock_single_run.return_value = True
result = prefix_check.check_if_route_exists(mock_module)
assert_equals(result, True)
@mock.patch('library.prefix_check.time.sleep')
@mock.patch('library.prefix_check.single_route_check_run')
@mock.patch('library.prefix_check.AnsibleModule')
def test_route_check_route_timeout_occurs(mock_module,
mock_single_run, mock_sleep):
"""
prefix_check - test action when prefix not found. timeout occurs
"""
# define some important variables for the test.
mock_module.timeout = 5
poll_interval = 5
# on failure, single_route_check_run() will return false
mock_single_run.return_value = False
# Run the function under test
result = prefix_check.check_if_route_exists(mock_module)
# Confirm that function failed
assert_equals(result, False)
# Also check that the poll interval works correctly.
# Mock time.sleep and check that it was executed
# (poll_interface/timeout) times. In this case 5 times.
assert_equals(mock_sleep.call_count, poll_interval)
@mock.patch('library.prefix_check.AnsibleModule')
def test_ip_route_show_execution(mock_module):
"""
prefix_check - test ip route show execution
"""
# define necessary variables. Prefix was initial
# set in the main() function. Needs to be hardcoded in this test.
mock_module.prefix = '10.1.1.0/24'
# AnsibleModule.run_command outputs list of output. Make
# sure to mimic this for the test.
mock_module.run_command.return_value = (1,'ip route stuff', None)
# Run the function under test
prefix_check.single_route_check_run(mock_module)
#Confirm command outputs used to get prefix info
mock_module.run_command.assert_called_with('/sbin/ip route show 10.1.1.0/24')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment