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