Exemplo n.º 1
0
class TestMetaMainHasInfo(unittest.TestCase):
    collection = RulesCollection()
    collection.register(MetaMainHasInfoRule())

    def setUp(self):
        self.runner = RunFromText(self.collection)

    def test_no_galaxy_info(self):
        results = self.runner.run_role_meta_main(NO_GALAXY_INFO)
        assert len(results) == 1
        self.assertIn("No 'galaxy_info' found", str(results))

    def test_missing_info(self):
        results = self.runner.run_role_meta_main(MISSING_INFO)
        assert len(results) == 3
        self.assertIn("Role info should contain author", str(results))
        self.assertIn("Role info should contain min_ansible_version",
                      str(results))
        self.assertIn("Platform should contain name", str(results))

    def test_bad_types(self):
        results = self.runner.run_role_meta_main(BAD_TYPES)
        assert len(results) == 3
        self.assertIn("author should be a string", str(results))
        self.assertIn("description should be a string", str(results))
        self.assertIn("Platforms should be a list of dictionaries",
                      str(results))

    def test_platform_list_of_str(self):
        results = self.runner.run_role_meta_main(PLATFORMS_LIST_OF_STR)
        assert len(results) == 1
        self.assertIn("Platforms should be a list of dictionaries",
                      str(results))
class TestComparisonToLiteralBoolRule(unittest.TestCase):
    collection = RulesCollection()
    collection.register(ComparisonToLiteralBoolRule())

    def setUp(self) -> None:
        self.runner = RunFromText(self.collection)

    def test_when(self) -> None:
        results = self.runner.run_role_tasks_main(PASS_WHEN)
        self.assertEqual(0, len(results))

    def test_when_not_false(self) -> None:
        results = self.runner.run_role_tasks_main(PASS_WHEN_NOT_FALSE)
        self.assertEqual(0, len(results))

    def test_when_not_null(self) -> None:
        results = self.runner.run_role_tasks_main(PASS_WHEN_NOT_NULL)
        self.assertEqual(0, len(results))

    def test_literal_true(self) -> None:
        results = self.runner.run_role_tasks_main(FAIL_LITERAL_TRUE)
        self.assertEqual(1, len(results))

    def test_literal_false(self) -> None:
        results = self.runner.run_role_tasks_main(FAIL_LITERAL_FALSE)
        assert len(results) == 2, results
Exemplo n.º 3
0
class TestOctalPermissionsRuleWithFile(unittest.TestCase):

    collection = RulesCollection()
    VALID_MODES = [
        0o777, 0o775, 0o770, 0o755, 0o750, 0o711, 0o710, 0o700, 0o666, 0o664,
        0o660, 0o644, 0o640, 0o600, 0o555, 0o551, 0o550, 0o511, 0o510, 0o500,
        0o444, 0o440, 0o400
    ]

    INVALID_MODES = [
        777,
        775,
        770,
        755,
        750,
        711,
        710,
        700,
        666,
        664,
        660,
        644,
        640,
        622,
        620,
        600,
        555,
        551,
        550,  # 511 == 0o777, 510 == 0o776, 500 == 0o764
        444,
        440,
        400
    ]

    def setUp(self):
        self.rule = OctalPermissionsRule()
        self.collection.register(self.rule)
        self.runner = RunFromText(self.collection)

    def test_success(self):
        results = self.runner.run_playbook(SUCCESS_TASKS)
        self.assertEqual(0, len(results))

    def test_fail(self):
        results = self.runner.run_playbook(FAIL_TASKS)
        self.assertEqual(4, len(results))

    def test_valid_modes(self):
        for mode in self.VALID_MODES:
            self.assertFalse(self.rule.is_invalid_permission(mode),
                             msg="0o%o should be a valid mode" % mode)

    def test_invalid_modes(self):
        for mode in self.INVALID_MODES:
            self.assertTrue(self.rule.is_invalid_permission(mode),
                            msg="%d should be an invalid mode" % mode)
Exemplo n.º 4
0
class TestEnvVarsInCommand(unittest.TestCase):
    collection = RulesCollection()
    collection.register(EnvVarsInCommandRule())

    def setUp(self):
        self.runner = RunFromText(self.collection)

    def test_success(self):
        results = self.runner.run_playbook(SUCCESS_PLAY_TASKS)
        self.assertEqual(0, len(results))

    def test_fail(self):
        results = self.runner.run_playbook(FAIL_PLAY_TASKS)
        self.assertEqual(2, len(results))
Exemplo n.º 5
0
class TestRoleRelativePath(unittest.TestCase):
    collection = RulesCollection()
    collection.register(RoleRelativePath())

    def setUp(self):
        self.runner = RunFromText(self.collection)

    def test_fail(self):
        results = self.runner.run_role_tasks_main(FAIL_TASKS)
        self.assertEqual(4, len(results))

    def test_success(self):
        results = self.runner.run_role_tasks_main(SUCCESS_TASKS)
        self.assertEqual(0, len(results))
