github.com/sneal/packer@v0.5.2/builder/docker/step_export_test.go (about) 1 package docker 2 3 import ( 4 "bytes" 5 "errors" 6 "github.com/mitchellh/multistep" 7 "io/ioutil" 8 "os" 9 "testing" 10 ) 11 12 func testStepExportState(t *testing.T) multistep.StateBag { 13 state := testState(t) 14 state.Put("container_id", "foo") 15 return state 16 } 17 18 func TestStepExport_impl(t *testing.T) { 19 var _ multistep.Step = new(StepExport) 20 } 21 22 func TestStepExport(t *testing.T) { 23 state := testStepExportState(t) 24 step := new(StepExport) 25 defer step.Cleanup(state) 26 27 // Create a tempfile for our output path 28 tf, err := ioutil.TempFile("", "packer") 29 if err != nil { 30 t.Fatalf("err: %s", err) 31 } 32 tf.Close() 33 defer os.Remove(tf.Name()) 34 35 config := state.Get("config").(*Config) 36 config.ExportPath = tf.Name() 37 driver := state.Get("driver").(*MockDriver) 38 driver.ExportReader = bytes.NewReader([]byte("data!")) 39 40 // run the step 41 if action := step.Run(state); action != multistep.ActionContinue { 42 t.Fatalf("bad action: %#v", action) 43 } 44 45 // verify we did the right thing 46 if !driver.ExportCalled { 47 t.Fatal("should've exported") 48 } 49 if driver.ExportID != "foo" { 50 t.Fatalf("bad: %#v", driver.ExportID) 51 } 52 53 // verify the data exported to the file 54 contents, err := ioutil.ReadFile(tf.Name()) 55 if err != nil { 56 t.Fatalf("err: %s", err) 57 } 58 59 if string(contents) != "data!" { 60 t.Fatalf("bad: %#v", string(contents)) 61 } 62 } 63 64 func TestStepExport_error(t *testing.T) { 65 state := testStepExportState(t) 66 step := new(StepExport) 67 defer step.Cleanup(state) 68 69 // Create a tempfile for our output path 70 tf, err := ioutil.TempFile("", "packer") 71 if err != nil { 72 t.Fatalf("err: %s", err) 73 } 74 tf.Close() 75 76 if err := os.Remove(tf.Name()); err != nil { 77 t.Fatalf("err: %s", err) 78 } 79 80 config := state.Get("config").(*Config) 81 config.ExportPath = tf.Name() 82 driver := state.Get("driver").(*MockDriver) 83 driver.ExportError = errors.New("foo") 84 85 // run the step 86 if action := step.Run(state); action != multistep.ActionHalt { 87 t.Fatalf("bad action: %#v", action) 88 } 89 90 // verify we have an error 91 if _, ok := state.GetOk("error"); !ok { 92 t.Fatal("should have error") 93 } 94 95 // verify we didn't make that file 96 if _, err := os.Stat(tf.Name()); err == nil { 97 t.Fatal("export path shouldn't exist") 98 } 99 }