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)
class TestComparisonToLiteralBoolRule(unittest.TestCase):
    collection = RulesCollection()
    collection.register(ComparisonToLiteralBoolRule())

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

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

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

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

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

    def test_literal_false(self):
        results = self.runner.run_role_tasks_main(FAIL_LITERAL_FALSE)
        self.assertEqual(1, len(results))
示例#3
0
class TestSkipPlaybookItems(unittest.TestCase):
    rulesdir = os.path.join('lib', 'ansiblelint', 'rules')
    collection = RulesCollection.create_from_directory(rulesdir)

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

    def test_pre_tasks(self):
        results = self.runner.run_playbook(PLAYBOOK_PRE_TASKS)
        self.assertEqual(2, len(results))

    def test_post_tasks(self):
        results = self.runner.run_playbook(PLAYBOOK_POST_TASKS)
        self.assertEqual(2, len(results))

    def test_play_handlers(self):
        results = self.runner.run_playbook(PLAYBOOK_HANDLERS)
        self.assertEqual(2, len(results))

    def test_two_plays(self):
        results = self.runner.run_playbook(PLAYBOOK_TWO_PLAYS)
        self.assertEqual(2, len(results))

    def test_with_block(self):
        results = self.runner.run_playbook(PLAYBOOK_WITH_BLOCK)
        self.assertEqual(4, len(results))
class TestRoleRelativePath(unittest.TestCase):
    collection = RulesCollection()
    collection.register(RoleRelativePath())

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

    @pytest.mark.xfail(
        Version(get_dist_version('ansible')) >= Version('2.10.dev0')
        and Version(get_dist_version('ansible-base')) >= Version('2.10.dev0'),
        reason='Post-split Ansible Core Engine does not have '
        'the module used in the test playbook.'
        ' Ref: https://github.com/ansible/ansible-lint/issues/703.'
        ' Ref: https://github.com/ansible/ansible/pull/68598.',
        raises=SystemExit,
        strict=True,
    )
    def test_fail(self):
        results = self.runner.run_role_tasks_main(FAIL_TASKS)
        self.assertEqual(4, len(results))

    @pytest.mark.xfail(
        Version(get_dist_version('ansible')) >= Version('2.10.dev0')
        and Version(get_dist_version('ansible-base')) >= Version('2.10.dev0'),
        reason='Post-split Ansible Core Engine does not have '
        'the module used in the test playbook.'
        ' Ref: https://github.com/ansible/ansible-lint/issues/703.'
        ' Ref: https://github.com/ansible/ansible/pull/68598.',
        raises=SystemExit,
        strict=True,
    )
    def test_success(self):
        results = self.runner.run_role_tasks_main(SUCCESS_TASKS)
        self.assertEqual(0, len(results))
示例#5
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) == 2
        self.assertIn("Platforms should be a list of dictionaries",
                      str(results))
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)
示例#7
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))
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))
示例#9
0
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))
示例#10
0
class TestShellWithoutPipeFail(unittest.TestCase):
    collection = RulesCollection()
    collection.register(ShellWithoutPipefail())

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

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

    def test_success(self):
        results = self.runner.run_playbook(SUCCESS_TASKS)
        self.assertEqual(0, len(results))
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))
class TestPackageHasRetry(unittest.TestCase):
    collection = RulesCollection()
    collection.register(PackageHasRetryRule())

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

    def test_file_positive(self):
        results = self.runner.run_playbook(SUCCESS)
        self.assertEqual(0, len(results))

    def test_file_negative(self):
        results = self.runner.run_playbook(FAILURE)
        self.assertEqual(2, len(results))
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))
示例#14
0
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))
class TestSkipInsideYaml(unittest.TestCase):
    rulesdir = os.path.join('lib', 'ansiblelint', 'rules')
    collection = RulesCollection.create_from_directory(rulesdir)

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

    def test_role_tasks(self):
        results = self.runner.run_role_tasks_main(ROLE_TASKS)
        self.assertEqual(5, len(results))

    def test_playbook(self):
        results = self.runner.run_playbook(PLAYBOOK)
        self.assertEqual(7, len(results))

    def test_role_meta(self):
        results = self.runner.run_role_meta_main(ROLE_META)
        self.assertEqual(0, len(results))
