Reading Notes #141

Image from hubspot.comSuggestion of the week

  • Moving to Microsoft Azure (Filip Ekberg) - Another story about a blog migration on Azure. It's always interesting to learn what kind of trouble the people got while doing that.

Cloud


Programming


Mobile


Miscellaneous




Reading Notes #140

Azure2014-05_new_features 

Suggestion of the week

 

Cloud

Programming


~Frank


Reading Notes #139

Suggestion of the week


Cloud


Programming


Mobile


Miscellaneous


~Frank


Reading Notes #138

Suggestion of the week


Cloud


Programming


Mobile


~Frank

Reading Notes #137

Suggestion of the week


Cloud


Programming


Mobile


Miscellaneous



~Frank


Reading Notes #136

 

Suggestion of the week


Cloud


Programming


Miscellaneous



How to be sure that your PowerShell script are doing what you think?

Recently, I was required to write PowerShell script to automate some Microsoft Azure deployments. I was at first really happy to share those scripts because I knew other developers would use it. They would add functionalities and the library would grow. The idea was nice, but how to be sure that while adding some functionalities they don’t break something else? I didn’t know any unit test framework for Powershell, so I decided to do a quick search online. I was sure some kind of framework already exists. What I didn’t expect, however, was to find a framework that was really easy to use, complete and free! In this post, I will introduce you to Pester, a wonderful PowerShell framework available on github.

What is Pester?

Pester is a Business Development Driven (BDD) unit tests framework that implements a lot of functionalities like mock and exceptions management.

How to Install

The framework is available on github.com but it can also be downloaded from different repositories. My personal favorite is to download it from Chocolatey because it’s the most painless method. The Chocolatey’s command to install Pester is:
cinst pester 

Before continuing, if you try a Pester command right now, you will probably have the error message: “The term ‘___’ is not recognized as the name of a cmdlet…”. One way to fix that is to add Pester to your Profile. This can be done by executing the following command.

{Import-Module "C:\Chocolatey\lib\pester.2.0.4\tools\Pester.psm1"} | New-Item -path $profile -type file -force;. $profile

Get Started

Now that Pester is available on your machine, let’s start by creating our first test. Pester can scaffold for you an empty script file and a test file. If you add Pester to an already existing library, don’t be scared, Pester will not override the existing files.

Open a PowerShell console and type the following command:

New-Fixture c:\dev\pesterDemo pesterDemocd c:\dev\pesterDemo

You can try the new test by invoking Pester with this command:

Invoke-Pester

ResultDefaultPesterTest

As you can see a test already exists, and it failed.

Simple test

Let’s create a reel test that will read an XML file to extract a property. Here is the content of the XML file and the code for both the script and the test file.

manifest.xml
<service>
    <name>Employee Provider</name>
    <version>1.2</version>
</service>

pesterDemo.Tests.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
. "$here\$sut"

Describe "#pesterDemo#" {

    Context "Test with reel XML document."{

        It "Loads a reel manifest." {
            $xml = getManifest ".\manifest.xml"
                $xml.OuterXMl | Should Be "<service><name>Employee Provider</name><version>1.2</version></service>"
        }

        It "Return the service name."{
            $sName = GetServiceName ".\manifest.xml"
                $sName | Should Be "Employee Provider"
        }

    }
}

pesterDemo.ps1
function GetManifest($manifestPath){
        if(Test-Path $manifestPath)
        {
            [xml]$xml = Get-Content $manifestPath
            return $xml
    }
    else{
        Throw "Error. Ïnvalid Manifest file path."
    }
}

function GetServiceName($manifestPath){
        $manifest = GetManifest($manifestPath)
        return $manifest.service.name
}

Result of the test

The script file contains two simple functions. The first one [GetManifest] versifies if the file exists and returns his content. Otherwise, an exception is thrown. The second function [GetServiceName] retreives the content of the XML file, and returns the value of the property [name] of the node [service].

The test file contains two tests. The first one Loads a reel manifest test that the manifest.xml file is correctly loaded and validates the content. The second test Return the service name, validate that the name of the service is the same as expected. We can now invoke the tests.

Invoke-Pester

ResultFirstPesterTest

As you can see, all tests succeeded and the output is really clear. Now what if the path passed in parameter doesn’t exist? Let assume that in our design, we wanted that the code to throw an exception. A good thing for us, Pester already has everything to manage exceptions.

Let’s do a test to validate this “requirement.” First, define a code block that calls [GetManifest] with a wrong path. Then pipe the result in the Pester command Should. If you add the filling code to the pesterDemo.tests.ps1, and invoke-Pester all tests should succeed.

It "Throw an exception when loads a manifest with invalid path." {
{$xml = GetManifest ".\WrongPath\manifest.xml" }  |  Should Throw
}

Test with Mock

Sometimes, we don’t want our tests to interact with reel components. That’s why mocks are so useful. Thankfully for us, creating a mock with Pester is a piece of cake. In all the previous tests, a real XML file was used. To make some validation on the content of the file, I could have as many different files as different scenarios, or simpler I could use some mock.

With Pester, we must set up a context where our mock will be present. Here is a test where I mock the XML file. As expected, any path will work since the file is not reel.

Context "Test with Mocks."{

    Mock GetManifest{return [xml]"<service><name>Employee Provider</name><version>1.2</version></service>"}

    It "Any path will works"{
        $xml =  GetManifest -MockWith ".\wrongPath\manifest.xml"
            $xml.OuterXMl | Should Be "<service><name>Employee Provider</name><version>1.2</version></service>"
    }
}

Wrapping up

Of course, this little introduction is not an exhaustive list of all the features of Pester. I hope that with these little code snippets, I gave you the drive you needed to test your PowerShell scripts. So next time you write this amazing script to deploy your app on Microsoft Azure, think of Pester.

References



~Frank


Reading Notes #135

 

What is DevOps_coverSuggestion of the week


Cloud


Programming


Book

  • What is DevOps? (Mike Loukides) - Interesting short and free book that gives you a historical overview of devOps.

Miscellaneous



Reading Notes #134

Cloud


Programming

In my previous post, Git for Team Foundation Developers , I showed how to create a project in Visual Studio Online,

Miscellaneous




~Frank


Reading Notes #133

Breakpoint

 

Suggestion of the week

 

Cloud

Programming

Miscellaneous

Reading Notes #132

TypeScript_CoverSuggestion of the week

  • TypeScript for C# Programmers (Steve Fenton) - Great book that in less than hundred pages, explains to me how to code in TypeScript. I feel so comfortable already I will add TypeScript in my next web project, and I will strongly recommend this book to everyone. If you are a C# developer, know your base in JavaScript this book is available in PDF for free!

Cloud


Programming


Miscellaneous



Reading Notes #131

typescript1Suggestion of the week

  • Build End-to-End Apps with TypeScript (Gil Fink) - Amazing tutorial that gives the opportunity to learn a lot about recent language and tools. A mandatory reading for all web developers.

Cloud


Programming


Miscellaneous


~Frank


Reading Notes #130

Suggestion of the week


Cloud


Programming


UX


Reading Notes #129

OlympicsSuggestion of the week


Cloud


Programming


Databases


Miscellaneous

Reading Notes #128

Suggestion of the week

  • What is Windows Azure? - This post gives the definition of Azure that is short enough to be said in one breath.

Cloud

Programming

Miscellaneous



Reading Notes #127

Suggestion of the week

Cloud

Programming

Databases

Integration

  • Streaming Xml Transformations (Christos Karras) - Great tutorial that explains very clearly what are our solutions when we are in front of a complex XML transformation.

Miscellaneous


As strong as a team

cloudteamEverybody knows that we are stronger together. At work, we are grouped in small teams to accomplish more and... but wait, are we really working as one big strong force or are we working in a group but focusing on our own personal achievements? Let me share with you how as a team, we achieved what we thought was impossible.

The Context

The project was an application integrating different parts of the client's environment. A lot of technology was involved and of course, the schedule planned for it was really tight. We were a small team with different levels of experience.

The fall

As a team, we thought that splitting the job was the best option. Every member took a piece of the puzzle and started working on his side.
The juniors got themselves overwhelmed in complex patterns and best practices. They forgot the client's needs and tried to stubbornly use new technologies. The more experienced were pushed in this swirl of queries and technological challenges, lost their landmark, their concentration and the big picture.
The solution we were building was looking like an ugly patchwork that was not even functional. Continuing this way was a guarantee of failure.

The recovery

Of course, I wouldn't be writing this post if we did continue, instead we met and looked at what we could do better. Hopefully, for us, most of what we did was not a loss. If we re-focused, with a bit of a ramp-up and some extra hours put in, everything was still possible.
We did some lunch-and-learn to share the knowledge. We wrote templates to get all on the same base when we were starting new sections. We also produced short documents that explained how to get started with the concepts and technologies present in the project. The goal was to keep it short and simple so it will be readable at a glimpse.
However, all this would have been lost without code-review for everyone. Often code-reviews are perceived negatively. A review is the chance to improve not only "your" code but to standardize variable names, namespace structures, code patterns and to get to know the functions/ tools available.
Another valuable thing was our daily scrum meetings. That way, as soon as one team member had issues the best resources could jump-in to resolve the problems, or share his previous experience on how to solve this kind of complication.
With all these efforts we not only did we deliver a great solution, but we built something better: a team. Not only that people worked with each other, yet they were team-members that worked in synergy and that, is a great accomplishment.

The Lesson

I discovered something that I already know! A chain is only as strong as its weakest link. It sounds so simple but is it not always like that with the truth? Feel free to share what you did to improve your teamwork in the comment section.


~Frank

References

  • www.scrum.org

Reading Notes #126

Cloud Architecture Patterns - CoverSuggestion of the week

Books

  • Cloud Architecture Patterns (Bill Wilder) - Very instructive books that explains many different patterns with clear and practical examples. All the patterns presented are also implemented in an application Page of Photos (or PoP for short). A great book that I strongly recommend.

 

Cloud


Programming

  • Code Kata - I love it. I didn't know those kind of websides exists! I will make my visit.

Miscellaneous

~Frank


Reading Notes #125

frozenfanceSuggestion of the week

Cloud

Programming

Architecture

Miscellaneous


~Frank


Reading Notes #124

winter_pathSuggestion of the week

Cloud


Programming


Miscellaneous


Reading Notes #123

VisualStudioOnllineSuggestion of the week


Cloud


Programming

You can find out more about these conventions in the blog post Custom Code First Conventions (EF6 onwards)

Architecture


Miscellaneous


Reading Notes #122

2014Cloud

Programming

Architecture