github.com/jbronn/packer@v0.1.6-0.20140120165540-8a1364dbd817/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 // This test tests the case that a user var is used for an integer 70 // configuration. 71 func TestDecodeConfig_userVarConversion(t *testing.T) { 72 type Local struct { 73 Val int 74 } 75 76 raw := map[string]interface{}{ 77 "packer_user_variables": map[string]string{ 78 "foo": "42", 79 }, 80 81 "val": "{{user `foo`}}", 82 } 83 84 var result Local 85 _, err := DecodeConfig(&result, raw) 86 if err != nil { 87 t.Fatalf("err: %s", err) 88 } 89 90 if result.Val != 42 { 91 t.Fatalf("invalid: %#v", result.Val) 92 } 93 } 94 95 func TestDownloadableURL(t *testing.T) { 96 // Invalid URL: has hex code in host 97 _, err := DownloadableURL("http://what%20.com") 98 if err == nil { 99 t.Fatal("expected err") 100 } 101 102 // Invalid: unsupported scheme 103 _, err = DownloadableURL("ftp://host.com/path") 104 if err == nil { 105 t.Fatal("expected err") 106 } 107 108 // Valid: http 109 u, err := DownloadableURL("HTTP://packer.io/path") 110 if err != nil { 111 t.Fatalf("err: %s", err) 112 } 113 114 if u != "http://packer.io/path" { 115 t.Fatalf("bad: %s", u) 116 } 117 118 // No path 119 u, err = DownloadableURL("HTTP://packer.io") 120 if err != nil { 121 t.Fatalf("err: %s", err) 122 } 123 124 if u != "http://packer.io" { 125 t.Fatalf("bad: %s", u) 126 } 127 } 128 129 func TestDownloadableURL_FilePaths(t *testing.T) { 130 tf, err := ioutil.TempFile("", "packer") 131 if err != nil { 132 t.Fatalf("tempfile err: %s", err) 133 } 134 defer os.Remove(tf.Name()) 135 tf.Close() 136 137 tfPath, err := filepath.EvalSymlinks(tf.Name()) 138 if err != nil { 139 t.Fatalf("tempfile err: %s", err) 140 } 141 142 tfPath = filepath.Clean(tfPath) 143 144 // Relative filepath. We run this test in a func so that 145 // the defers run right away. 146 func() { 147 wd, err := os.Getwd() 148 if err != nil { 149 t.Fatalf("getwd err: %s", err) 150 } 151 152 err = os.Chdir(filepath.Dir(tfPath)) 153 if err != nil { 154 t.Fatalf("chdir err: %s", err) 155 } 156 defer os.Chdir(wd) 157 158 filename := filepath.Base(tfPath) 159 u, err := DownloadableURL(filename) 160 if err != nil { 161 t.Fatalf("err: %s", err) 162 } 163 164 if u != fmt.Sprintf("file://%s", tfPath) { 165 t.Fatalf("unexpected: %s", u) 166 } 167 }() 168 169 // Test some cases with and without a schema prefix 170 for _, prefix := range []string{"", "file://"} { 171 // Nonexistent file 172 _, err = DownloadableURL(prefix + "i/dont/exist") 173 if err != nil { 174 t.Fatalf("err: %s", err) 175 } 176 177 // Good file 178 u, err := DownloadableURL(prefix + tfPath) 179 if err != nil { 180 t.Fatalf("err: %s", err) 181 } 182 183 if u != fmt.Sprintf("file://%s", tfPath) { 184 t.Fatalf("unexpected: %s", u) 185 } 186 } 187 } 188 189 func TestScrubConfig(t *testing.T) { 190 type Inner struct { 191 Baz string 192 } 193 type Local struct { 194 Foo string 195 Bar string 196 Inner 197 } 198 c := Local{"foo", "bar", Inner{"bar"}} 199 expect := "Config: {Foo:foo Bar:<Filtered> Inner:{Baz:<Filtered>}}" 200 conf := ScrubConfig(c, c.Bar) 201 if conf != expect { 202 t.Fatalf("got %s, expected %s", conf, expect) 203 } 204 }