Exemplo n.º 6
0
class TestShellWithoutPipeFail(unittest.TestCase):
    collection = RulesCollection()
    collection.register(ShellWithoutPipefail())

    def setUp(self) -> None:
        self.runner = RunFromText(self.collection)

    def test_fail(self) -> None:
        results = self.runner.run_playbook(FAIL_TASKS)
        self.assertEqual(3, len(results))

    def test_success(self) -> None:
        results = self.runner.run_playbook(SUCCESS_TASKS)
        self.assertEqual(0, len(results))
class TestUseHandlerRatherThanWhenChanged(unittest.TestCase):
    collection = RulesCollection()
    collection.register(UseHandlerRatherThanWhenChangedRule())

    def setUp(self):
        self.runner = RunFromText(self.collection)

    def test_success(self):
        results = self.runner.run_role_tasks_main(SUCCESS_TASKS)
        self.assertEqual(0, len(results))

    def test_fail(self):
        results = self.runner.run_role_tasks_main(FAIL_TASKS)
        self.assertEqual(5, len(results))
class TestComparisonToEmptyStringRule(unittest.TestCase):
    collection = RulesCollection()
    collection.register(ComparisonToEmptyStringRule())

    def setUp(self):
        self.runner = RunFromText(self.collection)

    def test_success(self):
        results = self.runner.run_role_tasks_main(SUCCESS_TASKS)
        self.assertEqual(0, len(results))

    def test_fail(self):
        results = self.runner.run_playbook(FAIL_TASKS)
        self.assertEqual(2, len(results))
Exemplo n.º 9
0
def rule_runner(request: SubRequest, config_options: Namespace) -> RunFromText:
    """Return runner for a specific rule class."""
    rule_class = request.param
    config_options.enable_list.append(rule_class().id)
    collection = RulesCollection(options=config_options)
    collection.register(rule_class())
    return RunFromText(collection)
Exemplo n.º 10
0
def test_pre_tasks(default_text_runner: RunFromText, playbook: str,
                   length: int) -> None:
    # When
    results = default_text_runner.run_playbook(playbook)

    # Then
    assert len(results) == length
 def test_rule_command_instead_of_shell(
     default_text_runner: RunFromText, text: str, expected: int
 ) -> None:
     """Validate that rule works as intended."""
     results = default_text_runner.run_playbook(text)
     for result in results:
         assert result.rule.id == UseCommandInsteadOfShellRule.id, result
     assert len(results) == expected
Exemplo n.º 12
0
class TestTaskNoLocalAction(unittest.TestCase):
    collection = RulesCollection()
    collection.register(TaskNoLocalAction())

    def setUp(self):
        self.runner = RunFromText(self.collection)

    def test_local_action(self):
        results = self.runner.run_role_tasks_main(TASK_LOCAL_ACTION)
        self.assertEqual(1, len(results))
class TestVariableHasSpaces(unittest.TestCase):
    collection = RulesCollection()
    collection.register(VariableHasSpacesRule())

    def setUp(self):
        self.runner = RunFromText(self.collection)

    def test_variable_has_spaces(self):
        results = self.runner.run_role_tasks_main(TASK_VARIABLES)
        self.assertEqual(3, len(results))
Exemplo n.º 14
0
class TestLineTooLongRule(unittest.TestCase):
    collection = RulesCollection()
    collection.register(LineTooLongRule())

    def setUp(self):
        self.runner = RunFromText(self.collection)

    def test_long_line(self):
        results = self.runner.run_role_tasks_main(LONG_LINE)
        self.assertEqual(1, len(results))
Exemplo n.º 15
0
class TestSudoRule(unittest.TestCase):
    collection = RulesCollection()
    collection.register(SudoRule())

    def setUp(self):
        self.runner = RunFromText(self.collection)

    def test_run_role_fail(self):
        results = self.runner.run_role_tasks_main(ROLE_2_ERRORS)
        self.assertEqual(2, len(results))

    def test_run_role_pass(self):
        results = self.runner.run_role_tasks_main(ROLE_0_ERRORS)
        self.assertEqual(0, len(results))

    def test_play_root_and_task_fail(self):
        results = self.runner.run_playbook(PLAY_4_ERRORS)
        self.assertEqual(4, len(results))

    def test_play_task_fail(self):
        results = self.runner.run_playbook(PLAY_1_ERROR)
        self.assertEqual(1, len(results))
class TestMetaChangeFromDefault(unittest.TestCase):
    collection = RulesCollection()
    collection.register(MetaChangeFromDefaultRule())

    def setUp(self):
        self.runner = RunFromText(self.collection)

    def test_default_galaxy_info(self):
        results = self.runner.run_role_meta_main(DEFAULT_GALAXY_INFO)
        self.assertIn("Should change default metadata: author", str(results))
        self.assertIn("Should change default metadata: description", str(results))
        self.assertIn("Should change default metadata: company", str(results))
        self.assertIn("Should change default metadata: license", str(results))
