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_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 test_check_2(self, mock_path, mock_snapid, mock_logerror, mock_compreply, mock_sqpath): mock_snapid.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 self.db['first_snap_id'] = 0 self.db['second_snap_id'] = 1 comp = Comparator() comp.generate_test_files(main_file, self.hostname, self.chk, self.diff, self.db, self.snap_del, "mock_pre", action="check") mock_logerror.assert_called() mock_compreply.assert_called()
def test_check_4(self, mock_info, mock_error): comp = Comparator() self.db['check_from_sqlite'] = False oper = Operator() flag = comp.compare_xml(oper, self.db, 'mock_rpc_or_command', 'pre', 'pre') mock_info.assert_called() self.assertTrue(flag)
def check_diff_as_arg(self): """ If diff is called with both pre_snapfile and post_snap_file then just process the diff and exit """ 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)
def test_is_gt(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_pre") self.assertEqual(oper.no_passed, 1) self.assertEqual(oper.no_failed, 0)
def test_dynamic_info_err_msg_2(self, mock_path): self.chk = False comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_not-range_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) path = {'info': "Test Succeeded!! memory heap utilisation of the FPCs is within the range of $1-$2% and its value is {{post['memory-heap-utilization']}}", 'not-range': 'memory-heap-utilization, 0, 30', 'err': "Test Failed!!! memory heap utilisation of the FPCs is not within the range of $1-$2% and its value is {{post['memory-heap-utilization']}}"} ele_list = ['cpu-total', '1'] info = comp.get_info_mssg(path,ele_list) err = comp.get_err_mssg(path,ele_list) self.assertEqual(info,"Test Succeeded!! memory heap utilisation of the FPCs is within the range of 0-30% and its value is {{post['memory-heap-utilization']}}") self.assertEqual(err,"Test Failed!!! memory heap utilisation of the FPCs is not within the range of 0-30% and its value is {{post['memory-heap-utilization']}}")
def test_dynamic_info_err_msg_1(self, mock_path): self.chk = False comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_is-gt_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) path = {'info': "Test Succeeded!! cpu total is greater than $1 and its value is {{post['cpu-total']}}", 'is-gt': 'cpu-total, 1', 'err': "Test Failed!!! cpu total is less than $1 and its value is {{post['cpu-total']}}"} ele_list = ['cpu-total', '1'] info = comp.get_info_mssg(path,ele_list) err = comp.get_err_mssg(path,ele_list) self.assertEqual(info,"Test Succeeded!! cpu total is greater than 1 and its value is {{post['cpu-total']}}") self.assertEqual(err,"Test Failed!!! cpu total is less than 1 and its value is {{post['cpu-total']}}")
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_in_range_ignore_null_skip(self, mock_path): self.chk = False comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_in-range_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_in-range_pre") self.assertEqual(oper.no_passed, 0) self.assertEqual(oper.no_failed, 0)
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, Loader=yaml.FullLoader) 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_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 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_all_same_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_all-same-ignore-null_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) 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) self.assertEqual(oper.no_failed, 0)
def test_not_equal_ignore_null_5(self, mock_path, mock_debug, mock_info): self.chk = False comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_not-equal_ignore-null_5.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, 1) self.assertEqual(oper.no_failed, 0)
def test_list_not_less_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-less_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_no-diff_post") self.assertEqual(oper.no_passed, 1) #null case will pass self.assertEqual(oper.no_failed, 1)
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_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_no_diff_2(self, 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') 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, 6)
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_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_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_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_list_not_more_pass(self, mock_path): self.chk = True comp = Comparator() conf_file = os.path.join(os.path.dirname(__file__), 'configs', 'main_list-not-more.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) self.assertEqual(oper.no_failed, 0)
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_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_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_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_regex_ignore_null_true(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, 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") self.assertEqual(oper.no_failed, 1)
def test_no_diff_pass(self, sqlite_mock_path, mock_path): 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)