Remove an iSCSI Target Portal with PowerShell

I ran into a small problem with removing iSCSI Target Portals using PowerShell the other day and thought it might be worth documenting.

Pretend you have an iSCSI Target Portal configured with a Target Portal Address of 192.168.129.24:

ss_iscsi_gettargetportal

You might therefore expect that you could remove this Target Portal with the command:

Unfortunately this won’t work:

ss_iscsi_removetargetportal1

And neither does this:

ss_iscsi_removetargetportal2

What you actually have to do is specify both the Target Portal Address and the Initiator Portal Address when deleting an iSCSI Target Portal:

ss_iscsi_removetargetportalcorrect

Over and out.

 

WMF 5.0 Download Removed from Download Center

Bit of a bump in the WMF 5.0 road today: Microsoft has removed the WMF 5.0 RTM download from the download center because of a significant bug:

We recently released Windows Management Framework (WMF) 5.0 RTM delivering many requested improvements and fixes, via the Microsoft Download Center as announced in a previous blog post. However, we have discovered a bug which resets the PowerShell module environment during installation. As this issue can have a serious impact on our customers, we are taking the action to stop delivery of WMF 5.0 RTM, and have removed the packages from the Download Center. Additionally, we will be unpublishing Azure DSC Extension Handler versions 2.11 and 2.12 as they automatically install WMF 5.0 RTM.

So if you’re planning on rolling WMF5.0 out to production, best hold off for the moment. See the original blog post here.

 

Creating Professional DSC Resources – Part 6

The purpose of this series of articles is to try and document a few of the lessons I learned while releasing new DSC resources as well as contributing to the existing Microsoft Community DSC resources. These articles are not intended to tell you how to write DSC resources from a programming perspective, but to give you some ideas on what might be expected of a DSC resource you’re releasing to the public. For example, unit and integration tests (don’t worry if you aren’t familiar with those terms).

These articles are also not intended to tell you what you must do to release your resource, but more document what will help your resource be easier to use and extend by other people. Some of these these things are obvious for people who have come from the development community, but may be quite new to operations people.

If you missed any previous articles you can find them here:

 

Recap

In the last couple of articles I covered the importance of automated testing and covered unit testing in particular (I’ll get to integration testing later). I had covered creating new unit tests using the unit test templates that are available here (although they will probably move here). I also covered how to complete the Pester Test Initialization and the Get-TargetResource and Set-TargetResource function areas of the unit test.

 

Unit Testing Completion

The final task in completing the unit tests is to complete the Set-TargetResource  in tests and also optionally tests for any other supporting functions your DSC Resource may have required.

In these unit tests I am using a DSC Resource for creating iSCSI Virtual Disks to illustrate the process. You don’t need to know anything about iSCSI Virtual Disks to understand these articles or resources, but if you’re interested to know the cmdlets I’m using for these, see this page. I’m using the *_iSCSIVirtualDisk cmdlets in this DSC Resource.

Function Test-TargetResource

This area will contain the actual Pester tests that test the Test-TargetResource function. These are fairly similar to the Set-TargetResource except we will be checking these two things:

  1. The output of the Test-TargetFunction is correct. E.g. it returns false if changes are required, which will cause Set-TargetFunction to be called.
  2. The expected Mocks are called by the Function.

This area may contain a large number of tests depending on the complexity of your DSC Resource. In most cases, you should expect there to create the tests from the following list, but often you will need even more for 100% code coverage:

  • Does the function return false when the resource being configured does exist and should, but one of the configured parameters does not match the current values? This test is usually repeated for each parameter in the DSC Resource.
  • Does the function return true when the resource being configured does exist and should, and all the configured parameters match the current values?
  • Does the function return false when the resource being configured does not exist but should?
  • Does the function return false when the resource being configured does exist but should not?
  • Does the function return true when the resource being configured does not exist and should not?

The bottom four of these tests are very similar. So I’ll only show examples of the top two contexts here.

Context ‘Virtual Disk exists and should but has a different …’

In this scenario we Mock the Get-iSCSIVirtualDisk cmdlet to return the object we defined in the Pester Test Initialization section. This is the behavior we’d expect if the resource being configured does exist:

This context will perform two tests:

  1. Should return false – The Test-TargetResource should return false because we are changing the Description parameter so that the resource will require changes (e.g. Set-TargetResource should be called).
  2. Should call the expected mocks – The Test-TargetResource should call the mocked cmdlets the expected number of times. In all contexts in this function this will always be just once.

