github.phpd.cn/hashicorp/packer@v1.3.2/template/interpolate/funcs_test.go (about) 1 package interpolate 2 3 import ( 4 "os" 5 "path/filepath" 6 "strconv" 7 "strings" 8 "testing" 9 "time" 10 11 "github.com/hashicorp/packer/version" 12 ) 13 14 func TestFuncBuildName(t *testing.T) { 15 cases := []struct { 16 Input string 17 Output string 18 }{ 19 { 20 `{{build_name}}`, 21 "foo", 22 }, 23 } 24 25 ctx := &Context{BuildName: "foo"} 26 for _, tc := range cases { 27 i := &I{Value: tc.Input} 28 result, err := i.Render(ctx) 29 if err != nil { 30 t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err) 31 } 32 33 if result != tc.Output { 34 t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result) 35 } 36 } 37 } 38 39 func TestFuncBuildType(t *testing.T) { 40 cases := []struct { 41 Input string 42 Output string 43 }{ 44 { 45 `{{build_type}}`, 46 "foo", 47 }, 48 } 49 50 ctx := &Context{BuildType: "foo"} 51 for _, tc := range cases { 52 i := &I{Value: tc.Input} 53 result, err := i.Render(ctx) 54 if err != nil { 55 t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err) 56 } 57 58 if result != tc.Output { 59 t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result) 60 } 61 } 62 } 63 64 func TestFuncEnv(t *testing.T) { 65 cases := []struct { 66 Input string 67 Output string 68 }{ 69 { 70 `{{env "PACKER_TEST_ENV"}}`, 71 `foo`, 72 }, 73 74 { 75 `{{env "PACKER_TEST_ENV_NOPE"}}`, 76 ``, 77 }, 78 } 79 80 os.Setenv("PACKER_TEST_ENV", "foo") 81 defer os.Setenv("PACKER_TEST_ENV", "") 82 83 ctx := &Context{EnableEnv: true} 84 for _, tc := range cases { 85 i := &I{Value: tc.Input} 86 result, err := i.Render(ctx) 87 if err != nil { 88 t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err) 89 } 90 91 if result != tc.Output { 92 t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result) 93 } 94 } 95 } 96 97 func TestFuncEnv_disable(t *testing.T) { 98 cases := []struct { 99 Input string 100 Output string 101 Error bool 102 }{ 103 { 104 `{{env "PACKER_TEST_ENV"}}`, 105 "", 106 true, 107 }, 108 } 109 110 ctx := &Context{EnableEnv: false} 111 for _, tc := range cases { 112 i := &I{Value: tc.Input} 113 result, err := i.Render(ctx) 114 if (err != nil) != tc.Error { 115 t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err) 116 } 117 118 if result != tc.Output { 119 t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result) 120 } 121 } 122 } 123 124 func TestFuncIsotime(t *testing.T) { 125 ctx := &Context{} 126 i := &I{Value: "{{isotime}}"} 127 result, err := i.Render(ctx) 128 if err != nil { 129 t.Fatalf("err: %s", err) 130 } 131 132 val, err := time.Parse(time.RFC3339, result) 133 if err != nil { 134 t.Fatalf("err: %s", err) 135 } 136 137 currentTime := time.Now().UTC() 138 if currentTime.Sub(val) > 2*time.Second { 139 t.Fatalf("val: %v (current: %v)", val, currentTime) 140 } 141 } 142 143 func TestFuncPwd(t *testing.T) { 144 wd, err := os.Getwd() 145 if err != nil { 146 t.Fatalf("err: %s", err) 147 } 148 149 cases := []struct { 150 Input string 151 Output string 152 }{ 153 { 154 `{{pwd}}`, 155 wd, 156 }, 157 } 158 159 ctx := &Context{} 160 for _, tc := range cases { 161 i := &I{Value: tc.Input} 162 result, err := i.Render(ctx) 163 if err != nil { 164 t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err) 165 } 166 167 if result != tc.Output { 168 t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result) 169 } 170 } 171 } 172 173 func TestFuncTemplatePath(t *testing.T) { 174 path := "foo/bar" 175 expected, _ := filepath.Abs(filepath.Dir(path)) 176 177 cases := []struct { 178 Input string 179 Output string 180 }{ 181 { 182 `{{template_dir}}`, 183 expected, 184 }, 185 } 186 187 ctx := &Context{ 188 TemplatePath: path, 189 } 190 for _, tc := range cases { 191 i := &I{Value: tc.Input} 192 result, err := i.Render(ctx) 193 if err != nil { 194 t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err) 195 } 196 197 if result != tc.Output { 198 t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result) 199 } 200 } 201 } 202 203 func TestFuncSplit(t *testing.T) { 204 cases := []struct { 205 Input string 206 Output string 207 ErrorExpected bool 208 }{ 209 { 210 `{{split build_name "-" 0}}`, 211 "foo", 212 false, 213 }, 214 { 215 `{{split build_name "-" 1}}`, 216 "bar", 217 false, 218 }, 219 { 220 `{{split build_name "-" 2}}`, 221 "", 222 true, 223 }, 224 } 225 226 ctx := &Context{BuildName: "foo-bar"} 227 for _, tc := range cases { 228 i := &I{Value: tc.Input} 229 result, err := i.Render(ctx) 230 if (err == nil) == tc.ErrorExpected { 231 t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err) 232 } 233 234 if result != tc.Output { 235 t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result) 236 } 237 } 238 } 239 240 func TestFuncTimestamp(t *testing.T) { 241 expected := strconv.FormatInt(InitTime.Unix(), 10) 242 243 cases := []struct { 244 Input string 245 Output string 246 }{ 247 { 248 `{{timestamp}}`, 249 expected, 250 }, 251 } 252 253 ctx := &Context{} 254 for _, tc := range cases { 255 i := &I{Value: tc.Input} 256 result, err := i.Render(ctx) 257 if err != nil { 258 t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err) 259 } 260 261 if result != tc.Output { 262 t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result) 263 } 264 } 265 } 266 267 func TestFuncUser(t *testing.T) { 268 cases := []struct { 269 Input string 270 Output string 271 }{ 272 { 273 `{{user "foo"}}`, 274 `foo`, 275 }, 276 277 { 278 `{{user "what"}}`, 279 ``, 280 }, 281 } 282 283 ctx := &Context{ 284 UserVariables: map[string]string{ 285 "foo": "foo", 286 }, 287 } 288 for _, tc := range cases { 289 i := &I{Value: tc.Input} 290 result, err := i.Render(ctx) 291 if err != nil { 292 t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err) 293 } 294 295 if result != tc.Output { 296 t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result) 297 } 298 } 299 } 300 301 func TestFuncPackerVersion(t *testing.T) { 302 template := `{{packer_version}}` 303 304 ctx := &Context{} 305 i := &I{Value: template} 306 307 result, err := i.Render(ctx) 308 if err != nil { 309 t.Fatalf("Input: %s\n\nerr: %s", template, err) 310 } 311 312 // Only match the X.Y.Z portion of the whole version string. 313 if !strings.HasPrefix(result, version.Version) { 314 t.Fatalf("Expected input to include: %s\n\nGot: %s", 315 version.Version, result) 316 } 317 }