Ejemplo n.º 1
0
 def test_handler_command_name(self):
     # handler_command_name attempts to discriminate parts of a vaguely
     # camel-cased string, removes any "handler" parts, joins again with
     # hyphens, and returns the whole lot in lower case.
     expected = {
         "NodeHandler": "node",
         "SpadeDiggingHandler": "spade-digging",
         "SPADE_Digging_Handler": "spade-digging",
         "SpadeHandlerForDigging": "spade-for-digging",
         "JamesBond007": "james-bond007",
         "JamesBOND": "james-bond",
         "James-BOND-007": "james-bond-007",
         }
     observed = {
         name_in: utils.handler_command_name(name_in)
         for name_in in expected
         }
     self.assertItemsEqual(
         expected.items(), observed.items())
     # handler_command_name also ensures that all names are encoded into
     # byte strings.
     expected_types = {
         name_out: bytes
         for name_out in observed.values()
         }
     observed_types = {
         name_out: type(name_out)
         for name_out in observed.values()
         }
     self.assertItemsEqual(
         expected_types.items(), observed_types.items())
Ejemplo n.º 2
0
    def test_complains_about_too_few_arguments(self):
        configs = self.fake_profile()
        [profile_name] = configs
        resources = configs[profile_name]["description"]["resources"]
        resource_name = random.choice(resources)["name"]
        command = "maas", profile_name, handler_command_name(resource_name)

        with CaptureStandardIO() as stdio:
            error = self.assertRaises(SystemExit, main, command)

        self.assertThat(error.code, Equals(2))
        self.assertThat(
            stdio.getError(),
            DocTestMatches(
                dedent(
                    """\
                usage: maas [-h] COMMAND ...
                <BLANKLINE>
                ...
                <BLANKLINE>
                too few arguments
                """
                )
            ),
        )
Ejemplo n.º 3
0
 def test_handler_command_name_non_ASCII(self):
     # handler_command_name will not break if passed a string with
     # non-ASCII characters. However, those characters will not be present
     # in the returned name.
     self.assertEqual(
         "a-b-c", utils.handler_command_name("a\u1234_b\u5432_c\u9876")
     )
Ejemplo n.º 4
0
def register_handler(profile, handler, parser):
    """Register a resource's handler."""
    help_title, help_body = parse_docstring(handler["doc"])
    handler_name = handler_command_name(handler["name"])
    handler_parser = parser.subparsers.add_parser(
        handler_name, help=help_title, description=help_title,
        epilog=help_body)
    register_actions(profile, handler, handler_parser)
Ejemplo n.º 5
0
Archivo: api.py Proyecto: ocni-dtu/maas
def register_handler(profile, handler, parser):
    """Register a resource's handler."""
    help_title, help_body = parse_docstring(handler["doc"])
    handler_name = handler_command_name(handler["name"])
    handler_parser = parser.subparsers.add_parser(
        handler_name, help=help_title, description=help_title, epilog=help_body
    )
    register_actions(profile, handler, handler_parser)
Ejemplo n.º 6
0
def get_action_class(handler, action):
    """Return custom action handler class."""
    handler_name = handler_command_name(handler["name"]).replace('-', '_')
    action_name = '%s_%s' % (handler_name, safe_name(action["name"]).replace(
        '-', '_'))
    action_module = try_import_module('maascli.actions.%s' % action_name)
    if action_module is not None:
        return action_module.action_class
    return None
Ejemplo n.º 7
0
    def test_complains_about_too_few_arguments(self):
        configs = self.fake_profile()
        [profile_name] = configs
        resources = configs[profile_name]["description"]["resources"]
        resource_name = random.choice(resources)["name"]
        command = "maas", profile_name, handler_command_name(resource_name)

        with CaptureStandardIO() as stdio:
            error = self.assertRaises(SystemExit, main, command)

        self.assertEqual(error.code, 2)
        error = stdio.getError()
        self.assertIn("usage: maas [-h] COMMAND ...", error)
        self.assertIn("too few arguments", error)
Ejemplo n.º 8
0
 def test_handlers_registered_using_correct_names(self):
     profile = self.make_profile()
     parser = ArgumentParser()
     api.register_api_commands(parser)
     for resource in list(profile.values())[0]["description"]["resources"]:
         for action in resource["auth"]["actions"]:
             # Profile names are matched as-is.
             [profile_name] = profile
             # Handler names are processed with handler_command_name before
             # being added to the argument parser tree.
             handler_name = handler_command_name(resource["name"])
             # Action names are processed with safe_name before being added
             # to the argument parser tree.
             action_name = safe_name(action["name"])
             # Parsing these names as command-line arguments yields an
             # options object. Its execute attribute is an instance of
             # Action (or a subclass thereof).
             options = parser.parse_args(
                 (profile_name, handler_name, action_name))
             self.assertIsInstance(options.execute, api.Action)
Ejemplo n.º 9
0
 def test_handlers_registered_using_correct_names(self):
     profile = self.make_profile()
     parser = ArgumentParser()
     api.register_api_commands(parser)
     for resource in profile.values()[0]["description"]["resources"]:
         for action in resource["auth"]["actions"]:
             # Profile names are matched as-is.
             profile_name = profile.keys()[0]
             # Handler names are processed with handler_command_name before
             # being added to the argument parser tree.
             handler_name = handler_command_name(resource["name"])
             # Action names are processed with safe_name before being added
             # to the argument parser tree.
             action_name = safe_name(action["name"])
             # Parsing these names as command-line arguments yields an
             # options object. Its execute attribute is an instance of
             # Action (or a subclass thereof).
             options = parser.parse_args(
                 (profile_name, handler_name, action_name))
             self.assertIsInstance(options.execute, api.Action)
Ejemplo n.º 10
0
 def test_handler_command_name_non_ASCII(self):
     # handler_command_name will not break if passed a string with
     # non-ASCII characters. However, those characters will not be present
     # in the returned name.
     self.assertEqual(
         "a-b-c", utils.handler_command_name(u"a\u1234_b\u5432_c\u9876"))