def helper_file_negative(self, filename, err_count): """Failure test""" template = self.load_template(filename) bad_runner = Runner(self.collection, self.transforms, filename, template, [], ['us-east-1'], []) bad_runner.transform() errs = bad_runner.run() self.assertEqual(err_count, len(errs))
def test_templates(self): """Test Successful JSON Parsing""" for _, values in self.filenames.items(): filename = values.get('filename') failures = values.get('failures') results_filename = values.get('results_filename') template = cfnlint.decode.cfn_yaml.load(filename) runner = Runner(self.rules, filename, template, ['us-east-1']) matches = list() matches.extend(runner.transform()) if not matches: matches.extend(runner.run()) if results_filename: with open(results_filename) as json_data: correct = json.load(json_data) assert len(matches) == len(correct), 'Expected {} failures, got {} on {}'.format(len(correct), len(matches), filename) for c in correct: matched = False for match in matches: if c['Location']['Start']['LineNumber'] == match.linenumber and \ c['Location']['Start']['ColumnNumber'] == match.columnnumber and \ c['Rule']['Id'] == match.rule.id: matched = True assert matched is True, 'Expected error {} at line {}, column {} in matches for {}'.format(c['Rule']['Id'], c['Location']['Start']['LineNumber'], c['Location']['Start']['ColumnNumber'], filename) else: assert len(matches) == failures, 'Expected {} failures, got {} on {}'.format(failures, len(matches), filename)
def helper_file_positive(self): """Success test""" for filename in self.success_templates: template = self.load_template(filename) good_runner = Runner(self.collection, self.transforms, filename, template, [], ['us-east-1'], []) good_runner.transform() self.assertEqual([], good_runner.run())
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_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 = 'templates/good/override/complete.yaml' template = self.load_template(filename) custom_spec = json.load(open('templates/override_spec/complete.json')) 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 = 'templates/bad/override/complete.yaml' template = self.load_template(filename) custom_spec = json.load(open('templates/override_spec/complete.json')) cfnlint.helpers.set_specs(custom_spec) bad_runner = Runner(self.collection, [], filename, template, [], ['us-east-1'], []) errs = bad_runner.run() self.assertEqual(3, len(errs))
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))
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 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_templates(self): """Test ignoring certain rules""" filename = 'test/fixtures/templates/bad/core/directives.yaml' failures = 5 template = cfnlint.decode.cfn_yaml.load(filename) runner = Runner(self.rules, filename, template, ['us-east-1']) matches = [] matches.extend(runner.transform()) if not matches: matches.extend(runner.run()) assert len( matches) == failures, 'Expected {} failures, got {} on {}'.format( failures, len(matches), filename)
def test_templates(self): """Test Successful JSON Parsing""" for _, values in self.filenames.items(): filename = values.get('filename') failures = values.get('failures') template = cfnlint.decode.cfn_yaml.load(filename) runner = Runner(self.rules, filename, template, ['us-east-1']) matches = [] matches.extend(runner.transform()) if not matches: matches.extend(runner.run()) assert len(matches) == failures, 'Expected {} failures, got {} on {}'.format( failures, len(matches), filename)
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 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
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 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 helper_transform_template(self, template, test_function): """Success test with template parameter""" good_runner = Runner([], self.transforms, 'test', template, ['us-east-1'], []) good_runner.transform() self.assertTrue(test_function(good_runner.cfn.template))
def helper_transform_cfn(self, template, test_function): """Test the bigger CFN template""" good_runner = Runner([], self.transforms, 'test', template, ['us-east-1'], []) good_runner.transform() self.assertTrue(test_function(good_runner.cfn))