diff --git a/provisioner/chef-client/provisioner_test.go b/provisioner/chef-client/provisioner_test.go index b6a5a9d2e..934403040 100644 --- a/provisioner/chef-client/provisioner_test.go +++ b/provisioner/chef-client/provisioner_test.go @@ -1,8 +1,10 @@ package chefclient import ( + "bytes" "io/ioutil" "os" + "strings" "testing" "github.com/mitchellh/packer/packer" @@ -135,3 +137,55 @@ func TestProvisionerPrepare_serverUrl(t *testing.T) { t.Fatalf("err: %s", err) } } + +func TestProvisioner_createDir(t *testing.T) { + p1 := &Provisioner{config: Config{PreventSudo: true}} + p2 := &Provisioner{config: Config{PreventSudo: false}} + comm := &packer.MockCommunicator{} + ui := &packer.BasicUi{ + Reader: new(bytes.Buffer), + Writer: new(bytes.Buffer), + } + + if err := p1.createDir(ui, comm, "/tmp/foo"); err != nil { + t.Fatalf("err: %s", err) + } + + if strings.HasPrefix(comm.StartCmd.Command, "sudo") { + t.Fatalf("createDir should not use sudo, got: \"%s\"", comm.StartCmd.Command) + } + + if err := p2.createDir(ui, comm, "/tmp/foo"); err != nil { + t.Fatalf("err: %s", err) + } + + if !strings.HasPrefix(comm.StartCmd.Command, "sudo") { + t.Fatalf("createDir should use sudo, got: \"%s\"", comm.StartCmd.Command) + } +} + +func TestProvisioner_removeDir(t *testing.T) { + p1 := &Provisioner{config: Config{PreventSudo: true}} + p2 := &Provisioner{config: Config{PreventSudo: false}} + comm := &packer.MockCommunicator{} + ui := &packer.BasicUi{ + Reader: new(bytes.Buffer), + Writer: new(bytes.Buffer), + } + + if err := p1.removeDir(ui, comm, "/tmp/foo"); err != nil { + t.Fatalf("err: %s", err) + } + + if strings.HasPrefix(comm.StartCmd.Command, "sudo") { + t.Fatalf("removeDir should not use sudo, got: \"%s\"", comm.StartCmd.Command) + } + + if err := p2.removeDir(ui, comm, "/tmp/foo"); err != nil { + t.Fatalf("err: %s", err) + } + + if !strings.HasPrefix(comm.StartCmd.Command, "sudo") { + t.Fatalf("removeDir should use sudo, got: \"%s\"", comm.StartCmd.Command) + } +}