Ejemplo n.º 1
0
    def test_command_loading(self, _get_user_command_path):
        _get_user_command_path.return_value = self.user_command_path

        available_commands = Command.get_available_commands()

        # Check that we have the command list that we expect
        self.assertEquals(
            sorted(available_commands),
            sorted(
                [b.replace('-', '_') for b in self.expected_builtins] +
                ["user_command_1"]
            ),
        )

        # Check that we can load (i.e. import) every builtin command
        for expected_builtin in self.expected_builtins:
            self.assertIn(expected_builtin.replace("-", "_"), available_commands)
            cmd_cls = Command.load_command_class(expected_builtin)
            self.assertIsNotNone(cmd_cls)
            self.assertEquals(cmd_cls.get_name(), expected_builtin)

        # Check that we can load the user command
        user_cmd_cls = Command.load_command_class("user-command-1")
        self.assertIsNotNone(user_cmd_cls)
        self.assertEquals(user_cmd_cls.get_name(), "user-command-1")

        # Check that load_command_class() returns None for junk commands
        unexpected_cmd = Command.load_command_class("no-such-command")
        self.assertIsNone(unexpected_cmd)
Ejemplo n.º 2
0
    def test_command_loading(self, _get_user_command_path):
        _get_user_command_path.return_value = self.user_command_path

        available_commands = Command.get_available_commands()

        # Check that we have the command list that we expect
        self.assertEquals(
            sorted(available_commands),
            sorted([b.replace('-', '_')
                    for b in self.expected_builtins] + ["user_command_1"]),
        )

        # Check that we can load (i.e. import) every builtin command
        for expected_builtin in self.expected_builtins:
            self.assertIn(expected_builtin.replace("-", "_"),
                          available_commands)
            cmd_cls = Command.load_command_class(expected_builtin)
            self.assertIsNotNone(cmd_cls)
            self.assertEquals(cmd_cls.get_name(), expected_builtin)

        # Check that we can load the user command
        user_cmd_cls = Command.load_command_class("user-command-1")
        self.assertIsNotNone(user_cmd_cls)
        self.assertEquals(user_cmd_cls.get_name(), "user-command-1")

        # Check that load_command_class() returns None for junk commands
        unexpected_cmd = Command.load_command_class("no-such-command")
        self.assertIsNone(unexpected_cmd)
def compare_type(cmd_name, api_model, expected_differences):
    print(cmd_name)
    cmd = Command.load_command_class("measure")(["measure", cmd_name]).create()
    cmd.add_arguments()

    args = {}

    for arg in cmd.parser._actions:
        args[arg.dest] = arg

    seen_diffs = False

    for field_name, model_field in sorted(api_model["properties"].items()):
        if field_name in expected_differences and expected_differences[
                field_name] is None:
            continue
        if model_field["readOnly"]:
            continue

        explicit_values = expected_differences.get(field_name, {})

        opt_name = explicit_values.get("alias", field_name)

        if opt_name in args:
            cmd_field = args.get(opt_name)

            expected_default = explicit_values.get(
                "default", model_field.get("defaultValue"))
            if cmd_field.default != expected_default:
                print("\t", field_name, "DEFAULT", repr(cmd_field.default),
                      repr(expected_default))
                seen_diffs |= True

            expected_min = explicit_values.get("minimum",
                                               model_field.get("minimum"))
            cmd_min = getattr(cmd_field.type, "minimum", None)
            if cmd_min != expected_min:
                print("\t", field_name, "MINIMUM", cmd_min, expected_min)
                seen_diffs |= True

            expected_max = explicit_values.get("maximum",
                                               model_field.get("maximum"))
            cmd_max = getattr(cmd_field.type, "maximum", None)
            if cmd_max != expected_max:
                print("\t", field_name, "MAXIMUM", cmd_max, expected_max)
                seen_diffs |= True
        else:
            print("\t", field_name, "\t", "MISSING")
            seen_diffs |= True

    if not seen_diffs:
        print("\t", "OK")
Ejemplo n.º 4
0
 def _generate_usage(self):
     usage = "Usage: ripe-atlas <command> [arguments]\n\n"
     usage += "Commands:\n"
     longest_command = 0
     classes = []
     for c in Command.get_available_commands():
         if c == "shibboleet":
             continue
         cmd_class = Command.load_command_class(c)
         classes.append(cmd_class)
         cmd_name = cmd_class.get_name()
         if len(cmd_name) > longest_command:
             longest_command = len(cmd_name)
     for cmd_cls in classes:
         usage += "\t{} {}\n".format(
             cmd_cls.get_name().ljust(longest_command + 1),
             cmd_cls.DESCRIPTION,
         )
     usage += ("\nFor help on a particular command, try "
               "ripe-atlas <command> --help")
     return usage
Ejemplo n.º 5
0
    def fetch_command_class(self, command, arg_options):
        """Fetches the class responsible for the given command."""

        cmd_cls = Command.load_command_class(command)

        if cmd_cls is None:
            # Module containing the command class wasn't found
            raise RipeAtlasToolsException("No such command")

        #
        # If the imported module contains a `Factory` class, execute that
        # to get the `cmd` we're going to use.  Otherwise, we expect there
        # to be a `Command` class in there.
        #

        if issubclass(cmd_cls, Factory):
            cmd = cmd_cls(arg_options).create()
        else:
            cmd = cmd_cls(*self.args, **self.kwargs)

        return cmd
Ejemplo n.º 6
0
    def test_deprecated_aliases(self):
        aliases = [
            ("measurement", "measurement-info"),
            ("measurements", "measurement-search"),
            ("probe", "probe-info"),
            ("probes", "probe-search"),
        ]

        # Check that each alias is loaded correctly and outputs a warning
        stderr = sys.stderr
        sys.stderr = StringIO()
        try:
            for alias, cmd_name in aliases:
                sys.stderr.truncate()
                cmd_cls = Command.load_command_class(alias)
                self.assertIn(
                    "{} is a deprecated alias for {}".format(alias, cmd_name),
                    sys.stderr.getvalue(),
                )
                self.assertIsNotNone(cmd_cls)
                self.assertEquals(cmd_cls.get_name(), cmd_name)
        finally:
            sys.stderr = stderr
Ejemplo n.º 7
0
    def test_deprecated_aliases(self):
        aliases = [
            ("measurement", "measurement-info"),
            ("measurements", "measurement-search"),
            ("probe", "probe-info"),
            ("probes", "probe-search"),
        ]

        # Check that each alias is loaded correctly and outputs a warning
        stderr = sys.stderr
        sys.stderr = StringIO()
        try:
            for alias, cmd_name in aliases:
                sys.stderr.truncate()
                cmd_cls = Command.load_command_class(alias)
                self.assertIn(
                    "{} is a deprecated alias for {}".format(alias, cmd_name),
                    sys.stderr.getvalue(),
                )
                self.assertIsNotNone(cmd_cls)
                self.assertEquals(cmd_cls.get_name(), cmd_name)
        finally:
            sys.stderr = stderr