github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/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 TestFuncTimestamp(t *testing.T) { 204 expected := strconv.FormatInt(InitTime.Unix(), 10) 205 206 cases := []struct { 207 Input string 208 Output string 209 }{ 210 { 211 `{{timestamp}}`, 212 expected, 213 }, 214 } 215 216 ctx := &Context{} 217 for _, tc := range cases { 218 i := &I{Value: tc.Input} 219 result, err := i.Render(ctx) 220 if err != nil { 221 t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err) 222 } 223 224 if result != tc.Output { 225 t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result) 226 } 227 } 228 } 229 230 func TestFuncUser(t *testing.T) { 231 cases := []struct { 232 Input string 233 Output string 234 }{ 235 { 236 `{{user "foo"}}`, 237 `foo`, 238 }, 239 240 { 241 `{{user "what"}}`, 242 ``, 243 }, 244 } 245 246 ctx := &Context{ 247 UserVariables: map[string]string{ 248 "foo": "foo", 249 }, 250 } 251 for _, tc := range cases { 252 i := &I{Value: tc.Input} 253 result, err := i.Render(ctx) 254 if err != nil { 255 t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err) 256 } 257 258 if result != tc.Output { 259 t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result) 260 } 261 } 262 } 263 264 func TestFuncPackerVersion(t *testing.T) { 265 template := `{{packer_version}}` 266 267 ctx := &Context{} 268 i := &I{Value: template} 269 270 result, err := i.Render(ctx) 271 if err != nil { 272 t.Fatalf("Input: %s\n\nerr: %s", template, err) 273 } 274 275 // Only match the X.Y.Z portion of the whole version string. 276 if !strings.HasPrefix(result, version.Version) { 277 t.Fatalf("Expected input to include: %s\n\nGot: %s", 278 version.Version, result) 279 } 280 }