Skip to content

Set-Date don't work and admin-elevated test is silently skipped #3099

Description

@iSazonov
  1. Currently Set-Date tests is silently skipped during elevated test phase in Start-PSPester because the tests use Test-IsElevated function instead of RequireAdminOnWindows tag.
  2. Really if we manually run the test - one is failed:
PS > Start-PSPester -Path \test\powershell\Modules\Microsoft.PowerShell.Utility\Set-Date.Tests.ps1 -binDir "C:\Program Files\PowerShell\6.0.0.15"
VERBOSE: Running pester tests at '\test\powershell\Modules\Microsoft.PowerShell.Utility\Set-Date.Tests.ps1'
with tag 'CI' and ExcludeTag 'Slow'

Describing Set-Date
 [-] Set-Date should be able to set the date in an elevated context 850ms
   Expected: the expression not to throw an exception. Message was {The parameter is incorrect}
       from \test\powershell\Modules\Microsoft.PowerShell.Utility\Set-Date.Tests.ps1:11 char:22
       +         { get-date | set-date } | Should not throw
       +                      ~~~~~~~~
   at line: 11 in \test\powershell\Modules\Microsoft.PowerShell.Utility\Set-Date.Tests.ps1
   11:         { get-date | set-date } | Should not throw
 [!] Set-Date should produce an error in a non-elevated context 90ms
Tests completed in 940ms
Passed: 0 Failed: 1 Skipped: 1 Pending: 0

The command is manually executed failed too (I tested on Windows 10 with PS Core beta 15):

PS > get-date | set-date
set-date : The parameter is incorrect
At line:1 char:12
+ get-date | set-date
+            ~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Set-Date], Win32Exception
    + FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.SetDateCommand

PS > get-date

Saturday, February 4, 2017 9:09:32 PM


PS > set-date

cmdlet Set-Date at command pipeline position 1
Supply values for the following parameters:
Date: 2/4/2017
set-date : The parameter is incorrect
At line:1 char:1
+ set-date
+ ~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Set-Date], Win32Exception
    + FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.SetDateCommand
  • So in Set-Date.Tests.ps1 the test must be marked by RequireAdminOnWindows tag in order to run in admin-elevated phase.
    And we could remove Test-IsElevated from the tests and from Test.Helpers.psm1.

/cc James Truher (@JamesWTruher) sergei (@vors)

  • Set-Date requires a fix (for Windows only?).

/cc Andy Jordan (@andschwa)

Activity

  1. changed the title [-]Set-Date don't work and admin-elevated test silently skipped[/-] [+]Set-Date don't work and admin-elevated test is silently skipped[/+] on Feb 4, 2017
  2. vors commented on Feb 4, 2017

    @vors
    Collaborator

    Good catch

  3. iSazonov commented on Jan 25, 2018

    @iSazonov
    CollaboratorAuthor

    Tests isn't fixed.

  4. stknohg commented on Jan 25, 2018

    @stknohg
    Contributor

    Ilya (@iSazonov)

    I am trying to fix Set-Date.Tests.ps1. I have 2 problems.

    1. RequireAdminOnWindows tag only affects on Windows.
      There is no tags like RequireAdminOnLinux, RequireAdminOnMac.

    2. RequireAdminOnWindows tag is used only to skip elevated context tests.
      The third test("Set-Date should produce an error in a non-elevated context") requires explicitly non-elevated context.

    I think we have to keep using Test-IsElevated function and $IsElevated variable.


    The first error in this Issue

    set-date : The parameter is incorrect

    was caused by program bug, and I fixed it by #6006 .
    Now Set-Date.Tests.ps1 works fine without changes.

  5. iSazonov commented on Jan 25, 2018

    @iSazonov
    CollaboratorAuthor

    We always run test on CI Travis under sudo.

  6. iSazonov commented on Jan 25, 2018

    @iSazonov
    CollaboratorAuthor

    We shouldn't use Test-IsElevated - as I said above tests is silently skipped.

  7. stknohg commented on Jan 25, 2018

    @stknohg
    Contributor

    I wrote a prototype.

    # RequireAdminOnWindows tag can use for the first two tests.
    Describe "Set-Date for admin" -Tag @('CI', 'RequireAdminOnWindows') {
        It "Set-Date should be able to set the date in an elevated context" {
            { Get-Date | Set-Date } | Should Not Throw
        }
    
        It "Set-Date should be able to set the date with -Date parameter" {
            $target = Get-Date
            $expected = $target
            Set-Date -Date $target | Should Be $expected
        }
    }
    
    # This test is executed in both non-elevated and elevated contexts, 
    # and fails in elevated context without $IsElevated .
    Describe "Set-Date for non-admin" -Tag "CI" {
        BeforeAll {
            $IsElevated = Test-IsElevated
        }
    
        It "Set-Date should produce an error in a non-elevated context" -Skip:($IsElevated) {
            { Get-Date | Set-Date } | ShouldBeErrorId "System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.SetDateCommand"
        }
    }

    Do you have a good way for the last test("Set-Date should produce an error in a non-elevated context") without Test-IsElevated and $IsElevated?

  8. iSazonov commented on Jan 25, 2018

    @iSazonov
    CollaboratorAuthor

    Takuya Shibata (@stknohg) Our script automatically process RequireAdminOnWindows and run tests on Windows with RequireAdminOnWindows only in elevated phase. On Unix all tests run under sudo.

    I guess we only need skip last test on Unix - see pattern https://github.com/PowerShell/PowerShell/blob/master/docs/testing-guidelines/WritingPesterTests.md

  9. stknohg commented on Jan 25, 2018

    @stknohg
    Contributor

    Ilya (@iSazonov) Thank you. I understood about the two CI passes.

    But I am a little confused.
    My understanding is

    • The pass with exclusion of tests with 'RequireAdminOnWindows' tag

      • Pattern 1 : CI run tests on Windows (non-elevated phase)
      • Pattern 2 : CI run tests on Linux/Mac (elevated(root) phase)
    • The pass where we run only 'RequireAdminOnWindows' tests

      • Pattern 3 : CI run tests on Windows (elevated phase)

    Is this correct?

  10. iSazonov commented on Jan 25, 2018

    @iSazonov
    CollaboratorAuthor

    Takuya Shibata (@stknohg) Correct. Test-IsElevated isn't correct. And our tests works incorrect - you can look logs and see that 1 and 2 test skipped and 3 passed on both CIs, the tests never run elevated on Windows.

  11. stknohg commented on Jan 26, 2018

    @stknohg
    Contributor

    Ilya (@iSazonov) Thank you.
    Reading the CI logs,I understood what you are pointing out.
    I will make Pull Request so please review.

  12. stknohg commented on Jan 26, 2018

    @stknohg
    Contributor

    I pushed Pull Request #6034, but CI tests on Linux/macOS failed.
    It seems to be that CI tests on Linux/macOS are run as normal user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions