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

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…

Powershell – Log like you mean it

How do you do logging in powershell? Why should you do logging? What should you log? Where do you put your log? How do you remove your log? How do you search your log? All important questions and how you answer then depends upon what your background is like and the preferences you have. This will be a 2 part blog post and this is part 1.


Why should you log?

Well it is not mandatory, however I have 2 reasons:
Help with debugging a script/module/functionSelf documenting script/module/function
Firstly; Do you know any program that does not contain any bugs? Working with IT for the last 2 decades, I cannot name one. When you create scripts/modules/functions, you will create bugs, that is where they live and try to make your life a living mess.

Secondly: Adding a little extra information to your logging will make them self documenting. Do you like writing documentation? Well I normally am not fond of it and use logging while debugging to get two birds with one stone.


What should you log?

Anyt…

Powershell - List information about your WIFI networks

This is just a quick post about this new function I have created. Basically this is a text-output to powershell object output function that uses netsh to query the WIFI information. This illustrates the importance of changing the authentication level on your WIFI-network. No matter if you use WEP/WPA/WPA2 your password is available in clear text in your profile.



Cheers

Tore