@ -15,16 +15,6 @@ import (
"github.com/stretchr/testify/assert"
)
func testConfig ( ) map [ string ] interface { } {
return map [ string ] interface { } {
"inline" : [ ] interface { } { "foo" , "bar" } ,
}
}
func init ( ) {
//log.SetOutput(ioutil.Discard)
}
func TestProvisionerPrepare_extractScript ( t * testing . T ) {
config := testConfig ( )
p := new ( Provisioner )
@ -335,11 +325,6 @@ func testUi() *packer.BasicUi {
}
}
func testObjects ( ) ( packer . Ui , packer . Communicator ) {
ui := testUi ( )
return ui , new ( packer . MockCommunicator )
}
func TestProvisionerProvision_ValidExitCodes ( t * testing . T ) {
config := testConfig ( )
delete ( config , "inline" )
@ -387,7 +372,8 @@ func TestProvisionerProvision_InvalidExitCodes(t *testing.T) {
}
func TestProvisionerProvision_Inline ( t * testing . T ) {
config := testConfig ( )
// skip_clean is set to true otherwise the last command executed by the provisioner is the cleanup.
config := testConfigWithSkipClean ( )
delete ( config , "inline" )
// Defaults provided by Packer
@ -400,7 +386,7 @@ func TestProvisionerProvision_Inline(t *testing.T) {
p . config . PackerBuildName = "vmware"
p . config . PackerBuilderType = "iso"
comm := new ( packer . MockCommunicator )
p. Prepare ( config )
_ = p. Prepare ( config )
err := p . Provision ( context . Background ( ) , ui , comm , make ( map [ string ] interface { } ) )
if err != nil {
t . Fatal ( "should not have error" )
@ -439,7 +425,8 @@ func TestProvisionerProvision_Scripts(t *testing.T) {
defer os . Remove ( tempFile . Name ( ) )
defer tempFile . Close ( )
config := testConfig ( )
// skip_clean is set to true otherwise the last command executed by the provisioner is the cleanup.
config := testConfigWithSkipClean ( )
delete ( config , "inline" )
config [ "scripts" ] = [ ] string { tempFile . Name ( ) }
config [ "packer_build_name" ] = "foobuild"
@ -465,11 +452,12 @@ func TestProvisionerProvision_Scripts(t *testing.T) {
func TestProvisionerProvision_ScriptsWithEnvVars ( t * testing . T ) {
tempFile , _ := ioutil . TempFile ( "" , "packer" )
config := testConfig ( )
ui := testUi ( )
defer os . Remove ( tempFile . Name ( ) )
defer tempFile . Close ( )
// skip_clean is set to true otherwise the last command executed by the provisioner is the cleanup.
config := testConfigWithSkipClean ( )
delete ( config , "inline" )
config [ "scripts" ] = [ ] string { tempFile . Name ( ) }
@ -499,10 +487,56 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) {
}
}
func TestProvisionerProvision_UISlurp ( t * testing . T ) {
// UI should be called n times
func TestProvisionerProvision_SkipClean ( t * testing . T ) {
tempFile , _ := ioutil . TempFile ( "" , "packer" )
defer func ( ) {
tempFile . Close ( )
os . Remove ( tempFile . Name ( ) )
} ( )
// UI should receive following messages / output
config := map [ string ] interface { } {
"scripts" : [ ] string { tempFile . Name ( ) } ,
"remote_path" : "c:/Windows/Temp/script.ps1" ,
}
tt := [ ] struct {
SkipClean bool
LastExecutedCommandRegex string
} {
{
SkipClean : true ,
LastExecutedCommandRegex : ` powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\) { set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]] { 8}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 12}\.ps1; &'c:/Windows/Temp/script.ps1'; exit \$LastExitCode }" ` ,
} ,
{
SkipClean : false ,
LastExecutedCommandRegex : ` powershell -executionpolicy bypass "& { if \(Test-Path variable:global:ProgressPreference\) { set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};\. c:/Windows/Temp/packer-ps-env-vars-[[:alnum:]] { 8}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 12}\.ps1; &'c:/Windows/Temp/packer-cleanup-[[:alnum:]] { 8}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 4}-[[:alnum:]] { 12}\.ps1'; exit \$LastExitCode }" ` ,
} ,
}
for _ , tc := range tt {
tc := tc
p := new ( Provisioner )
ui := testUi ( )
comm := new ( packer . MockCommunicator )
config [ "skip_clean" ] = tc . SkipClean
if err := p . Prepare ( config ) ; err != nil {
t . Fatalf ( "failed to prepare config when SkipClean is %t: %s" , tc . SkipClean , err )
}
err := p . Provision ( context . Background ( ) , ui , comm , make ( map [ string ] interface { } ) )
if err != nil {
t . Fatal ( "should not have error" )
}
// When SkipClean is false the last executed command should be the clean up command;
// otherwise it will be the execution command for the provisioning script.
cmd := comm . StartCmd . Command
re := regexp . MustCompile ( tc . LastExecutedCommandRegex )
matched := re . MatchString ( cmd )
if ! matched {
t . Fatalf ( ` Got unexpected command when SkipClean is %t: %s ` , tc . SkipClean , cmd )
}
}
}
func TestProvisionerProvision_UploadFails ( t * testing . T ) {
@ -771,3 +805,16 @@ func TestCancel(t *testing.T) {
// Don't actually call Cancel() as it performs an os.Exit(0)
// which kills the 'go test' tool
}
func testConfig ( ) map [ string ] interface { } {
return map [ string ] interface { } {
"inline" : [ ] interface { } { "foo" , "bar" } ,
}
}
func testConfigWithSkipClean ( ) map [ string ] interface { } {
return map [ string ] interface { } {
"inline" : [ ] interface { } { "foo" , "bar" } ,
"skip_clean" : true ,
}
}