class TestComparisonToEmptyStringRule(unittest.TestCase):
    collection = RulesCollection()
    collection.register(ComparisonToEmptyStringRule())

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

    def test_compare_when(self):
        results = self.runner.run_role_tasks_main(PASS_WHEN)
        self.assertEqual(0, len(results))

    def test_compare_is_empty(self):
        results = self.runner.run_playbook(FAIL_IS_EMPTY)
        self.assertEqual(1, len(results))

    def test_compare_is_not_empty(self):
        results = self.runner.run_role_tasks_main(FAIL_IS_NOT_EMPTY)
        self.assertEqual(1, len(results))
示例#17
0
class TestSkipInsideYaml(unittest.TestCase):
    rulesdir = os.path.join('lib', 'ansiblelint', 'rules')
    collection = RulesCollection.create_from_directory(rulesdir)

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

    def test_role_tasks(self):
        results = self.runner.run_role_tasks_main(ROLE_TASKS)
        self.assertEqual(5, len(results))

    def test_playbook(self):
        results = self.runner.run_playbook(PLAYBOOK)
        self.assertEqual(8, len(results))

    def test_role_meta(self):
        results = self.runner.run_role_meta_main(ROLE_META)
        self.assertEqual(0, 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))
示例#19
0
class TestDeprecatedModuleRule(unittest.TestCase):
    collection = RulesCollection()
    collection.register(DeprecatedModuleRule())

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

    def test_module_deprecated(self):
        results = self.runner.run_role_tasks_main(MODULE_DEPRECATED)
        self.assertEqual(1, len(results))
示例#20
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))
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 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))
示例#23
0
class TestRoleRelativePath(unittest.TestCase):
    collection = RulesCollection()
    collection.register(RoleRelativePath())

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

    def test_role_relative_path(self):
        results = self.runner.run_role_tasks_main(ROLE_RELATIVE_PATH)
        self.assertEqual(4, 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))
示例#25
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)
        self.assertIn("No 'galaxy_info' found",
                      str(results))

    def test_missing_info(self):
        results = self.runner.run_role_meta_main(MISSING_INFO)
        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))
示例#26
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 TestSkipInsideYaml(unittest.TestCase):
    rulesdir = os.path.join('lib', 'ansiblelint', 'rules')
    collection = RulesCollection([rulesdir])

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

    def test_role_tasks(self):
        results = self.runner.run_role_tasks_main(ROLE_TASKS)
        self.assertEqual(1, len(results))

    def test_role_tasks_with_block(self):
        results = self.runner.run_role_tasks_main(ROLE_TASKS_WITH_BLOCK)
        self.assertEqual(4, len(results))

    def test_playbook(self):
        results = self.runner.run_playbook(PLAYBOOK)
        self.assertEqual(7, len(results))

    def test_role_meta(self):
        results = self.runner.run_role_meta_main(ROLE_META)
        self.assertEqual(0, len(results))
示例#28
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))
示例#30
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))
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))
示例#32
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))
示例#33
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))
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))
 def setUp(self):
     self.runner = RunFromText(self.collection)
示例#36
0
def default_text_runner(default_rules_collection):
    return RunFromText(default_rules_collection)
 def setUp(self):
     self.runner = RunFromText(self.collection)
 def setUp(self):
     self.rule = OctalPermissionsRule()
     self.collection.register(self.rule)
     self.runner = RunFromText(self.collection)
示例#39
0
 def setUp(self):
     self.rule = OctalPermissionsRule()
     self.collection.register(self.rule)
     self.runner = RunFromText(self.collection)