github.phpd.cn/hashicorp/packer@v1.3.2/builder/docker/step_pull_test.go (about) 1 package docker 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 8 "github.com/hashicorp/packer/helper/multistep" 9 ) 10 11 func TestStepPull_impl(t *testing.T) { 12 var _ multistep.Step = new(StepPull) 13 } 14 15 func TestStepPull(t *testing.T) { 16 state := testState(t) 17 step := new(StepPull) 18 defer step.Cleanup(state) 19 20 config := state.Get("config").(*Config) 21 driver := state.Get("driver").(*MockDriver) 22 23 // run the step 24 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 25 t.Fatalf("bad action: %#v", action) 26 } 27 28 // verify we did the right thing 29 if !driver.PullCalled { 30 t.Fatal("should've pulled") 31 } 32 if driver.PullImage != config.Image { 33 t.Fatalf("bad: %#v", driver.PullImage) 34 } 35 } 36 37 func TestStepPull_error(t *testing.T) { 38 state := testState(t) 39 step := new(StepPull) 40 defer step.Cleanup(state) 41 42 driver := state.Get("driver").(*MockDriver) 43 driver.PullError = errors.New("foo") 44 45 // run the step 46 if action := step.Run(context.Background(), state); action != multistep.ActionHalt { 47 t.Fatalf("bad action: %#v", action) 48 } 49 50 // verify we have an error 51 if _, ok := state.GetOk("error"); !ok { 52 t.Fatal("should have error") 53 } 54 } 55 56 func TestStepPull_login(t *testing.T) { 57 state := testState(t) 58 step := new(StepPull) 59 defer step.Cleanup(state) 60 61 config := state.Get("config").(*Config) 62 driver := state.Get("driver").(*MockDriver) 63 64 config.Login = true 65 66 // run the step 67 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 68 t.Fatalf("bad action: %#v", action) 69 } 70 71 // verify we pulled 72 if !driver.PullCalled { 73 t.Fatal("should've pulled") 74 } 75 76 // verify we logged in 77 if !driver.LoginCalled { 78 t.Fatal("should've logged in") 79 } 80 if !driver.LogoutCalled { 81 t.Fatal("should've logged out") 82 } 83 } 84 85 func TestStepPull_noPull(t *testing.T) { 86 state := testState(t) 87 step := new(StepPull) 88 defer step.Cleanup(state) 89 90 config := state.Get("config").(*Config) 91 config.Pull = false 92 93 driver := state.Get("driver").(*MockDriver) 94 95 // run the step 96 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 97 t.Fatalf("bad action: %#v", action) 98 } 99 100 // verify we did the right thing 101 if driver.PullCalled { 102 t.Fatal("shouldn't have pulled") 103 } 104 }