github.com/blend/go-sdk@v1.20220411.3/template/template_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package template 9 10 import ( 11 "bytes" 12 "fmt" 13 "strings" 14 "testing" 15 "time" 16 17 "github.com/blend/go-sdk/assert" 18 "github.com/blend/go-sdk/env" 19 "github.com/blend/go-sdk/uuid" 20 ) 21 22 func TestTemplateFromFile(t *testing.T) { 23 assert := assert.New(t) 24 25 temp, err := NewFromFile("testdata/test.template.yml") 26 assert.Nil(err) 27 28 temp = temp. 29 WithVar("name", "test-service"). 30 WithVar("container-name", "nginx"). 31 WithVar("container-image", "nginx:1.7.9"). 32 WithVar("container-port", "disabled") 33 34 buffer := bytes.NewBuffer(nil) 35 err = temp.Process(buffer) 36 assert.Nil(err) 37 38 result := buffer.String() 39 assert.True(strings.Contains(result, "name: test-service")) 40 assert.True(strings.Contains(result, "replicas: 2")) 41 assert.False(strings.Contains(result, "containerPort:")) 42 43 temp = temp.WithVar("container-port", 80) 44 err = temp.Process(buffer) 45 assert.Nil(err) 46 result = buffer.String() 47 assert.True(strings.Contains(result, "port: 80")) 48 } 49 50 func TestTemplateTemplate(t *testing.T) { 51 assert := assert.New(t) 52 53 main := `{{ template "test" . }}` 54 test := `{{ define "test" }}{{ .Var "foo" }}{{end}}` 55 56 buffer := bytes.NewBuffer(nil) 57 err := New().WithBody(main).WithInclude(test).WithVar("foo", "bar").Process(buffer) 58 assert.Nil(err) 59 assert.Equal("bar", buffer.String()) 60 61 buffer = bytes.NewBuffer(nil) 62 err = New().WithBody(main).WithVar("foo", "bar").Process(buffer) 63 assert.NotNil(err) 64 } 65 66 func TestTemplateVar(t *testing.T) { 67 assert := assert.New(t) 68 69 test := `{{ .Var "foo" }}` 70 temp := New().WithBody(test).WithVar("foo", "bar") 71 72 buffer := bytes.NewBuffer(nil) 73 err := temp.Process(buffer) 74 assert.Nil(err) 75 assert.Equal("bar", buffer.String()) 76 } 77 78 func TestTemplateVarMissing(t *testing.T) { 79 assert := assert.New(t) 80 81 test := `{{ .Var "baz" }}` 82 temp := New().WithBody(test).WithVar("foo", "bar") 83 84 buffer := bytes.NewBuffer(nil) 85 err := temp.Process(buffer) 86 assert.NotNil(err) 87 } 88 89 func TestTemplateEnv(t *testing.T) { 90 assert := assert.New(t) 91 92 varName := uuid.V4().String() 93 env.Env().Set(varName, "bar") 94 defer env.Restore() 95 96 test := fmt.Sprintf(`{{ .Env "%s" }}`, varName) 97 temp := New().WithBody(test) 98 99 buffer := bytes.NewBuffer(nil) 100 err := temp.Process(buffer) 101 assert.Nil(err, fmt.Sprintf("%+v", err)) 102 assert.Equal("bar", buffer.String()) 103 } 104 105 func TestTemplateHasEnv(t *testing.T) { 106 assert := assert.New(t) 107 108 varName := uuid.V4().String() 109 env.Env().Set(varName, "bar") 110 defer env.Restore() 111 112 test := fmt.Sprintf(`{{ if .HasEnv "%s" }}yep{{else}}nope{{end}}`, varName) 113 temp := New().WithBody(test) 114 115 buffer := bytes.NewBuffer(nil) 116 err := temp.Process(buffer) 117 assert.Nil(err) 118 assert.Equal("yep", buffer.String()) 119 } 120 121 func TestTemplateHasEnvFalsey(t *testing.T) { 122 assert := assert.New(t) 123 124 varName := uuid.V4().String() 125 126 test := fmt.Sprintf(`{{ if .HasEnv "%s" }}yep{{else}}nope{{end}}`, varName) 127 temp := New().WithBody(test) 128 129 buffer := bytes.NewBuffer(nil) 130 err := temp.Process(buffer) 131 assert.Nil(err) 132 assert.Equal("nope", buffer.String()) 133 } 134 135 func TestTemplateEnvMissing(t *testing.T) { 136 assert := assert.New(t) 137 138 varName := uuid.V4().String() 139 140 test := fmt.Sprintf(`{{ .Env "%s" }}`, varName) 141 temp := New().WithBody(test) 142 143 buffer := bytes.NewBuffer(nil) 144 err := temp.Process(buffer) 145 assert.NotNil(err) 146 } 147 148 func TestTemplateExpandEnv(t *testing.T) { 149 assert := assert.New(t) 150 151 varName := uuid.V4().String() 152 153 envVars := env.Env() 154 envVars.Set(varName, "bar") 155 156 templateBody := fmt.Sprintf(`{{ .ExpandEnv "${%s}.foo" }}`, varName) 157 temp := New().WithBody(templateBody).WithEnvVars(envVars) 158 159 buffer := bytes.NewBuffer(nil) 160 err := temp.Process(buffer) 161 assert.Nil(err) 162 assert.Equal("bar.foo", buffer.String()) 163 } 164 165 func TestTemplateReadFile(t *testing.T) { 166 assert := assert.New(t) 167 168 test := `{{ read_file "testdata/inline_file" }}` 169 temp := New().WithBody(test) 170 171 buffer := bytes.NewBuffer(nil) 172 err := temp.Process(buffer) 173 assert.Nil(err) 174 assert.Equal("this is a test", buffer.String()) 175 } 176 177 func TestTemplateFileExists(t *testing.T) { 178 assert := assert.New(t) 179 180 test := `{{ if file_exists "testdata/inline_file" }}yep{{else}}nope{{end}}` 181 temp := New().WithBody(test) 182 183 buffer := bytes.NewBuffer(nil) 184 err := temp.Process(buffer) 185 assert.Nil(err) 186 assert.Equal("yep", buffer.String()) 187 } 188 189 func TestTemplateFileExistsFalsey(t *testing.T) { 190 assert := assert.New(t) 191 192 fileName := uuid.V4().String() 193 194 test := fmt.Sprintf(`{{ if file_exists "testdata/%s" }}yep{{else}}nope{{end}}`, fileName) 195 temp := New().WithBody(test) 196 197 buffer := bytes.NewBuffer(nil) 198 err := temp.Process(buffer) 199 assert.Nil(err) 200 assert.Equal("nope", buffer.String()) 201 } 202 203 func TestTemplateViewFuncTimeUnix(t *testing.T) { 204 assert := assert.New(t) 205 206 test := `{{ .Var "now" | unix }}` 207 temp := New().WithBody(test).WithVar("now", time.Date(2017, 05, 20, 21, 00, 00, 00, time.UTC)) 208 209 buffer := bytes.NewBuffer(nil) 210 err := temp.Process(buffer) 211 assert.Nil(err) 212 assert.Equal("1495314000", buffer.String()) 213 } 214 215 func TestTemplateViewFuncSince(t *testing.T) { 216 assert := assert.New(t) 217 218 test := `{{ .Var "ts" | since }}` 219 temp := New().WithBody(test). 220 WithVar("ts", time.Date(2018, 05, 20, 21, 00, 00, 00, time.UTC)) 221 222 buffer := bytes.NewBuffer(nil) 223 err := temp.Process(buffer) 224 assert.Nil(err) 225 assert.NotEmpty(buffer.String()) 226 } 227 228 func TestTemplateViewFuncTimeSub(t *testing.T) { 229 assert := assert.New(t) 230 231 test := `{{ time_sub ( .Var "ts1" ) ( .Var "ts2" ) }}` 232 temp := New().WithBody(test). 233 WithVar("ts1", time.Date(2018, 05, 20, 21, 00, 00, 00, time.UTC)). 234 WithVar("ts2", time.Date(2018, 05, 20, 21, 00, 00, int(500*time.Millisecond), time.UTC)) 235 236 buffer := bytes.NewBuffer(nil) 237 err := temp.Process(buffer) 238 assert.Nil(err) 239 assert.Equal("-500ms", buffer.String()) 240 } 241 242 func TestTemplateViewFuncSinceUTC(t *testing.T) { 243 assert := assert.New(t) 244 245 test := `{{ .Var "ts" | since_utc }}` 246 temp := New().WithBody(test). 247 WithVar("ts", time.Date(2018, 05, 20, 21, 00, 00, 00, time.UTC)) 248 249 buffer := bytes.NewBuffer(nil) 250 err := temp.Process(buffer) 251 assert.Nil(err) 252 assert.NotEmpty(buffer.String()) 253 } 254 255 func TestTemplateGenerateKey(t *testing.T) { 256 assert := assert.New(t) 257 258 test := `{{ generate_key 64 }}` 259 temp := New().WithBody(test) 260 261 buffer := bytes.NewBuffer(nil) 262 err := temp.Process(buffer) 263 assert.Nil(err) 264 265 assert.True(len(buffer.String()) > 64) 266 } 267 268 func TestTemplateViewFuncAsString(t *testing.T) { 269 assert := assert.New(t) 270 271 test := `{{ .Var "foo" | as_string }}` 272 temp := New().WithBody(test).WithVar("foo", 123) 273 274 buffer := bytes.NewBuffer(nil) 275 err := temp.Process(buffer) 276 assert.Nil(err) 277 assert.Equal("123", buffer.String()) 278 } 279 280 func TestTemplateViewFuncAsBytes(t *testing.T) { 281 assert := assert.New(t) 282 283 test := `{{ .Var "foo" | as_bytes }}` 284 temp := New().WithBody(test).WithVar("foo", "123") 285 286 buffer := bytes.NewBuffer(nil) 287 err := temp.Process(buffer) 288 assert.Nil(err) 289 assert.Equal("[49 50 51]", buffer.String()) 290 } 291 292 func TestTemplateViewFuncParseTime(t *testing.T) { 293 assert := assert.New(t) 294 295 test := `{{ .Var "foo" | parse_time "2006/01/02" | day }}` 296 temp := New().WithBody(test).WithVar("foo", "2017/05/30") 297 298 buffer := bytes.NewBuffer(nil) 299 err := temp.Process(buffer) 300 assert.Nil(err) 301 assert.Equal("30", buffer.String()) 302 } 303 304 func TestTemplateViewFuncTimeFromUnix(t *testing.T) { 305 assert := assert.New(t) 306 307 test := `{{ .Var "foo" | parse_unix | year }}` 308 temp := New().WithBody(test).WithVar("foo", time.Date(2017, 05, 20, 21, 00, 00, 00, time.UTC).Unix()) 309 310 buffer := bytes.NewBuffer(nil) 311 err := temp.Process(buffer) 312 assert.Nil(err) 313 assert.Equal("2017", buffer.String()) 314 } 315 316 func TestTemplateViewFuncTimeMonth(t *testing.T) { 317 assert := assert.New(t) 318 319 test := `{{ .Var "foo" | month }}` 320 temp := New().WithBody(test).WithVar("foo", time.Date(2017, 05, 20, 21, 00, 00, 00, time.UTC)) 321 322 buffer := bytes.NewBuffer(nil) 323 err := temp.Process(buffer) 324 assert.Nil(err) 325 assert.Equal("5", buffer.String()) 326 } 327 328 func TestTemplateViewFuncTimeDay(t *testing.T) { 329 assert := assert.New(t) 330 331 test := `{{ .Var "foo" | day }}` 332 temp := New().WithBody(test).WithVar("foo", time.Date(2017, 05, 20, 21, 00, 00, 00, time.UTC)) 333 334 buffer := bytes.NewBuffer(nil) 335 err := temp.Process(buffer) 336 assert.Nil(err) 337 assert.Equal("20", buffer.String()) 338 } 339 340 func TestTemplateViewFuncTimeHour(t *testing.T) { 341 assert := assert.New(t) 342 343 test := `{{ .Var "foo" | hour }}` 344 temp := New().WithBody(test).WithVar("foo", time.Date(2017, 05, 20, 21, 00, 00, 00, time.UTC)) 345 346 buffer := bytes.NewBuffer(nil) 347 err := temp.Process(buffer) 348 assert.Nil(err) 349 assert.Equal("21", buffer.String()) 350 } 351 352 func TestTemplateViewFuncTimeMinute(t *testing.T) { 353 assert := assert.New(t) 354 355 test := `{{ .Var "foo" | minute }}` 356 temp := New().WithBody(test).WithVar("foo", time.Date(2017, 05, 20, 21, 16, 17, 18, time.UTC)) 357 358 buffer := bytes.NewBuffer(nil) 359 err := temp.Process(buffer) 360 assert.Nil(err) 361 assert.Equal("16", buffer.String()) 362 } 363 364 func TestTemplateViewFuncTimeSecond(t *testing.T) { 365 assert := assert.New(t) 366 367 test := `{{ .Var "foo" | second }}` 368 temp := New().WithBody(test).WithVar("foo", time.Date(2017, 05, 20, 21, 16, 17, 18, time.UTC)) 369 370 buffer := bytes.NewBuffer(nil) 371 err := temp.Process(buffer) 372 assert.Nil(err) 373 assert.Equal("17", buffer.String()) 374 } 375 376 func TestTemplateViewFuncTimeFromUnixString(t *testing.T) { 377 assert := assert.New(t) 378 379 test := `{{ .Var "foo" | as_string | parse_int64 | parse_unix | year }}` 380 temp := New().WithBody(test).WithVar("foo", time.Date(2017, 05, 20, 21, 00, 00, 00, time.UTC).Unix()) 381 382 buffer := bytes.NewBuffer(nil) 383 err := temp.Process(buffer) 384 assert.Nil(err) 385 assert.Equal("2017", buffer.String()) 386 } 387 388 func TestTemplateViewFuncParseBool(t *testing.T) { 389 assert := assert.New(t) 390 391 test := `{{ if .Var "foo" | parse_bool }}yep{{end}}` 392 temp := New().WithBody(test).WithVar("foo", "true") 393 394 buffer := bytes.NewBuffer(nil) 395 err := temp.Process(buffer) 396 assert.Nil(err) 397 assert.Equal("yep", buffer.String()) 398 } 399 400 func TestTemplateViewFuncParseInt(t *testing.T) { 401 assert := assert.New(t) 402 403 test := `{{ if .Var "foo" | parse_int | eq 123 }}yep{{end}}` 404 temp := New().WithBody(test).WithVar("foo", "123") 405 406 buffer := bytes.NewBuffer(nil) 407 err := temp.Process(buffer) 408 assert.Nil(err) 409 assert.Equal("yep", buffer.String()) 410 } 411 412 func TestTemplateViewFuncParseInt64(t *testing.T) { 413 assert := assert.New(t) 414 415 test := `{{ .Var "foo" | parse_int64 }}` 416 temp := New().WithBody(test).WithVar("foo", fmt.Sprintf("%d", (1<<33))) 417 418 buffer := bytes.NewBuffer(nil) 419 err := temp.Process(buffer) 420 assert.Nil(err) 421 assert.Equal("8589934592", buffer.String()) 422 } 423 424 func TestTemplateViewFuncParseFloat64(t *testing.T) { 425 assert := assert.New(t) 426 427 test := `{{ .Var "foo" | parse_float64 }}` 428 temp := New().WithBody(test).WithVar("foo", "3.14") 429 430 buffer := bytes.NewBuffer(nil) 431 err := temp.Process(buffer) 432 assert.Nil(err) 433 assert.Equal("3.14", buffer.String()) 434 } 435 436 func TestTemplateViewFuncFormatMoney(t *testing.T) { 437 assert := assert.New(t) 438 439 test := `{{ .Var "foo" | parse_float64 | format_money }}` 440 temp := New().WithBody(test).WithVar("foo", "3.00") 441 442 buffer := bytes.NewBuffer(nil) 443 err := temp.Process(buffer) 444 assert.Nil(err) 445 assert.Equal("$3.00", buffer.String()) 446 } 447 448 func TestTemplateViewFuncFormatPct(t *testing.T) { 449 assert := assert.New(t) 450 451 test := `{{ .Var "foo" | parse_float64 | format_pct }}` 452 temp := New().WithBody(test).WithVar("foo", "0.24") 453 454 buffer := bytes.NewBuffer(nil) 455 err := temp.Process(buffer) 456 assert.Nil(err) 457 assert.Equal("24.00%", buffer.String()) 458 } 459 460 func TestTemplateViewFuncFormatFileSize(t *testing.T) { 461 assert := assert.New(t) 462 463 test := `{{ .Var "foo" | parse_int | format_filesize }}` 464 temp := New().WithBody(test).WithVar("foo", 1<<20) 465 466 buffer := bytes.NewBuffer(nil) 467 err := temp.Process(buffer) 468 assert.Nil(err) 469 assert.Equal("1mB", buffer.String()) 470 } 471 472 func TestTemplateViewFuncBase64(t *testing.T) { 473 assert := assert.New(t) 474 475 test := `{{ .Var "foo" | base64 }}` 476 temp := New().WithBody(test).WithVar("foo", "bar") 477 478 buffer := bytes.NewBuffer(nil) 479 err := temp.Process(buffer) 480 assert.Nil(err) 481 assert.Equal("YmFy", buffer.String()) 482 } 483 484 func TestTemplateViewFuncBase64Decode(t *testing.T) { 485 assert := assert.New(t) 486 487 test := `{{ .Var "foo" | base64 | base64decode }}` 488 temp := New().WithBody(test).WithVar("foo", "bar") 489 490 buffer := bytes.NewBuffer(nil) 491 err := temp.Process(buffer) 492 assert.Nil(err) 493 assert.Equal("bar", buffer.String()) 494 } 495 496 func TestTemplateViewFuncSplit(t *testing.T) { 497 assert := assert.New(t) 498 499 test := `{{ .Var "foo" | split "," }}` 500 temp := New().WithBody(test).WithVar("foo", "bar,baz,biz") 501 buffer := bytes.NewBuffer(nil) 502 err := temp.Process(buffer) 503 assert.Nil(err) 504 assert.Equal("[bar baz biz]", buffer.String()) 505 } 506 507 func TestTemplateViewFuncFirst(t *testing.T) { 508 assert := assert.New(t) 509 510 test := `{{ .Var "foo" | split "," | first }}` 511 temp := New().WithBody(test).WithVar("foo", "bar,baz,biz") 512 buffer := bytes.NewBuffer(nil) 513 err := temp.Process(buffer) 514 assert.Nil(err) 515 assert.Equal("bar", buffer.String()) 516 } 517 func TestTemplateViewFuncIndex(t *testing.T) { 518 assert := assert.New(t) 519 520 test := `{{ .Var "foo" | split "," | at_index 1 }}` 521 temp := New().WithBody(test).WithVar("foo", "bar,baz,biz") 522 buffer := bytes.NewBuffer(nil) 523 err := temp.Process(buffer) 524 assert.Nil(err, fmt.Sprintf("%+v", err)) 525 assert.Equal("baz", buffer.String()) 526 } 527 528 func TestTemplateViewFuncLast(t *testing.T) { 529 assert := assert.New(t) 530 531 test := `{{ .Var "foo" | split "," | last }}` 532 temp := New().WithBody(test).WithVar("foo", "bar,baz,biz") 533 buffer := bytes.NewBuffer(nil) 534 err := temp.Process(buffer) 535 assert.Nil(err) 536 assert.Equal("biz", buffer.String()) 537 } 538 539 func TestTemplateViewFuncSlice(t *testing.T) { 540 assert := assert.New(t) 541 542 test := `{{ .Var "foo" | split "," | slice 1 3 }}` 543 temp := New().WithBody(test).WithVar("foo", "bar,baz,biz,boof") 544 buffer := bytes.NewBuffer(nil) 545 err := temp.Process(buffer) 546 assert.Nil(err) 547 assert.Equal("[baz biz]", buffer.String()) 548 } 549 func TestTemplateViewFuncJoin(t *testing.T) { 550 assert := assert.New(t) 551 552 test := `{{ .Var "foo" | split "," | slice 1 3 | join "/" }}` 553 temp := New().WithBody(test).WithVar("foo", "bar,baz,biz,boof") 554 buffer := bytes.NewBuffer(nil) 555 err := temp.Process(buffer) 556 assert.Nil(err) 557 assert.Equal("baz/biz", buffer.String()) 558 } 559 560 func TestTemplateViewFuncHasPrefix(t *testing.T) { 561 assert := assert.New(t) 562 563 test := `{{ if .Var "foo" | has_prefix "http" }}yep{{end}}` 564 temp := New().WithBody(test).WithVar("foo", "http://foo.bar.com") 565 buffer := bytes.NewBuffer(nil) 566 err := temp.Process(buffer) 567 assert.Nil(err) 568 assert.Equal("yep", buffer.String()) 569 } 570 571 func TestTemplateViewFuncHasSuffix(t *testing.T) { 572 assert := assert.New(t) 573 574 test := `{{ if .Var "foo" | has_suffix "com" }}yep{{end}}` 575 temp := New().WithBody(test).WithVar("foo", "http://foo.bar.com") 576 buffer := bytes.NewBuffer(nil) 577 err := temp.Process(buffer) 578 assert.Nil(err) 579 assert.Equal("yep", buffer.String()) 580 } 581 582 func TestViewfuncConcat(t *testing.T) { 583 assert := assert.New(t) 584 585 test := `{{ concat "foo" "." "bar" "." "baz" }}` 586 temp := New().WithBody(test) 587 buffer := bytes.NewBuffer(nil) 588 err := temp.Process(buffer) 589 assert.Nil(err) 590 assert.Equal("foo.bar.baz", buffer.String()) 591 } 592 593 func TestViewfuncPrefix(t *testing.T) { 594 assert := assert.New(t) 595 596 test := `{{ "foo" | prefix "bar." }}` 597 temp := New().WithBody(test) 598 buffer := bytes.NewBuffer(nil) 599 err := temp.Process(buffer) 600 assert.Nil(err) 601 assert.Equal("bar.foo", buffer.String()) 602 } 603 604 func TestViewfuncSuffix(t *testing.T) { 605 assert := assert.New(t) 606 607 test := `{{ "foo" | suffix ".bar" }}` 608 temp := New().WithBody(test) 609 buffer := bytes.NewBuffer(nil) 610 err := temp.Process(buffer) 611 assert.Nil(err) 612 assert.Equal("foo.bar", buffer.String()) 613 } 614 func TestTemplateViewFuncContains(t *testing.T) { 615 assert := assert.New(t) 616 617 test := `{{ if .Var "foo" | contains "bar" }}yep{{end}}` 618 temp := New().WithBody(test).WithVar("foo", "http://foo.bar.com") 619 buffer := bytes.NewBuffer(nil) 620 err := temp.Process(buffer) 621 assert.Nil(err) 622 assert.Equal("yep", buffer.String()) 623 } 624 625 func TestTemplateViewFuncMatches(t *testing.T) { 626 assert := assert.New(t) 627 628 test := `{{ if .Var "foo" | matches "^[a-z]+$" }}yep{{else}}nope{{end}}` 629 temp := New().WithBody(test).WithVar("foo", "http://foo.bar.com") 630 buffer := bytes.NewBuffer(nil) 631 err := temp.Process(buffer) 632 assert.Nil(err) 633 assert.Equal("nope", buffer.String()) 634 } 635 636 func TestTemplateViewFuncSha256(t *testing.T) { 637 assert := assert.New(t) 638 639 test := `{{ .Var "foo" | sha256 }}` 640 temp := New().WithBody(test).WithVar("foo", "this is only a test") 641 buffer := bytes.NewBuffer(nil) 642 err := temp.Process(buffer) 643 assert.Nil(err) 644 assert.Equal("1661186b8e38e79f434e4549a2d53f84716cfff7c45d334bbc67c9d41d1e3be6", buffer.String()) 645 } 646 647 func TestTemplateViewFuncSha512(t *testing.T) { 648 assert := assert.New(t) 649 650 test := `{{ .Var "foo" | sha512 }}` 651 temp := New().WithBody(test).WithVar("foo", "this is only a test") 652 buffer := bytes.NewBuffer(nil) 653 err := temp.Process(buffer) 654 assert.Nil(err) 655 assert.Equal("819bf8f4c3c5508a061d0637d09858cf098ef8ef7cafa312d07fca8480703eccf1a00b24b8915e24f926a8106331d7bc064e63c04262dbed65e05b28e208e53e", buffer.String()) 656 } 657 658 func TestTemplateViewFuncSemverMajor(t *testing.T) { 659 assert := assert.New(t) 660 661 test := `{{ .Var "foo" | parse_semver | semver_major }}` 662 temp := New().WithBody(test).WithVar("foo", "1.2.3-beta1") 663 buffer := bytes.NewBuffer(nil) 664 err := temp.Process(buffer) 665 assert.Nil(err) 666 assert.Equal("1", buffer.String()) 667 } 668 669 func TestTemplateViewFuncSemverMinor(t *testing.T) { 670 assert := assert.New(t) 671 672 test := `{{ .Var "foo" | parse_semver | semver_minor }}` 673 temp := New().WithBody(test).WithVar("foo", "1.2.3-beta1") 674 buffer := bytes.NewBuffer(nil) 675 err := temp.Process(buffer) 676 assert.Nil(err) 677 assert.Equal("2", buffer.String()) 678 } 679 680 func TestTemplateViewFuncSemverPatch(t *testing.T) { 681 assert := assert.New(t) 682 683 test := `{{ .Var "foo" | parse_semver | semver_patch }}` 684 temp := New().WithBody(test).WithVar("foo", "1.2.3-beta1") 685 buffer := bytes.NewBuffer(nil) 686 err := temp.Process(buffer) 687 assert.Nil(err) 688 assert.Equal("3", buffer.String()) 689 } 690 691 type label struct { 692 Name string `yaml:"name"` 693 Vaue string `yaml:"value"` 694 } 695 696 func TestTemplateViewFuncYAML(t *testing.T) { 697 assert := assert.New(t) 698 699 test := ` 700 type: foo 701 meta: 702 name: 703 labels: 704 {{ .Var "labels" | to_yaml | indent_tabs 1 }} 705 ` 706 temp := New().WithBody(test).WithVar("labels", []label{ 707 {"foo", "bar"}, 708 {"bar", "baz"}, 709 {"moobar", "zoobar"}, 710 }) 711 buffer := bytes.NewBuffer(nil) 712 err := temp.Process(buffer) 713 assert.Nil(err) 714 assert.NotEmpty(buffer.String()) 715 } 716 717 func TestTemplateViewFuncJSON(t *testing.T) { 718 assert := assert.New(t) 719 720 test := ` 721 type: foo 722 meta: 723 name: 724 labels: 725 {{ .Var "labels" | to_json | indent_tabs 1 }} 726 ` 727 temp := New().WithBody(test).WithVar("labels", []label{ 728 {"foo", "bar"}, 729 {"bar", "baz"}, 730 {"moobar", "zoobar"}, 731 }) 732 buffer := bytes.NewBuffer(nil) 733 err := temp.Process(buffer) 734 assert.Nil(err) 735 assert.NotEmpty(buffer.String()) 736 } 737 738 func TestTemplateWithVars(t *testing.T) { 739 assert := assert.New(t) 740 741 temp := New().WithVars(map[string]interface{}{ 742 "foo": "baz", 743 "bar": "buz", 744 }) 745 assert.True(temp.HasVar("foo")) 746 assert.True(temp.HasVar("bar")) 747 assert.False(temp.HasVar("baz")) 748 assert.False(temp.HasVar("buz")) 749 val, err := temp.Var("foo") 750 assert.Nil(err) 751 assert.Equal("baz", val) 752 val, err = temp.Var("bar") 753 assert.Nil(err) 754 assert.Equal("buz", val) 755 } 756 757 func TestTemplateWithDelims(t *testing.T) { 758 assert := assert.New(t) 759 760 curly := `{{ .Var "foo" }}` 761 pointy := `<< .Var "foo" >>` 762 temp := New().WithBody(curly+pointy).WithDelims("<<", ">>").WithVar("foo", "bar") 763 764 buffer := bytes.NewBuffer(nil) 765 err := temp.Process(buffer) 766 assert.Nil(err) 767 assert.Equal(curly+"bar", buffer.String()) 768 769 temp = New().WithBody(curly+pointy).WithDelims("", "").WithVar("foo", "bar") 770 buffer.Reset() 771 err = temp.Process(buffer) 772 assert.Nil(err) 773 assert.Equal("bar"+pointy, buffer.String()) 774 } 775 776 func TestOrdinalNames(t *testing.T) { 777 assert := assert.New(t) 778 tmp := New().WithBody("{{ generate_ordinal_names \"cockroachdb-%d\" 5 | join \",\" }}") 779 780 buffer := new(bytes.Buffer) 781 assert.Nil(tmp.Process(buffer)) 782 assert.Equal("cockroachdb-0,cockroachdb-1,cockroachdb-2,cockroachdb-3,cockroachdb-4", buffer.String()) 783 } 784 785 func TestViewfuncNow(t *testing.T) { 786 assert := assert.New(t) 787 788 tmp := New().WithBody("{{ now | unix | parse_int64 }}") 789 buffer := new(bytes.Buffer) 790 assert.Nil(tmp.Process(buffer)) 791 assert.NotEmpty(buffer.String()) 792 } 793 794 func TestViewfuncNowUTC(t *testing.T) { 795 assert := assert.New(t) 796 797 tmp := New().WithBody("{{ now_utc | unix | parse_int64 }}") 798 buffer := new(bytes.Buffer) 799 assert.Nil(tmp.Process(buffer)) 800 assert.NotEmpty(buffer.String()) 801 } 802 803 func TestViewFuncTimeFormat(t *testing.T) { 804 assert := assert.New(t) 805 806 tmp := New().WithBody(`{{ .Var "data" | time_format "2006-01-02" }}`).WithVar("data", time.Date(2018, 10, 06, 12, 0, 0, 0, time.UTC)) 807 buffer := new(bytes.Buffer) 808 assert.Nil(tmp.Process(buffer)) 809 assert.Equal("2018-10-06", buffer.String()) 810 } 811 812 func TestViewFuncTimeIsZero(t *testing.T) { 813 assert := assert.New(t) 814 815 tmp := New().WithBody(`{{ if .Var "ts1" | time_is_zero }}yep{{else}}nope{{end}}`).WithVar("ts1", time.Date(2018, 10, 06, 12, 0, 0, 0, time.UTC)) 816 buffer := new(bytes.Buffer) 817 assert.Nil(tmp.Process(buffer)) 818 assert.Equal("nope", buffer.String()) 819 820 tmp.SetVar("ts1", time.Time{}) 821 822 buffer = new(bytes.Buffer) 823 assert.Nil(tmp.Process(buffer)) 824 assert.Equal("yep", buffer.String()) 825 } 826 827 func TestViewFuncTimeIsEpoch(t *testing.T) { 828 assert := assert.New(t) 829 830 tmp := New().WithBody(`{{ if .Var "ts1" | time_is_epoch }}yep{{else}}nope{{end}}`).WithVar("ts1", time.Date(2018, 10, 06, 12, 0, 0, 0, time.UTC)) 831 buffer := new(bytes.Buffer) 832 assert.Nil(tmp.Process(buffer)) 833 assert.Equal("nope", buffer.String()) 834 835 tmp.SetVar("ts1", time.Time{}) 836 837 buffer = new(bytes.Buffer) 838 assert.Nil(tmp.Process(buffer)) 839 assert.Equal("nope", buffer.String()) 840 841 tmp.SetVar("ts1", time.Unix(0, 0)) 842 843 buffer = new(bytes.Buffer) 844 assert.Nil(tmp.Process(buffer)) 845 assert.Equal("yep", buffer.String()) 846 } 847 848 func TestViewFuncRFC3339(t *testing.T) { 849 assert := assert.New(t) 850 851 tmp := New().WithBody(`{{ .Var "data" | rfc3339 }}`).WithVar("data", time.Date(2018, 10, 06, 12, 0, 0, 0, time.UTC)) 852 buffer := new(bytes.Buffer) 853 assert.Nil(tmp.Process(buffer)) 854 assert.Equal("2018-10-06T12:00:00Z", buffer.String()) 855 } 856 857 func TestViewFuncTimeShort(t *testing.T) { 858 assert := assert.New(t) 859 860 tmp := New().WithBody("{{ .Var \"data\" | time_short }}").WithVar("data", time.Date(2018, 10, 06, 12, 0, 0, 0, time.UTC)) 861 buffer := new(bytes.Buffer) 862 assert.Nil(tmp.Process(buffer)) 863 assert.Equal("10/06/2018 12:00:00 PM", buffer.String()) 864 } 865 866 func TestViewFuncTimeMedium(t *testing.T) { 867 assert := assert.New(t) 868 869 tmp := New().WithBody("{{ .Var \"data\" | time_medium }}").WithVar("data", time.Date(2018, 10, 06, 12, 0, 0, 0, time.UTC)) 870 buffer := new(bytes.Buffer) 871 assert.Nil(tmp.Process(buffer)) 872 assert.Equal("Oct 06, 2018 12:00:00 PM", buffer.String()) 873 } 874 875 func TestViewFuncTimeKitchen(t *testing.T) { 876 assert := assert.New(t) 877 878 tmp := New().WithBody("{{ .Var \"data\" | time_kitchen }}").WithVar("data", time.Date(2018, 10, 06, 12, 0, 0, 0, time.UTC)) 879 buffer := new(bytes.Buffer) 880 assert.Nil(tmp.Process(buffer)) 881 assert.Equal("12:00PM", buffer.String()) 882 } 883 884 func TestViewFuncDateMonthDay(t *testing.T) { 885 assert := assert.New(t) 886 887 tmp := New().WithBody("{{ .Var \"data\" | date_month_day }}").WithVar("data", time.Date(2018, 10, 06, 12, 0, 0, 0, time.UTC)) 888 buffer := new(bytes.Buffer) 889 assert.Nil(tmp.Process(buffer)) 890 assert.Equal("10/6", buffer.String()) 891 } 892 893 func TestViewFuncTimeInLocation(t *testing.T) { 894 assert := assert.New(t) 895 896 tmp := New().WithBody("{{ .Var \"data\" | in_loc \"UTC\" }}").WithVar("data", time.Date(2018, 10, 06, 12, 0, 0, 0, time.UTC)) 897 buffer := new(bytes.Buffer) 898 assert.Nil(tmp.Process(buffer)) 899 assert.Equal("2018-10-06 12:00:00 +0000 UTC", buffer.String()) 900 } 901 902 func TestViewFuncToDuration(t *testing.T) { 903 assert := assert.New(t) 904 905 tmp := New().WithBody(`{{ .Var "data" | to_duration}}`).WithVar("data", int64(time.Minute+(2*time.Second)+(3*time.Millisecond)+(4*time.Microsecond)+(5*time.Nanosecond))) 906 buffer := new(bytes.Buffer) 907 assert.Nil(tmp.Process(buffer)) 908 assert.Equal("1m2.003004005s", buffer.String()) 909 } 910 911 func TestViewFuncRound(t *testing.T) { 912 assert := assert.New(t) 913 914 tmp := New().WithBody("{{ .Var \"data\" | round 2 }}").WithVar("data", 12.34567) 915 buffer := new(bytes.Buffer) 916 err := tmp.Process(buffer) 917 assert.Nil(err, fmt.Sprintf("%+v", err)) 918 assert.Equal("12.35", buffer.String()) 919 } 920 921 func TestViewFuncCeil(t *testing.T) { 922 assert := assert.New(t) 923 924 tmp := New().WithBody("{{ .Var \"data\" | ceil }}").WithVar("data", 12.34567) 925 buffer := new(bytes.Buffer) 926 assert.Nil(tmp.Process(buffer)) 927 assert.Equal("13", buffer.String()) 928 } 929 930 func TestViewFuncFloor(t *testing.T) { 931 assert := assert.New(t) 932 933 tmp := New().WithBody("{{ .Var \"data\" | floor }}").WithVar("data", 12.34567) 934 buffer := new(bytes.Buffer) 935 assert.Nil(tmp.Process(buffer)) 936 assert.Equal("12", buffer.String()) 937 } 938 939 func TestViewfuncUUID(t *testing.T) { 940 assert := assert.New(t) 941 942 tmp := New().WithBody("{{ uuidv4 | as_string | parse_uuid }}") 943 buffer := new(bytes.Buffer) 944 assert.Nil(tmp.Process(buffer)) 945 assert.NotEmpty(buffer.String()) 946 } 947 948 func TestViewfuncToUpper(t *testing.T) { 949 assert := assert.New(t) 950 951 tmp := New().WithBody(`{{ "foo" | to_upper }}`) 952 buffer := new(bytes.Buffer) 953 assert.Nil(tmp.Process(buffer)) 954 assert.Equal("FOO", buffer.String()) 955 } 956 957 func TestViewfuncToLower(t *testing.T) { 958 assert := assert.New(t) 959 960 tmp := New().WithBody(`{{ "FOO" | to_lower }}`) 961 buffer := new(bytes.Buffer) 962 assert.Nil(tmp.Process(buffer)) 963 assert.Equal("foo", buffer.String()) 964 } 965 966 func TestViewfuncTrimSpace(t *testing.T) { 967 assert := assert.New(t) 968 969 tmp := New().WithBody(`{{ " foo " | trim_space }}`) 970 buffer := new(bytes.Buffer) 971 assert.Nil(tmp.Process(buffer)) 972 assert.Equal("foo", buffer.String()) 973 } 974 975 func TestViewfuncSplitN(t *testing.T) { 976 assert := assert.New(t) 977 978 tmp := New().WithBody(`{{ "a:b:c:d" | split_n ":" 2 }}`) 979 buffer := new(bytes.Buffer) 980 assert.Nil(tmp.Process(buffer)) 981 assert.Equal("[a b:c:d]", buffer.String()) 982 } 983 984 func TestViewfuncRandomLetters(t *testing.T) { 985 assert := assert.New(t) 986 987 tmp := New().WithBody(`{{ random_letters 10 }}`) 988 buffer := new(bytes.Buffer) 989 assert.Nil(tmp.Process(buffer)) 990 assert.Len(buffer.String(), 10) 991 } 992 993 func TestViewfuncRandomLettersWithNumbers(t *testing.T) { 994 assert := assert.New(t) 995 996 tmp := New().WithBody(`{{ random_letters_with_numbers 10 }}`) 997 buffer := new(bytes.Buffer) 998 assert.Nil(tmp.Process(buffer)) 999 assert.Len(buffer.String(), 10) 1000 } 1001 1002 func TestViewfuncRandomLettersWithNumbersAndSymbols(t *testing.T) { 1003 assert := assert.New(t) 1004 1005 tmp := New().WithBody(`{{ generate_password 10 }}`) 1006 buffer := new(bytes.Buffer) 1007 assert.Nil(tmp.Process(buffer)) 1008 assert.Len(buffer.String(), 10) 1009 } 1010 1011 func TestViewfuncURLEncode(t *testing.T) { 1012 assert := assert.New(t) 1013 1014 tmp := New().WithBody(`{{ "foo bar" | urlencode }}`) 1015 buffer := new(bytes.Buffer) 1016 assert.Nil(tmp.Process(buffer)) 1017 assert.Equal(`foo+bar`, buffer.String()) 1018 } 1019 1020 func TestViewfuncURLScheme(t *testing.T) { 1021 assert := assert.New(t) 1022 1023 tmp := New().WithBody(`{{ "http://foo.com/bar?fuzz=buzz" | parse_url | url_scheme }}`) 1024 buffer := new(bytes.Buffer) 1025 assert.Nil(tmp.Process(buffer)) 1026 assert.Equal("http", buffer.String()) 1027 } 1028 1029 func TestViewfuncURLHost(t *testing.T) { 1030 assert := assert.New(t) 1031 1032 tmp := New().WithBody(`{{ "http://foo.com/bar?fuzz=buzz" | parse_url | url_host }}`) 1033 buffer := new(bytes.Buffer) 1034 assert.Nil(tmp.Process(buffer)) 1035 assert.Equal("foo.com", buffer.String()) 1036 } 1037 1038 func TestViewfuncURLPort(t *testing.T) { 1039 assert := assert.New(t) 1040 1041 tmp := New().WithBody(`{{ "http://foo.com:8080/bar?fuzz=buzz" | parse_url | url_port }}`) 1042 buffer := new(bytes.Buffer) 1043 assert.Nil(tmp.Process(buffer)) 1044 assert.Equal("8080", buffer.String()) 1045 } 1046 1047 func TestViewfuncURLPath(t *testing.T) { 1048 assert := assert.New(t) 1049 1050 tmp := New().WithBody(`{{ "http://foo.com:8080/bar?fuzz=buzz" | parse_url | url_path }}`) 1051 buffer := new(bytes.Buffer) 1052 assert.Nil(tmp.Process(buffer)) 1053 assert.Equal("/bar", buffer.String()) 1054 } 1055 1056 func TestViewfuncURLQuery(t *testing.T) { 1057 assert := assert.New(t) 1058 1059 tmp := New().WithBody(`{{ "http://foo.com:8080/bar?fuzz=buzz&up=down" | parse_url | url_query "up" }}`) 1060 buffer := new(bytes.Buffer) 1061 assert.Nil(tmp.Process(buffer)) 1062 assert.Equal("down", buffer.String()) 1063 } 1064 1065 func TestTemplateProcess(t *testing.T) { 1066 assert := assert.New(t) 1067 1068 tmp := New().WithBody(`{{ read_file "testdata/process.yml" | process . }}`). 1069 WithVar("db.database", "postgres"). 1070 WithVar("db.username", "root"). 1071 WithVar("db.password", "password") 1072 1073 buffer := new(bytes.Buffer) 1074 err := tmp.Process(buffer) 1075 assert.Nil(err, fmt.Sprintf("%+v", err)) 1076 assert.NotEmpty(buffer.String()) 1077 } 1078 1079 func TestViewfuncCSV(t *testing.T) { 1080 assert := assert.New(t) 1081 1082 tmp := New().WithBody(`{{ .Var "things" | csv}}`).WithVar("things", []string{"a", "b", "c"}) 1083 buffer := new(bytes.Buffer) 1084 assert.Nil(tmp.Process(buffer)) 1085 assert.Equal("a,b,c", buffer.String()) 1086 } 1087 1088 func TestViewfuncTSV(t *testing.T) { 1089 assert := assert.New(t) 1090 1091 tmp := New().WithBody(`{{ .Var "things" | tsv}}`).WithVar("things", []string{"a", "b", "c"}) 1092 buffer := new(bytes.Buffer) 1093 assert.Nil(tmp.Process(buffer)) 1094 assert.Equal("a\tb\tc", buffer.String()) 1095 } 1096 1097 func TestViewfuncQuote(t *testing.T) { 1098 assert := assert.New(t) 1099 1100 tmp := New().WithBody("{{ .Vars.foo | quote }}").WithVar("foo", "foo") 1101 assert.Equal(`"foo"`, tmp.MustProcessString()) 1102 1103 tmp = New().WithBody("{{ .Vars.foo | quote }}").WithVar("foo", "\"foo\"") 1104 assert.Equal(`"foo"`, tmp.MustProcessString()) 1105 1106 tmp = New().WithBody("{{ .Vars.foo | quote }}").WithVar("foo", "\n\"foo\"\t") 1107 assert.Equal(`"foo"`, tmp.MustProcessString()) 1108 } 1109 1110 func TestViewfuncSeqInt(t *testing.T) { 1111 assert := assert.New(t) 1112 1113 tmp := New().WithBody("{{ range $x := seq_int 0 5 }}{{$x}}{{end}}") 1114 assert.Equal(`01234`, tmp.MustProcessString()) 1115 1116 tmp = New().WithBody("{{ range $x := seq_int 5 0 }}{{$x}}{{end}}") 1117 assert.Equal(`54321`, tmp.MustProcessString()) 1118 } 1119 1120 func TestViewFuncAdd(t *testing.T) { 1121 assert := assert.New(t) 1122 1123 tmp := New().WithBody("{{ add 1 2 3 4 | to_int }}") 1124 assert.Equal(`10`, tmp.MustProcessString()) 1125 1126 tmp = New().WithBody("{{ add 4 3 2 1 | to_int }}") 1127 assert.Equal(`10`, tmp.MustProcessString()) 1128 } 1129 1130 func TestViewFuncMul(t *testing.T) { 1131 assert := assert.New(t) 1132 1133 tmp := New().WithBody("{{ mul 1 2 3 4 | to_int }}") 1134 assert.Equal(`24`, tmp.MustProcessString()) 1135 1136 tmp = New().WithBody("{{ mul 4 3 2 1 | to_int }}") 1137 assert.Equal(`24`, tmp.MustProcessString()) 1138 } 1139 1140 func TestViewFuncSub(t *testing.T) { 1141 assert := assert.New(t) 1142 1143 tmp := New().WithBody("{{ sub 1 2 3 4 | to_int }}") 1144 assert.Equal(`-8`, tmp.MustProcessString()) 1145 1146 tmp = New().WithBody("{{ sub 4 3 2 1 | to_int }}") 1147 assert.Equal(`-2`, tmp.MustProcessString()) 1148 } 1149 1150 func TestViewFuncDiv(t *testing.T) { 1151 assert := assert.New(t) 1152 1153 tmp := New().WithBody("{{ div 1 2 4 }}") 1154 assert.Equal(`0.125`, tmp.MustProcessString()) 1155 1156 tmp = New().WithBody("{{ div 4 2 1 }}") 1157 assert.Equal(`2`, tmp.MustProcessString()) 1158 }