The purpose of cloning the $TestVirtualDisk object is so we can modify the properties to simulate a property difference without modifying the $TestVirtualDisk object.

You should expect to repeat this context for each parameter that might be different.

 

Context ‘Virtual Disk does not exist but should’

In this scenario we Mock the Get-iSCSIVirtualDisk cmdlet to return nothing. This is the behavior we’d expect if the resource being configured does not exist:

As you can see, there is not too much different with these tests and you shouldn’t have any problems figuring out the remaining ones. Just remember, the goal is always to get 100% code coverage.

 

Unit Testing Supporting Functions

It is quite common that you might have implemented some supporting functions in your DSC Resource. These supporting functions are usually called by your standard *-TargetResource functions. If that is the case there are two important things you should do:

  1. Write unit tests that cover all code paths in your supporting functions.
  2. Add mocks to your *-TargetResource unit tests that prevent any constructive/destructive cmdlets that exist in your supporting functions from being called.

The first item is fairly self explanatory. For example, I often implement a get-* function in my DSC Resources which is used to pull the actual objects that will be used by the *-TargetResource functions (e.g. Get-VirtualDisk):

To unit test this function I’d write unit tests that tested the following contexts:

  1. Context ‘Virtual Disk does not exist’
  2. Context ‘Virtual Disk does exist’

For example:

As you can see, there isn’t much to it.

Earn a Chocolate Fish: If you look at the above supporting function and unit tests carefully, you’ll notice that I haven’t got 100% code coverage on it!

To get 100% code coverage I would have had to implement a unit test that covered the situation where the Get-iSCSIVirtualDisk function threw an exception that wasn’t a [Microsoft.Iscsi.Target.Commands.IscsiCmdException] exception.

In case you’re wondering, the Get-iSCSIVirtualDisk function throws a [Microsoft.Iscsi.Target.Commands.IscsiCmdException] when the cmdlet is called with the path parameter set to a path that does not contain a valid iSCSI Virtual Hard Disk file.

 

Unit Testing Exceptions

When creating unit tests you’ll often need to test a scenario where the function that is being tested is expected to throw an exception. If you read the Pester documentation, you’d might write a test for an exception like this:

This would of course will work. It will ensure that the code throws an exception in this situation. The problem is we aren’t really sure if it is the exception that we expected it to throw. It could have been thrown by some other part of our code.

So to improve on this we need to do things:

  1. Customize the exception that is thrown.
  2. Change the unit test so that it checks for the customized exception.

 

Customize the Exception

To create a custom exception we need to create a new exception object containing our custom error message. The exception object is then used to create a custom Error Record:

In the above code, you just need to customize the $errorId and $errorMessage variables. The $errorId should just contain a simply string identifier for this particular type of error, but the $errorMessage can contain a full description of the error, including related parameters.

Once you’ve created the $errorRecord object you can call the ThrowTerminatingError method of the $PSCmdLet object, passing the $errorRecord object as the parameter.

Important: the $PSCmdLet object is only available in Functions that include the [CmdletBinding()] function attribute. So ensure your *-TargetResource and supporting functions include this attribute if you want to be able to access this object.

 

Test for the Customized Exception

To test for the custom exception object we need to create an identical object in the unit test and test for it:

The above code creates an identical exception object to the one produced by the exception in our DSC Resource code. The exception object can then be passed to the should throw cmdlet. If a different exception is thrown by the code then the test will fail – it will only pass if the exception object is exactly the same.

Important: Make sure both the $errorId and $errorMessage variables are exactly the same as what would be produced by the code when your unit test calls it. This includes ensuring that if your $errorMessage contains any parameters that the unit test $errorMessage contains the same parameter values.

 

That about completes creating unit tests. After you’ve implemented a few unit tests you’ll no doubt come up with your own method of implementing them, but hopefully this has given you a place to start.

 

Up Next – Integration Tests

In the next article, I’ll cover the integration tests. There are often the most difficult to implement, but if you can take the time to implement them then your DSC Resources are guaranteed to be extremely robust and bugs are far less likely to slip through.

Further parts in this series:

Creating Professional DSC Resources – Part 5

The purpose of this series of articles is to try and document a few of the lessons I learned while releasing new DSC resources as well as contributing to the existing Microsoft Community DSC resources. These articles are not intended to tell you how to write DSC resources from a programming perspective, but to give you some ideas on what might be expected of a DSC resource you’re releasing to the public. For example, unit and integration tests (don’t worry if you aren’t familiar with those terms).

