Reputation: 12334
I have the following working code that is used to create an HDInsight cluster:
New-AzureHDInsightClusterConfig -ClusterSizeInNodes $ClusterNodes `
-VirtualNetworkId ($VNet.Id) `
-SubnetName $subnetName `
-ClusterType $ClusterType | `
Set-AzureHDInsightDefaultStorage -StorageAccountName "$StorageAccountName.blob.core.windows.net" `
-StorageAccountKey $StorageAccountKey `
-StorageContainerName $StorageContainerName | `
New-AzureHDInsightCluster -Credential $ClusterCreds `
-Location $Location `
-Name $ClusterName `
-Version $HDInsightVersion
Notice that I am using pipelining. Now I'd like to write some automated tests (using Pester) to test this code. In order to do so I wrap the cmdlet calls in what I refer to as proxy functions and pass on parameter values using splatting, this makes it easy to mock them for the purposes of testing. Here's an example:
function Set-AzureHDInsightDefaultStorageProxy{
<#
.SYNOPSIS
Wrap Azure cmdlet Set-AzureHDInsightDefaultStorage thus enabling mocking
.DESCRIPTION
Wrap Azure cmdlet Set-AzureHDInsightDefaultStorage thus enabling mocking
#>
[CmdletBinding()]
Param(
$StorageAccountName,
$StorageAccountKey,
$StorageContainerName
)
Set-AzureHDInsightDefaultStorage @PSBoundParameters
}
New-AzureHDInsightClusterConfigProxy -ClusterSizeInNodes $ClusterNodes `
-VirtualNetworkId ($VNet.Id) `
-SubnetName $subnetName `
-ClusterType $ClusterType | `
Set-AzureHDInsightDefaultStorageProxy -StorageAccountName "$StorageAccountName.blob.core.windows.net" `
-StorageAccountKey $StorageAccountKey `
-StorageContainerName $StorageContainerName | `
New-AzureHDInsightClusterProxy -Credential $ClusterCreds `
-Location $Location `
-Name $ClusterName `
-Version $HDInsightVersion
When I try and run this code I get an error:
Set-AzureHDInsightDefaultStorageProxy -StorageAccountName ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ [<<==>>] Exception: The input object cannot be bound to any parameters for the command either because the command does not take pipe line input or the input and its properties do not match any of the parameters that take pipeline input.
OK, so I need to modify my function to accept pipeline input. I take a read of Write PowerShell Functions That Accept Pipelined Input and based on that I rewrote my proxy function to:
function Set-AzureHDInsightDefaultStorageProxy{
[CmdletBinding()]
Param(
$StorageAccountName,
$StorageAccountKey,
$StorageContainerName
)
Set-AzureHDInsightDefaultStorage -Config $input -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey -StorageContainerName $StorageContainerName
}
but that failed with the same error.
Clearly my lack of Powershell skills/knowledge are letting me down here so I'm hoping someone out there can tell me how to rewrite my function to successfully accept and use pipeline input.
Here's the definition of the cmdlet for which I am writing the proxy function: Set-AzureHDInsightDefaultStorage. I notice that the -Config parameter is set to allow pipeline input:
so I guess I need to specify the same in my proxy function, but I don't know how to do that.
Upvotes: 1
Views: 348
Reputation: 12334
OK, think I've figured it out:
function Set-AzureHDInsightDefaultStorageProxy{
<#
.SYNOPSIS
Wrap Azure cmdlet Set-AzureHDInsightDefaultStorage thus enabling mocking
.DESCRIPTION
Wrap Azure cmdlet Set-AzureHDInsightDefaultStorage thus enabling mocking
#>
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=$true)]$Config,
$StorageAccountName,
$StorageAccountKey,
$StorageContainerName
)
Set-AzureHDInsightDefaultStorage -Config $input -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey -StorageContainerName $StorageContainerName
}
its still not working as I'd like because I'm not passing on a correct value from the preceding cmdlet in the pipeline so I'm getting an error:
Error: 08/12/2014 12:19:55: At E:\DeployUtilities.psm1:984 char:21 + Set- AzureHDInsightDefaultStorageProxy
-StorageAccountName ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~ [<<==>>] Exception: Cannot convert 'System.Object[]' to the type 'Microsoft.WindowsAzure.Manag ement.HDInsight.Cmdlet.DataObjects.AzureHDInsightConfig' required by parameter 'Config'. Specified method is not supported. --> Specified method is not supported.
but that's a different (self-explanatory) problem, which I'll now endeavour to solve!
Upvotes: 1