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 test_init_with_name(self):  # pylint: disable=no-self-use
     parser = ArgumentParser()
     config = CloudCleanerConfig(parser=parser, args=[])
     config.add_subparser = Mock()
     server = Server()
     server.register(config)
     config.add_subparser.assert_called_once_with(Server.type_name)
Beispiel #3
0
 def test_resource_type(self):  # pylint: disable=no-self-use
     parser = ArgumentParser()
     config = CloudCleanerConfig(
         parser=parser, args=["--os-auth-url", "http://no.com", "fip"])
     config.add_subparser = Mock()
     fip = Fip()
     fip.register(config)
     config.add_subparser.assert_called_once_with(Fip.type_name)
 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)
Beispiel #5
0
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")
    ALL_RESOURCES[config.get_resource()].process()
    if config.get_arg("force"):
        print("Resources fetched, cleaning")
        ALL_RESOURCES[config.get_resource()].clean()
    else:
        print("No changes made, force option not enabled")
 def test_cloud_cleaner_server(self):
     config = CloudCleanerConfig(args=[])
     ALL_RESOURCES["server"].process = Mock()
     ALL_RESOURCES["server"].clean = Mock()
     cloud_clean(args=[
         "--os-auth-url", "http://no.com", "server", "--name", "derp"
     ],
                 config=config)
     self.assertEqual("server", config.get_resource())
     self.assertEqual("derp", config.get_arg("name"))
     self.assertEqual(1, len(ALL_RESOURCES["server"].process.mock_calls))
     self.assertEqual(0, len(ALL_RESOURCES["server"].clean.mock_calls))
 def test_cloud_cleaner_noopts(self):
     parser = ArgumentParser()
     config_module.DEFAULT_ARGUMENTS = []
     config = CloudCleanerConfig(parser=parser)
     if version_info.major == 3:
         # Raised because no resource
         with self.assertRaises(MissingRequiredOptions):
             cloud_clean([], config)
     else:
         with self.assertRaises(SystemExit):
             cloud_clean([], config)
     self.assertIsNone(config.get_resource())
Beispiel #8
0
 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())
Beispiel #9
0
 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)
Beispiel #10
0
 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)
Beispiel #11
0
 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_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)
Beispiel #14
0
 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())
Beispiel #15
0
 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"))
Beispiel #16
0
 def test_get_cloud_is_none(self):
     parser = ArgumentParser()
     config = CloudCleanerConfig(parser=parser, args=[])
     self.assertIsNone(config.get_cloud())