fix tests

This commit is contained in:
Megan Marsh 2019-12-11 16:29:35 -08:00
parent 5d266b20d0
commit 0ca7c9f397
13 changed files with 49 additions and 42 deletions

View File

@ -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")
}

View File

@ -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 {

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)

View File

@ -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 }"`)

View File

@ -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)
}

View File

@ -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)
}
})

View File

@ -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

View File

@ -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")
}

View File

@ -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)
}
}

View File

@ -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
}
```