These articles are also not intended to tell you what you must do to release your resource, but more document what will help your resource be easier to use and extend by other people. Some of these these things are obvious for people who have come from the development community, but may be quite new to operations people.

If you missed any previous articles you can find them here:

 

Recap

Yesterday I talked about  the importance of automated testing and covered unit testing in particular (I’ll get to integration testing later). I had covered creating new unit tests using the unit test templates that are available here (although they will probably move here). I also covered how to complete the Pester Test Initialization and the Function Get-TargetResource areas of the unit test.

 

Unit Testing Continued

The next task in completing the unit tests is to complete the Set-TargetResource area in the unit test.

In these unit tests I am using a DSC Resource for creating iSCSI Virtual Disks to illustrate the process. You don’t need to know anything about iSCSI Virtual Disks to understand these articles or resources, but if you’re interested to know the cmdlets I’m using for these, see this page. I’m using the *_iSCSIVirtualDisk cmdlets in this DSC Resource.

Function Set-TargetResource

This area will contain the actual Pester tests that test the Set-TargetResource function. Unlike the Get-TargetResource this area may contain a large number of tests depending on the complexity of your DSC Resource. In most cases, you should expect there to create the tests from the following list, but often you will need even more for 100% code coverage:

  • Does each parameter get set/updated correctly when the resource being configured does exist and should? This test is usually repeated for each parameter in the DSC Resource.
  • Does it work when the resource being configured does not exist but should?
  • Does it work when the resource being configured does exist but should not?
  • Does it work when the resource being configured does not exist and should not?

Context ‘Virtual Disk exists and should but has a different …’

In this scenario we Mock the Get-iSCSIVirtualDisk cmdlet to return the object we defined in the Pester Test Initialization section. This is the behavior we’d expect if the resource being configured does exist.

We are also going to Mock the Set-iSCSIVirtualDisk, New-iSCSIVirtualDisk and Remove-iSCSIVirtualDisk. This is so we can ensure the expected cmdlets are called as well as preventing the real cmdlets from being run:

This context will perform two tests:

  1. Should not throw error – The Set-TargetResource should not throw an error when called in this context.
  2. Should call the expected mocks – The Set-TargetResource should call the mocked cmdlets the expected number of times.

The purpose of cloning the $TestVirtualDisk object is so we can modify the properties to simulate a property difference without modifying the $TestVirtualDisk object.

It is also important to ensure that we are not only checking that the expected cmdlets are called, but also that the other cmdlets in this function are not called. This is why we are checking the New-iSCSIVirtualDisk and Remove-iSCSIVirtualDisk are being called zero times.

You should expect to repeat this context for each parameter that might be updated.

Note: It is possible that updating some parameters may not be possible because of limitations in the underlying cmdlets. In this case I like to throw an exception so that the user is made aware that they are configuring a scenarios that can not be performed. In that case the test would be to ensure the correct exception occurs. I’ll cover testing exceptions in a later article.

 

Context ‘Virtual Disk does not exist but should’

In this scenario we Mock the Get-iSCSIVirtualDisk cmdlet to return nothing. This is the behavior we’d expect if the resource being configured does not exist.

We are also going to Mock the Set-iSCSIVirtualDisk, New-iSCSIVirtualDisk and Remove-iSCSIVirtualDisk. This is so we can ensure the expected cmdlets are called as well as preventing the real cmdlets from being run:

The context tests are very similar to all the other tests so I won’t go into detail on them here. It is important to note that the expected Mocks will be different.

 

Context ‘Virtual Disk exists but should not’

In this scenario we Mock the Get-iSCSIVirtualDisk cmdlet to return the object we defined in the Pester Test Initialization section. This is the behavior we’d expect if the resource being configured does exist.

We are also going to Mock the Set-iSCSIVirtualDisk, New-iSCSIVirtualDisk and Remove-iSCSIVirtualDisk. This is so we can ensure the expected cmdlets are called as well as preventing the real cmdlets from being run:

The context tests are very similar to all the other tests so I won’t go into detail on them here. It is important to note that the expected Mocks will be different.

 

Context ‘Virtual Disk does not exist and should not’

In this scenario we Mock the Get-iSCSIVirtualDisk cmdlet to return the object we defined in the Pester Test Initialization section. This is the behavior we’d expect if the resource being configured does not exist.

We are also going to Mock the Set-iSCSIVirtualDisk, New-iSCSIVirtualDisk and Remove-iSCSIVirtualDisk. This is so we can ensure the expected cmdlets are called as well as preventing the real cmdlets from being run:

