def test_close_match(self): # Test for near match to 'bypass_reason' - ratio method kwargs = {'byepass_reason': 'True'} unused, close_matches = find_unused_arguments(kwargs, [], self.test_app.tasks) self.assertEqual(len(unused), 1) self.assertEqual(list(unused.keys())[0], list(kwargs.keys())[0]) self.assertEqual(len(close_matches), 1) self.assertEqual('bypass_reason', close_matches[list(kwargs.keys())[0]]) # Test for near match to 'short' - ratio method kwargs = {'sgort': 'True'} unused, close_matches = find_unused_arguments(kwargs, [], self.test_app.tasks) self.assertEqual(len(unused), 1) self.assertEqual(list(unused.keys())[0], list(kwargs.keys())[0]) self.assertEqual(len(close_matches), 1) self.assertEqual('short', close_matches[list(kwargs.keys())[0]]) # Test for no near matches kwargs = {'a_completely_bogus_argument': 'doesnt_matter'} unused, close_matches = find_unused_arguments(kwargs, [], self.test_app.tasks) self.assertEqual(len(unused), 1) self.assertEqual(list(unused.keys())[0], list(kwargs.keys())[0]) self.assertEqual(len(close_matches), 0)
def test_only_applicable(self): unused, _ = find_unused_arguments({}, [], self.test_app.tasks) self.assertEqual(len(unused), 0) kwargs = {'chain': 'noop', 'uid': 'FireX-mdelahou-161215-150725-21939'} unused, _ = find_unused_arguments(kwargs, ["chain"], self.test_app.tasks) self.assertEqual(len(unused), 0)
def test_not_applicable(self): kwargs = self.base_kwargs kwargs["uid"] = "valid stuff" kwargs["not_applicable"] = "invalid stuff" unused, _ = find_unused_arguments(kwargs, ["chain"], self.test_app.tasks) self.assertEqual(len(unused), 1)
def test_white_list(self): kwargs = { 'chain': 'noop', 'uid': 'FireX-mdelahou-161215-150725-21939', 'list_arg': "a list", 'str_arg': "a str" } whitelist_arguments(["list_arg"]) whitelist_arguments('str_arg') unused, _ = find_unused_arguments(kwargs, ["chain", "anything"], self.test_app.tasks) self.assertEqual(len(unused), 0)
def validate_argument_applicability(cls, chain_args, args, all_tasks): if isinstance(args, argparse.Namespace): args = vars(args) if isinstance(args, dict): args = list(args.keys()) unused_chain_args, matches = find_unused_arguments(chain_args=chain_args, ignore_list=args, all_tasks=all_tasks) if not unused_chain_args: # everything is used. Good job! return True logger.error("Invalid arguments provided. The following arguments are not used by any microservices:") for arg in unused_chain_args: if arg in matches: logger.error("--" + arg + " (Did you mean '%s'?)" % matches[arg]) else: logger.error("--" + arg) return False