I think I have started to get my head around unit tests for CRASSH; recently my initial implementation of paramiko's “ssh_session.recv_ready" was ballsed up, python3 was forgiving python2 would bork, creating an issue for many. My first unit test was a basic "print" which I ran on Travis-CI against both python2 and python3 if the build passed then syntax checking for both versions would pass, nice!
Unit tests that check the internal functions of CRASSH are all well and good, but the purpose of the script is to log into routers and do stuff, so I really wanted to spin up a router and check the script against that.
Travis-CI's infrastructure comes in two flavors, Standard and Container Based, which essential boils down to one that allows
sudo and one that doesn't*; using the standard environment I have managed to spin up a dynamips router for testing CRASSH against.
If you take a look at my .travis.yml, I'll step you through how this works:
By default all builds are Container Based, this forces the Standard build.
python: - '2.6' - '3.5'
Currently tests all run on linux, I plan to add OSX tests in the future but here we instruct Travis-CI to perform the tests twice, once against each version.
before_install: - ifconfig -a - sudo /sbin/modprobe tun - sudo apt-get update -qq - sudo apt-get install -qq dynamips
The first thing I do is prepare the OS, the
ifconfig -a is for troublehooting so I can see the network stack before I start; then I install the Tun/Tap kernel driver, update the OS and install dynamips.
install: - pip install . - pip install paramiko
Now all the dependencies are installed, the router needs to be loaded...
before_script: - wget -q $IOS_URL - ls -l - sudo dynamips -s 0:0:tap:tap0 -P 3725 -T 2001 --idle-pc=0x607081e0 -C tests/r1.txt c3725-advsecurityk9-mz.124-25c.bin & - sleep 90 - sudo ifconfig tap0 inet 184.108.40.206 netmask 255.255.255.252 - ifconfig -a - ping -c 4 220.127.116.11
$IOS_URL is a secure variable so that charlatan's cannot download the Cisco IOS; so wget downloads the IOS from my secret location and dynamips starts up (and backrounds, note:
Once dynamips is running we wait 90secs to give time for the router to boot, we configure an interface to connect to the router with
sudo ifconfig tap0 and I
ping it to make sure it works.
Now that the router is running, standard python unit tests start with
script: py.test --cisco
The magic of
test_cisco_shver is that dynamips was booted with the config in r1.txt the test logs into the r1 router and compares a
show ver to an expected output if they match (line for line) then the test passes and Travis-CI shuts everything down; if the test fails I get notified.
Status of builds can be reviewed on the CRASSH Travis Page: https://travis-ci.org/linickx/crassh or is shown in the readme.