Exemple #1
0
    def test_method_signatures(self):
        """Test that all methods have the same signature."""
        errors = {}
        cls = self.driver
        # Create fictional driver instance (py3 needs bound methods)
        tmp_obj = cls(hostname='test', username='******', password='******')
        attrs = [m for m, v in inspect.getmembers(tmp_obj)]
        for attr in attrs:
            func = getattr(tmp_obj, attr)
            if attr.startswith('_') or not inspect.ismethod(func):
                continue
            try:
                orig = getattr(NetworkDriver, attr)
                orig_spec = argspec(orig)
            except AttributeError:
                orig_spec = 'Method does not exist in napalm.base'
            func_spec = argspec(func)
            if orig_spec != func_spec:
                errors[attr] = (orig_spec, func_spec)

        EXTRA_METHODS = [
            '__init__',
        ]
        for method in EXTRA_METHODS:
            orig_spec = argspec(getattr(NetworkDriver, method))
            func_spec = argspec(getattr(cls, method))
            if orig_spec != func_spec:
                errors[attr] = (orig_spec, func_spec)

        assert not errors, "Some methods vary. \n{}".format(errors.keys())
Exemple #2
0
def netmiko_args(optional_args):
    """Check for Netmiko arguments that were passed in as NAPALM optional arguments.

    Return a dictionary of these optional args  that will be passed into the Netmiko
    ConnectHandler call.
    """
    fields = py23_compat.argspec(BaseConnection.__init__)
    args = fields[0]
    defaults = fields[3]

    check_self = args.pop(0)
    if check_self != 'self':
        raise ValueError("Error processing Netmiko arguments")

    netmiko_argument_map = dict(zip(args, defaults))

    # Netmiko arguments that are integrated into NAPALM already
    netmiko_filter = ['ip', 'host', 'username', 'password', 'device_type', 'timeout']

    # Filter out all of the arguments that are integrated into NAPALM
    for k in netmiko_filter:
        netmiko_argument_map.pop(k)

    # Check if any of these arguments were passed in as NAPALM optional_args
    netmiko_optional_args = {}
    for k, v in netmiko_argument_map.items():
        try:
            netmiko_optional_args[k] = optional_args[k]
        except KeyError:
            pass

    # Return these arguments for use with establishing Netmiko SSH connection
    return netmiko_optional_args
Exemple #3
0
def mocked_method(path, name, count):
    parent_method = getattr(NetworkDriver, name)
    parent_method_args = py23_compat.argspec(parent_method)
    modifier = 0 if 'self' not in parent_method_args.args else 1

    def _mocked_method(*args, **kwargs):
        # Check len(args)
        if len(args) + len(kwargs) + modifier > len(parent_method_args.args):
            raise TypeError(
                "{}: expected at most {} arguments, got {}".format(
                    name, len(parent_method_args.args), len(args) + modifier))

        # Check kwargs
        unexpected = [x for x in kwargs if x not in parent_method_args.args]
        if unexpected:
            raise TypeError("{} got an unexpected keyword argument '{}'".format(name,
                                                                                unexpected[0]))
        return mocked_data(path, name, count)

    return _mocked_method