packer-cn/builder/googlecompute/step_create_image_test.go

75 lines
1.7 KiB
Go
Raw Normal View History

2013-12-13 21:26:00 -05:00
package googlecompute
import (
"errors"
2013-12-13 21:26:00 -05:00
"testing"
"github.com/mitchellh/multistep"
)
func TestStepCreateImage_impl(t *testing.T) {
var _ multistep.Step = new(StepCreateImage)
}
func TestStepCreateImage(t *testing.T) {
state := testState(t)
step := new(StepCreateImage)
defer step.Cleanup(state)
config := state.Get("config").(*Config)
driver := state.Get("driver").(*DriverMock)
2013-12-13 21:26:00 -05:00
// run the step
if action := step.Run(state); action != multistep.ActionContinue {
t.Fatalf("bad action: %#v", action)
}
// Verify state
if driver.CreateImageName != config.ImageName {
t.Fatalf("bad: %#v", driver.CreateImageName)
}
if driver.CreateImageDesc != config.ImageDescription {
t.Fatalf("bad: %#v", driver.CreateImageDesc)
2013-12-13 21:26:00 -05:00
}
if driver.CreateImageZone != config.Zone {
t.Fatalf("bad: %#v", driver.CreateImageZone)
2013-12-13 21:26:00 -05:00
}
if driver.CreateImageDisk != config.DiskName {
t.Fatalf("bad: %#v", driver.CreateImageDisk)
2013-12-13 21:26:00 -05:00
}
nameRaw, ok := state.GetOk("image_name")
if !ok {
t.Fatal("should have name")
}
if name, ok := nameRaw.(string); !ok {
t.Fatal("name is not a string")
} else if name != config.ImageName {
t.Fatalf("bad name: %s", name)
2013-12-13 21:26:00 -05:00
}
}
func TestStepCreateImage_errorOnChannel(t *testing.T) {
2013-12-13 21:26:00 -05:00
state := testState(t)
step := new(StepCreateImage)
defer step.Cleanup(state)
errCh := make(chan error, 1)
errCh <- errors.New("error")
driver := state.Get("driver").(*DriverMock)
driver.CreateImageErrCh = errCh
2013-12-13 21:26:00 -05:00
// 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")
}
if _, ok := state.GetOk("image_name"); ok {
2013-12-13 21:26:00 -05:00
t.Fatal("should NOT have image")
}
}