github.com/emate/packer@v0.8.1-0.20150625195101-fe0fde195dc6/common/config_test.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"runtime"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  func TestChooseString(t *testing.T) {
    14  	cases := []struct {
    15  		Input  []string
    16  		Output string
    17  	}{
    18  		{
    19  			[]string{"", "foo", ""},
    20  			"foo",
    21  		},
    22  		{
    23  			[]string{"", "foo", "bar"},
    24  			"foo",
    25  		},
    26  		{
    27  			[]string{"", "", ""},
    28  			"",
    29  		},
    30  	}
    31  
    32  	for _, tc := range cases {
    33  		result := ChooseString(tc.Input...)
    34  		if result != tc.Output {
    35  			t.Fatalf("bad: %#v", tc.Input)
    36  		}
    37  	}
    38  }
    39  
    40  func TestDownloadableURL(t *testing.T) {
    41  	// Invalid URL: has hex code in host
    42  	_, err := DownloadableURL("http://what%20.com")
    43  	if err == nil {
    44  		t.Fatal("expected err")
    45  	}
    46  
    47  	// Invalid: unsupported scheme
    48  	_, err = DownloadableURL("ftp://host.com/path")
    49  	if err == nil {
    50  		t.Fatal("expected err")
    51  	}
    52  
    53  	// Valid: http
    54  	u, err := DownloadableURL("HTTP://packer.io/path")
    55  	if err != nil {
    56  		t.Fatalf("err: %s", err)
    57  	}
    58  
    59  	if u != "http://packer.io/path" {
    60  		t.Fatalf("bad: %s", u)
    61  	}
    62  
    63  	// No path
    64  	u, err = DownloadableURL("HTTP://packer.io")
    65  	if err != nil {
    66  		t.Fatalf("err: %s", err)
    67  	}
    68  
    69  	if u != "http://packer.io" {
    70  		t.Fatalf("bad: %s", u)
    71  	}
    72  }
    73  
    74  func TestDownloadableURL_FilePaths(t *testing.T) {
    75  	tf, err := ioutil.TempFile("", "packer")
    76  	if err != nil {
    77  		t.Fatalf("tempfile err: %s", err)
    78  	}
    79  	defer os.Remove(tf.Name())
    80  	tf.Close()
    81  
    82  	tfPath, err := filepath.EvalSymlinks(tf.Name())
    83  	if err != nil {
    84  		t.Fatalf("tempfile err: %s", err)
    85  	}
    86  
    87  	tfPath = filepath.Clean(tfPath)
    88  
    89  	filePrefix := "file://"
    90  	if runtime.GOOS == "windows" {
    91  		filePrefix += "/"
    92  	}
    93  
    94  	// Relative filepath. We run this test in a func so that
    95  	// the defers run right away.
    96  	func() {
    97  		wd, err := os.Getwd()
    98  		if err != nil {
    99  			t.Fatalf("getwd err: %s", err)
   100  		}
   101  
   102  		err = os.Chdir(filepath.Dir(tfPath))
   103  		if err != nil {
   104  			t.Fatalf("chdir err: %s", err)
   105  		}
   106  		defer os.Chdir(wd)
   107  
   108  		filename := filepath.Base(tfPath)
   109  		u, err := DownloadableURL(filename)
   110  		if err != nil {
   111  			t.Fatalf("err: %s", err)
   112  		}
   113  
   114  		expected := fmt.Sprintf("%s%s",
   115  			filePrefix,
   116  			strings.Replace(tfPath, `\`, `/`, -1))
   117  		if u != expected {
   118  			t.Fatalf("unexpected: %#v != %#v", u, expected)
   119  		}
   120  	}()
   121  
   122  	// Test some cases with and without a schema prefix
   123  	for _, prefix := range []string{"", "file://"} {
   124  		// Nonexistent file
   125  		_, err = DownloadableURL(prefix + "i/dont/exist")
   126  		if err != nil {
   127  			t.Fatalf("err: %s", err)
   128  		}
   129  
   130  		// Good file
   131  		u, err := DownloadableURL(prefix + tfPath)
   132  		if err != nil {
   133  			t.Fatalf("err: %s", err)
   134  		}
   135  
   136  		expected := fmt.Sprintf("%s%s",
   137  			filePrefix,
   138  			strings.Replace(tfPath, `\`, `/`, -1))
   139  		if u != expected {
   140  			t.Fatalf("unexpected: %s != %s", u, expected)
   141  		}
   142  	}
   143  }
   144  
   145  func TestScrubConfig(t *testing.T) {
   146  	type Inner struct {
   147  		Baz string
   148  	}
   149  	type Local struct {
   150  		Foo string
   151  		Bar string
   152  		Inner
   153  	}
   154  	c := Local{"foo", "bar", Inner{"bar"}}
   155  	expect := "Config: {Foo:foo Bar:<Filtered> Inner:{Baz:<Filtered>}}"
   156  	conf := ScrubConfig(c, c.Bar)
   157  	if conf != expect {
   158  		t.Fatalf("got %s, expected %s", conf, expect)
   159  	}
   160  }