Fix/add tests

This commit is contained in:
Matt Dainty 2018-12-07 16:23:03 +00:00
parent 3585b8023f
commit 19bd28cd72
3 changed files with 50 additions and 24 deletions

View File

@ -34,3 +34,15 @@ func (t *MockProvisioner) Provision(ui Ui, comm Communicator) error {
func (t *MockProvisioner) Cancel() { func (t *MockProvisioner) Cancel() {
t.CancelCalled = true t.CancelCalled = true
} }
func (t *MockProvisioner) Communicator() Communicator {
return t.ProvCommunicator
}
func (t *MockProvisioner) ElevatedUser() string {
return "user"
}
func (t *MockProvisioner) ElevatedPassword() string {
return "password"
}

View File

@ -0,0 +1,38 @@
package provisioner
import (
"regexp"
"testing"
"github.com/hashicorp/packer/packer"
)
func testConfig() map[string]interface{} {
return map[string]interface{}{
"inline": []interface{}{"foo", "bar"},
}
}
func TestProvisioner_GenerateElevatedRunner(t *testing.T) {
// Non-elevated
config := testConfig()
p := new(packer.MockProvisioner)
p.Prepare(config)
comm := new(packer.MockCommunicator)
p.ProvCommunicator = comm
path, err := GenerateElevatedRunner("whoami", p)
if err != nil {
t.Fatalf("Did not expect error: %s", err.Error())
}
if comm.UploadCalled != true {
t.Fatalf("Should have uploaded file")
}
matched, _ := regexp.MatchString("C:/Windows/Temp/packer-elevated-shell.*", path)
if !matched {
t.Fatalf("Got unexpected file: %s", path)
}
}

View File

@ -654,30 +654,6 @@ func TestProvision_uploadEnvVars(t *testing.T) {
} }
} }
func TestProvision_generateElevatedShellRunner(t *testing.T) {
// Non-elevated
config := testConfig()
p := new(Provisioner)
p.Prepare(config)
comm := new(packer.MockCommunicator)
p.communicator = comm
path, err := p.generateElevatedRunner("whoami")
if err != nil {
t.Fatalf("Did not expect error: %s", err.Error())
}
if comm.UploadCalled != true {
t.Fatalf("Should have uploaded file")
}
matched, _ := regexp.MatchString("C:/Windows/Temp/packer-elevated-shell.*", path)
if !matched {
t.Fatalf("Got unexpected file: %s", path)
}
}
func TestRetryable(t *testing.T) { func TestRetryable(t *testing.T) {
config := testConfig() config := testConfig()