Conversation
You can see an example here: |
More example. Here you can see how to test ansible workflows: napalm-automation/napalm-ansible#66 Workflow is flexible enough even to simulate failures. |
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 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). |
napalm_base/mock.py
Outdated
|
||
|
||
|
||
class MockDevice: |
There was a problem hiding this comment.
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):
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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.
test/unit/TestMockDriver.py
Outdated
} | ||
|
||
|
||
class TestMockDriver(): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...
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 What you are suggesting would be accomplished instead by having different mocked devices. Something like:
For example: |
* 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)
…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)
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:
MockDriver
and passing apath
and aprofile
.d.get_facts()
it will try to find$PATH/get_facts.1
and return the json inside. If you dod.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 betnapalm-salt
and others could benefit of this.