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

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 …

Build your local powershell module repository - ProGet

So Windows Powershell Blog released a blog a couple of days ago (link). Not too long after, a discussion emerged about it being to complicated to setup. Even though the required software is open source (nugetgalleryserver), it looks like you need to have Visual Studio Installed to compile it. I looked into doing it without visual stuidio, however I have been unable to come up with a solution. I even tweeted about it since I am not an developer. Maybe someone how is familiar with “msbuild” could do a post on how to do it without VS.

Anyhow one of my twitter-friends (@sstranger) came to the rescue and pointed me in the direction of ProGet, hence the title of this post. ProGet comes in 2 different licensing modes
Free (reduced functionality)Enterprise (paid version with extra features)The good news is that the free version supports hosting a local PowershellGet repository which was my intention anyway. So off we go and create a Configration that can install ProGet for us. This is the conf…

Monitoring Orchestrator runbook events from Operations Manager

Today I will follow up on my colleague’s post Mr ITblog (Knut Huglen) about monitoring Orchestrator Runbook events.  He has build a nice double up SNMP loopback feature that does self monitoring in Orchestrator resulting in entries written to a special Windows Eventlog. Now we need to raise alerts in SCOM when one of his runbooks fails or sends a platform event, who knows there could be trouble lurking in his paradise.

We are not going to do anything fancy, however these are the steps we will be focusing on today:
Create a Management Pack for our customizations Create rules that collects the events from the orchestrator serverOff we go then and fire up the SCOM console and a powershell window. First we create a MP, I am going to use powershell to do this, however you may use the SCOM console as well (Administration – ManagementPacks – Action: Create Management Pack):



Import the Management Pack into SCOM and move on to the Authoring section in the SCOM console. Create a new rule:



Give the…