From cff060a4904c4e907900c6348b63b5bad05240be Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 9 Nov 2013 17:21:24 -0800 Subject: [PATCH] builder/docker: ability to disable pull --- builder/docker/config.go | 14 ++++++++ builder/docker/config_test.go | 62 ++++++++++++++++++++------------ builder/docker/step_pull.go | 6 ++++ builder/docker/step_pull_test.go | 21 +++++++++++ 4 files changed, 81 insertions(+), 22 deletions(-) diff --git a/builder/docker/config.go b/builder/docker/config.go index e62930fd2..864ad1fee 100644 --- a/builder/docker/config.go +++ b/builder/docker/config.go @@ -11,6 +11,7 @@ type Config struct { ExportPath string `mapstructure:"export_path"` Image string + Pull bool tpl *packer.ConfigTemplate } @@ -27,6 +28,19 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { return nil, nil, err } + // Default Pull if it wasn't set + hasPull := false + for _, k := range md.Keys { + if k == "Pull" { + hasPull = true + break + } + } + + if !hasPull { + c.Pull = true + } + errs := common.CheckUnusedConfig(md) templates := map[string]*string{ diff --git a/builder/docker/config_test.go b/builder/docker/config_test.go index d26c21973..3b279ab22 100644 --- a/builder/docker/config_test.go +++ b/builder/docker/config_test.go @@ -23,50 +23,68 @@ func testConfigStruct(t *testing.T) *Config { return c } -func TestConfigPrepare_exportPath(t *testing.T) { - raw := testConfig() - - // No export path - delete(raw, "export_path") - _, warns, errs := NewConfig(raw) +func testConfigErr(t *testing.T, warns []string, err error) { if len(warns) > 0 { t.Fatalf("bad: %#v", warns) } - if errs == nil { + if err == nil { t.Fatal("should error") } +} - // Good export path - raw["export_path"] = "good" - _, warns, errs = NewConfig(raw) +func testConfigOk(t *testing.T, warns []string, err error) { if len(warns) > 0 { t.Fatalf("bad: %#v", warns) } - if errs != nil { - t.Fatalf("bad: %s", errs) + if err != nil { + t.Fatalf("bad: %s", err) } } +func TestConfigPrepare_exportPath(t *testing.T) { + raw := testConfig() + + // No export path + delete(raw, "export_path") + _, warns, errs := NewConfig(raw) + testConfigErr(t, warns, errs) + + // Good export path + raw["export_path"] = "good" + _, warns, errs = NewConfig(raw) + testConfigOk(t, warns, errs) +} + func TestConfigPrepare_image(t *testing.T) { raw := testConfig() // No image delete(raw, "image") _, warns, errs := NewConfig(raw) - if len(warns) > 0 { - t.Fatalf("bad: %#v", warns) - } - if errs == nil { - t.Fatal("should error") - } + testConfigErr(t, warns, errs) // Good image raw["image"] = "path" _, warns, errs = NewConfig(raw) - if len(warns) > 0 { - t.Fatalf("bad: %#v", warns) + testConfigOk(t, warns, errs) +} + +func TestConfigPrepare_pull(t *testing.T) { + raw := testConfig() + + // No pull set + delete(raw, "pull") + c, warns, errs := NewConfig(raw) + testConfigOk(t, warns, errs) + if !c.Pull { + t.Fatal("should pull by default") } - if errs != nil { - t.Fatalf("bad: %s", errs) + + // Pull set + raw["pull"] = false + c, warns, errs = NewConfig(raw) + testConfigOk(t, warns, errs) + if c.Pull { + t.Fatal("should not pull") } } diff --git a/builder/docker/step_pull.go b/builder/docker/step_pull.go index 19e5a69ab..6571a6563 100644 --- a/builder/docker/step_pull.go +++ b/builder/docker/step_pull.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" + "log" ) type StepPull struct{} @@ -13,6 +14,11 @@ func (s *StepPull) Run(state multistep.StateBag) multistep.StepAction { driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) + if !config.Pull { + log.Println("Pull disabled, won't docker pull") + return multistep.ActionContinue + } + ui.Say(fmt.Sprintf("Pulling Docker image: %s", config.Image)) if err := driver.Pull(config.Image); err != nil { err := fmt.Errorf("Error pulling Docker image: %s", err) diff --git a/builder/docker/step_pull_test.go b/builder/docker/step_pull_test.go index 93e9cf830..5c66425bb 100644 --- a/builder/docker/step_pull_test.go +++ b/builder/docker/step_pull_test.go @@ -50,3 +50,24 @@ func TestStepPull_error(t *testing.T) { t.Fatal("should have error") } } + +func TestStepPull_noPull(t *testing.T) { + state := testState(t) + step := new(StepPull) + defer step.Cleanup(state) + + config := state.Get("config").(*Config) + config.Pull = false + + driver := state.Get("driver").(*MockDriver) + + // run the step + if action := step.Run(state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v", action) + } + + // verify we did the right thing + if driver.PullCalled { + t.Fatal("shouldn't have pulled") + } +}