Exemplo n.º 17
0
class TestDeprecatedModuleRule(unittest.TestCase):
    collection = RulesCollection()
    collection.register(DeprecatedModuleRule())

    def setUp(self) -> None:
        self.runner = RunFromText(self.collection)

    def test_module_deprecated(self) -> None:
        results = self.runner.run_role_tasks_main(MODULE_DEPRECATED)
        self.assertEqual(1, len(results))
        # based on version and blend of ansible being used, we may
        # get a missing module, so we future proof the test
        assert ("couldn't resolve module" not in results[0].message
                or "Deprecated module" not in results[0].message)
Exemplo n.º 18
0
class TestMetaVideoLinks(unittest.TestCase):
    collection = RulesCollection()
    collection.register(MetaVideoLinksRule())

    def setUp(self):
        self.runner = RunFromText(self.collection)

    def test_video_links(self):
        results = self.runner.run_role_meta_main(META_VIDEO_LINKS)
        self.assertIn("Expected item in 'video_links' to be a dictionary", str(results))
        self.assertIn(
            "'video_links' to contain only keys 'url' and 'title'", str(results)
        )
        self.assertIn("URL format 'www.myvid.com/vid' is not recognized", str(results))
Exemplo n.º 19
0
class TestMetaTagValid(unittest.TestCase):
    collection = RulesCollection()
    collection.register(MetaTagValidRule())

    def setUp(self):
        self.runner = RunFromText(self.collection)

    def test_valid_tag_rule(self):
        results = self.runner.run_role_meta_main(META_TAG_VALID)
        self.assertIn("Use 'galaxy_tags' rather than 'categories'",
                      str(results))
        self.assertIn("Expected 'categories' to be a list", str(results))
        self.assertIn("invalid: 'my s q l'", str(results))
        self.assertIn("invalid: 'MYTAG'", str(results))
Exemplo n.º 20
0
class TestDeprecatedModuleRule(unittest.TestCase):
    collection = RulesCollection()
    collection.register(DeprecatedModuleRule())

    def setUp(self):
        self.runner = RunFromText(self.collection)

    @pytest.mark.xfail(
        ANSIBLE_MAJOR_VERSION > (2, 9),
        reason='Ansible devel has changed so ansible-lint needs fixing. '
        'Ref: https://github.com/ansible/ansible-lint/issues/675',
        raises=SystemExit,
        strict=True,
    )
    def test_module_deprecated(self):
        results = self.runner.run_role_tasks_main(MODULE_DEPRECATED)
        self.assertEqual(1, len(results))
Exemplo n.º 21
0
 def test_ignore_errors_false(rule_runner: RunFromText) -> None:
     """The task uses ignore_errors: false, oddly enough."""
     results = rule_runner.run_playbook(IGNORE_ERRORS_FALSE)
     assert len(results) == 0
Exemplo n.º 22
0
 def test_fqcn_builtin_fail(rule_runner: RunFromText) -> None:
     """Test rule matches."""
     results = rule_runner.run_playbook(FAIL_PLAY)
     assert len(results) == 1
     for result in results:
         assert result.message == FQCNBuiltinsRule.shortdesc
Exemplo n.º 23
0
 def test_failed_when(rule_runner: RunFromText) -> None:
     """Instead of ignore_errors, this task uses failed_when."""
     results = rule_runner.run_playbook(FAILED_WHEN)
     assert len(results) == 0
Exemplo n.º 24
0
def rule_runner(request):
    """Return runner for a specific rule class."""
    rule_class = request.param
    collection = RulesCollection()
    collection.register(rule_class())
    return RunFromText(collection)
Exemplo n.º 25
0
def default_text_runner(default_rules_collection):
    """Return RunFromText instance for the default set of collections."""
    return RunFromText(default_rules_collection)
 def setUp(self):
     self.runner = RunFromText(self.collection)
Exemplo n.º 27
0
 def test_invalid_var_name_playbook(rule_runner: RunFromText) -> None:
     """Test rule matches."""
     results = rule_runner.run_playbook(FAIL_PLAY)
     assert len(results) == 2
     for result in results:
         assert result.rule.id == VariableNamingRule.id
Exemplo n.º 28
0
 def test_fqcn_builtin_pass(rule_runner: RunFromText) -> None:
     """Test rule does not match."""
     results = rule_runner.run_playbook(SUCCESS_PLAY)
     assert len(results) == 0, results
Exemplo n.º 29
0
 def setUp(self) -> None:
     self.rule = OctalPermissionsRule()
     self.collection.register(self.rule)
     self.runner = RunFromText(self.collection)
Exemplo n.º 30
0
 def test_ignore_errors_true(rule_runner: RunFromText) -> None:
     """The task uses ignore_errors."""
     results = rule_runner.run_playbook(IGNORE_ERRORS_TRUE)
     assert len(results) == 1