class TestInclude(BaseTestCase):
    """Used for Testing Rules"""

    def setUp(self):
        """Setup"""
        self.collection = RulesCollection()
        self.collection.register(Configuration())

    def tearDown(self):
        """Tear Down"""
        # Reset the Spec override to prevent other tests to fail
        cfnlint.helpers.initialize_specs()

    def test_fail_run(self):
        """Failure test required"""
        filename = 'test/fixtures/templates/bad/override/include.yaml'
        template = self.load_template(filename)

        with open('test/fixtures/templates/override_spec/include.json') as fp:
            custom_spec = json.load(fp)
        cfnlint.helpers.set_specs(custom_spec)

        bad_runner = Runner(self.collection, filename, template, ['us-east-1'], [])
        errs = bad_runner.run()
        self.assertEqual(2, len(errs))
Exemple #2
0
class TestOverrideRequired(BaseTestCase):
    """Used for Testing Rules"""

    def setUp(self):
        """Setup"""
        self.collection = RulesCollection()
        self.collection.register(Required())

    def tearDown(self):
        """Tear Down"""
        # Reset the Spec override to prevent other tests to fail
        cfnlint.helpers.initialize_specs()

    def test_success_run(self):
        """Success test"""
        filename = 'templates/good/override/required.yaml'
        template = self.load_template(filename)
        custom_spec = json.load(open('templates/override_spec/required.json'))

        cfnlint.helpers.override_specs(custom_spec)

        good_runner = Runner(self.collection, [], filename, template, [], ['us-east-1'], [])
        self.assertEqual([], good_runner.run())

    def test_fail_run(self):
        """Failure test required"""
        filename = 'templates/bad/override/required.yaml'
        template = self.load_template(filename)
        custom_spec = json.load(open('templates/override_spec/required.json'))

        cfnlint.helpers.override_specs(custom_spec)

        bad_runner = Runner(self.collection, [], filename, template, [], ['us-east-1'], [])
        errs = bad_runner.run()
        self.assertEqual(1, len(errs))
class TestBaseTemplate(BaseRuleTestCase):
    """Test base template"""
    def setUp(self):
        """Setup"""
        self.collection = RulesCollection()
        self.collection.register(Base())

    def test_file_negative(self):
        """Failure test"""
        failure = 'test/fixtures/templates/bad/template.yaml'
        try:
            Runner(self.collection, failure, True)
            self.assertEqual(1, 0)
        except Exception:
            pass
Exemple #4
0
class TestPropertyVpcId(BaseRuleTestCase):
    """Test Password Property Configuration"""
    def setUp(self):
        """Setup"""
        self.collection = RulesCollection()
        self.collection.register(ImageId())

    success_templates = [
        'templates/good/generic.yaml',
        'templates/quickstart/nist_high_master.yaml',
        'templates/quickstart/nist_config_rules.yaml',
        'templates/quickstart/nist_iam.yaml',
        'templates/quickstart/nist_logging.yaml',
        'templates/quickstart/nist_vpc_production.yaml',
        'templates/quickstart/openshift_master.yaml',
        'templates/quickstart/openshift.yaml',
        'templates/quickstart/cis_benchmark.yaml',
        'templates/good/minimal.yaml', 'templates/good/transform.yaml',
        'templates/good/conditions.yaml'
    ]

    def test_file_positive(self):
        """Success test"""
        self.helper_file_positive()

    def test_file_negative_nist_app(self):
        """Failure test"""
        self.helper_file_negative('templates/quickstart/nist_application.yaml',
                                  3)

    def test_file_negative_nist_mgmt(self):
        """Failure test"""
        self.helper_file_negative(
            'templates/quickstart/nist_vpc_management.yaml', 1)

    def test_file_negative(self):
        """Failure test"""
        self.helper_file_negative('templates/bad/properties_imageid.yaml', 1)
    def test_update_iam_policies(self):
        """Success update documentation"""

        class TestRuleError(CloudFormationLintRule):
            """ Def Rule """
            id = 'E1000'
            shortdesc = 'Test Error'
            description = 'Test Description'
            source_url = 'https://github.com/aws-cloudformation/cfn-python-lint/'
            tags = ['resources']

        class TestRuleExpiremental(CloudFormationLintRule):
            """ Def Rule """
            id = 'E1001'
            shortdesc = 'Test Expiremental'
            description = 'Test Description'
            source_url = 'https://github.com/aws-cloudformation/cfn-python-lint/'
            tags = ['resources']
            experimental = True

        class TestRuleWarning(CloudFormationLintRule):
            """ Def Rule """
            id = 'W1001'
            shortdesc = 'Test Warning'
            description = 'Test Description'
            source_url = 'https://github.com/aws-cloudformation/cfn-python-lint/'
            tags = ['resources', 'iam']

        collection = RulesCollection(include_rules=['I'], include_experimental=True)
        collection.register(TestRuleError())
        collection.register(TestRuleWarning())
        collection.register(TestRuleExpiremental())

        if sys.version_info.major == 3:
            builtin_module_name = 'builtins'
        else:
            builtin_module_name = '__builtin__'

        mo = mock_open(read_data=self.TEST_TEXT)
        mo.return_value.__iter__ = lambda self: self
        mo.return_value.__iter__ = lambda self: iter(self.readline, '')
        with patch('{}.open'.format(builtin_module_name), mo) as mock_builtin_open:
            cfnlint.maintenance.update_documentation(collection)

            expected_calls = [
                call('\n'),
                call('Regular Text\n'),
                call('## Rules\n'),
                call('The following **{}** rules are applied by this linter:\n'.format(
                    len(collection))),
                call('(_This documentation is generated from the Rules, do not alter this manually_)\n\n'),
                call('| Rule ID  | Title | Description | Config<br />(Name:Type:Default) | Source | Tags |\n'),
                call('| -------- | ----- | ----------- | ---------- | ------ | ---- |\n'),
                call(
                    '| E0000<a name="E0000"></a> | Parsing error found when parsing the template | Checks for Null values and Duplicate values in resources |  | [Source]() | `base` |\n'),
                call(
                    '| E0001<a name="E0001"></a> | Error found when transforming the template | Errors found when performing transformation on the template |  | [Source]() | `base`,`transform` |\n'),
                call(
                    '| E0002<a name="E0002"></a> | Error processing rule on the template | Errors found when processing a rule on the template |  | [Source]() | `base`,`rule` |\n'),
                call(
                    '| E1000<a name="E1000"></a> | Test Error | Test Description |  | [Source](https://github.com/aws-cloudformation/cfn-python-lint/) | `resources` |\n'),
                call(
                    '| W1001<a name="W1001"></a> | Test Warning | Test Description |  | [Source](https://github.com/aws-cloudformation/cfn-python-lint/) | `resources`,`iam` |\n'),
                call('### Experimental rules\n'),
                call('| Rule ID  | Title | Description | Source | Tags |\n'),
                call('| -------- | ----- | ----------- | ------ | ---- |\n'),
                call(
                    '| E1001<a name="E1001"></a> | Test Expiremental | Test Description |  | [Source](https://github.com/aws-cloudformation/cfn-python-lint/) | `resources` |\n'),
            ]
            mock_builtin_open.return_value.write.assert_has_calls(expected_calls)
            self.assertEqual(len(expected_calls), mock_builtin_open.return_value.write.call_count)