aws / aws-tools-for-powershell

The AWS Tools for PowerShell lets developers and administrators manage their AWS services from the PowerShell scripting environment.
Apache License 2.0
235 stars 78 forks source link

Credential and NetworkCredential should not accept by-value pipeline input #167

Closed indented-automation closed 2 years ago

indented-automation commented 3 years ago

Description

When testing my code, and mocking Get-EC2Volume using Pester I expect to be able to supply an input pipeline for Get-EC2Volume.

Get-EC2Volume accepts by-value pipeline input for InstanceId, Credential, and NetworkCredential.

Pipeline input for InstanceId is somewhat expected. By-Value pipeline input for credential parameters is not.

PowerShell will attempt to bind all parameters accepting by-value input which causes a parameter binding failure.

Reproduction Steps

Set-Content 'module.tests.ps1' -Value @'
Describe 'Some tests' {
    BeforeAll {
        Mock Get-EC2Instance {
            [PSCustomObject]@{
                Instances = @(
                    [PSCustomObject]@{
                        BlockDeviceMappings = @(
                            [PSCustomObject]@{
                                DeviceName = '/dev/sda1'
                                Ebs        = [PSCustomObject]@{
                                    VolumeId = 'vol-11111111'
                                }
                            }
                            [PSCustomObject]@{
                                DeviceName = '/dev/sdf'
                                Ebs        = [PSCustomObject]@{
                                    VolumeId = 'vol-22222222'
                                }
                            }
                        )
                    }
                )
            }
        }

        Mock Get-EC2Volume {
            [PSCustomObject]@{
                VolumeId = 'vol-22222222'
                Tags     = @()
            }
        }
    }

    It 'Should execute on a pipeline without failing' {
        Invoke-Something

        Should -Invoke Get-EC2Volume
    }
}
'@

Set-Content 'module.psm1' -Value @'
function Invoke-Something {
    (Get-EC2Instance -InstanceId $instanceId).Instances.BlockDeviceMappings.Ebs |
        Get-EC2Volume
}
'@

Import-Module .\module.psm1 -Force

$configuration = @{
    Run = @{
        Path = '.\module.tests.ps1'
    }
    Output = @{
        Verbosity = 'Detailed'
    }
}
Invoke-Pester -Configuration $configuration

The snippet above raises an error as the command attempts to bind the input pipeline to credential parameters.

Starting discovery in 1 files.
Discovering in C:\Development\cp-powershell-imagehelper\module.tests.ps1.
Found 1 tests. 26ms
Discovery finished in 33ms.

Running tests from 'C:\Development\cp-powershell-imagehelper\module.tests.ps1'
Describing Some tests
PesterMock_f491b2df-48aa-450b-b9e6-f45bfd19fc8c: C:\Development\cp-powershell-imagehelper\module.psm1:3
Line |
   3 |          Get-EC2Volume
     |          ~~~~~~~~~~~~~
     | Cannot process argument transformation on parameter 'NetworkCredential'. userName

PesterMock_f491b2df-48aa-450b-b9e6-f45bfd19fc8c: C:\Development\cp-powershell-imagehelper\module.psm1:3
Line |
   3 |          Get-EC2Volume
     |          ~~~~~~~~~~~~~
     | Cannot process argument transformation on parameter 'NetworkCredential'. userName

  [-] Should execute on a pipeline without failing 118ms (117ms|2ms)
   Expected Get-EC2Volume to be called at least 1 times but was called 0 times
   at Should -Invoke Get-EC2Volume, C:\Development\cp-powershell-imagehelper\module.tests.ps1:37
   at <ScriptBlock>, C:\Development\cp-powershell-imagehelper\module.tests.ps1:37
Tests completed in 251ms
Tests Passed: 0, Failed: 1, Skipped: 0 NotRun: 0

Environment

Resolution

Remove ValueFromPipeline from Credential parameters.


This is a :bug: bug-report

github-actions[bot] commented 2 years ago

We have noticed this issue has not recieved attention in 1 year. We will close this issue for now. If you think this is in error, please feel free to comment and reopen the issue.