github.com/dacamp/packer@v0.10.2/provisioner/file/provisioner_test.go (about) 1 package file 2 3 import ( 4 "github.com/mitchellh/packer/packer" 5 "io/ioutil" 6 "os" 7 "strings" 8 "testing" 9 ) 10 11 func testConfig() map[string]interface{} { 12 return map[string]interface{}{ 13 "destination": "something", 14 } 15 } 16 17 func TestProvisioner_Impl(t *testing.T) { 18 var raw interface{} 19 raw = &Provisioner{} 20 if _, ok := raw.(packer.Provisioner); !ok { 21 t.Fatalf("must be a provisioner") 22 } 23 } 24 25 func TestProvisionerPrepare_InvalidKey(t *testing.T) { 26 var p Provisioner 27 config := testConfig() 28 29 // Add a random key 30 config["i_should_not_be_valid"] = true 31 err := p.Prepare(config) 32 if err == nil { 33 t.Fatal("should have error") 34 } 35 } 36 37 func TestProvisionerPrepare_InvalidSource(t *testing.T) { 38 var p Provisioner 39 config := testConfig() 40 config["source"] = "/this/should/not/exist" 41 42 err := p.Prepare(config) 43 if err == nil { 44 t.Fatalf("should require existing file") 45 } 46 } 47 48 func TestProvisionerPrepare_ValidSource(t *testing.T) { 49 var p Provisioner 50 51 tf, err := ioutil.TempFile("", "packer") 52 if err != nil { 53 t.Fatalf("error tempfile: %s", err) 54 } 55 defer os.Remove(tf.Name()) 56 57 config := testConfig() 58 config["source"] = tf.Name() 59 60 err = p.Prepare(config) 61 if err != nil { 62 t.Fatalf("should allow valid file: %s", err) 63 } 64 } 65 66 func TestProvisionerPrepare_EmptyDestination(t *testing.T) { 67 var p Provisioner 68 69 config := testConfig() 70 delete(config, "destination") 71 err := p.Prepare(config) 72 if err == nil { 73 t.Fatalf("should require destination path") 74 } 75 } 76 77 type stubUi struct { 78 sayMessages string 79 } 80 81 func (su *stubUi) Ask(string) (string, error) { 82 return "", nil 83 } 84 85 func (su *stubUi) Error(string) { 86 } 87 88 func (su *stubUi) Machine(string, ...string) { 89 } 90 91 func (su *stubUi) Message(string) { 92 } 93 94 func (su *stubUi) Say(msg string) { 95 su.sayMessages += msg 96 } 97 98 func TestProvisionerProvision_SendsFile(t *testing.T) { 99 var p Provisioner 100 tf, err := ioutil.TempFile("", "packer") 101 if err != nil { 102 t.Fatalf("error tempfile: %s", err) 103 } 104 defer os.Remove(tf.Name()) 105 106 if _, err = tf.Write([]byte("hello")); err != nil { 107 t.Fatalf("error writing tempfile: %s", err) 108 } 109 110 config := map[string]interface{}{ 111 "source": tf.Name(), 112 "destination": "something", 113 } 114 115 if err := p.Prepare(config); err != nil { 116 t.Fatalf("err: %s", err) 117 } 118 119 ui := &stubUi{} 120 comm := &packer.MockCommunicator{} 121 err = p.Provision(ui, comm) 122 if err != nil { 123 t.Fatalf("should successfully provision: %s", err) 124 } 125 126 if !strings.Contains(ui.sayMessages, tf.Name()) { 127 t.Fatalf("should print source filename") 128 } 129 130 if !strings.Contains(ui.sayMessages, "something") { 131 t.Fatalf("should print destination filename") 132 } 133 134 if comm.UploadPath != "something" { 135 t.Fatalf("should upload to configured destination") 136 } 137 138 if comm.UploadData != "hello" { 139 t.Fatalf("should upload with source file's data") 140 } 141 }