![]() ![]() The easiest way to do this would be to have the test runner step return a non 0 exit code, but this causes us a problem. So if we fail the test, we want things to get cancelled. ![]() If any of our tests have failed, we want to cancel the build there is no point going through with the image creation if the resulting images are failing our tests as it will just waste time and resources. We save this file as 1 in a test folder within our project. In this simple example, we have a single context and describe section, and then 3 tests to check for 3 different Windows features. You can make your tests as complex as you like, so long as you can find a way to check the thing you are interested in. In this example, we are going to keep it simple, and we want to check that some Windows Features are enabled. The first thing we need to do is write the tests that we are going to run, so we need to determine what it is on the VM that you want to validate. Using Packer to do this is a relatively simple process, as the Pester tests we want to run are just PowerShell scripts as far as Packer is concerned and can be executed with the PowerShell provisioner. That way if the tests fail, we abort before creating an image, and we can also utilise Packers already existing connection to run the tests. We will also have to do all the work to get Azure DevOps to be able to talk to the VM and run the tests.Ī better approach is to run the tests as part of the Packer process as an additional step after the VM is configured, but before we Sysprep and take an image. While this will work, it is going to be a relatively slow process, given that we need to wait for a VM to be created. Initially, I had considered waiting until after Packer did it’s work and created an image, and then create a VM from that image to run tests against. There are two points we can run our test. ![]() I’m not going to cover using Pester in this article, but if you want to get started with it I recommend reading this introduction to Pester. If you have basic knowledge of PowerShell, you will be able to write Pester test. If you’re not familiar with Pester, it is a testing framework for PowerShell. We are going to use Pester to run the tests against this VM. This example is going to continue with the approach of using Azure DevOps, but you can use any other build tool, you’ll need to tailor the process to that. ![]() To be able to follow the rest of this article, you will need a working build pipeline that creates an Image using Packer. I’m not going to cover creating an image build with Packer here, as I covered that extensively in this article. To combat this, I want to add tests into our build process, so that we can check that the VM is configured how we think it should be.Īll of the code required to run this process can be found on GitHub. This is a somewhat naïve approach, as it is entirely possible that either things fail to install but don’t generate an error for DSC to catch, or we forget to configure things or misconfigure them. In the pipeline we created in the previous article, we are assuming that if the build ran through without errors, then our image is complete, and everything we needed installing is installed. In this article, we are going to expand on this and look at testing these images. This process works great, and I am still using it today. We created a pipeline that used Packer and PowerShell DSC to provision a VM in Azure, install the required software and configuration and then take an image of the VM that we could then use as a base for creating new VM’s or VM Scale Sets. In an article earlier this year, we looked at using Azure DevOps to build Azure VM images using Packer. ![]()
0 Comments
Leave a Reply. |