chocolatey-community / chocolatey-test-environment

A testing setup related to how the Chocolatey Package Verifier runs testing. Used for manual testing or prior to submission
Apache License 2.0
118 stars 186 forks source link

Why does vagrant display error if no package is tested? #8

Closed dtgm closed 8 years ago

dtgm commented 8 years ago
Write-Output "Testing package if a line is uncommented. Otherwise you will see an error."

why vagrant do that?

Vagrantfile https://github.com/chocolatey/chocolatey-test-environment/blob/master/Vagrantfile#L95-L108

$ErrorActionPreference = "Stop"
$env:PATH +=";$env:SystemDrive\\ProgramData\\chocolatey\\bin"
Write-Output "Testing package if a line is uncommented. Otherwise you will see an error."
# THIS IS WHAT YOU CHANGE
# - uncomment one of the two and edit it appropriately
# - See the README for details
#choco.exe install -fdvy INSERT_NAME --version INSERT_VERSION  --allow-downgrade
#choco.exe install -fdvy INSERT_NAME  --allow-downgrade --source "'c:\\packages;http://chocolatey.org/api/v2/'"
# vagrant has issues knowing that there was an error
if ($LASTEXITCODE -ne 0) {
  exit 1
}

vagrant provision output

PS C:\dev\chocolatey-test-environment> vagrant provision
==> default: Running provisioner: shell...
    default: Running: shell/PrepareWindows.ps1 as c:\tmp\vagrant-shell.ps1
==> default: IE Enhanced Security Configuration (ESC) has been disabled.
==> default: IE first run welcome screen has been disabled.
==> default: Setting Windows Update service to Manual startup type.
==> default: Running provisioner: shell...
    default: Running: shell/InstallNet4.ps1 as c:\tmp\vagrant-shell.ps1
==> default: Running provisioner: shell...
    default: Running: shell/InstallChocolatey.ps1 as c:\tmp\vagrant-shell.ps1
==> default: Chocolatey v0.9.10-beta1-151-ga83d5c5
==> default: Nothing to change. Config already set.
==> default: Chocolatey v0.9.10-beta1-151-ga83d5c5
==> default: Nothing to change. Config already set.
==> default: Chocolatey v0.9.10-beta1-151-ga83d5c5
==> default: Nothing to change. Config already set.
==> default: Running provisioner: shell...
    default: Running: shell/NotifyGuiAppsOfEnvironmentChanges.ps1 as c:\tmp\vagrant-shell.ps1
==> default: 1
==> default:
==> default: SUCCESS: Specified value was saved.
==> default: Running provisioner: shell...
    default: Running: inline PowerShell script
==> default: Testing package if a line is uncommented. Otherwise you will see an error.
The following WinRM command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

powershell -ExecutionPolicy Bypass -OutputFormat Text -file c:\tmp\vagrant-shell.ps1

Stdout from the command:

Stderr from the command:
dtgm commented 8 years ago

Vagrantfile

$packageTestScript = <<SCRIPT
if ($LASTEXITCODE -ne 0) {
  exit $LASTEXITCODE
}
SCRIPT
PS C:\dev\chocolatey-test-environment> vagrant provision
==> default: Running provisioner: shell...
    default: Running: shell/PrepareWindows.ps1 as c:\tmp\vagrant-shell.ps1
==> default: IE Enhanced Security Configuration (ESC) has been disabled.
==> default: IE first run welcome screen has been disabled.
==> default: Setting Windows Update service to Manual startup type.
==> default: Running provisioner: shell...
    default: Running: shell/InstallNet4.ps1 as c:\tmp\vagrant-shell.ps1
==> default: Running provisioner: shell...
    default: Running: shell/InstallChocolatey.ps1 as c:\tmp\vagrant-shell.ps1
==> default: Chocolatey v0.9.10-beta1-151-ga83d5c5
==> default: Nothing to change. Config already set.
==> default: Chocolatey v0.9.10-beta1-151-ga83d5c5
==> default: Nothing to change. Config already set.
==> default: Chocolatey v0.9.10-beta1-151-ga83d5c5
==> default: Nothing to change. Config already set.
==> default: Running provisioner: shell...
    default: Running: shell/NotifyGuiAppsOfEnvironmentChanges.ps1 as c:\tmp\vagrant-shell.ps1
==> default: 1
==> default:
==> default:
==> default: SUCCESS: Specified value was saved.
==> default: Running provisioner: shell...
    default: Running: inline PowerShell script

So $LASTEXITCODE != 0 before the SCRIPT runs

last win32 application ran is setx.exe trigger 1 (?) https://github.com/chocolatey/chocolatey-test-environment/blob/master/shell/NotifyGuiAppsOfEnvironmentChanges.ps1#L20


In above vagrant provision output, setx.exe trigger 1 generates output line ==> default: SUCCESS: Specified value was saved.

ferventcoder commented 8 years ago