The context tests are very similar to all the other tests so I won’t go into detail on them here. It is important to note that the expected Mocks will be different.

 

Unit Tests to be Continued…

In the next article, I’ll cover the unit tests for Get-TargetResource as well as unit testing any additional functions. Thanks again for reading and I hope it is useful.

Further parts in this series:

 

ss_vscode_previewbutton

Markdown Preview in Visual Studio Code

I used to keep around a copy of Markdown Pad to enable me to preview any markdown files I created. However, after an update to a Awesomium SDK Markdown Pad stopped working. I reported the issue but it hasn’t been resolved yet.

But after Irwin Strachan mentioned using Visual Studio Code as a Markdown editor, I wondered if there was an extension that allowed it to Preview the Markdown as well – because that was really all that was missing for me to do away with Markdown Pad. So I went and did a search for Markdown extensions in VS Code:

ss_vscode_markdownextensions

Want to install extensions in VS Code? Press F1 and type ‘ext install’.

I found a few, but after playing around with them a bit I noticed a small button in the latest version of Visual Studio Code at the top right corner when a markdown file is open:

ss_vscode_previewbutton

The Preview button!

I wonder what that might do?

ss_vscode_preview

That is a great looking markdown preview.

Oh, now look at that! Just what I needed. I don’t know which version of Visual Studio Code this was added in, but it is fantastic!

You can also use the Visual Studio Code split window function to edit and Preview at the same time:

ss_vscode_splitpreview

Changing the markdown automatically updates the preview – nice!

While we’re on the topic of markdown, Irwin also pointed me at this awesome markdown tutorial – it makes learning markdown fun!

Now, Visual Studio Code is not on par with ISE Steroids when it comes to editing PowerShell files, but when you’re dealing with lots of different types of files (including PS files) it can be a real time saver. I highly recommend downloading a copy of Visual Studio Code, installing the PowerShell Extension and giving it a try.

 

Creating Professional DSC Resources – Part 4

The purpose of this series of articles is to try and document a few of the lessons I learned while releasing new DSC resources as well as contributing to the existing Microsoft Community DSC resources. These articles are not intended to tell you how to write DSC resources from a programming perspective, but to give you some ideas on what might be expected of a DSC resource you’re releasing to the public. For example, unit and integration tests (don’t worry if you aren’t familiar with those terms).

These articles are also not intended to tell you what you must do to release your resource, but more document what will help your resource be easier to use and extend by other people. Some of these these things are obvious for people who have come from the development community, but may be quite new to operations people.

If you missed any previous articles you can find them here:

 

Automated Testing

Automated testing is something that is familiar to most developers, but for operations people it is usually a new concept. However, it is one of the most important things you can add to your DSC Resources – and most DSC resource projects won’t even accept your code contributions if they don’t contain automated tests.

So, what are automated tests? Well, they are just PowerShell scripts that you create and run that will check your DSC Resource is working correctly. Usually automated tests are run on your DSC Resources every time you commit your code – and they’ll tell you if anything has gone wrong. I could spend the next day listing reasons why automated testing is extremely important, but that is not the purpose for this post.

PowerShell contains a great automated test framework called Pester that allows you to describe your tests using special PowerShell functions.

Important: If you aren’t familiar with Pester and automated testing, you should get familiar with it before reading any further. This series is a fantastic place to start. Even if you’re familiar with Pester it is a good read.

An example of a Pester test on a DSC Resource:

The above test is a unit test of the xFirewall resource in the xNetworking module. the Don’t worry if you don’t completely understand this yet, that is the purpose of this article – although you should understand the basic structure of the Pester test – if you don’t, you’ll definitely want to go and review this series.

 

Types of Automated Tests

There are two types of automated tests you should create for your DSC Resources:

  • Unit Tests – these test that each function in your DSC Resource works correctly in isolation. This means that if you call the function with a set of parameters you get expected output.
  • Integration Tests – these tests ensure that your DSC Resource works in a real environment – e.g. work correctly when they are actually integrated into a DSC Configuration file.

For every DSC Resource in your DSC Resource Module you should ensure that there is one unit test file and one integration test file (although usually for integration tests a support file is also needed, but we’ll cover this later).

 

Test Folders

You should place all tests inside a Tests folder in the root of your DSC Module folder:

ss_dsc_testfolders

Unit tests should be placed in a Unit folder within Tests and … I’m sure you get where I’m going here.

 

Unit Tests

Unit tests are a good place to start when creating automated tests for your DSC Resources because they’re u sually quite straight forward. Each DSC Resource in your DSC Module should contain it’s own unit test file. For example, here are the unit tests that are included with the xNetworking DSC Resource module:

