top of page
  • AutorenbildKevin Bates

Docker Pester - Run crossplatform tests locally!

Recently there have been some changes in my backpack concerning the portable system that I carry with myself to perform my job. I have had an HP laptop handed to me on my first day of work at my current company (about two years ago) and I actually had no problems whatsoever at the start. Now this laptop lived for about 3/4 years before it decided to stop working. My Laptop would not turn on and I could not work for 1.5 days cause we had no spare laptops at the time.


Now that was a bummer.. My company replaced my system with the same model and since I actually liked the Laptop I was handed in the first place I was excited to have another one of these. Another 3/4 years down the line the exact same thing happens. Again I'm unable to work for 1.5 days before my replacement arrives. Again it is the same model. Then I learned something interessting. My company does BYOD (Bring your own device) and they actually pay for that system. But as, almost, always with cool stuff there is a hook. The laptop my company hands out have a 2 year lease on them before you can have a new one. Very reasonable policy in my eyes and also I thought at the time that my lease would soon expire and I could order a new one.


No...


As I learned a little while after receiving my 3rd laptop (still same model) I found out that my lease was reset both times I received a replacement and I thought that I would be stuck in an endless HP Laptop loop, where I would get a new one every 3/4 years.


Now I'm in luck that I have good people above me that allowed me to ditch that HP misery (Not at all implying that's the case for every HP Laptop!) and order myself my own System to be able to work on that. So I started thinking what I would like to use. It would have to perform well since I do developmemt on it and I also am fond of big and nice screens, since I work in a different spot nearly every day and I don't always have the possibility for external screens. Ah and I code Powershell.


Could I buy myself a Macbook and have a good time working on it mostly writing Powershell code? Powershell 7 is around the edge and crossplatform is available since v6 so that is kind of covered. VSCode is available.. What do I need more? Honestly I tried to inform myself to the best I could, but soon it got clear to me that I was going to get a Macbook.

At the time I write this I have worked on my Macbook for one week now and I have to say I love it! I have noticed no shortcomings up until now and I have done some cool linux stuff on my machine! I have a Linux ansible lab on my Macbook for example. But the main thing I discovered for me this past few weeks was Docker.


I know I'm late to the party, but I started to look at it and play around with it. My main interesst was actually to run Powershell on linux and see how that's working out. But then it hit me. Run Pester tests on docker machines with powershell on them. You can completly destroy your container and there will be a new one up and running in a matter of seconds.

I know it's a simple idea and I by no means think for a second, that I'm the first one to come up with this, but still. So I started to write a Module.


In this Blogpost you'll learn how to run the Pester tests for your Powershell project on multiple operating systems locally.


DockerPester makes this possible.


DockerPester


Ok so now you should have installed Docker on your machine, which makes you ready to use DockerPester.


Installing DockerPester


The Module is currently not available on the Gallery. So at the moment the only option of using it you have is to fork and clone the repo.


First run of DockerPester


So to start off you just need a Powershell Project of yours with some Pester Tests. Create a Hashtable that contains the following Key Value pairs:


{% highlight powershell %} $ParamSet = @{ ContainerName = "DockerPester" #Name of the Container used to Test. Image = "mcr.microsoft.com/powershell:7.0.0-rc.2-alpine-3.8" #Image used for the Container InputFolder = "/Users/kevin/code/PSHarmonize"#Module or Folder you pass with your Tests in them PathOnContainer = "/var" #Path you want to copy to in your container PathToTests = "Tests" #Path in 'InputFolder' where your Tests are located PrerequisiteModule = "PSHTML" #Pass Modules that are prerequisites to your tests to download from gallery in container. Executor = "LNX" #The executor is the OS of your Machine - Docker host. If you run on Windows you set "WIN" if you run on MacOS or Linux you set "LNX" } {% endhighlight %}

Each Parameter we define in the hashtable is shortly described with a comment on the same line. Make sure you're in the right context for the Image you define here and the Executor matches your OS. Also make sure to change the 'InputFolder' to an actual project with Pestertests in them. 'PathToTests' is the Path in your Powershell Project Folder where your Tests are located.

To run your first DockerPester run use the following code:

{% highlight powershell %} Invoke-DockerPester @ParamSet {% endhighlight %}

So here we basically just splat the Parameters we defined in the Hash $ParamSet to 'Invoke-DockerPester'.

So what will that do. First it will spin up a Container with the Image we defined in our Hash. The ContainerName we set will be the name of that container. Then it will copy the Powershell Module Project Folder from your local Computer that you defined under 'InputFolder' to the path you defined in 'PathOnContainer' on the Container. After that it will start downloading the Prerequisite modules that you defined to download from the gallery and it will install the newest version of Pester and yes, the output on that is not sooooooo nice I will admit that, but hey, we all need room to grow right?

After the installation of the modules it will start executing the Pester Tests. And when that's done it will return the Passthru Object from Pester back to you.

The Output you will see should look like this:

{% highlight powershell %} PS /Users/kevin> Invoke-DockerPester @ParamSetf6b8047075adc5ee491b772b9321e85bf0e628d9add2a1ff9f79ea254894227a DockerPester Installing package 'PSHTML' Downloaded 0.00 MB out of 0.64 MB. [ ] Installing package 'Pester' Downloaded 0.00 MB out of 0.85 MB. [ ] Installing package 'Pester' Process Package Manifest [oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo ] Pester v4.10.1 Executing all tests in '/var/PSHarmonize/Tests'

Executing script /var/PSHarmonize/Tests/Functions.Letters.Tests.ps1

Describing [PSHarmonize][Functions] Testing Letter Functions

3 Ansichten0 Kommentare

Aktuelle Beiträge

Alle ansehen

留言


bottom of page