So we could run a successful command before trying the pkg install

dtgm commented 8 years ago

That makes sense. But why/how is $LASTEXITCODE being triggered in the script?

dtgm commented 8 years ago

It doesn't look like setx.exe trigger 1 in shell\NotifyGuiAppsOfEnvironmentChanges.ps1 is achieving its intended effect.

Vagrantfile:

$packageTestScript = <<SCRIPT
if ($LASTEXITCODE -ne 0) {
  exit 1
}
SCRIPT
PS C:\dev\chocolatey-test-environment> vagrant provision
==> default: Running provisioner: shell...
    default: Running: shell/PrepareWindows.ps1 as c:\tmp\vagrant-shell.ps1
==> default: IE Enhanced Security Configuration (ESC) has been disabled.
==> default:
==> default: IE first run welcome screen has been disabled.
==> default:
==> default: Setting Windows Update service to Manual startup type.
==> default: Running provisioner: shell...
    default: Running: shell/InstallNet4.ps1 as c:\tmp\vagrant-shell.ps1
==> default: Running provisioner: shell...
    default: Running: shell/InstallChocolatey.ps1 as c:\tmp\vagrant-shell.ps1
==> default: Chocolatey v0.9.10-beta1-151-ga83d5c5
==> default: Nothing to change. Config already set.
==> default: Chocolatey v0.9.10-beta1-151-ga83d5c5
==> default: Nothing to change. Config already set.
==> default: Chocolatey v0.9.10-beta1-151-ga83d5c5
==> default: Nothing to change. Config already set.
==> default: Running provisioner: shell...
    default: Running: shell/NotifyGuiAppsOfEnvironmentChanges.ps1 as c:\tmp\vagrant-shell.ps1
==> default: 1
==> default:
==> default: SUCCESS: Specified value was saved.
==> default: Running provisioner: shell...
    default: Running: inline PowerShell script
The following WinRM command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

powershell -ExecutionPolicy Bypass -OutputFormat Text -file c:\tmp\vagrant-shell.ps1

Stdout from the command:

Stderr from the command:

Vagrant seems to be running a win32 application that returns not 0 after running the shell\NotifyGuiAppsOfEnvironmentChanges.ps1 before running the user SCRIPT block.

Adding setx.exe trigger 1 inside the Vagrantfile SCRIPT block reverts whatever Vagrant is doing.

$packageTestScript = <<SCRIPT
setx.exe trigger 1  # sets $LASTEXITCODE to 0

if ($LASTEXITCODE -ne 0) {
  exit 1
}
SCRIPT
PS C:\dev\chocolatey-test-environment> vagrant provision
==> default: Running provisioner: shell...
    default: Running: shell/PrepareWindows.ps1 as c:\tmp\vagrant-shell.ps1
==> default: IE Enhanced Security Configuration (ESC) has been disabled.
==> default: IE first run welcome screen has been disabled.
==> default: Setting Windows Update service to Manual startup type.
==> default: Running provisioner: shell...
    default: Running: shell/InstallNet4.ps1 as c:\tmp\vagrant-shell.ps1
==> default: Running provisioner: shell...
    default: Running: shell/InstallChocolatey.ps1 as c:\tmp\vagrant-shell.ps1
==> default: Chocolatey v0.9.10-beta1-151-ga83d5c5
==> default: Nothing to change. Config already set.
==> default: Chocolatey v0.9.10-beta1-151-ga83d5c5
==> default: Nothing to change. Config already set.
==> default: Chocolatey v0.9.10-beta1-151-ga83d5c5
==> default: Nothing to change. Config already set.
==> default: Running provisioner: shell...
    default: Running: shell/NotifyGuiAppsOfEnvironmentChanges.ps1 as c:\tmp\vagrant-shell.ps1
==> default: 1
==> default:
==> default: SUCCESS: Specified value was saved.
==> default: Running provisioner: shell...
    default: Running: inline PowerShell script
==> default: SUCCESS: Specified value was saved.
dtgm commented 8 years ago

Also, "SETX writes variables to the master environment in the registry."

It's about 1/5 second faster and probably more reliable to instead do

powershell -NoProfile -Command "exit 0"


NEVERMIND, not faster. setx in virtualbox is nearly instant.

it would actually be about 10x slower to use powershell -NoProfile -Command "exit 0"

dtgm commented 8 years ago

host shell:

(measure-command {setx.exe trigger 1}).milliseconds 
600

(measure-command {powershell -noprofile -command "exit 0"}).milliseconds
350

virtualbox shell:

(measure-command {setx.exe trigger 1}).milliseconds
23

(measure-command {powershell -noprofile -command "exit 0"}).milliseconds
209
ferventcoder commented 8 years ago

That makes sense. But why/how is $LASTEXITCODE being triggered in the script?

Limitation of Vagrant/PowerShell - it doesn't catch errors when calling executables through PowerShell. The check fixes that.