Yesterday, when I ran chef generate cookbook test_software_cookbook it set up a bunch of scaffolding for me that includes an integration test at test_software_cookbook/test/integration/default/default_test.rb. The command resource allows me to run a specific command on the instance and validate a set of properties. I use the command resource to validate that Node.js is installed. The first thing I would need to get an Express app running is to make sure I have Node.js and npm installed on my node. For now, I'm starting with integration tests and using InSpec. For unit tests with Chef, I'd use chefspec. Is there a reason to write unit tests for infrastructure code? Sure! Especially when we are writing complex code that needs to exercise different paths. Testing whether your infrastructure application does the right thing means you are introducing brittleness into your testing suite which makes testing harder as well as more error prone. That kind of test belongs in a test suite for Chef or Puppet and not within the infrastructure code.ĭepending on the type of infrastructure unit you are writing, for example with Chef whether it's a cookbook that is a library cookbook versus a wrapper cookbook, you might want to unit test whether a specific file gets created. For example, writing a test that a package using default package resources gets installed in the right place. In infrastructure coding, unit tests are often written incorrectly and test the infrastructure application. One critical difference in infrastructure coding versus project code is unit tests are generally the first tests and largest number of tests to write as per the testing pyramid. Then I'll refactor the code I just wrote to make it better. Then I'll write the infrastructure code that will make the test pass. This test will fail because I haven't written any infrastructure code. First, I'll write the test that describes a basic feature I want to add. I'm going to follow the Red-Green-Refactor cycle in developing my infrastructure code. Check out all these great InSpec Azure Resources to validate different parts of your infrastructure!ĭescribing the Infrastructure Testing Process Note: It's possible to use InSpec without Test Kitchen, and validate the infrastructure that we've built with Azure Resource Management templates or Terraform as well. In this configuration, it's set to inspec, but we can also use Serverspec or Bats. The verifier is what we use to test the infrastructure configuration that we have defined. kitchen.yml file, we can see the configuration verifier. ~/.azure/credentials configured with your Service Principal configuration.For example, there is a nodejs community cookbook that has a lot more bells and whistles that might provide the necessary functionality for deploying Node.js/npm and managing npm packages. If you don't have ChefDK, Chef Workstation is a good starting point with the same software included.īefore I get too into the details, the goal of the article is to show InSpec and not the perfect way to write Chef code, especially for this application. Towards the end of 2018, Chef released the Chef Workstation as a replacement for the Chef Development Kit. Additionally, Test Kitchen can be configured to work with a variety of provisioners and drivers, so if you use Puppet or Ansible for example, you can totally use those instead.Īs mentioned in yesterday's post, I've got the Chef Development Kit(ChefDK) installed on my system which includes InSpec. Everything in this post depends on the work from Testing Infrastructure with Test Kitchen and Chef on Azure so if you are trying to replicate these steps make sure to check out that post first. In this post, I'm going to walk through how to test a sample Node.js project using InSpec, Test Kitchen, and the kitchen-azurerm driver. How do I write tests for the infrastructure that I'm building though? Once I've have a Test Kitchen configuration that works I can copy that configuration from project to project modifying what is needed. We've selected our favorite tips and tricks created by Michael Crump as well as planned fresh technical content on Azure all April! Miss a day (or more)? Catch up with the series.ĭon't have Azure? Grab a free subscription.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |