github.com/rsyabuta/packer@v1.1.4-0.20180119234903-5ef0c2280f0b/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 42 cases := []struct { 43 InputString string 44 OutputURL string 45 ErrExpected bool 46 }{ 47 // Invalid URL: has hex code in host 48 {"http://what%20.com", "", true}, 49 // Valid: http 50 {"HTTP://packer.io/path", "http://packer.io/path", false}, 51 // No path 52 {"HTTP://packer.io", "http://packer.io", false}, 53 // Invalid: unsupported scheme 54 {"ftp://host.com/path", "", true}, 55 } 56 57 for _, tc := range cases { 58 u, err := DownloadableURL(tc.InputString) 59 if u != tc.OutputURL { 60 t.Fatal(fmt.Sprintf("Error with URL %s: got %s but expected %s", 61 tc.InputString, tc.OutputURL, u)) 62 } 63 if (err != nil) != tc.ErrExpected { 64 if tc.ErrExpected == true { 65 t.Fatal(fmt.Sprintf("Error with URL %s: we expected "+ 66 "DownloadableURL to return an error but didn't get one.", 67 tc.InputString)) 68 } else { 69 t.Fatal(fmt.Sprintf("Error with URL %s: we did not expect an "+ 70 " error from DownloadableURL but we got: %s", 71 tc.InputString, err)) 72 } 73 } 74 } 75 } 76 77 func TestDownloadableURL_WindowsFiles(t *testing.T) { 78 if runtime.GOOS == "windows" { 79 dirCases := []struct { 80 InputString string 81 OutputURL string 82 ErrExpected bool 83 }{ // TODO: add different directories 84 { 85 "C:\\Temp\\SomeDir\\myfile.txt", 86 "file:///C:/Temp/SomeDir/myfile.txt", 87 false, 88 }, 89 { // need windows drive 90 "\\Temp\\SomeDir\\myfile.txt", 91 "", 92 true, 93 }, 94 { // need windows drive 95 "/Temp/SomeDir/myfile.txt", 96 "", 97 true, 98 }, 99 { // UNC paths; why not? 100 "\\\\?\\c:\\Temp\\SomeDir\\myfile.txt", 101 "", 102 true, 103 }, 104 { 105 "file:///C:\\Temp\\SomeDir\\myfile.txt", 106 "file:///c:/Temp/SomeDir/myfile.txt", 107 false, 108 }, 109 { 110 "file:///c:/Temp/Somedir/myfile.txt", 111 "file:///c:/Temp/SomeDir/myfile.txt", 112 false, 113 }, 114 } 115 // create absolute-pathed tempfile to play with 116 err := os.Mkdir("C:\\Temp\\SomeDir", 0755) 117 if err != nil { 118 t.Fatalf("err creating test dir: %s", err) 119 } 120 fi, err := os.Create("C:\\Temp\\SomeDir\\myfile.txt") 121 if err != nil { 122 t.Fatalf("err creating test file: %s", err) 123 } 124 fi.Close() 125 defer os.Remove("C:\\Temp\\SomeDir\\myfile.txt") 126 defer os.Remove("C:\\Temp\\SomeDir") 127 128 // Run through test cases to make sure they all parse correctly 129 for _, tc := range dirCases { 130 u, err := DownloadableURL(tc.InputString) 131 if (err != nil) != tc.ErrExpected { 132 t.Fatalf("Test Case failed: Expected err = %#v, err = %#v, input = %s", 133 tc.ErrExpected, err, tc.InputString) 134 } 135 if u != tc.OutputURL { 136 t.Fatalf("Test Case failed: Expected %s but received %s from input %s", 137 tc.OutputURL, u, tc.InputString) 138 } 139 } 140 } 141 } 142 143 func TestDownloadableURL_FilePaths(t *testing.T) { 144 tf, err := ioutil.TempFile("", "packer") 145 if err != nil { 146 t.Fatalf("tempfile err: %s", err) 147 } 148 defer os.Remove(tf.Name()) 149 tf.Close() 150 151 tfPath, err := filepath.EvalSymlinks(tf.Name()) 152 if err != nil { 153 t.Fatalf("tempfile err: %s", err) 154 } 155 156 tfPath = filepath.Clean(tfPath) 157 158 filePrefix := "file://" 159 if runtime.GOOS == "windows" { 160 filePrefix += "/" 161 } 162 163 // Relative filepath. We run this test in a func so that 164 // the defers run right away. 165 func() { 166 wd, err := os.Getwd() 167 if err != nil { 168 t.Fatalf("getwd err: %s", err) 169 } 170 171 err = os.Chdir(filepath.Dir(tfPath)) 172 if err != nil { 173 t.Fatalf("chdir err: %s", err) 174 } 175 defer os.Chdir(wd) 176 177 filename := filepath.Base(tfPath) 178 u, err := DownloadableURL(filename) 179 if err != nil { 180 t.Fatalf("err: %s", err) 181 } 182 183 expected := fmt.Sprintf("%s%s", 184 filePrefix, 185 strings.Replace(tfPath, `\`, `/`, -1)) 186 if u != expected { 187 t.Fatalf("unexpected: %#v != %#v", u, expected) 188 } 189 }() 190 191 // Test some cases with and without a schema prefix 192 for _, prefix := range []string{"", filePrefix} { 193 // Nonexistent file 194 _, err = DownloadableURL(prefix + "i/dont/exist") 195 if err != nil { 196 t.Fatalf("err: %s", err) 197 } 198 199 // Good file 200 u, err := DownloadableURL(prefix + tfPath) 201 if err != nil { 202 t.Fatalf("err: %s", err) 203 } 204 205 expected := fmt.Sprintf("%s%s", 206 filePrefix, 207 strings.Replace(tfPath, `\`, `/`, -1)) 208 if u != expected { 209 t.Fatalf("unexpected: %s != %s", u, expected) 210 } 211 } 212 } 213 214 func test_FileExistsLocally(t *testing.T) { 215 if runtime.GOOS == "windows" { 216 dirCases := []struct { 217 Input string 218 Output bool 219 }{ 220 // file exists locally 221 {"file:///C:/Temp/SomeDir/myfile.txt", true}, 222 // file is not supposed to exist locally 223 {"https://myfile.iso", true}, 224 // file does not exist locally 225 {"file:///C/i/dont/exist", false}, 226 } 227 // create absolute-pathed tempfile to play with 228 err := os.Mkdir("C:\\Temp\\SomeDir", 0755) 229 if err != nil { 230 t.Fatalf("err creating test dir: %s", err) 231 } 232 fi, err := os.Create("C:\\Temp\\SomeDir\\myfile.txt") 233 if err != nil { 234 t.Fatalf("err creating test file: %s", err) 235 } 236 fi.Close() 237 defer os.Remove("C:\\Temp\\SomeDir\\myfile.txt") 238 defer os.Remove("C:\\Temp\\SomeDir") 239 240 // Run through test cases to make sure they all parse correctly 241 for _, tc := range dirCases { 242 fileOK := FileExistsLocally(tc.Input) 243 if !fileOK { 244 t.Fatalf("Test Case failed: Expected %#v, received = %#v, input = %s", 245 tc.Output, fileOK, tc.Input) 246 } 247 } 248 } 249 } 250 251 func TestScrubConfig(t *testing.T) { 252 type Inner struct { 253 Baz string 254 } 255 type Local struct { 256 Foo string 257 Bar string 258 Inner 259 } 260 c := Local{"foo", "bar", Inner{"bar"}} 261 expect := "Config: {Foo:foo Bar:<Filtered> Inner:{Baz:<Filtered>}}" 262 conf := ScrubConfig(c, c.Bar) 263 if conf != expect { 264 t.Fatalf("got %s, expected %s", conf, expect) 265 } 266 }