2013-07-02 22:11:30 -04:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
2018-09-06 09:52:28 -04:00
|
|
|
"bytes"
|
2019-03-19 13:11:19 -04:00
|
|
|
"context"
|
2013-07-02 22:11:30 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-07-06 16:42:26 -04:00
|
|
|
"path/filepath"
|
2013-07-02 22:11:30 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2016-07-06 16:42:26 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-07-02 22:11:30 -04:00
|
|
|
)
|
|
|
|
|
2013-07-04 15:50:00 -04:00
|
|
|
func testConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"destination": "something",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-02 22:11:30 -04:00
|
|
|
func TestProvisioner_Impl(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &Provisioner{}
|
|
|
|
if _, ok := raw.(packer.Provisioner); !ok {
|
|
|
|
t.Fatalf("must be a provisioner")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-13 20:28:56 -04:00
|
|
|
func TestProvisionerPrepare_InvalidKey(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Add a random key
|
|
|
|
config["i_should_not_be_valid"] = true
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-02 22:11:30 -04:00
|
|
|
func TestProvisionerPrepare_InvalidSource(t *testing.T) {
|
|
|
|
var p Provisioner
|
2013-07-04 15:50:00 -04:00
|
|
|
config := testConfig()
|
|
|
|
config["source"] = "/this/should/not/exist"
|
2013-07-02 22:11:30 -04:00
|
|
|
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should require existing file")
|
|
|
|
}
|
2016-09-15 16:15:56 -04:00
|
|
|
|
|
|
|
config["generated"] = false
|
|
|
|
err = p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should required existing file")
|
|
|
|
}
|
2013-07-02 22:11:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare_ValidSource(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error tempfile: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
2013-07-04 15:50:00 -04:00
|
|
|
config := testConfig()
|
|
|
|
config["source"] = tf.Name()
|
2016-09-15 16:15:56 -04:00
|
|
|
err = p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should allow valid file: %s", err)
|
|
|
|
}
|
2013-07-02 22:11:30 -04:00
|
|
|
|
2016-09-15 16:15:56 -04:00
|
|
|
config["generated"] = false
|
2013-07-04 15:50:00 -04:00
|
|
|
err = p.Prepare(config)
|
2013-07-02 22:11:30 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should allow valid file: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-15 16:15:56 -04:00
|
|
|
func TestProvisionerPrepare_GeneratedSource(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
|
|
|
|
config := testConfig()
|
|
|
|
config["source"] = "/this/should/not/exist"
|
|
|
|
config["generated"] = true
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should allow non-existing file: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-02 22:11:30 -04:00
|
|
|
func TestProvisionerPrepare_EmptyDestination(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
|
2013-07-04 15:50:00 -04:00
|
|
|
config := testConfig()
|
|
|
|
delete(config, "destination")
|
2013-07-02 22:11:30 -04:00
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should require destination path")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerProvision_SendsFile(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error tempfile: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
2013-07-04 15:50:00 -04:00
|
|
|
|
2013-07-02 22:11:30 -04:00
|
|
|
if _, err = tf.Write([]byte("hello")); err != nil {
|
|
|
|
t.Fatalf("error writing tempfile: %s", err)
|
|
|
|
}
|
2013-07-04 15:50:00 -04:00
|
|
|
|
|
|
|
config := map[string]interface{}{
|
2013-07-05 14:00:18 -04:00
|
|
|
"source": tf.Name(),
|
2013-07-04 15:50:00 -04:00
|
|
|
"destination": "something",
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.Prepare(config); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2013-07-02 22:11:30 -04:00
|
|
|
|
2018-09-06 09:52:28 -04:00
|
|
|
b := bytes.NewBuffer(nil)
|
|
|
|
ui := &packer.BasicUi{
|
|
|
|
Writer: b,
|
|
|
|
}
|
2013-08-25 23:29:50 -04:00
|
|
|
comm := &packer.MockCommunicator{}
|
2019-12-11 19:29:35 -05:00
|
|
|
err = p.Provision(context.Background(), ui, comm, make(map[interface{}]interface{}))
|
2013-07-02 22:11:30 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should successfully provision: %s", err)
|
|
|
|
}
|
2013-07-04 15:50:00 -04:00
|
|
|
|
2018-09-06 09:52:28 -04:00
|
|
|
if !strings.Contains(b.String(), tf.Name()) {
|
2013-07-02 22:11:30 -04:00
|
|
|
t.Fatalf("should print source filename")
|
|
|
|
}
|
2013-07-04 15:50:00 -04:00
|
|
|
|
2018-09-06 09:52:28 -04:00
|
|
|
if !strings.Contains(b.String(), "something") {
|
2013-07-02 22:11:30 -04:00
|
|
|
t.Fatalf("should print destination filename")
|
|
|
|
}
|
2013-07-04 15:50:00 -04:00
|
|
|
|
2013-08-25 23:29:50 -04:00
|
|
|
if comm.UploadPath != "something" {
|
2013-07-02 22:11:30 -04:00
|
|
|
t.Fatalf("should upload to configured destination")
|
|
|
|
}
|
2013-07-04 15:50:00 -04:00
|
|
|
|
2013-08-25 23:29:50 -04:00
|
|
|
if comm.UploadData != "hello" {
|
2013-07-02 22:11:30 -04:00
|
|
|
t.Fatalf("should upload with source file's data")
|
|
|
|
}
|
|
|
|
}
|
2016-07-06 16:42:26 -04:00
|
|
|
|
|
|
|
func TestProvisionDownloadMkdirAll(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
path string
|
|
|
|
}{
|
|
|
|
{"dir"},
|
|
|
|
{"dir/"},
|
|
|
|
{"dir/subdir"},
|
|
|
|
{"dir/subdir/"},
|
|
|
|
{"path/to/dir"},
|
|
|
|
{"path/to/dir/"},
|
|
|
|
}
|
|
|
|
tmpDir, err := ioutil.TempDir("", "packer-file")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error tempdir: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
tf, err := ioutil.TempFile(tmpDir, "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error tempfile: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
config := map[string]interface{}{
|
|
|
|
"source": tf.Name(),
|
|
|
|
}
|
|
|
|
var p Provisioner
|
|
|
|
for _, test := range tests {
|
|
|
|
path := filepath.Join(tmpDir, test.path)
|
|
|
|
config["destination"] = filepath.Join(path, "something")
|
|
|
|
if err := p.Prepare(config); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-09-06 09:52:28 -04:00
|
|
|
b := bytes.NewBuffer(nil)
|
|
|
|
ui := &packer.BasicUi{
|
|
|
|
Writer: b,
|
|
|
|
}
|
2016-07-06 16:42:26 -04:00
|
|
|
comm := &packer.MockCommunicator{}
|
|
|
|
err = p.ProvisionDownload(ui, comm)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should successfully provision: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-09-06 09:52:28 -04:00
|
|
|
if !strings.Contains(b.String(), tf.Name()) {
|
2016-07-06 16:42:26 -04:00
|
|
|
t.Fatalf("should print source filename")
|
|
|
|
}
|
|
|
|
|
2018-09-06 09:52:28 -04:00
|
|
|
if !strings.Contains(b.String(), "something") {
|
2016-07-06 16:42:26 -04:00
|
|
|
t.Fatalf("should print destination filename")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
t.Fatalf("stat of download dir should not error: %s", err)
|
|
|
|
}
|
2016-09-18 22:36:18 -04:00
|
|
|
|
|
|
|
if _, err := os.Stat(config["destination"].(string)); err != nil {
|
|
|
|
t.Fatalf("stat of destination file should not error: %s", err)
|
|
|
|
}
|
2016-07-06 16:42:26 -04:00
|
|
|
}
|
|
|
|
}
|