github.com/supr/packer@v0.3.10-0.20131015195147-7b09e24ac3c1/common/config_test.go (about)

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