Skip main navigation

CI/CD Pipelines for Deploying and Configuring Environments

.

Throughout this course, we’ve talked a lot about PowerShell. In this step, we will demonstrate how to set up a CI pipeline in Visual Studio and we’ll test your PowerShell Scripts.

The goal of Infrastructure as Code (IaC) is to generate a build process that creates consistent infrastructure and deploys the application. Changes are committed, and the build process spins up a new server and deploys the application. This means that testing is always performed on a clean machine with a known configuration.

It’s possible with source control to create several builds such as development, test, and production, and to choose which one to target.

Automated testing is critical to any DevOps build process. A set of tests should be run each time your build process is changed. If you’re using Windows PowerShell for your code, you can use the Pester module (Windows 10 and Windows Server 2016, or download it from the PowerShell gallery).

Pester is a test framework for PowerShell. It provides a language that allows you to define test cases, and the Invoke-Pester cmdlet to execute these tests and report the results.

With Export-ODataEndpointProxy, New-WebServiceProxy, Invoke-WebRequest and Invoke-RestMethod, you can interact with web APIs. Because Pester tests are simply PowerShell code, this means that you can use Pester to test all of these things even if they’re not actually written in PowerShell.

The following is a simple example of creating tests with Pester:


A function for dividing two numbers, returns the result of the operation.

function division {

param (

[int]$a,

[int]$b

)

return $a / $b

}



Add # and say unit tests for the method division below…

Describe -Tags ‘BasicTests’ ‘divisiontest’{

It ‘divides positive numbers’{

divisiontest 6 3 | Should Be 2



}



It ‘divides negative numbers’ {

divisiontest -6 -3 | Should Be 2

}



It ‘throws error for divide by zero’ {

{divisiontest 6 0} | Should Throw

}

}

The function divides the first parameter by the second. The tests are wrapped in the Describe block and, in this case, test for the correct division of positive and negative numbers. The third test is to determine if the function correctly throws an error when attempting to divide by zero. The results look like this:

Describing divisiontest

[+] divides positive numbers 70ms

[+] divides negative numbers 25ms

[+] throws error for divide by zero 45ms

When you test IaC routines, check the installed Windows features and roles, the network configuration (IP addresses and subnet masks), or the existence of particular folders (use Test-Path).

Be sure to use the same code to create the infrastructure for development, test, and production environments to ensure that you have consistency across the environments.

This prevents any assumptions made in development or testing that might cause problems in production. You now have a reliable and tested configuration to ensure smooth rollout to production.

Join the discussion

Continuous integration and continuous delivery (CI/CD) are often cited as pillars of successful DevOps. Do you agree with this statement and why? Can you think of any other key elements of successful DevOps? Share your thoughts with the group.

Use the Discussion section below and let us know your thoughts. Try to respond to at least one other post and once you’re happy with your contribution, click the Mark as complete button to move on to the next step.

This article is from the free online

Microsoft Future Ready: DevOps Development, Implementation and Azure Automation

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now