def test_xpath_with_functions(self, mock_path): self.chk = False comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_xpath_functions.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "post") self.assertEqual(oper.no_passed, 1) self.assertEqual(oper.no_failed, 0) self.assertEqual(oper.result, 'Passed')
def test_conditional_operator_pass(self, mock_path): self.chk = False comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_conditional_op_pass.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_all-same-success_pre") self.assertEqual(oper.no_passed, 4) self.assertEqual(oper.no_failed, 0) self.assertEqual(oper.result, 'Passed')
def test_list_not_less_ignore_null_skip(self, mock_path): self.chk = True comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_list-not-less_ignore-null_skip.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file, Loader=yaml.FullLoader) oper = comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_no-diff_pre", self.action, "snap_no-diff_post") self.assertEqual(oper.no_passed, 0) self.assertEqual(oper.no_failed, 1)
def test_is_equal_success_ignore_null_2(self, mock_path, mock_debug, mock_info): self.chk = False comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_is-equal_ignore-null_6.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file, Loader=yaml.FullLoader) oper = comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_is-equal_pre_special") self.assertEqual(oper.no_passed, 0) self.assertEqual(oper.no_failed, 1)
def test_delta_Index_error(self, mock_path, mock_error): with self.assertRaises(Exception): self.chk = True comp = Comparator() #created changes in the test file conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_test_delta_index_error.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "pre_list_not_more_no_node", self.action, "post_list_not_more_no_node")
def test_is_gt_fail(self, mock_path): self.chk = False comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_is-gt.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_is-gt_fail_pre") self.assertEqual(oper.no_passed, 0) self.assertEqual(oper.no_failed, 1)
def test_regex_errors_1_(self, mock_path): self.chk = True comp = Comparator() # created changes in the test file conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_regex_1.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "pre_regex", self.action, "post_regex_empty") self.assertEqual(oper.no_failed, 1)
def test_no_diff(self, mock_path): self.chk = True comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_no-diff.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_no-diff_pre", self.action, "snap_no-diff_post") self.assertEqual(oper.no_passed, 2) self.assertEqual(oper.no_failed, 4)
def test_conditional_operator_error_1(self, mock_path): self.chk = False comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_conditional_op_error_1.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file, Loader=yaml.FullLoader) oper = comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_all-same-success_pre") self.assertEqual(oper.no_passed, 4) #one not run due to short-cutting self.assertEqual(oper.no_failed, 0) self.assertEqual(oper.result, "Failed")
def test_incorrect_test_file(self, mock_info): self.chk = False comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_incorrect_2.yml') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) filename = os.path.join('/etc', 'jsnapy', 'testfiles', "dummy.yml") with patch('logging.Logger.error') as mock_error: comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_is-equal_pre") err = "ERROR!! File %s not found" % filename c_list = mock_error.call_args_list[0] self.assertNotEqual(c_list[0][0].find(err), -1)
def test_compare_diff(self, mock_path, mock_info): self.diff = True comp = Comparator() mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_empty_test.yml') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) with patch('jnpr.jsnapy.check.diff') as mock_compare: comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_no-diff_pre", self.action, "snap_no-diff_post") self.assertTrue(mock_compare.called)
def test_not_exists_ignore_null_fail(self, mock_path, mock_debug, mock_info): self.chk = False comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_not-exists_ignore-null_fail.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_not-exists_pre") self.assertEqual(oper.no_passed, 0) self.assertEqual(oper.no_failed, 1)
def test_list_not_more_missing_node(self, mock_path): self.chk = True comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_list_not_more_node_missing.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "pre_list_not_more_node_missing", self.action, "post_list_not_more_node_missing") self.assertEqual(oper.no_passed, 0) self.assertEqual(oper.no_failed, 1)
def test_incorrect_test_command(self, mock_path, mock_info): self.chk = False mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_incorrect_3.yml') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) with patch('logging.Logger.error') as mock_log: comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_is-equal_pre") err = "ERROR occurred, test keys 'command' or 'rpc' not defined properly" c_list = mock_log.call_args_list[0] self.assertNotEqual(c_list[0][0].find(err), -1)
def test_all_same_ignore_null_3(self, mock_path, mock_debug, mock_info): self.chk = False comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_all-same-ignore-null_3.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_all-same-success_pre") self.assertEqual( oper.no_passed, 0) #comparison between None values. All values are None. self.assertEqual(oper.no_failed, 1)
def test_list_not_more_ignore_null_skip_2(self, mock_path): #Test to check if xml element present in second snapshot but not present in second snapshot and if ignore-NULL #flag is set, it should return failure. self.chk = True comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_list-not-more_ignore-null_skip_2.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file, Loader=yaml.FullLoader) oper = comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_exists_pre", self.action, "snap_exists_post") self.assertEqual(oper.no_passed, 0) self.assertEqual(oper.no_failed, 1)
def test_list_not_more_ignore_null_fail_1(self, mock_path): self.chk = True comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_list-not-more_ignore-null_fail_1.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file, Loader=yaml.FullLoader) oper = comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_no-diff_pre", self.action, "snap_3") self.assertEqual( oper.no_passed, 2) #the name is kinda misleading but in line with the convention self.assertEqual(oper.no_failed, 0)
def test_incorrect_test_format_2(self, mock_path, mock_info): self.chk = True comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_incorrect_4.yml') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') with patch('logging.Logger.error') as mock_log: comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_no-diff_pre", self.action, "snap_no-diff_post1") err = "ERROR!! for checking snapshots in text format use '--diff' option" c_list = mock_log.call_args_list[0] self.assertNotEqual(c_list[0][0].find(err), -1)
def test_exists_fail(self, mock_path, mock_debug, mock_info): self.chk = False comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_exists.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files( main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_exists_fail_pre") self.assertEqual(oper.no_passed, 0) self.assertEqual(oper.no_failed, 1)
def test_contains_ignore_null_fail_1(self, mock_path): self.chk = False comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_contains_ignore-null_fail_1.yml') config_file = open(conf_file, 'r') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') main_file = yaml.load(config_file) oper = comp.generate_test_files( main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_contains_pre") self.assertEqual(oper.no_passed, 0) self.assertEqual(oper.no_failed, 1)
def test_is_equal_success_ignore_null_2(self, mock_path, mock_debug, mock_info): self.chk = False comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_is-equal_ignore-null_6.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files( main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_is-equal_pre_special") self.assertEqual(oper.no_passed, 0) self.assertEqual(oper.no_failed, 1)
def test_all_same_ignore_null_3(self, mock_path, mock_debug, mock_info): self.chk = False comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_all-same-ignore-null_3.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files( main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_all-same-success_pre") self.assertEqual(oper.no_passed, 0) #comparison between None values. All values are None. self.assertEqual(oper.no_failed, 1)
def test_no_test_file(self, mock_info): self.chk = False comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_incorrect_1.yml') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) with patch('logging.Logger.error') as mock_error: comp.generate_test_files( main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_is-equal_pre") err = "ERROR!! No test file found, Please mention test files !!" c_list = mock_error.call_args_list[0] self.assertNotEqual(c_list[0][0].find(err), -1)
def compare_tests(self, hostname, config_data, pre_snap=None, post_snap=None, action=None, **kwargs): """ called by check and snapcheck argument, to compare snap files calls the function to compare snapshots based on arguments given (--check, --snapcheck, --diff) :param hostname: device name :return: return object of Operator containing test details """ comp = Comparator(**kwargs) chk = self.args.check diff = self.args.diff pre_snap_file = self.args.pre_snapfile if pre_snap is None else pre_snap if chk or diff or action in ["check", "diff"]: post_snap_file = self.args.post_snapfile if post_snap is None else post_snap test_obj = comp.generate_test_files( config_data, hostname, chk, diff, self.db, self.snap_del, pre_snap_file, action, post_snap_file, ) else: test_obj = comp.generate_test_files( config_data, hostname, chk, diff, self.db, self.snap_del, pre_snap_file, action, ) return test_obj
def test_regex_errors(self, mock_path): with self.assertRaises(IndexError): self.chk = True comp = Comparator() # created changes in the test file conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_regex.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files( main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "pre_regex", self.action, "post_regex_empty")
def test_xml_comparator(self, mock_path): self.chk = True comp = Comparator() # created changes in the test file conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_xml_comparator.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files( main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "pre_xml_compare", self.action, "post_xml_compare") self.assertEqual(oper.no_failed,1)
def test_regex_errors(self, mock_path): with self.assertRaises(IndexError): self.chk = True comp = Comparator() # created changes in the test file conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_regex.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file, Loader=yaml.FullLoader) oper = comp.generate_test_files( main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "pre_regex", self.action, "post_regex_empty")
def test_delta_Index_error(self, mock_path, mock_error): with self.assertRaises(Exception): self.chk = True comp = Comparator() #created changes in the test file conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_test_delta_index_error.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files( main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "pre_list_not_more_no_node", self.action, "post_list_not_more_no_node")
def check_arguments(self): """ checks combination of arguments given from command line and display help if correct set of combination is not given. :return: print message in command line, regarding correct usage of JSNAPy """ ## only four test operation is permitted, if given anything apart from this, then it should print error message if (self.args.snap is False and self.args.snapcheck is False and self.args.check is False and self.args.diff is False and self.args.version is False): self.logger.error( colorama.Fore.RED + "Arguments not given correctly, Please refer help message", extra=self.log_detail) self.parser.print_help() sys.exit(1) if (((self.args.snap is True and (self.args.pre_snapfile is None or self.args.file is None)) or (self.args.snapcheck is True and self.args.file is None) or (self.args.check is True and self.args.file is None)) and (self.args.testfiles is None or self.args.hostname is None)): self.logger.error( colorama.Fore.RED + "Arguments not given correctly, Please refer help message", extra=self.log_detail) self.parser.print_help() sys.exit(1) if self.args.diff is True: if (self.args.pre_snapfile is not None and os.path.isfile(self.args.pre_snapfile)) and ( self.args.post_snapfile is not None and os.path.isfile(self.args.post_snapfile)): comp = Comparator() comp.compare_diff(self.args.pre_snapfile, self.args.post_snapfile, None) sys.exit(1) else: if (self.args.file is None) and (self.args.testfiles is None or self.args.hostname is None): self.parser.print_help() sys.exit(1)
def test_list_not_less_ignore_null_skip(self, mock_path): self.chk = True comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_list-not-less_ignore-null_skip.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files( main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_no-diff_pre", self.action, "snap_no-diff_post") self.assertEqual(oper.no_passed, 0) self.assertEqual(oper.no_failed, 1)
def test_check_1(self, mock_path, mock_loginfo, mock_sqpath, mock_snapname, mock_logerror, mock_compreply): self.chk = False mock_snapname.return_value = 'mock_pre', 'text' mock_sqpath.return_value = os.path.join(os.path.dirname(__file__), 'configs') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_rpc_test.yml') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) self.db['check_from_sqlite'] = True comp = Comparator() comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "mock_pre") #,action= "check") mock_loginfo.assert_called() mock_logerror.assert_called() mock_compreply.assert_called()
def test_list_not_less_ignore_null_skip_1(self, mock_path): self.chk = True comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_list-not-less_ignore-null_skip_1.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files( main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_no-diff_pre", self.action, "snap_no-diff_post") self.assertEqual(oper.no_passed, 1) # even if no nodes is found, comparison between null and null passes self.assertEqual(oper.no_failed, 1)
def test_delta_percentage(self, mock_path, mock_error): self.chk = True comp = Comparator() # created changes in the test file conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_delta_percentage.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file, Loader=yaml.FullLoader) oper = comp.generate_test_files( main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "pre_delta_percentage", self.action, "post_delta_percentage") self.assertEqual(oper.no_passed, 0) self.assertEqual(oper.no_failed, 6)
def test_no_diff_pass(self, sqlite_mock_path, mock_path): self.hostname = '10.216.193.114' self.chk = True self.db['check_from_sqlite'] = True comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_no-diff_sql.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') sqlite_mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file, Loader=yaml.FullLoader) oper = comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_no-diff_pre", self.action, "snap_no-diff_post1") self.assertEqual(oper.no_passed, 6) self.assertEqual(oper.no_failed, 0)
def test_list_not_more_missing_node(self, mock_path): self.chk = True comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_list_not_more_node_missing.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files( main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "pre_list_not_more_node_missing", self.action, "post_list_not_more_node_missing") self.assertEqual(oper.no_passed, 0) self.assertEqual(oper.no_failed, 1)
def test_list_not_more_ignore_null_fail_1(self, mock_path): self.chk = True comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_list-not-more_ignore-null_fail_1.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files( main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_no-diff_pre", self.action, "snap_3") self.assertEqual(oper.no_passed, 2)#the name is kinda misleading but in line with the convention self.assertEqual(oper.no_failed, 0)
def test_no_diff_2_pass(self, mock_sqlite_path, mock_path): self.chk = True comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_dot-dot.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') mock_sqlite_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files( main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_no-diff_pre", self.action, "snap_3") self.assertEqual(oper.no_passed, 6) self.assertEqual(oper.no_failed, 0)
def test_incorrect_test_format(self, mock_path, mock_info, mock_sys): def fun(): raise BaseException mock_sys.return_value = fun self.chk = True mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_incorrect_4.yml') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) self.db['check_from_sqlite'] = True with patch('logging.Logger.error') as mock_log: try: comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_no-diff_pre", self.action, "snap_no-diff_post1") except BaseException: err = "ERROR!! Data stored in database is not in %s format" c_list = mock_log.call_args_list[0] self.assertNotEqual(c_list[0][0].find(err), -1)
def check_arguments(self): """ checks combination of arguments given from command line and display help if correct set of combination is not given. :return: print message in command line, regarding correct usage of JSNAPy """ ## only four test operation is permitted, if given anything apart from this, then it should print error message if (self.args.snap is False and self.args.snapcheck is False and self.args.check is False and self.args.diff is False and self.args.version is False): self.logger.error(colorama.Fore.RED + "Arguments not given correctly, Please refer help message", extra=self.log_detail) self.parser.print_help() sys.exit(1) if(((self.args.snap is True and (self.args.pre_snapfile is None or self.args.file is None)) or (self.args.snapcheck is True and self.args.file is None) or (self.args.check is True and self.args.file is None)) and (self.args.testfiles is None or self.args.hostname is None) ): self.logger.error(colorama.Fore.RED + "Arguments not given correctly, Please refer help message", extra=self.log_detail) self.parser.print_help() sys.exit(1) if self.args.diff is True: if (self.args.pre_snapfile is not None and os.path.isfile(self.args.pre_snapfile)) and ( self.args.post_snapfile is not None and os.path.isfile(self.args.post_snapfile)): comp = Comparator() comp.compare_diff( self.args.pre_snapfile, self.args.post_snapfile, None) sys.exit(1) else: if (self.args.file is None) and ( self.args.testfiles is None or self.args.hostname is None): self.parser.print_help() sys.exit(1)
def test_no_diff_pass(self, sqlite_mock_path, mock_path): self.hostname = '10.216.193.114' self.chk = True self.db['check_from_sqlite'] = True comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_no-diff_sql.yml') mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') sqlite_mock_path.return_value = os.path.join(os.path.dirname(__file__), 'configs') config_file = open(conf_file, 'r') main_file = yaml.load(config_file) oper = comp.generate_test_files( main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "snap_no-diff_pre", self.action, "snap_no-diff_post1") self.assertEqual(oper.no_passed, 6) self.assertEqual(oper.no_failed, 0)
def compare_tests( self, hostname, config_data, pre_snap=None, post_snap=None, action=None): """ called by check and snapcheck argument, to compare snap files calls the function to compare snapshots based on arguments given (--check, --snapcheck, --diff) :param hostname: device name :return: return object of Operator containing test details """ comp = Comparator() chk = self.args.check diff = self.args.diff pre_snap_file = self.args.pre_snapfile if pre_snap is None else pre_snap if (chk or diff or action in ["check", "diff"]): post_snap_file = self.args.post_snapfile if post_snap is None else post_snap test_obj = comp.generate_test_files( config_data, hostname, chk, diff, self.db, self.snap_del, pre_snap_file, action, post_snap_file) else: test_obj = comp.generate_test_files( config_data, hostname, chk, diff, self.db, self.snap_del, pre_snap_file, action) return test_obj
def test_check_xml_reply_3(self, mock_log): comp = Comparator() self.db['check_from_sqlite'] = False comp.get_xml_reply(self.db, 'nofile') mock_log.assert_called()
def test_compare_diff(self, mock_diff): comp = Comparator() comp.compare_diff('pre', 'pre', False) mock_diff.assert_called_with('pre', 'pre')
def test_compare_diff_pre_post_file_not_present(self, mock_log): comp = Comparator() comp.compare_diff('pre_no_such_file', 'post_no_such_file', False) mock_log.assert_called()