def helper_file_positive_template(self, filename): """Success test with template parameter""" template = self.load_template(filename) good_runner = Runner(self.collection, filename, template, ['us-east-1'], []) good_runner.transform() self.assertEqual([], good_runner.run())
def helper_file_negative(self, filename, err_count, regions=None): """Failure test""" regions = regions or ['us-east-1'] template = self.load_template(filename) bad_runner = Runner(self.collection, filename, template, regions, []) bad_runner.transform() errs = bad_runner.run() self.assertEqual(err_count, len(errs))
def helper_file_positive(self): """Success test""" for filename in self.success_templates: template = self.load_template(filename) good_runner = Runner(self.collection, filename, template, ['us-east-1'], []) good_runner.transform() failures = good_runner.run() assert [] == failures, 'Got failures {} on {}'.format( failures, filename)
def test_success_run(self): """Success test""" filename = 'test/fixtures/templates/good/override/required.yaml' template = self.load_template(filename) with open('test/fixtures/templates/override_spec/required.json') as fp: custom_spec = json.load(fp) cfnlint.helpers.set_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 = 'test/fixtures/templates/bad/override/required.yaml' template = self.load_template(filename) with open('test/fixtures/templates/override_spec/required.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(1, len(errs))
def helper_file_rule_config(self, filename, config, err_count): """Success test with rule config included""" template = self.load_template(filename) self.collection.rules[0].configure(config) good_runner = Runner(self.collection, filename, template, ['us-east-1'], []) good_runner.transform() failures = good_runner.run() self.assertEqual( err_count, len(failures), 'Expected {} failures but got {} on {}'.format( err_count, failures, filename)) self.collection.rules[0].configure(config)
def test_bad_condition_formating(self): """ setup the cfn object """ template = { 'Conditions': { 'isNotProduction': { 'Fn::Not': [{ 'Fn::Equals': [{'Ref:' 'myEnvironment'}, 'prod'] }] }, 'isProduction': [{ 'Fn::Equals': [{'Ref:' 'myEnvironment'}, 'prod'] }] }, 'Resources': {} } runner = Runner([], 'test.yaml', template, ['us-east-1'], []) scenarios = runner.cfn.conditions.get_scenarios(['isProduction']) self.assertEqual(scenarios, [{ 'isProduction': True }, { 'isProduction': False }])
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
def get_cnflint_errors(template_path: str, region: str = "us-east-1") -> List[Match]: regions = [region] template = cfnlint.decode.cfn_yaml.load(template_path) rules = get_rules( ["cfn_lint_ax.rules"], ignore_rules=[], include_rules=["I"], include_experimental=True, ) runner = Runner(rules=rules, filename=template_path, template=template, regions=regions) runner.transform() errors = runner.run() return errors
def test_no_failure_on_list(self): """ setup the cfn object """ template = { 'Conditions': [{ 'isProduction': { 'Fn::Equals': [{'Ref:' 'myEnvironment'}, 'prod'] } }], 'Resources': {} } runner = Runner([], 'test.yaml', template, ['us-east-1'], []) self.assertEqual(runner.cfn.conditions.Conditions, {})
def test_no_failure_on_missing(self): """ setup the cfn object """ template = { 'Conditions': { 'isProduction': { 'Fn::Equals': [{'Ref:' 'myEnvironment'}, 'prod'] } }, 'Resources': {} } runner = Runner([], 'test.yaml', template, ['us-east-1'], []) scenarios = runner.cfn.conditions.get_scenarios(['isNotProduction']) self.assertEqual(scenarios, []) # Empty results if any item in the list doesn't exist scenarios = runner.cfn.conditions.get_scenarios( ['isProduction', 'isNotProduction']) self.assertEqual(scenarios, [])
def setUp(self): """ setup the cfn object """ filename = 'test/fixtures/templates/good/core/conditions.yaml' template = { 'Parameters': { 'NatType': { 'Type': 'String', 'AllowedValues': ['None', 'Single NAT', 'High Availability'] }, 'myEnvironment': { 'Type': 'String', 'AllowedValues': ['Dev', 'Stage', 'Prod'] } }, 'Conditions': { 'isProduction': { 'Fn::Equals': [{ 'Ref': 'myEnvironment' }, 'Prod'] }, 'isPrimary': { 'Fn::Equals': [ 'True', { 'Fn::FindInMap': ['location', { 'Ref': 'AWS::Region' }, 'primary'] } ] }, 'isPrimaryAndProduction': { 'Fn::And': [{ 'Condition': 'isProduction' }, { 'Condition': 'isPrimary' }] }, 'isProductionOrStaging': { 'Fn::Or': [{ 'Condition': 'isProduction' }, { 'Fn::Equals': [{ 'Ref': 'myEnvironment' }, 'Stage'] }] }, 'isNotProduction': { 'Fn::Not': [{ 'Condition': 'isProduction' }] }, 'isDevelopment': { 'Fn::Equals': ['Dev', { 'Ref': 'myEnvironment' }] }, 'isPrimaryAndProdOrStage': { 'Fn::And': [{ 'Condition': 'isProductionOrStaging' }, { 'Fn::Equals': [ 'True', { 'Fn::FindInMap': [ 'location', { 'Ref': 'AWS::Region' }, 'primary' ] } ] }] }, 'DeployNatGateway': { 'Fn::Not': [{ 'Fn::Equals': [{ 'Ref': 'NatType' }, 'None'] }] }, 'Az1Nat': { 'Fn::Or': [{ 'Fn::Equals': [{ 'Ref': 'NatType' }, 'Single NAT'] }, { 'Fn::Equals': [{ 'Ref': 'NatType' }, 'High Availability'] }] } }, 'Resources': {} } self.runner = Runner([], filename, template, ['us-east-1'], []) self.conditions = self.runner.cfn.conditions