aztfmod / rovergo

The next version of Rover, the command line tool for Azure CAF Landingzones. Developed in Go
MIT License
10 stars 7 forks source link

Add unit tests #53

Closed benc-uk closed 3 years ago

benc-uk commented 3 years ago

Unit tests are the wild new craze taking the software development world by storm.

We should get on this new hotness and some of these so called "unit tests" to rover v2

To be agreed:

sebastus commented 3 years ago

Here's a conference video on the topic of "unit testing". He says that with infra code, "unit testing" is really not. Because 99% of what infra code does is work with the outside world (Azure), if you try to isolate that, there's nothing to do. Then he goes on to show how Terratest works - with a single module. Later he shows how to test multiple modules simultaneously - where all modules are needed to create the overall solution so you can then validate the installation somehow. (think network + vm)

Later in the video he talks about testing in stages. Assume that a test has 3 stages: deployment, validation, teardown. The first time you run the test, skip teardown. This leaves the infra out in the environment so you can poke at it. Next time you run the test, skip deployment. This means that each time you run the test, you only run the validate step. Validate typically takes seconds while deployment and teardown take (many) minutes. Since each run of the test is a different process, you have to store some state on disk. There's a method for this demonstrated as well.

sebastus commented 3 years ago

I added a single unit test as a pattern for more. It creates a cobra command, adds the needed command line parameters, then does a BuildOptions. Assertions on the Options struct that gets created are the test. Tests can be extended easily to become integration tests by tweaking the last 4 lines of the test code - just reference the actual Action rather than the mock action.