def test_validate(self): t = tempfile.NamedTemporaryFile(suffix=".yml") t.write(yaml.dump({'policies': [ {'name': 'foo', 'resource': 's3', 'filters': [ {"tag:custodian_tagging": "not-null"}], 'actions': [{ 'type': 'unmark', 'tags': ['custodian_cleanup']}]}]}, Dumper=yaml.SafeDumper)) t.flush() self.addCleanup(t.close) temp_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, temp_dir) exit_code = [] def exit(code): exit_code.append(code) self.patch(sys, 'exit', exit) self.patch(sys, 'argv', [ 'custodian', 'validate', '-c', t.name]) cli.main() self.assertEqual(exit_code, [1])
def test_elasticache_insecure(self): mock_cache('ElastiCacheCluster', 'mock-elasticache-resources.json', self.CACHE_FILENAME) # TODO: instead or mocked cache, look at boto stubber # https://botocore.readthedocs.io/en/latest/reference/stubber.html tmp_argv = build_arv('elasticache-redis-insecure.yml', self.CACHE_FILENAME) sys.argv = tmp_argv main() with open('tmp/elasticache-redis-insecure/resources.json') as f: resources = json.load(f) count = jmespath.search('length([])', resources) self.assertEqual(4, count) count = jmespath.search('length([]."c7n:MatchedFilters"[?contains(@, `AuthTokenEnabled`)][])', resources) self.assertEqual(2, count) count = jmespath.search('length([]."c7n:MatchedFilters"[?contains(@, `TransitEncryptionEnabled`)][])', resources) self.assertEqual(2, count) count = jmespath.search('length([]."c7n:MatchedFilters"[?contains(@, `AtRestEncryptionEnabled`)][])', resources) self.assertEqual(2, count)
def run_and_expect_exception(self, argv, exception): """ Run cli.main() with supplied argv and expect supplied exception. """ self.patch(sys, 'argv', argv) try: cli.main() except exception: return self.fail('Error: did not raise {}.'.format(exception))
def run_and_expect_failure(self, argv, exit_code): """ Run cli.main() with supplied argv and expect exit_code. """ self.patch(sys, 'argv', argv) out, err = self.capture_output() with self.assertRaises(SystemExit) as cm: cli.main() self.assertEqual(cm.exception.code, exit_code) return out.getvalue(), err.getvalue()
def run_and_expect_exception(self, argv, exception): """ Run cli.main() with supplied argv and expect supplied exception. """ self.patch(sys, 'argv', argv) #clear_resources() try: cli.main() except exception: return self.fail('Error: did not raise {}.'.format(exception))
def run_and_expect_failure(self, argv, exit_code): """ Run cli.main() with supplied argv and expect exit_code. """ self.patch(sys, 'argv', argv) out, err = self.capture_output() #clear_resources() with self.assertRaises(SystemExit) as cm: cli.main() self.assertEqual(cm.exception.code, exit_code) return out.getvalue(), err.getvalue()
def run_and_expect_success(self, argv, capture=True): """ Run cli.main() with supplied argv and expect normal execution. """ self.patch(sys, 'argv', argv) out, err = self.capture_output() try: cli.main() except SystemExit as e: self.fail('Expected sys.exit would not be called. Exit code was ({})'.format(e.message)) return out.getvalue(), err.getvalue()
def run_and_expect_exception(self, argv, exception): """ Run cli.main() with supplied argv and expect supplied exception. """ self.patch_account_id() self.patch(sys, "argv", argv) # clear_resources() try: cli.main() except exception: return self.fail("Error: did not raise {}.".format(exception))
def run_and_expect_success(self, argv): """ Run cli.main() with supplied argv and expect normal execution. """ self.patch_account_id() self.patch(sys, "argv", argv) out, err = self.capture_output() try: cli.main() except SystemExit as e: self.fail( "Expected sys.exit would not be called. Exit code was ({})". format(e.code)) return out.getvalue(), err.getvalue()
def run_and_expect_success(self, argv): """ Run cli.main() with supplied argv and expect normal execution. """ self.patch_account_id() self.patch(sys, "argv", argv) out, err = self.capture_output() try: cli.main() except SystemExit as e: self.fail( "Expected sys.exit would not be called. Exit code was ({})".format( e.code ) ) return out.getvalue(), err.getvalue()
def test_run(self): t = tempfile.NamedTemporaryFile(suffix=".yml") t.write(yaml.dump({'policies': []}, Dumper=yaml.SafeDumper)) t.flush() self.addCleanup(t.close) temp_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, temp_dir) exit_code = [] def exit(code): exit_code.append(code) self.patch(sys, 'exit', exit) self.patch(sys, 'argv', [ 'custodian', 'run', '-c', t.name, "-s", temp_dir]) cli.main() self.assertEqual(exit_code, [0])
def test_run(self): t = tempfile.NamedTemporaryFile(suffix=".yml") t.write(yaml.dump({'policies': []}, Dumper=yaml.SafeDumper)) t.flush() self.addCleanup(t.close) temp_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, temp_dir) exit_code = [] def exit(code): exit_code.append(code) self.patch(sys, 'exit', exit) self.patch(sys, 'argv', ['custodian', 'run', '-c', t.name, "-s", temp_dir]) cli.main() self.assertEqual(exit_code, [0])
def test_version(self): self.patch(sys, "argv", ['custodian', 'version']) out = StringIO() self.patch(sys, "stdout", out) cli.main() self.assertEqual(out.getvalue().strip(), version.version)