packer-cn/builder/googlecompute/step_update_gcloud_test.go

86 lines
1.9 KiB
Go
Raw Normal View History

2013-12-13 21:19:21 -05:00
package googlecompute
import (
"strings"
"testing"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
func TestStepUpdateGcloud_impl(t *testing.T) {
var _ multistep.Step = new(StepUpdateGcloud)
2013-12-13 21:19:21 -05:00
}
func TestStepUpdateGcloud(t *testing.T) {
2013-12-13 21:19:21 -05:00
state := testState(t)
step := new(StepUpdateGcloud)
2013-12-13 21:19:21 -05:00
defer step.Cleanup(state)
comm := new(packer.MockCommunicator)
state.Put("communicator", comm)
// run the step
if action := step.Run(state); action != multistep.ActionContinue {
t.Fatalf("bad action: %#v", action)
}
// Verify
if !comm.StartCalled {
t.Fatal("start should be called")
}
if strings.HasPrefix(comm.StartCmd.Command, "sudo") {
t.Fatal("should not sudo")
}
if !strings.Contains(comm.StartCmd.Command, "gcloud -q components update") {
2013-12-13 21:19:21 -05:00
t.Fatalf("bad command: %#v", comm.StartCmd.Command)
}
}
func TestStepUpdateGcloud_badExitStatus(t *testing.T) {
2013-12-13 21:19:21 -05:00
state := testState(t)
step := new(StepUpdateGcloud)
2013-12-13 21:19:21 -05:00
defer step.Cleanup(state)
comm := new(packer.MockCommunicator)
comm.StartExitStatus = 12
state.Put("communicator", comm)
// run the step
if action := step.Run(state); action != multistep.ActionHalt {
t.Fatalf("bad action: %#v", action)
}
if _, ok := state.GetOk("error"); !ok {
t.Fatal("should have error")
}
}
func TestStepUpdateGcloud_nonRoot(t *testing.T) {
2013-12-13 21:19:21 -05:00
state := testState(t)
step := new(StepUpdateGcloud)
2013-12-13 21:19:21 -05:00
defer step.Cleanup(state)
comm := new(packer.MockCommunicator)
state.Put("communicator", comm)
config := state.Get("config").(*Config)
config.SSHUsername = "bob"
// run the step
if action := step.Run(state); action != multistep.ActionContinue {
t.Fatalf("bad action: %#v", action)
}
// Verify
if !comm.StartCalled {
t.Fatal("start should be called")
}
if !strings.HasPrefix(comm.StartCmd.Command, "sudo") {
t.Fatal("should sudo")
}
if !strings.Contains(comm.StartCmd.Command, "gcloud -q components update") {
2013-12-13 21:19:21 -05:00
t.Fatalf("bad command: %#v", comm.StartCmd.Command)
}
}