github.com/rothwerx/packer@v0.9.0/builder/docker/step_pull_test.go (about)

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