From 0ca7c9f3976972627b606c25ed95e52ac3765288 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 11 Dec 2019 16:29:35 -0800 Subject: [PATCH] fix tests --- packer/provisioner_test.go | 10 +++---- packer/rpc/provisioner_test.go | 2 +- provisioner/ansible-local/provisioner_test.go | 4 +-- provisioner/ansible/provisioner_test.go | 2 +- provisioner/file/provisioner_test.go | 2 +- provisioner/powershell/provisioner.go | 26 ++++++++++++------- provisioner/powershell/provisioner_test.go | 15 ++++++----- .../puppet-masterless/provisioner_test.go | 4 +-- provisioner/sleep/provisioner_test.go | 2 +- .../windows-restart/provisioner_test.go | 12 ++++----- provisioner/windows-shell/provisioner_test.go | 8 +++--- template/interpolate/funcs.go | 2 +- .../extending/custom-provisioners.html.md | 2 +- 13 files changed, 49 insertions(+), 42 deletions(-) diff --git a/packer/provisioner_test.go b/packer/provisioner_test.go index 641702428..10e546704 100644 --- a/packer/provisioner_test.go +++ b/packer/provisioner_test.go @@ -114,7 +114,7 @@ func TestPausedProvisionerProvision(t *testing.T) { ui := testUi() comm := new(MockCommunicator) - prov.Provision(context.Background(), ui, comm) + prov.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if !mock.ProvCalled { t.Fatal("prov should be called") } @@ -143,7 +143,7 @@ func TestPausedProvisionerProvision_waits(t *testing.T) { }, } - err := prov.Provision(context.Background(), testUi(), new(MockCommunicator)) + err := prov.Provision(context.Background(), testUi(), new(MockCommunicator), make(map[interface{}]interface{})) if err != nil { t.Fatalf("prov failed: %v", err) @@ -164,7 +164,7 @@ func TestPausedProvisionerCancel(t *testing.T) { return ctx.Err() } - err := prov.Provision(topCtx, testUi(), new(MockCommunicator)) + err := prov.Provision(topCtx, testUi(), new(MockCommunicator), make(map[interface{}]interface{})) if err == nil { t.Fatal("should have err") } @@ -198,7 +198,7 @@ func TestDebuggedProvisionerProvision(t *testing.T) { ui := testUi() comm := new(MockCommunicator) writeReader(ui, "\n") - prov.Provision(context.Background(), ui, comm) + prov.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if !mock.ProvCalled { t.Fatal("prov should be called") } @@ -224,7 +224,7 @@ func TestDebuggedProvisionerCancel(t *testing.T) { return ctx.Err() } - err := prov.Provision(topCtx, testUi(), new(MockCommunicator)) + err := prov.Provision(topCtx, testUi(), new(MockCommunicator), make(map[interface{}]interface{})) if err == nil { t.Fatal("should have error") } diff --git a/packer/rpc/provisioner_test.go b/packer/rpc/provisioner_test.go index bbcbc88d8..61e6ade18 100644 --- a/packer/rpc/provisioner_test.go +++ b/packer/rpc/provisioner_test.go @@ -39,7 +39,7 @@ func TestProvisionerRPC(t *testing.T) { // Test Provision ui := &testUi{} comm := &packer.MockCommunicator{} - if err := pClient.Provision(topCtx, ui, comm); err == nil { + if err := pClient.Provision(topCtx, ui, comm, make(map[interface{}]interface{})); err == nil { t.Fatalf("Provison should have err") } if !p.ProvCalled { diff --git a/provisioner/ansible-local/provisioner_test.go b/provisioner/ansible-local/provisioner_test.go index d2af8abfb..412541a29 100644 --- a/provisioner/ansible-local/provisioner_test.go +++ b/provisioner/ansible-local/provisioner_test.go @@ -134,7 +134,7 @@ func TestProvisionerProvision_PlaybookFiles(t *testing.T) { } comm := &communicatorMock{} - if err := p.Provision(context.Background(), new(packer.NoopUi), comm); err != nil { + if err := p.Provision(context.Background(), new(packer.NoopUi), comm, make(map[interface{}]interface{})); err != nil { t.Fatalf("err: %s", err) } @@ -168,7 +168,7 @@ func TestProvisionerProvision_PlaybookFilesWithPlaybookDir(t *testing.T) { } comm := &communicatorMock{} - if err := p.Provision(context.Background(), new(packer.NoopUi), comm); err != nil { + if err := p.Provision(context.Background(), new(packer.NoopUi), comm, make(map[interface{}]interface{})); err != nil { t.Fatalf("err: %s", err) } diff --git a/provisioner/ansible/provisioner_test.go b/provisioner/ansible/provisioner_test.go index 27fa27dc7..6310fbcba 100644 --- a/provisioner/ansible/provisioner_test.go +++ b/provisioner/ansible/provisioner_test.go @@ -349,7 +349,7 @@ func TestAnsibleLongMessages(t *testing.T) { Writer: new(bytes.Buffer), } - err = p.Provision(context.Background(), ui, comm) + err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err != nil { t.Fatalf("err: %s", err) } diff --git a/provisioner/file/provisioner_test.go b/provisioner/file/provisioner_test.go index 46bccca6d..6f229d512 100644 --- a/provisioner/file/provisioner_test.go +++ b/provisioner/file/provisioner_test.go @@ -127,7 +127,7 @@ func TestProvisionerProvision_SendsFile(t *testing.T) { Writer: b, } comm := &packer.MockCommunicator{} - err = p.Provision(context.Background(), ui, comm) + err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err != nil { t.Fatalf("should successfully provision: %s", err) } diff --git a/provisioner/powershell/provisioner.go b/provisioner/powershell/provisioner.go index 40b29c07a..88c07702f 100644 --- a/provisioner/powershell/provisioner.go +++ b/provisioner/powershell/provisioner.go @@ -223,19 +223,25 @@ func extractScript(p *Provisioner) (string, error) { func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, generatedData interface{}) error { ui.Say(fmt.Sprintf("Provisioning with Powershell...")) p.communicator = comm - p.generatedData = map[string]interface{}{} - for key, val := range generatedData.(map[interface{}]interface{}) { - keyString, ok := key.(string) - if ok { - p.generatedData[keyString] = val - } else { - log.Printf("Error casting generated data key to a string.") + p.generatedData = make(map[string]interface{}) + // test that generatedData is a map, not an empty interface. + dataMap, ok := generatedData.(map[interface{}]interface{}) + if ok { + for key, val := range dataMap { + keyString, ok := key.(string) + if ok { + p.generatedData[keyString] = val + } else { + log.Printf("Error casting generated data key to a string.") + } } + if winRMPass, ok := p.generatedData["WinRMPassword"]; ok { + packer.LogSecretFilter.Set(winRMPass.(string)) + } + } else { + log.Printf("error reading generated data from builder") } - winRMPass := p.generatedData["WinRMPassword"] - packer.LogSecretFilter.Set(winRMPass.(string)) - scripts := make([]string, len(p.config.Scripts)) copy(scripts, p.config.Scripts) diff --git a/provisioner/powershell/provisioner_test.go b/provisioner/powershell/provisioner_test.go index cef4611d1..07bbd8815 100644 --- a/provisioner/powershell/provisioner_test.go +++ b/provisioner/powershell/provisioner_test.go @@ -355,7 +355,7 @@ func TestProvisionerProvision_ValidExitCodes(t *testing.T) { comm := new(packer.MockCommunicator) comm.StartExitStatus = 200 p.Prepare(config) - err := p.Provision(context.Background(), ui, comm) + err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err != nil { t.Fatal("should not have error") } @@ -378,7 +378,7 @@ func TestProvisionerProvision_InvalidExitCodes(t *testing.T) { comm := new(packer.MockCommunicator) comm.StartExitStatus = 201 // Invalid! p.Prepare(config) - err := p.Provision(context.Background(), ui, comm) + err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err == nil { t.Fatal("should have error") } @@ -399,7 +399,7 @@ func TestProvisionerProvision_Inline(t *testing.T) { p.config.PackerBuilderType = "iso" comm := new(packer.MockCommunicator) p.Prepare(config) - err := p.Provision(context.Background(), ui, comm) + err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err != nil { t.Fatal("should not have error") } @@ -419,7 +419,7 @@ func TestProvisionerProvision_Inline(t *testing.T) { config["remote_path"] = "c:/Windows/Temp/inlineScript.ps1" p.Prepare(config) - err = p.Provision(context.Background(), ui, comm) + err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err != nil { t.Fatal("should not have error") } @@ -448,7 +448,7 @@ func TestProvisionerProvision_Scripts(t *testing.T) { p := new(Provisioner) comm := new(packer.MockCommunicator) p.Prepare(config) - err := p.Provision(context.Background(), ui, comm) + err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err != nil { t.Fatal("should not have error") } @@ -484,7 +484,7 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) { p := new(Provisioner) comm := new(packer.MockCommunicator) p.Prepare(config) - err := p.Provision(context.Background(), ui, comm) + err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err != nil { t.Fatal("should not have error") } @@ -511,7 +511,7 @@ func TestProvisionerProvision_UploadFails(t *testing.T) { comm := new(packer.ScriptUploadErrorMockCommunicator) p.Prepare(config) p.config.StartRetryTimeout = 1 * time.Second - err := p.Provision(context.Background(), ui, comm) + err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if !strings.Contains(err.Error(), packer.ScriptUploadErrorMockCommunicatorError.Error()) { t.Fatalf("expected Provision() error %q to contain %q", err.Error(), @@ -621,6 +621,7 @@ func TestProvision_createCommandText(t *testing.T) { p.config.PackerBuilderType = "iso" // Non-elevated + p.generatedData = make(map[string]interface{}) cmd, _ := p.createCommandText() re := regexp.MustCompile(`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 }"`) diff --git a/provisioner/puppet-masterless/provisioner_test.go b/provisioner/puppet-masterless/provisioner_test.go index f8a0ae782..cd01cec32 100644 --- a/provisioner/puppet-masterless/provisioner_test.go +++ b/provisioner/puppet-masterless/provisioner_test.go @@ -494,7 +494,7 @@ func TestProvisionerProvision_extraArguments(t *testing.T) { t.Fatalf("err: %s", err) } - err = p.Provision(context.Background(), ui, comm) + err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err != nil { t.Fatalf("err: %s", err) } @@ -514,7 +514,7 @@ func TestProvisionerProvision_extraArguments(t *testing.T) { t.Fatalf("err: %s", err) } - err = p.Provision(context.Background(), ui, comm) + err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err != nil { t.Fatalf("err: %s", err) } diff --git a/provisioner/sleep/provisioner_test.go b/provisioner/sleep/provisioner_test.go index 6ec26c59e..3a0d1bd28 100644 --- a/provisioner/sleep/provisioner_test.go +++ b/provisioner/sleep/provisioner_test.go @@ -48,7 +48,7 @@ func TestProvisioner_Provision(t *testing.T) { p := &Provisioner{ Duration: tt.fields.Duration, } - if err := p.Provision(tt.args.ctx, nil, nil); (err != nil) != tt.wantErr { + if err := p.Provision(tt.args.ctx, nil, nil, make(map[interface{}]interface{})); (err != nil) != tt.wantErr { t.Errorf("Provisioner.Provision() error = %v, wantErr %v", err, tt.wantErr) } }) diff --git a/provisioner/windows-restart/provisioner_test.go b/provisioner/windows-restart/provisioner_test.go index ffe9c48a2..0a2eda530 100644 --- a/provisioner/windows-restart/provisioner_test.go +++ b/provisioner/windows-restart/provisioner_test.go @@ -104,7 +104,7 @@ func TestProvisionerProvision_Success(t *testing.T) { waitForRestart = func(context.Context, *Provisioner, packer.Communicator) error { return nil } - err := p.Provision(context.Background(), ui, comm) + err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err != nil { t.Fatal("should not have error") } @@ -140,7 +140,7 @@ func TestProvisionerProvision_CustomCommand(t *testing.T) { waitForRestart = func(context.Context, *Provisioner, packer.Communicator) error { return nil } - err := p.Provision(context.Background(), ui, comm) + err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err != nil { t.Fatal("should not have error") } @@ -163,7 +163,7 @@ func TestProvisionerProvision_RestartCommandFail(t *testing.T) { comm.StartExitStatus = 1 p.Prepare(config) - err := p.Provision(context.Background(), ui, comm) + err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err == nil { t.Fatal("should have error") } @@ -182,7 +182,7 @@ func TestProvisionerProvision_WaitForRestartFail(t *testing.T) { waitForCommunicator = func(context.Context, *Provisioner) error { return fmt.Errorf("Machine did not restart properly") } - err := p.Provision(context.Background(), ui, comm) + err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err == nil { t.Fatal("should have error") } @@ -216,7 +216,7 @@ func TestProvision_waitForRestartTimeout(t *testing.T) { } go func() { - err = p.Provision(context.Background(), ui, comm) + err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) waitDone <- true }() <-waitContinue @@ -327,7 +327,7 @@ func TestProvision_Cancel(t *testing.T) { // Create two go routines to provision and cancel in parallel // Provision will block until cancel happens go func() { - done <- p.Provision(topCtx, ui, comm) + done <- p.Provision(topCtx, ui, comm, make(map[interface{}]interface{})) }() // Expect interrupt error diff --git a/provisioner/windows-shell/provisioner_test.go b/provisioner/windows-shell/provisioner_test.go index 32ec528e1..b9ce1cf05 100644 --- a/provisioner/windows-shell/provisioner_test.go +++ b/provisioner/windows-shell/provisioner_test.go @@ -292,7 +292,7 @@ func TestProvisionerProvision_Inline(t *testing.T) { p.config.PackerBuilderType = "iso" comm := new(packer.MockCommunicator) p.Prepare(config) - err := p.Provision(context.Background(), ui, comm) + err := p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err != nil { t.Fatal("should not have error") } @@ -311,7 +311,7 @@ func TestProvisionerProvision_Inline(t *testing.T) { config["remote_path"] = "c:/Windows/Temp/inlineScript.bat" p.Prepare(config) - err = p.Provision(context.Background(), ui, comm) + err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err != nil { t.Fatal("should not have error") } @@ -342,7 +342,7 @@ func TestProvisionerProvision_Scripts(t *testing.T) { p := new(Provisioner) comm := new(packer.MockCommunicator) p.Prepare(config) - err = p.Provision(context.Background(), ui, comm) + err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err != nil { t.Fatal("should not have error") } @@ -381,7 +381,7 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) { p := new(Provisioner) comm := new(packer.MockCommunicator) p.Prepare(config) - err = p.Provision(context.Background(), ui, comm) + err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{})) if err != nil { t.Fatal("should not have error") } diff --git a/template/interpolate/funcs.go b/template/interpolate/funcs.go index 341c4630b..a3a8e36ee 100644 --- a/template/interpolate/funcs.go +++ b/template/interpolate/funcs.go @@ -174,7 +174,7 @@ func funcGenGenerated(ctx *Context) interface{} { if heldPlace, ok := data[s]; ok { return heldPlace, nil } else { - return "", fmt.Errorf("loaded data, but couldnt find winrm in it", s) + return "", fmt.Errorf("loaded data, but couldnt find %s in it", s) } } diff --git a/website/source/docs/extending/custom-provisioners.html.md b/website/source/docs/extending/custom-provisioners.html.md index 3d3a1721d..7cfd55939 100644 --- a/website/source/docs/extending/custom-provisioners.html.md +++ b/website/source/docs/extending/custom-provisioners.html.md @@ -36,7 +36,7 @@ explaining what each method should do. ``` go type Provisioner interface { Prepare(...interface{}) error - Provision(Ui, Communicator) error + Provision(Ctx, Ui, Communicator, new(interface{})) error } ```