ss_dsc_xnetworkingunittests

A note about code coverage: A unit test script will contain lots of individual tests. The purpose of each individual unit test is usually to test a single path through a function that the computer might take when running that function. The goal therefore is to add as many tests as needed to make sure each code path is covered. This is called code coverage. The goal is to have 100% code coverage for your DSC Resource.

In these unit tests I am using a DSC Resource for creating iSCSI Virtual Disks to illustrate the process. You don’t need to know anything about iSCSI Virtual Disks to understand these articles or resources, but if you’re interested to know the cmdlets I’m using for these, see this page. I’m using the *_iSCSIVirtualDisk cmdlets in this DSC Resource.

 

Creating a New Unit Test

You could just go ahead and create a new unit from scratch, but there is a much easier way. There is a unit test template file that you can use to create new unit tests. You need to copy the unit test template file and rename it to match your DSC Resource.

The easiest way to do this is to clone the repository containing the test template files and copy the unit_template.ps1 file to your Tests/Unit folder:

ss_dsc_createnewunittestfromtemplate

You’ll now have a new unit test file that you can open in your PowerShell editor of choice:

ss_dsc_editingnewunittest

Next, customize the TODO area in the header with the your DSC Resource Name and DSC Module Name:

ss_dsc_customizeunittestheader

Feel free to remove the TODO comments if you want (I always do).

Now, onto the job of actually adding our Pester tests. You’ll want to locate the Pester Tests region:

ss_dsc_unittestspestertestsregion

This is usually the only area of code in this file you need edit. There are several areas you need to customize here:

  1. Pester Test Initialization – this is where you would define any variables or objects that you might use for testing.
  2. Function Get-TargetResource – this would contain the Pester Tests to test the Get-TargetResource function.
  3. Function Test-TargetResource – this would contain the Pester Tests to test the Test-TargetResource function.
  4. Function Set-TargetResource – this would contain the Pester Tests to test the Set-TargetResource function.
  5. Pester Tests for any Helper Cmdlets – you would add Describe blocks for any additional functions you have defined in your resource to ensure those functions are tested.

Once there sections are completed for a DSC Resource the tests can be invoked using the Invoke-Pester command.

But first I’ll provide more detail on what you should add into each section.

 

Pester Test Initialization

The purpose of this section is to initialize any variables or objects that you’re going to use for testing.

For example, if you were testing a resource that was for creating an iSCSI Virtual Disk you might define the parameters of the iSCSI Virtual Disk that you’re going to use for testing:

The $TestVirtualDisk object is used to @splat onto the Set-TargetResource and Test-TargetResource functions and for comparison. It saves us lots of typing and therefore potential mistakes.

The $MockVirtualDisk object is used as output to the Mocks that get defined on the Get-iSCSIVirtualDisk cmdlet when we want to simulate the scenario where the iSCSI Virtual Disk already exists.

You don’t of course have to do this, but you’ll find that having some objects defined up front that you can use as test objects will make your code smaller and easier to understand as well as making the tests run faster.

 

Function Get-TargetResource

This area will contain the actual Pester tests that test the Get-TargetResource function. Usually there are only a small number of tests you can perform in this function:

  • Does it work when the resource being configured does not exist?
  • Does it work and return the expected parameters when the resource being configured does exist?

For example:

In the above code we have two Context blocks – one for each of our scenarios above.

Context ‘Virtual Disk does not exist’

In this scenario we Mock the Get-iSCSIVirtualDisk cmdlet to return nothing. This is the behavior we’d expect if the resource being configured didn’t exist.

We can now check the object returned is what is expected and that the Get-iSCSIVirtualDisk was called the expected number of times – in this case once.

Context ‘Virtual Disk does exist’

In this scenario we Mock the Get-iSCSIVirtualDisk cmdlet to return the object we defined in the Pester Test Initialization section. This is the behavior we’d expect if the resource being configured does exist.

We can therefore check the object returned is what is expected and all the returned object properties match and that the Get-iSCSIVirtualDisk was called the expected number of times – again this is once.

 

Unit Tests to be Continued…

I wanted to put all the unit test info in a single part of this series, but as you can see it is getting quite long. I’ll complete the rest of the information on unit tests tomorrow.

So in the next article, I’ll cover the unit tests for Set-TargetResource and Get-TargetResource as well as unit testing any additional functions. Thanks again for reading and I hope it is useful.

Further parts in this series: