Wednesday, August 17, 2022

Creating Chef Test Kitchen in Azure Using Azure Pipelines

When you are working with Chef, you can use chef’s integrated testing framework to test recipes to ensures recipes run as expected before applying recipes to production. This blog explains how to configure, chef test kitchen in an Azure VM using Azure DevOps pipeline.

Pre-requisites:

  • Chef extension is added to your Azure DevOps Organization from market place 
  • Basic knowledge on how to setup Azure DevOps build pipeline
  • Basic understanding of Chef and how to use kitchen.yml file to setup a test kitchen

Let’s get started.

Go to Pipelines of Azure DevOps project and create a new build pipeline by selecting classic editor and empty YAML task option.

We are going to use Azure DevOps hosted agents as a chef workstation. So, Add an Install Chef Components task and select Chef Workstation under Install Component drop down to install chef workstation inside the build agent.


Now we need to make sure Azure DevOps pipeline could communicate successfully with the Azure to provision a test kitchen in Azure. Add Chef Helpers task and select Setup Test Kitchen Environment option, from Helpers drop down in the task and select the relevant service connection to Azure subscription under Azure RM Endpoint drop down.




Next, add an Execute Chef Component task to pipeline and select Test-Kitchen from Chef Component drop down. Provide argument “create” to create a test kitchen. Go to Advanced section of the task and add Environment variable KITCHEN_YAML to location of the kitchen.yml file in Azure build agent.




As the above task creates a VM  in Azure, we are ready to run recipes in the Azure VM in the next task. Add another Execute Chef Component task and select Test-Kitchen option in Chef-Component drop down. Give “verify” as an Argument. This will run the recipes defined in the kitchen.yml against the Azure VM and run chef inspec scripts to verify configurations added successfully based on your chef recipes.

Once kitchen verify step is completed, add Chef Execute Component task to destroy the test kitchen (Azure VM) provisioned in the Azure by giving destroy command, to clean up the Azure resources created for test purpose.



If you happy with the test run, you can publish cookbook as package in pipeline or to Azure Artifacts.

No comments:

Post a Comment