1# (c) 2016 Red Hat Inc. 2# 3# This file is part of Ansible 4# 5# Ansible is free software: you can redistribute it and/or modify 6# it under the terms of the GNU General Public License as published by 7# the Free Software Foundation, either version 3 of the License, or 8# (at your option) any later version. 9# 10# Ansible is distributed in the hope that it will be useful, 11# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# GNU General Public License for more details. 14# 15# You should have received a copy of the GNU General Public License 16# along with Ansible. If not, see <http://www.gnu.org/licenses/>. 17 18# Make coding more python3-ish 19from __future__ import absolute_import, division, print_function 20 21__metaclass__ = type 22 23from ansible_collections.junipernetworks.junos.tests.unit.compat.mock import ( 24 patch, 25 MagicMock, 26) 27from ansible_collections.junipernetworks.junos.plugins.modules import ( 28 junos_ping, 29) 30from ansible_collections.junipernetworks.junos.tests.unit.modules.utils import ( 31 set_module_args, 32) 33from .junos_module import TestJunosModule, load_fixture 34 35 36class TestJunosPingModule(TestJunosModule): 37 module = junos_ping 38 39 def setUp(self): 40 super(TestJunosPingModule, self).setUp() 41 42 self.mock_get_connection = patch( 43 "ansible_collections.junipernetworks.junos.plugins.modules.junos_ping.get_connection" 44 ) 45 self.get_connection = self.mock_get_connection.start() 46 47 self.conn = self.get_connection() 48 self.conn.get = MagicMock() 49 50 def tearDown(self): 51 super(TestJunosPingModule, self).tearDown() 52 self.mock_get_connection.stop() 53 54 def test_junos_ping_expected_success(self): 55 set_module_args(dict(count=2, dest="10.10.10.10")) 56 self.conn.get = MagicMock( 57 return_value=load_fixture( 58 "junos_ping_ping_10.10.10.10_count_2", content="str" 59 ) 60 ) 61 result = self.execute_module() 62 self.assertEqual(result["commands"], "ping 10.10.10.10 count 2") 63 64 def test_junos_ping_expected_failure(self): 65 set_module_args(dict(count=4, dest="10.10.10.20", state="absent")) 66 self.conn.get = MagicMock( 67 return_value=load_fixture( 68 "junos_ping_ping_10.10.10.20_count_4", content="str" 69 ) 70 ) 71 result = self.execute_module() 72 self.assertEqual(result["commands"], "ping 10.10.10.20 count 4") 73 74 def test_junos_ping_unexpected_success(self): 75 """ Test for successful pings when destination should not be reachable - FAIL. """ 76 set_module_args(dict(count=2, dest="10.10.10.10", state="absent")) 77 self.conn.get = MagicMock( 78 return_value=load_fixture( 79 "junos_ping_ping_10.10.10.10_count_2", content="str" 80 ) 81 ) 82 self.execute_module(failed=True) 83 84 def test_junos_ping_unexpected_failure(self): 85 """ Test for unsuccessful pings when destination should be reachable - FAIL. """ 86 set_module_args(dict(count=4, dest="10.10.10.20")) 87 self.conn.get = MagicMock( 88 return_value=load_fixture( 89 "junos_ping_ping_10.10.10.20_count_4", content="str" 90 ) 91 ) 92 self.execute_module(failed=True) 93 94 def test_junos_ping_failure_stats(self): 95 """Test for asserting stats when ping fails""" 96 set_module_args(dict(count=4, dest="10.10.10.20")) 97 self.conn.get = MagicMock( 98 return_value=load_fixture( 99 "junos_ping_ping_10.10.10.20_count_4", content="str" 100 ) 101 ) 102 result = self.execute_module(failed=True) 103 self.assertEqual(result["packet_loss"], "100%") 104 self.assertEqual(result["packets_rx"], 0) 105 self.assertEqual(result["packets_tx"], 4) 106 107 def test_junos_ping_success_stats(self): 108 set_module_args(dict(count=2, dest="10.10.10.10")) 109 self.conn.get = MagicMock( 110 return_value=load_fixture( 111 "junos_ping_ping_10.10.10.10_count_2", content="str" 112 ) 113 ) 114 result = self.execute_module() 115 self.assertEqual(result["commands"], "ping 10.10.10.10 count 2") 116 self.assertEqual(result["packet_loss"], "0%") 117 self.assertEqual(result["packets_rx"], 2) 118 self.assertEqual(result["packets_tx"], 2) 119 self.assertEqual(result["rtt"]["min"], 15.71) 120 self.assertEqual(result["rtt"]["avg"], 16.87) 121 self.assertEqual(result["rtt"]["max"], 18.04) 122 self.assertEqual(result["rtt"]["stddev"], 1.165) 123 124 def test_junos_ping_success_stats_with_options(self): 125 set_module_args( 126 dict(count=5, size=512, interval=2, dest="10.10.10.11") 127 ) 128 self.conn.get = MagicMock( 129 return_value=load_fixture( 130 "junos_ping_ping_10.10.10.11_count_5_size_512_interval_2", 131 content="str", 132 ) 133 ) 134 result = self.execute_module() 135 self.assertEqual( 136 result["commands"], "ping 10.10.10.11 count 5 size 512 interval 2" 137 ) 138 self.assertEqual(result["packet_loss"], "0%") 139 self.assertEqual(result["packets_rx"], 5) 140 self.assertEqual(result["packets_tx"], 5) 141 self.assertEqual(result["rtt"]["min"], 18.71) 142 self.assertEqual(result["rtt"]["avg"], 17.87) 143 self.assertEqual(result["rtt"]["max"], 20.04) 144 self.assertEqual(result["rtt"]["stddev"], 2.165) 145 146 def test_junos_ping_success_stats_with_df_rapid(self): 147 set_module_args(dict(df_bit=True, rapid=True, dest="10.10.10.12")) 148 self.conn.get = MagicMock( 149 return_value=load_fixture( 150 "junos_ping_ping_10.10.10.12_count_5_do-not-fragment_rapid", 151 content="str", 152 ) 153 ) 154 result = self.execute_module() 155 self.assertEqual( 156 result["commands"], 157 "ping 10.10.10.12 count 5 do-not-fragment rapid", 158 ) 159 self.assertEqual(result["packet_loss"], "0%") 160 self.assertEqual(result["packets_rx"], 5) 161 self.assertEqual(result["packets_tx"], 5) 162 self.assertEqual(result["rtt"]["min"], 2.58) 163 self.assertEqual(result["rtt"]["avg"], 2.63) 164 self.assertEqual(result["rtt"]["max"], 2.74) 165 self.assertEqual(result["rtt"]["stddev"], 0.058) 166