Skip to content
This repository has been archived by the owner on Sep 17, 2019. It is now read-only.

Dumb driver #269

Merged
merged 7 commits into from Jul 3, 2017
Merged

Dumb driver #269

merged 7 commits into from Jul 3, 2017

Conversation

dbarrosop
Copy link
Member

This implements a generic mock driver that anybody can use to mock workflows/tools without having to mock a real device. The way it works is as follows:

  1. You create a device using the MockDriver and passing a path and a profile.
  2. The path will be used to find mocked data so if you do d.get_facts() it will try to find $PATH/get_facts.1 and return the json inside. If you do d.get_facts() again it will try to find $PATH/get_facts.2.

You can check the test to see how it behaves.

Immediate uses for this would be to properly test napalm-ansible modules, napalm-yang and I bet napalm-salt and others could benefit of this.

@dbarrosop
Copy link
Member Author

@dbarrosop
Copy link
Member Author

More example. Here you can see how to test ansible workflows:

napalm-automation/napalm-ansible#66

Workflow is flexible enough even to simulate failures.

@ktbyers
Copy link
Contributor

ktbyers commented Jul 2, 2017

It might be nice to also support a named way to do this (if there is some logical way to do it).

For example, in addition to:

get_facts.1
get_facts.2

also support:

get_facts.strange_lldp_output_ios

if you set some variable like testname. Of course, we would need to figure a way to map this to other tools like ansible and Salt.

I am a bit worried as tests get more complex just sequencing indexes might be hard to keep track of (of course we can add this later).




class MockDevice:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to inherit from (object) or would use old-style classes in PY2 (which we don't want to do). So should be

class MockDevice(object):

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I always get them wrong...


self.opened = False
self.calls = {}
self.device = MockDevice(self, self.profile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it raise an exception here if profile is not set (i.e. a blank list)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, I think it's fine to avoid checking here. I might want to test that napalm-yang returns a meaningful error or that behaves in a certain way if the profile is invalid.

}


class TestMockDriver():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class needs to inherit from object. I just checked pytest and it looks like they just (8 days ago merged PR to deprecate old-style classes).

https://github.com/pytest-dev/pytest/pull/2525/files

I think safer for us is to consistently use new-style classes i.e. inherit from object in the tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, completely agree. I always get them wrong though :(

Would be great if __future__ had something like print_functions to force the new style...

@dbarrosop
Copy link
Member Author

dbarrosop commented Jul 3, 2017

if you set some variable like testname

So that feature is not really for that use case. That feature is for supporting multiple calls to the same method returning different results. For example, let's say you are writing a monitoring tool and need to call get_interfaces multiple times to test you are updating your db properly or that your configuration management system changes the hostname properly and reports it to some central tool.

What you are suggesting would be accomplished instead by having different mocked devices. Something like:

mocked/
    ios_12/
        get_lldp_neighbors.1
    ios_12_no_neighbors/
        get_lldp_neighbors.1
    ios_12_weird_output/
        get_lldp_neighbors.1

For example:

https://github.com/napalm-automation/napalm-ansible/pull/66/files#diff-51bd545870274958d2bca976325f59ceR2

@dbarrosop dbarrosop merged commit dbddbbb into develop Jul 3, 2017
@dbarrosop dbarrosop deleted the dumb_driver branch July 3, 2017 11:39
dbarrosop added a commit that referenced this pull request Jul 3, 2017
* Fix mac address format

* Documentation: Fix example mac addresses to be proper form

* Correctly check all keys comply (#266)

* Improve coverage of get_network_driver() (#263)

* Improve coverage of get_network_driver

* Revert import of napalm_logs for tests

* Driver prepending optional

* Test more branches in get_network_driver

* Added AttributeError Exception to __del__ function in NetworkDriver (#268)

* Added AttributeError Exception to __del__ function in NetworkDriver class

* Added AttributeError Exception to __del__ function in NetworkDriver

* Dumb driver (#269)

* Context manager propagates exceptions properly

* Implemented generic mock driver

* Add possibility to raise Exceptions on demand

* Mock cli commands individually

* Map junos' _rpc to cli

* Mocking configuration management methods

* Added tests for configuration methods

* Bump version to 0.24.2 (#270)
bewing added a commit to bewing/napalm-base that referenced this pull request Jul 18, 2017
…to tox

* 'develop' of github.com:napalm-automation/napalm-base:
  Bump version to 0.24.3 (napalm-automation#275)
  Validate string before evaluating (napalm-automation#274)
  Bump version to 0.24.2 (napalm-automation#270)
  Dumb driver (napalm-automation#269)
  Added AttributeError Exception to __del__ function in NetworkDriver (napalm-automation#268)
  Improve coverage of get_network_driver() (napalm-automation#263)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants