Skip to main content

Devops – Unit testing in Powershell

image

Powershell has really gained a strong momentum the last 3-4 years and is becoming increasingly the scripting language of choice if you live in the Windows part of the IT world. Together with the increasing popularity of the Devops phrase, unit testing is a key factor going forward.



One of the new phrase/term that is spoken often and warm about is Devops. It represents the evolvement from regular IT operation to IT operations with development. It is getting increasingly difficult to separate what is Operations and what is Development, they are combined into one entity. traditionally IT operations has very limited experience (that is zero) with the unit test concept. Testing is catching on and becoming one of the key elements that separates regulars from professionals in the Devops world. Everyone know they need to learn it and may have done it or are in the process of embracing it.

Powershell has really gained a strong momentum the last 3-4 years and is becoming increasingly the scripting language of choice if you live in the Windows part of the IT world. That is about to change as well since .Net has gone Open Source and Powershell DSC is aligning it self to embrace Linux in cooperation with Chef. Interesting as that is, it is a discussing for another post.

If you have done any tests in Powershell, you are probably familiar with either Pester or PSUnit or maybe you have rolled your own test framework. Currently I am only familiar with Pester and this series of posts will focus on unit testing with pester.



Scenario

To get us started I thought it would be a good idea to have a scenario that is kind of a slightly rewritten representation of one of my projects:

“You have developed a Powershell module that is build upon an API that has 2 cmdlet available for you. The system is called SillySystem:
  1. Import-Object (imports objects)
    1. Parameters
      • [HashTable]KeyValue
      • [pscredential]Credentials
      • [string]Uri
  2. Export-Object (Outputs objects)
    1. Parameters
      • [string]xPath
      • [pscredential]Credentials
      • [string]Uri
SillySystem has 2 types of objects and each objecttype has some properties/objects:
  1. Person
    • Name
    • DisplayName
    • ObjectID
  2. Group
    • Name
    • Description
    • ObjectID

Your/our module "SillyModule" have these exportable functions:
  • Get-Person
  • Update-Person
  • Get-Group
  • Update-Group
Being an automator you feel really good about the module, however you sense the need for an edge to present yourself as an Devops kind of person before you present the module. You decide to learn Unit Testing and write tests for you module.”



Tasks

In my past I have always coded first and maybe written Unit tests after the coding is done. Well okay, always like that. It it the low-level form of testing and it is not TDD (Test Driven Development) where you write tests first and then write code that make the tests pass. So the scenario fits my personal coding style.
  1. Read up on Unit Testing frameworks for powershell (Pester or PSUnit)
  2. Create the the SillySystem Import-Object and Export-Object API
  3. Walk through of the module and the functions (Get/Update-Person/Group)
  4. Create tests for the functions
  5. Refactor where needed ad update the tests
Cannot help you with step number 1, however I can share the code for the psudo SillySystem. Remember these functions is not required, however I chose to create them to demonstrate the Mock feature of unit testing (more on that in part 2). 

First up is Get-XMLdocument which is a helper function to load xml-data from the global scope (our database you might say). It does not take any paramteres, it just returns an XML-document to the to Import/Export functions:



(Get-XMLdocument)


The Export-Object function loads the xml from Get-XMLdocument and uses the xPath parameter to filter data:



(Export-object)


The Import-Object function update our object’s property values. It expects a mandatory parameter called KeyValue (a hashtable) that need these keys:
  • ObjectType (Person or Group)
  • ObjectID (a guid matching the ObjectID of the object we want to update)
  • ObjectName (the name of the property we want to update)
  • ObjectValue (the new value for the property we want to change)



(Import-Object)

That is it for part 1. In part 2 we will have a look at the wrapper functions and begin to create tests for the functions.

Cheers

Tore

Comments

Popular posts from this blog

Developing PowerShell modules for REST APIs – Part1

Over the years I have developed different PowerShell modules for different web APIs. I thought it would be a good idea to write a 2 series post about how you could go about to do this. This will be a 2 part blog series where we will run through the entire process of building a module for a REST API. I will try my best to keep this as simple as possible and leave more advanced stuff for a follow up post if the interest is there.What you needDepending on your experience with source control and PowerShell in general, you might want to use GIT or some other software repro for the code. In addition we are going to create a test REST API using the splendid UniversalDashboard PowerShell module created by Adam Driscoll. It is available on the PowershellGallery. Other prerequisites are built-in to Powershell. I will assume that you will be following along using at least PowerShell version 5 or greater.
What is HTTP metods for REST API.The primary or most common HTTP verbs used are POST, GET, PU…

Serialize data with PowerShell

Currently I am working on a big new module. In this module, I need to persist data to disk and reprocess them at some point even if the module/PowerShell session was closed. I needed to serialize objects and save them to disk. It needed to be very efficient to be able to support a high volume of objects. Hence I decided to turn this serializer into a module called HashData.



Other Serializing methods

In PowerShell we have several possibilities to serialize objects. There are two cmdlets you can use which are built in:
Export-CliXmlConvertTo-JSON
Both are excellent options if you do not care about the size of the file. In my case I needed something lean and mean in terms of the size on disk for the serialized object. Lets do some tests to compare the different types:


(Hashdata.Object.ps1)

You might be curious why I do not use the Export-CliXML cmdlet and just use the [System.Management.Automation.PSSerializer]::Serialize static method. The static method will generate the same xml, however we …

Developing PowerShell modules for REST APIs – Part2

This is part 2 of the REST API blogpost. In part1 we successfully setup two REST API endpoints using the UniversalDashboard PowerShell module. In this part we are going to create a simple module that support some CRUD operation against our API. As we are trying to keep things as simple as possible, we will not use any fancy framework (like Plaster) to build our module. We are also going to skip a very important step you should familiarize yourself with, Pester tests. Lets get to it.


The moduleWe will build a module called FilesAPI. The module folder will look like this:



In the functions folder I have already added the 2 helper functions from part 1, Get-AuthorizationHeader and ConvertTo-Base64. The other folders are just placeholders for important stuff like classes, private functions that you do not want to make available for the module consumer and tests for Pester tests. For such a small module that we are going to create, one could argue that it is much easier to just add the functi…