def test_extract_device_information_empty_mainfile(self, mock_log): js = SnapAdmin() js.main_file = None host_dict = {} js.extract_device_information(host_dict) mock_log.assert_called_with("\x1b[31m\nERROR occurred !! 'NoneType' object is not subscriptable", extra={'hostname': None})
def test_multiple_hostname(self, mock_path, mock_connect, mock_arg): argparse.ArgumentParser.parse_args = MagicMock() argparse.ArgumentParser.parse_args.return_value = argparse.Namespace( check=False, diff=False, file=None, hostname=None, login=None, passwd=None, port=None, post_snapfile=None, pre_snapfile=None, snap=False, snapcheck=False, verbosity=None, version=False) mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') js = SnapAdmin() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main1.yml') config_file = open(conf_file, 'r') js.main_file = yaml.load(config_file) js.login("snap_1") hosts = ['10.209.16.203', '10.209.16.204', '10.209.16.205'] self.assertEqual(js.host_list, hosts)
def test_hostname(self, mock_connect): argparse.ArgumentParser.parse_args = MagicMock() argparse.ArgumentParser.parse_args.return_value = argparse.Namespace( check=False, diff=False, file=None, hostname=None, login=None, passwd=None, port=None, post_snapfile=None, pre_snapfile=None, snap=False, snapcheck=False, verbosity=None, version=False) js = SnapAdmin() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_6.yml') config_file = open(conf_file, 'r') js.main_file = yaml.load(config_file) js.login("snap_1") expected_calls_made = [ call('10.216.193.114', 'abc', 'xyz', 'snap_1'), call('10.216.193.115', 'abc', 'xyz', 'snap_1'), call('10.216.193.116', 'abc', 'xyz', 'snap_1'), ] hosts = ['10.216.193.114', '10.216.193.115', '10.216.193.116'] self.assertEqual(js.host_list, hosts) mock_connect.assert_has_calls(expected_calls_made, any_order=True)
def test_extract_test_cases_wrong_path(self): js = SnapAdmin() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main.yml') config_file = open(conf_file, 'r') js.main_file = yaml.load(config_file, Loader=yaml.FullLoader) data = js.extract_test_cases(js.main_file) self.assertEqual(data, [])
def test_extract_device_information_attribute_error(self, mock_log): js = SnapAdmin() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_false_keyError_device.yml') config_file = open(conf_file, 'r') js.main_file = yaml.load(config_file, Loader=yaml.FullLoader) host_dict = {} js.extract_device_information(host_dict) mock_log.assert_called_with("\x1b[31mERROR!! KeyError 'device' key not found", extra={'hostname': None})
def test_extract_device_information_keyerror(self, mock_log): js = SnapAdmin() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_false_keyError.yml') config_file = open(conf_file, 'r') js.main_file = yaml.load(config_file, Loader=yaml.FullLoader) host_dict = {} js.extract_device_information(host_dict) mock_log.assert_called_with("\x1b[31m\nERROR occurred !! Hostname not given properly 'hosts'", extra={'hostname': None})
def test_extract_device_information_from_file(self): js = SnapAdmin() js.args.file = os.path.join(os.path.dirname(__file__), 'configs', 'main_1.yml') config_file = open(js.args.file, 'r') js.main_file = yaml.load(config_file, Loader=yaml.FullLoader) host_dict = {} js.extract_device_information(host_dict) hosts = ['1.1.1.1'] self.assertEqual(js.host_list, hosts)
def test_snap(self, mock_parse): argparse.ArgumentParser.parse_args = MagicMock() argparse.ArgumentParser.parse_args.return_value = argparse.Namespace() js = SnapAdmin() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main.yml') config_file = open(conf_file, 'r') js.main_file = yaml.load(config_file) js.generate_rpc_reply(None, "snap_mock", "10.216.193.114", js.main_file) self.assertTrue(mock_parse.called)
def test_port_without_include(self, mock_connect): #this test case is for scenarios when devices are mentioned in the cfg file itself js = SnapAdmin() # js.args.snap = True js.args.hostname = None conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_with_port.yml') config_file = open(conf_file, 'r') js.main_file = yaml.load(config_file) js.login("snap_1") hosts = ['10.216.193.114'] self.assertEqual(js.host_list, hosts) mock_connect.assert_called_with('10.216.193.114', 'abc', 'xyz', 'snap_1', port=44) #adding another device in the config dictionary #and checking the precedence b/w cmd and config params js.main_file['hosts'].append({ 'device': '10.216.193.115', 'username': '******', 'passwd': 'xyz', 'port': 45 }) js.args.port = 100 expected_calls_made = [ call('10.216.193.114', 'abc', 'xyz', 'snap_1', port=100), call('10.216.193.115', 'abc', 'xyz', 'snap_1', port=100) ] js.login("snap_1") mock_connect.assert_has_calls(expected_calls_made, any_order=True) #deleting the port paramater from the config file and keeping the port param on cmd args for host in js.main_file['hosts']: if 'port' in host: del host['port'] js.login("snap_1") mock_connect.assert_has_calls(expected_calls_made, any_order=True) #deleting the cmd line port param expected_calls_made = [ call('10.216.193.114', 'abc', 'xyz', 'snap_1'), call('10.216.193.115', 'abc', 'xyz', 'snap_1') ] js.args.port = None js.login("snap_1") mock_connect.assert_has_calls(expected_calls_made, any_order=True)
def test_generate_rpc_reply_correct_data(self, mock_parse, mock_path): # Testcase to check if proper config_data is passed to generate_rpc_reply # it should not give an error in that function argparse.ArgumentParser.parse_args = MagicMock() argparse.ArgumentParser.parse_args.return_value = argparse.Namespace() js = SnapAdmin() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main.yml') config_file = open(conf_file, 'r') js.main_file = yaml.load(config_file, Loader=yaml.FullLoader) mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') js.test_cases = None js.generate_rpc_reply( None, "snap_mock", "1.1.1.1", js.main_file) self.assertTrue(mock_parse.called)
def test_generate_rpc_reply_testcase_extracted_data(self, mock_parse, mock_extract, mock_path): # Testcase to check if proper config_data is passed to generate_rpc_reply # testcases is already extracted so it should not be called. # it should not give an error in that function argparse.ArgumentParser.parse_args = MagicMock() argparse.ArgumentParser.parse_args.return_value = argparse.Namespace() js = SnapAdmin() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main.yml') config_file = open(conf_file, 'r') js.main_file = yaml.load(config_file, Loader=yaml.FullLoader) mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') js.test_cases = [{'tests_include': ['check_chassis_fpc'], 'check_chassis_fpc': [{'command': 'show chassis fpc'}, {'item': {'xpath': '//fpc', 'id': 'slot', 'tests': [{'delta': 'memory-heap-utilization, 50%'}]}}]}] js.generate_rpc_reply( None, "snap_mock", "1.1.1.1", js.main_file) self.assertFalse(mock_extract.called) self.assertTrue(mock_parse.called)
def test_multiple_hostname(self, mock_path, mock_connect, mock_arg): argparse.ArgumentParser.parse_args = MagicMock() argparse.ArgumentParser.parse_args.return_value = argparse.Namespace( check=False, diff=False, file=None, hostname=None, login=None, passwd=None, port=None, post_snapfile=None, pre_snapfile=None, snap=False, snapcheck=False, verbosity=None, version=False) mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') js = SnapAdmin() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main1.yml') config_file = open(conf_file, 'r') js.main_file = yaml.load(config_file) js.login("snap_1") hosts = ['10.209.16.203', '10.209.16.204', '10.209.16.205'] self.assertEqual(js.host_list, hosts) #extending the test to check for device overlap among groups js.main_file['hosts'][0]['group'] = 'MX, EX' expected_calls_made = [ call('10.209.16.203', 'abc', 'def', 'snap_1'), call('10.209.16.204', 'abc', 'def', 'snap_1'), call('10.209.16.205', 'abc', 'def', 'snap_1'), call('10.209.16.206', 'abc', 'def', 'snap_1'), call('10.209.16.212', 'abc', 'def', 'snap_1'), ] js.login("snap_1") mock_connect.assert_has_calls(expected_calls_made, any_order=True)
def test_port_with_include(self, mock_connect, mock_path): #this test case is for scenarios when devices are included using some other file js = SnapAdmin() js.args.snap = True js.args.hostname = None mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main2_with_port.yml') config_file = open(conf_file, 'r') js.main_file = yaml.load(config_file) hosts = ['10.209.16.203', '10.209.16.204', '10.209.16.205'] expected_calls_made = [ call('10.209.16.203', 'abc', 'def', 'snap_1', port=100), call('10.209.16.204', 'abc', 'def', 'snap_1', port=101), call('10.209.16.205', 'abc', 'def', 'snap_1', port=102) ] js.login("snap_1") self.assertEqual(js.host_list, hosts) mock_connect.assert_has_calls(expected_calls_made, any_order=True) # mock_connect.assert_called_with('10.216.193.114','abc','xyz','snap_1',port=44) #Adding the cmd-line port param and checking the precedence b/w cmd and config params js.args.port = 55 expected_calls_made = [ call('10.209.16.203', 'abc', 'def', 'snap_1', port=55), call('10.209.16.204', 'abc', 'def', 'snap_1', port=55), call('10.209.16.205', 'abc', 'def', 'snap_1', port=55) ] js.login("snap_1") mock_connect.assert_has_calls(expected_calls_made, any_order=True)
def test_get_config_file_no_file(self, mock_exit): js = SnapAdmin() js.main_file = "hosts" js.args.file = "file_not_found.yml" js.get_config_file() mock_exit.assert_called_with(1)