def test_verbose_two(self): args = ["--os-auth-url", "http://no.com", "-vv", "server"] config = CloudCleanerConfig(args=args) ALL_RESOURCES["server"].register(config) config.parse_args() log = getLogger("cloud_cleaner") self.assertEqual(log.getEffectiveLevel(), DEBUG)
def cloud_clean( args=sys.argv[1:], # pylint: disable=W0102 config=None): """ Entrypoint for the cloud-clean CLI interface :param args: Command line arguments passed from user :param config: The config object to be used :return: None """ # Construct or configure cloud cleaner config if config is None: config = CloudCleanerConfig(args=args) else: config.set_args(args) # Register all the resource types and options with the configurator for resource in ALL_RESOURCES.values(): resource.register(config) config.parse_args() # Call the process method for the target resource type print("Options parsed, fetching resources") if config.get_arg("force"): ALL_RESOURCES[config.get_resource()].prep_deletion() ALL_RESOURCES[config.get_resource()].process() print("Resources fetched, cleaning") ALL_RESOURCES[config.get_resource()].clean() else: print("No changes made, force option not enabled") ALL_RESOURCES[config.get_resource()].process() if config.get_arg("email"): ALL_RESOURCES[config.get_resource()].send_emails()
def test_subcommand_incorrect(self): parser = ArgumentParser() config = CloudCleanerConfig(parser=parser, args=["notfound"]) config.set_args(["notfound"]) config.add_subparser("found") # ArgumentError raises SystemExit internally, apparently with self.assertRaises(SystemExit): config.parse_args()
def test_verbose_one(self): args = ["--os-auth-url", "http://no.com", "-v", "server"] config = CloudCleanerConfig(args=args) ALL_RESOURCES["server"].register(config) config.parse_args() log = getLogger("cloud_cleaner") self.assertEqual(log.getEffectiveLevel(), INFO) shade = config.get_shade() self.assertIsInstance(shade, OpenStackCloud)
def test_parser_with_name(self): parser = ArgumentParser() config = CloudCleanerConfig(parser=parser, args=["--os-auth-url", "http://no.com", "server", "--name", "test-.*"]) server = Server() server.register(config) config.parse_args() self.assertEqual("test-.*", config.get_arg("name")) self.assertEqual(Server.type_name, config.get_resource())
def test_set_args(self): parser = ArgumentParser() config = CloudCleanerConfig(parser=parser, args=[]) config.add_subparser("item") config.set_args(["--os-auth-url", "http://no.com", "item"]) config.parse_args() self.assertEqual("item", config.get_resource()) config.warning("Dummy warning") log = getLogger("cloud_cleaner") self.assertEqual(log.getEffectiveLevel(), WARNING) self.assertIsNone(config.get_arg("no_arg"))
def test_resource_handled_from_args(self): parser = ArgumentParser() config = CloudCleanerConfig( parser=parser, args=["--os-auth-url", "httpp://no.com", Fip.type_name]) fip = Fip() # If config hasn't yet been registered, then there will be an error # from within this method, as intended with self.assertRaises(AttributeError): fip.process() fip.register(config) config.parse_args() self.assertEqual(Fip.type_name, config.get_resource())
def __test_with_calls(self, args, calls): conn = Mock() conn.list_floating_ips = Mock(return_value=FLOATING_IPS) conn.delete_floating_ip = Mock() calls = [call(i) for i in calls] config = CloudCleanerConfig(args=args) config.get_conn = Mock(return_value=conn) fip = Fip() fip.register(config) config.parse_args() fip.process() fip.clean() self.assertEqual(conn.delete_floating_ip.call_args_list, calls)
def __test_with_call_order(self, args, calls): shade = Mock() shade.list_servers = Mock(return_value=SAMPLE_SERVERS) shade.delete_server = Mock() config = CloudCleanerConfig(args=args) config.get_shade = Mock(return_value=shade) calls = [call(c) for c in calls] server = Server(now=CURRENT_TIME) server.register(config) config.parse_args() server.process() server.clean() self.assertEqual(shade.delete_server.call_args_list, calls)
def test_email_with_calls(self): conn = Mock() conn.list_servers = Mock(return_value=SAMPLE_SERVERS) conn.get_user_by_id = Mock(return_value=SAMPLE_USER) conn.delete_server = Mock() config = CloudCleanerConfig(args=[ "--os-auth-url", "http://no.com", "server", "--age", "4d", "--skip-name", "test-.*" ]) config.get_conn = Mock(return_value=conn) calls = ['4', '5'] calls = [call(c) for c in calls] server = Server(now=CURRENT_TIME) server.send_emails() = Mock() server.register(config) config.parse_args() server.process() server.send_emails() self.assertEqual(conn.get_user_by_id.call_count, 2) server.clean() self.assertEqual(conn.delete_server.call_args_list, calls)
def test_email_with_delete(self): conn = Mock() conn.list_servers = Mock(return_value=SAMPLE_SERVERS) conn.get_user_by_id = Mock(return_value=SAMPLE_USER) conn.delete_server = Mock() config = CloudCleanerConfig(args=[ "--os-auth-url", "http://no.com", "server", "--age", "4d", "--skip-name", "test-.*" ]) config.get_conn = Mock(return_value=conn) server = Server(now=CURRENT_TIME) server.send_emails() = Mock() server.register(config) config.parse_args() server.prep_deletion() server.process() server.clean() server.process() server.send_emails() # Only server 4 should be deleted. As in the test above, servers 4 and # 5 fit the criteria, but this time around server 5 should be deleted, # and thus not considered for an email warning, whereas for server 4 # nothing should change. self.assertEqual(conn.get_user_by_id.call_count, 1)