github.com/jaredpalmer/terraform@v1.1.0-alpha20210908.0.20210911170307-88705c943a03/internal/lang/functions_test.go (about) 1 package lang 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/hashicorp/hcl/v2" 10 "github.com/hashicorp/hcl/v2/hclsyntax" 11 "github.com/hashicorp/terraform/internal/experiments" 12 "github.com/hashicorp/terraform/internal/lang/marks" 13 homedir "github.com/mitchellh/go-homedir" 14 "github.com/zclconf/go-cty/cty" 15 ) 16 17 // TestFunctions tests that functions are callable through the functionality 18 // in the langs package, via HCL. 19 // 20 // These tests are primarily here to assert that the functions are properly 21 // registered in the functions table, rather than to test all of the details 22 // of the functions. Each function should only have one or two tests here, 23 // since the main set of unit tests for a function should live alongside that 24 // function either in the "funcs" subdirectory here or over in the cty 25 // function/stdlib package. 26 // 27 // One exception to that is we can use this test mechanism to assert common 28 // patterns that are used in real-world configurations which rely on behaviors 29 // implemented either in this lang package or in HCL itself, such as automatic 30 // type conversions. The function unit tests don't cover those things because 31 // they call directly into the functions. 32 // 33 // With that said then, this test function should contain at least one simple 34 // test case per function registered in the functions table (just to prove 35 // it really is registered correctly) and possibly a small set of additional 36 // functions showing real-world use-cases that rely on type conversion 37 // behaviors. 38 func TestFunctions(t *testing.T) { 39 // used in `pathexpand()` test 40 homePath, err := homedir.Dir() 41 if err != nil { 42 t.Fatalf("Error getting home directory: %v", err) 43 } 44 45 tests := map[string][]struct { 46 src string 47 want cty.Value 48 }{ 49 // Please maintain this list in alphabetical order by function, with 50 // a blank line between the group of tests for each function. 51 52 "abs": { 53 { 54 `abs(-1)`, 55 cty.NumberIntVal(1), 56 }, 57 }, 58 59 "abspath": { 60 { 61 `abspath(".")`, 62 cty.StringVal((func() string { 63 cwd, err := os.Getwd() 64 if err != nil { 65 panic(err) 66 } 67 return filepath.ToSlash(cwd) 68 })()), 69 }, 70 }, 71 72 "alltrue": { 73 { 74 `alltrue(["true", true])`, 75 cty.True, 76 }, 77 }, 78 79 "anytrue": { 80 { 81 `anytrue([])`, 82 cty.False, 83 }, 84 }, 85 86 "base64decode": { 87 { 88 `base64decode("YWJjMTIzIT8kKiYoKSctPUB+")`, 89 cty.StringVal("abc123!?$*&()'-=@~"), 90 }, 91 }, 92 93 "base64encode": { 94 { 95 `base64encode("abc123!?$*&()'-=@~")`, 96 cty.StringVal("YWJjMTIzIT8kKiYoKSctPUB+"), 97 }, 98 }, 99 100 "base64gzip": { 101 { 102 `base64gzip("test")`, 103 cty.StringVal("H4sIAAAAAAAA/ypJLS4BAAAA//8BAAD//wx+f9gEAAAA"), 104 }, 105 }, 106 107 "base64sha256": { 108 { 109 `base64sha256("test")`, 110 cty.StringVal("n4bQgYhMfWWaL+qgxVrQFaO/TxsrC4Is0V1sFbDwCgg="), 111 }, 112 }, 113 114 "base64sha512": { 115 { 116 `base64sha512("test")`, 117 cty.StringVal("7iaw3Ur350mqGo7jwQrpkj9hiYB3Lkc/iBml1JQODbJ6wYX4oOHV+E+IvIh/1nsUNzLDBMxfqa2Ob1f1ACio/w=="), 118 }, 119 }, 120 121 "basename": { 122 { 123 `basename("testdata/hello.txt")`, 124 cty.StringVal("hello.txt"), 125 }, 126 }, 127 128 "can": { 129 { 130 `can(true)`, 131 cty.True, 132 }, 133 { 134 // Note: "can" only works with expressions that pass static 135 // validation, because it only gets an opportunity to run in 136 // that case. The following "works" (captures the error) because 137 // Terraform understands it as a reference to an attribute 138 // that does not exist during dynamic evaluation. 139 // 140 // "can" doesn't work with references that could never possibly 141 // be valid and are thus caught during static validation, such 142 // as an expression like "foo" alone which would be understood 143 // as an invalid resource reference. 144 `can({}.baz)`, 145 cty.False, 146 }, 147 }, 148 149 "ceil": { 150 { 151 `ceil(1.2)`, 152 cty.NumberIntVal(2), 153 }, 154 }, 155 156 "chomp": { 157 { 158 `chomp("goodbye\ncruel\nworld\n")`, 159 cty.StringVal("goodbye\ncruel\nworld"), 160 }, 161 }, 162 163 "chunklist": { 164 { 165 `chunklist(["a", "b", "c"], 1)`, 166 cty.ListVal([]cty.Value{ 167 cty.ListVal([]cty.Value{ 168 cty.StringVal("a"), 169 }), 170 cty.ListVal([]cty.Value{ 171 cty.StringVal("b"), 172 }), 173 cty.ListVal([]cty.Value{ 174 cty.StringVal("c"), 175 }), 176 }), 177 }, 178 }, 179 180 "cidrhost": { 181 { 182 `cidrhost("192.168.1.0/24", 5)`, 183 cty.StringVal("192.168.1.5"), 184 }, 185 }, 186 187 "cidrnetmask": { 188 { 189 `cidrnetmask("192.168.1.0/24")`, 190 cty.StringVal("255.255.255.0"), 191 }, 192 }, 193 194 "cidrsubnet": { 195 { 196 `cidrsubnet("192.168.2.0/20", 4, 6)`, 197 cty.StringVal("192.168.6.0/24"), 198 }, 199 }, 200 201 "cidrsubnets": { 202 { 203 `cidrsubnets("10.0.0.0/8", 8, 8, 16, 8)`, 204 cty.ListVal([]cty.Value{ 205 cty.StringVal("10.0.0.0/16"), 206 cty.StringVal("10.1.0.0/16"), 207 cty.StringVal("10.2.0.0/24"), 208 cty.StringVal("10.3.0.0/16"), 209 }), 210 }, 211 }, 212 213 "coalesce": { 214 { 215 `coalesce("first", "second", "third")`, 216 cty.StringVal("first"), 217 }, 218 219 { 220 `coalescelist(["first", "second"], ["third", "fourth"])`, 221 cty.TupleVal([]cty.Value{ 222 cty.StringVal("first"), cty.StringVal("second"), 223 }), 224 }, 225 }, 226 227 "coalescelist": { 228 { 229 `coalescelist(tolist(["a", "b"]), tolist(["c", "d"]))`, 230 cty.ListVal([]cty.Value{ 231 cty.StringVal("a"), 232 cty.StringVal("b"), 233 }), 234 }, 235 { 236 `coalescelist(["a", "b"], ["c", "d"])`, 237 cty.TupleVal([]cty.Value{ 238 cty.StringVal("a"), 239 cty.StringVal("b"), 240 }), 241 }, 242 }, 243 244 "compact": { 245 { 246 `compact(["test", "", "test"])`, 247 cty.ListVal([]cty.Value{ 248 cty.StringVal("test"), cty.StringVal("test"), 249 }), 250 }, 251 }, 252 253 "concat": { 254 { 255 `concat(["a", ""], ["b", "c"])`, 256 cty.TupleVal([]cty.Value{ 257 cty.StringVal("a"), 258 cty.StringVal(""), 259 cty.StringVal("b"), 260 cty.StringVal("c"), 261 }), 262 }, 263 }, 264 265 "contains": { 266 { 267 `contains(["a", "b"], "a")`, 268 cty.True, 269 }, 270 { // Should also work with sets, due to automatic conversion 271 `contains(toset(["a", "b"]), "a")`, 272 cty.True, 273 }, 274 }, 275 276 "csvdecode": { 277 { 278 `csvdecode("a,b,c\n1,2,3\n4,5,6")`, 279 cty.ListVal([]cty.Value{ 280 cty.ObjectVal(map[string]cty.Value{ 281 "a": cty.StringVal("1"), 282 "b": cty.StringVal("2"), 283 "c": cty.StringVal("3"), 284 }), 285 cty.ObjectVal(map[string]cty.Value{ 286 "a": cty.StringVal("4"), 287 "b": cty.StringVal("5"), 288 "c": cty.StringVal("6"), 289 }), 290 }), 291 }, 292 }, 293 294 "defaults": { 295 // This function is pretty specialized and so this is mainly 296 // just a test that it is defined at all. See the function's 297 // own unit tests for more interesting test cases. 298 { 299 `defaults({a: 4}, {a: 5})`, 300 cty.ObjectVal(map[string]cty.Value{ 301 "a": cty.NumberIntVal(4), 302 }), 303 }, 304 }, 305 306 "dirname": { 307 { 308 `dirname("testdata/hello.txt")`, 309 cty.StringVal("testdata"), 310 }, 311 }, 312 313 "distinct": { 314 { 315 `distinct(["a", "b", "a", "b"])`, 316 cty.ListVal([]cty.Value{ 317 cty.StringVal("a"), cty.StringVal("b"), 318 }), 319 }, 320 }, 321 322 "element": { 323 { 324 `element(["hello"], 0)`, 325 cty.StringVal("hello"), 326 }, 327 }, 328 329 "file": { 330 { 331 `file("hello.txt")`, 332 cty.StringVal("hello!"), 333 }, 334 }, 335 336 "fileexists": { 337 { 338 `fileexists("hello.txt")`, 339 cty.BoolVal(true), 340 }, 341 }, 342 343 "fileset": { 344 { 345 `fileset(".", "*/hello.*")`, 346 cty.SetVal([]cty.Value{ 347 cty.StringVal("subdirectory/hello.tmpl"), 348 cty.StringVal("subdirectory/hello.txt"), 349 }), 350 }, 351 { 352 `fileset(".", "subdirectory/hello.*")`, 353 cty.SetVal([]cty.Value{ 354 cty.StringVal("subdirectory/hello.tmpl"), 355 cty.StringVal("subdirectory/hello.txt"), 356 }), 357 }, 358 { 359 `fileset(".", "hello.*")`, 360 cty.SetVal([]cty.Value{ 361 cty.StringVal("hello.tmpl"), 362 cty.StringVal("hello.txt"), 363 }), 364 }, 365 { 366 `fileset("subdirectory", "hello.*")`, 367 cty.SetVal([]cty.Value{ 368 cty.StringVal("hello.tmpl"), 369 cty.StringVal("hello.txt"), 370 }), 371 }, 372 }, 373 374 "filebase64": { 375 { 376 `filebase64("hello.txt")`, 377 cty.StringVal("aGVsbG8h"), 378 }, 379 }, 380 381 "filebase64sha256": { 382 { 383 `filebase64sha256("hello.txt")`, 384 cty.StringVal("zgYJL7lI2f+sfRo3bkBLJrdXW8wR7gWkYV/vT+w6MIs="), 385 }, 386 }, 387 388 "filebase64sha512": { 389 { 390 `filebase64sha512("hello.txt")`, 391 cty.StringVal("xvgdsOn4IGyXHJ5YJuO6gj/7saOpAPgEdlKov3jqmP38dFhVo4U6Y1Z1RY620arxIJ6I6tLRkjgrXEy91oUOAg=="), 392 }, 393 }, 394 395 "filemd5": { 396 { 397 `filemd5("hello.txt")`, 398 cty.StringVal("5a8dd3ad0756a93ded72b823b19dd877"), 399 }, 400 }, 401 402 "filesha1": { 403 { 404 `filesha1("hello.txt")`, 405 cty.StringVal("8f7d88e901a5ad3a05d8cc0de93313fd76028f8c"), 406 }, 407 }, 408 409 "filesha256": { 410 { 411 `filesha256("hello.txt")`, 412 cty.StringVal("ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b"), 413 }, 414 }, 415 416 "filesha512": { 417 { 418 `filesha512("hello.txt")`, 419 cty.StringVal("c6f81db0e9f8206c971c9e5826e3ba823ffbb1a3a900f8047652a8bf78ea98fdfc745855a3853a635675458eb6d1aaf1209e88ead2d192382b5c4cbdd6850e02"), 420 }, 421 }, 422 423 "flatten": { 424 { 425 `flatten([["a", "b"], ["c", "d"]])`, 426 cty.TupleVal([]cty.Value{ 427 cty.StringVal("a"), 428 cty.StringVal("b"), 429 cty.StringVal("c"), 430 cty.StringVal("d"), 431 }), 432 }, 433 }, 434 435 "floor": { 436 { 437 `floor(-1.8)`, 438 cty.NumberFloatVal(-2), 439 }, 440 }, 441 442 "format": { 443 { 444 `format("Hello, %s!", "Ander")`, 445 cty.StringVal("Hello, Ander!"), 446 }, 447 }, 448 449 "formatlist": { 450 { 451 `formatlist("Hello, %s!", ["Valentina", "Ander", "Olivia", "Sam"])`, 452 cty.ListVal([]cty.Value{ 453 cty.StringVal("Hello, Valentina!"), 454 cty.StringVal("Hello, Ander!"), 455 cty.StringVal("Hello, Olivia!"), 456 cty.StringVal("Hello, Sam!"), 457 }), 458 }, 459 }, 460 461 "formatdate": { 462 { 463 `formatdate("DD MMM YYYY hh:mm ZZZ", "2018-01-04T23:12:01Z")`, 464 cty.StringVal("04 Jan 2018 23:12 UTC"), 465 }, 466 }, 467 468 "indent": { 469 { 470 fmt.Sprintf("indent(4, %#v)", Poem), 471 cty.StringVal("Fleas:\n Adam\n Had'em\n \n E.E. Cummings"), 472 }, 473 }, 474 475 "index": { 476 { 477 `index(["a", "b", "c"], "a")`, 478 cty.NumberIntVal(0), 479 }, 480 }, 481 482 "join": { 483 { 484 `join(" ", ["Hello", "World"])`, 485 cty.StringVal("Hello World"), 486 }, 487 }, 488 489 "jsondecode": { 490 { 491 `jsondecode("{\"hello\": \"world\"}")`, 492 cty.ObjectVal(map[string]cty.Value{ 493 "hello": cty.StringVal("world"), 494 }), 495 }, 496 }, 497 498 "jsonencode": { 499 { 500 `jsonencode({"hello"="world"})`, 501 cty.StringVal("{\"hello\":\"world\"}"), 502 }, 503 // We are intentionally choosing to escape <, >, and & characters 504 // to preserve backwards compatibility with Terraform 0.11 505 { 506 `jsonencode({"hello"="<cats & kittens>"})`, 507 cty.StringVal("{\"hello\":\"\\u003ccats \\u0026 kittens\\u003e\"}"), 508 }, 509 }, 510 511 "keys": { 512 { 513 `keys({"hello"=1, "goodbye"=42})`, 514 cty.TupleVal([]cty.Value{ 515 cty.StringVal("goodbye"), 516 cty.StringVal("hello"), 517 }), 518 }, 519 }, 520 521 "length": { 522 { 523 `length(["the", "quick", "brown", "bear"])`, 524 cty.NumberIntVal(4), 525 }, 526 }, 527 528 "list": { 529 // There are intentionally no test cases for "list" because 530 // it is a stub that always returns an error. 531 }, 532 533 "log": { 534 { 535 `log(1, 10)`, 536 cty.NumberFloatVal(0), 537 }, 538 }, 539 540 "lookup": { 541 { 542 `lookup({hello=1, goodbye=42}, "goodbye")`, 543 cty.NumberIntVal(42), 544 }, 545 }, 546 547 "lower": { 548 { 549 `lower("HELLO")`, 550 cty.StringVal("hello"), 551 }, 552 }, 553 554 "map": { 555 // There are intentionally no test cases for "map" because 556 // it is a stub that always returns an error. 557 }, 558 559 "matchkeys": { 560 { 561 `matchkeys(["a", "b", "c"], ["ref1", "ref2", "ref3"], ["ref1"])`, 562 cty.ListVal([]cty.Value{ 563 cty.StringVal("a"), 564 }), 565 }, 566 { // mixing types in searchset 567 `matchkeys(["a", "b", "c"], [1, 2, 3], [1, "3"])`, 568 cty.ListVal([]cty.Value{ 569 cty.StringVal("a"), 570 cty.StringVal("c"), 571 }), 572 }, 573 }, 574 575 "max": { 576 { 577 `max(12, 54, 3)`, 578 cty.NumberIntVal(54), 579 }, 580 }, 581 582 "md5": { 583 { 584 `md5("tada")`, 585 cty.StringVal("ce47d07243bb6eaf5e1322c81baf9bbf"), 586 }, 587 }, 588 589 "merge": { 590 { 591 `merge({"a"="b"}, {"c"="d"})`, 592 cty.ObjectVal(map[string]cty.Value{ 593 "a": cty.StringVal("b"), 594 "c": cty.StringVal("d"), 595 }), 596 }, 597 }, 598 599 "min": { 600 { 601 `min(12, 54, 3)`, 602 cty.NumberIntVal(3), 603 }, 604 }, 605 606 "nonsensitive": { 607 { 608 // Due to how this test is set up we have no way to get 609 // a sensitive value other than to generate one with 610 // another function, so this is a bit odd but does still 611 // meet the goal of verifying that the "nonsensitive" 612 // function is correctly registered. 613 `nonsensitive(sensitive(1))`, 614 cty.NumberIntVal(1), 615 }, 616 }, 617 618 "one": { 619 { 620 `one([])`, 621 cty.NullVal(cty.DynamicPseudoType), 622 }, 623 { 624 `one([true])`, 625 cty.True, 626 }, 627 }, 628 629 "parseint": { 630 { 631 `parseint("100", 10)`, 632 cty.NumberIntVal(100), 633 }, 634 }, 635 636 "pathexpand": { 637 { 638 `pathexpand("~/test-file")`, 639 cty.StringVal(filepath.Join(homePath, "test-file")), 640 }, 641 }, 642 643 "pow": { 644 { 645 `pow(1,0)`, 646 cty.NumberFloatVal(1), 647 }, 648 }, 649 650 "range": { 651 { 652 `range(3)`, 653 cty.ListVal([]cty.Value{ 654 cty.NumberIntVal(0), 655 cty.NumberIntVal(1), 656 cty.NumberIntVal(2), 657 }), 658 }, 659 { 660 `range(1, 4)`, 661 cty.ListVal([]cty.Value{ 662 cty.NumberIntVal(1), 663 cty.NumberIntVal(2), 664 cty.NumberIntVal(3), 665 }), 666 }, 667 { 668 `range(1, 8, 2)`, 669 cty.ListVal([]cty.Value{ 670 cty.NumberIntVal(1), 671 cty.NumberIntVal(3), 672 cty.NumberIntVal(5), 673 cty.NumberIntVal(7), 674 }), 675 }, 676 }, 677 678 "regex": { 679 { 680 `regex("(\\d+)([a-z]+)", "aaa111bbb222")`, 681 cty.TupleVal([]cty.Value{cty.StringVal("111"), cty.StringVal("bbb")}), 682 }, 683 }, 684 685 "regexall": { 686 { 687 `regexall("(\\d+)([a-z]+)", "...111aaa222bbb...")`, 688 cty.ListVal([]cty.Value{ 689 cty.TupleVal([]cty.Value{cty.StringVal("111"), cty.StringVal("aaa")}), 690 cty.TupleVal([]cty.Value{cty.StringVal("222"), cty.StringVal("bbb")}), 691 }), 692 }, 693 }, 694 695 "replace": { 696 { 697 `replace("hello", "hel", "bel")`, 698 cty.StringVal("bello"), 699 }, 700 }, 701 702 "reverse": { 703 { 704 `reverse(["a", true, 0])`, 705 cty.TupleVal([]cty.Value{cty.Zero, cty.True, cty.StringVal("a")}), 706 }, 707 }, 708 709 "rsadecrypt": { 710 { 711 fmt.Sprintf("rsadecrypt(%#v, %#v)", CipherBase64, PrivateKey), 712 cty.StringVal("message"), 713 }, 714 }, 715 716 "sensitive": { 717 { 718 `sensitive(1)`, 719 cty.NumberIntVal(1).Mark(marks.Sensitive), 720 }, 721 }, 722 723 "setintersection": { 724 { 725 `setintersection(["a", "b"], ["b", "c"], ["b", "d"])`, 726 cty.SetVal([]cty.Value{ 727 cty.StringVal("b"), 728 }), 729 }, 730 }, 731 732 "setproduct": { 733 { 734 `setproduct(["development", "staging", "production"], ["app1", "app2"])`, 735 cty.ListVal([]cty.Value{ 736 cty.TupleVal([]cty.Value{cty.StringVal("development"), cty.StringVal("app1")}), 737 cty.TupleVal([]cty.Value{cty.StringVal("development"), cty.StringVal("app2")}), 738 cty.TupleVal([]cty.Value{cty.StringVal("staging"), cty.StringVal("app1")}), 739 cty.TupleVal([]cty.Value{cty.StringVal("staging"), cty.StringVal("app2")}), 740 cty.TupleVal([]cty.Value{cty.StringVal("production"), cty.StringVal("app1")}), 741 cty.TupleVal([]cty.Value{cty.StringVal("production"), cty.StringVal("app2")}), 742 }), 743 }, 744 }, 745 746 "setsubtract": { 747 { 748 `setsubtract(["a", "b", "c"], ["a", "c"])`, 749 cty.SetVal([]cty.Value{ 750 cty.StringVal("b"), 751 }), 752 }, 753 }, 754 755 "setunion": { 756 { 757 `setunion(["a", "b"], ["b", "c"], ["d"])`, 758 cty.SetVal([]cty.Value{ 759 cty.StringVal("d"), 760 cty.StringVal("b"), 761 cty.StringVal("a"), 762 cty.StringVal("c"), 763 }), 764 }, 765 }, 766 767 "sha1": { 768 { 769 `sha1("test")`, 770 cty.StringVal("a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"), 771 }, 772 }, 773 774 "sha256": { 775 { 776 `sha256("test")`, 777 cty.StringVal("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"), 778 }, 779 }, 780 781 "sha512": { 782 { 783 `sha512("test")`, 784 cty.StringVal("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"), 785 }, 786 }, 787 788 "signum": { 789 { 790 `signum(12)`, 791 cty.NumberFloatVal(1), 792 }, 793 }, 794 795 "slice": { 796 { 797 // force a list type here for testing 798 `slice(tolist(["a", "b", "c", "d"]), 1, 3)`, 799 cty.ListVal([]cty.Value{ 800 cty.StringVal("b"), cty.StringVal("c"), 801 }), 802 }, 803 { 804 `slice(["a", "b", 3, 4], 1, 3)`, 805 cty.TupleVal([]cty.Value{ 806 cty.StringVal("b"), cty.NumberIntVal(3), 807 }), 808 }, 809 }, 810 811 "sort": { 812 { 813 `sort(["banana", "apple"])`, 814 cty.ListVal([]cty.Value{ 815 cty.StringVal("apple"), 816 cty.StringVal("banana"), 817 }), 818 }, 819 }, 820 821 "split": { 822 { 823 `split(" ", "Hello World")`, 824 cty.ListVal([]cty.Value{ 825 cty.StringVal("Hello"), 826 cty.StringVal("World"), 827 }), 828 }, 829 }, 830 831 "strrev": { 832 { 833 `strrev("hello world")`, 834 cty.StringVal("dlrow olleh"), 835 }, 836 }, 837 838 "substr": { 839 { 840 `substr("hello world", 1, 4)`, 841 cty.StringVal("ello"), 842 }, 843 }, 844 845 "sum": { 846 { 847 `sum([2340.5,10,3])`, 848 cty.NumberFloatVal(2353.5), 849 }, 850 }, 851 852 "textdecodebase64": { 853 { 854 `textdecodebase64("dABlAHMAdAA=", "UTF-16LE")`, 855 cty.StringVal("test"), 856 }, 857 }, 858 859 "textencodebase64": { 860 { 861 `textencodebase64("test", "UTF-16LE")`, 862 cty.StringVal("dABlAHMAdAA="), 863 }, 864 }, 865 866 "templatefile": { 867 { 868 `templatefile("hello.tmpl", {name = "Jodie"})`, 869 cty.StringVal("Hello, Jodie!"), 870 }, 871 }, 872 873 "timeadd": { 874 { 875 `timeadd("2017-11-22T00:00:00Z", "1s")`, 876 cty.StringVal("2017-11-22T00:00:01Z"), 877 }, 878 }, 879 880 "title": { 881 { 882 `title("hello")`, 883 cty.StringVal("Hello"), 884 }, 885 }, 886 887 "tobool": { 888 { 889 `tobool("false")`, 890 cty.False, 891 }, 892 }, 893 894 "tolist": { 895 { 896 `tolist(["a", "b", "c"])`, 897 cty.ListVal([]cty.Value{ 898 cty.StringVal("a"), cty.StringVal("b"), cty.StringVal("c"), 899 }), 900 }, 901 }, 902 903 "tomap": { 904 { 905 `tomap({"a" = 1, "b" = 2})`, 906 cty.MapVal(map[string]cty.Value{ 907 "a": cty.NumberIntVal(1), 908 "b": cty.NumberIntVal(2), 909 }), 910 }, 911 }, 912 913 "tonumber": { 914 { 915 `tonumber("42")`, 916 cty.NumberIntVal(42), 917 }, 918 }, 919 920 "toset": { 921 { 922 `toset(["a", "b", "c"])`, 923 cty.SetVal([]cty.Value{ 924 cty.StringVal("a"), cty.StringVal("b"), cty.StringVal("c"), 925 }), 926 }, 927 }, 928 929 "tostring": { 930 { 931 `tostring("a")`, 932 cty.StringVal("a"), 933 }, 934 }, 935 936 "transpose": { 937 { 938 `transpose({"a" = ["1", "2"], "b" = ["2", "3"]})`, 939 cty.MapVal(map[string]cty.Value{ 940 "1": cty.ListVal([]cty.Value{cty.StringVal("a")}), 941 "2": cty.ListVal([]cty.Value{cty.StringVal("a"), cty.StringVal("b")}), 942 "3": cty.ListVal([]cty.Value{cty.StringVal("b")}), 943 }), 944 }, 945 }, 946 947 "trim": { 948 { 949 `trim("?!hello?!", "!?")`, 950 cty.StringVal("hello"), 951 }, 952 }, 953 954 "trimprefix": { 955 { 956 `trimprefix("helloworld", "hello")`, 957 cty.StringVal("world"), 958 }, 959 }, 960 961 "trimspace": { 962 { 963 `trimspace(" hello ")`, 964 cty.StringVal("hello"), 965 }, 966 }, 967 968 "trimsuffix": { 969 { 970 `trimsuffix("helloworld", "world")`, 971 cty.StringVal("hello"), 972 }, 973 }, 974 975 "try": { 976 { 977 // Note: "try" only works with expressions that pass static 978 // validation, because it only gets an opportunity to run in 979 // that case. The following "works" (captures the error) because 980 // Terraform understands it as a reference to an attribute 981 // that does not exist during dynamic evaluation. 982 // 983 // "try" doesn't work with references that could never possibly 984 // be valid and are thus caught during static validation, such 985 // as an expression like "foo" alone which would be understood 986 // as an invalid resource reference. That's okay because this 987 // function exists primarily to ease access to dynamically-typed 988 // structures that Terraform can't statically validate by 989 // definition. 990 `try({}.baz, "fallback")`, 991 cty.StringVal("fallback"), 992 }, 993 { 994 `try("fallback")`, 995 cty.StringVal("fallback"), 996 }, 997 }, 998 999 "upper": { 1000 { 1001 `upper("hello")`, 1002 cty.StringVal("HELLO"), 1003 }, 1004 }, 1005 1006 "urlencode": { 1007 { 1008 `urlencode("foo:bar@localhost?foo=bar&bar=baz")`, 1009 cty.StringVal("foo%3Abar%40localhost%3Ffoo%3Dbar%26bar%3Dbaz"), 1010 }, 1011 }, 1012 1013 "uuidv5": { 1014 { 1015 `uuidv5("dns", "tada")`, 1016 cty.StringVal("faa898db-9b9d-5b75-86a9-149e7bb8e3b8"), 1017 }, 1018 { 1019 `uuidv5("url", "tada")`, 1020 cty.StringVal("2c1ff6b4-211f-577e-94de-d978b0caa16e"), 1021 }, 1022 { 1023 `uuidv5("oid", "tada")`, 1024 cty.StringVal("61eeea26-5176-5288-87fc-232d6ed30d2f"), 1025 }, 1026 { 1027 `uuidv5("x500", "tada")`, 1028 cty.StringVal("7e12415e-f7c9-57c3-9e43-52dc9950d264"), 1029 }, 1030 { 1031 `uuidv5("6ba7b810-9dad-11d1-80b4-00c04fd430c8", "tada")`, 1032 cty.StringVal("faa898db-9b9d-5b75-86a9-149e7bb8e3b8"), 1033 }, 1034 }, 1035 1036 "values": { 1037 { 1038 `values({"hello"="world", "what's"="up"})`, 1039 cty.TupleVal([]cty.Value{ 1040 cty.StringVal("world"), 1041 cty.StringVal("up"), 1042 }), 1043 }, 1044 }, 1045 1046 "yamldecode": { 1047 { 1048 `yamldecode("true")`, 1049 cty.True, 1050 }, 1051 { 1052 `yamldecode("key: 0ba")`, 1053 cty.ObjectVal(map[string]cty.Value{ 1054 "key": cty.StringVal("0ba"), 1055 }), 1056 }, 1057 }, 1058 1059 "yamlencode": { 1060 { 1061 `yamlencode(["foo", "bar", true])`, 1062 cty.StringVal("- \"foo\"\n- \"bar\"\n- true\n"), 1063 }, 1064 { 1065 `yamlencode({a = "b", c = "d"})`, 1066 cty.StringVal("\"a\": \"b\"\n\"c\": \"d\"\n"), 1067 }, 1068 { 1069 `yamlencode(true)`, 1070 // the ... here is an "end of document" marker, produced for implied primitive types only 1071 cty.StringVal("true\n...\n"), 1072 }, 1073 }, 1074 1075 "zipmap": { 1076 { 1077 `zipmap(["hello", "bar"], ["world", "baz"])`, 1078 cty.ObjectVal(map[string]cty.Value{ 1079 "hello": cty.StringVal("world"), 1080 "bar": cty.StringVal("baz"), 1081 }), 1082 }, 1083 }, 1084 } 1085 1086 experimentalFuncs := map[string]experiments.Experiment{} 1087 experimentalFuncs["defaults"] = experiments.ModuleVariableOptionalAttrs 1088 1089 t.Run("all functions are tested", func(t *testing.T) { 1090 data := &dataForTests{} // no variables available; we only need literals here 1091 scope := &Scope{ 1092 Data: data, 1093 BaseDir: "./testdata/functions-test", // for the functions that read from the filesystem 1094 } 1095 1096 // Check that there is at least one test case for each function, omitting 1097 // those functions that do not return consistent values 1098 allFunctions := scope.Functions() 1099 1100 // TODO: we can test the impure functions partially by configuring the scope 1101 // with PureOnly: true and then verify that they return unknown values of a 1102 // suitable type. 1103 for _, impureFunc := range impureFunctions { 1104 delete(allFunctions, impureFunc) 1105 } 1106 for f := range scope.Functions() { 1107 if _, ok := tests[f]; !ok { 1108 t.Errorf("Missing test for function %s\n", f) 1109 } 1110 } 1111 }) 1112 1113 for funcName, funcTests := range tests { 1114 t.Run(funcName, func(t *testing.T) { 1115 1116 // prepareScope starts as a no-op, but if a function is marked as 1117 // experimental in our experimentalFuncs table above then we'll 1118 // reassign this to be a function that activates the appropriate 1119 // experiment. 1120 prepareScope := func(t *testing.T, scope *Scope) {} 1121 1122 if experiment, isExperimental := experimentalFuncs[funcName]; isExperimental { 1123 // First, we'll run all of the tests without the experiment 1124 // enabled to see that they do actually fail in that case. 1125 for _, test := range funcTests { 1126 testName := fmt.Sprintf("experimental(%s)", test.src) 1127 t.Run(testName, func(t *testing.T) { 1128 data := &dataForTests{} // no variables available; we only need literals here 1129 scope := &Scope{ 1130 Data: data, 1131 BaseDir: "./testdata/functions-test", // for the functions that read from the filesystem 1132 } 1133 1134 expr, parseDiags := hclsyntax.ParseExpression([]byte(test.src), "test.hcl", hcl.Pos{Line: 1, Column: 1}) 1135 if parseDiags.HasErrors() { 1136 for _, diag := range parseDiags { 1137 t.Error(diag.Error()) 1138 } 1139 return 1140 } 1141 1142 _, diags := scope.EvalExpr(expr, cty.DynamicPseudoType) 1143 if !diags.HasErrors() { 1144 t.Errorf("experimental function %q succeeded without its experiment %s enabled\nexpr: %s", funcName, experiment.Keyword(), test.src) 1145 } 1146 }) 1147 } 1148 1149 // Now make the experiment active in the scope so that the 1150 // function will actually work when we test it below. 1151 prepareScope = func(t *testing.T, scope *Scope) { 1152 t.Helper() 1153 t.Logf("activating experiment %s to test %q", experiment.Keyword(), funcName) 1154 experimentsSet := experiments.NewSet() 1155 experimentsSet.Add(experiment) 1156 scope.SetActiveExperiments(experimentsSet) 1157 } 1158 } 1159 1160 for _, test := range funcTests { 1161 t.Run(test.src, func(t *testing.T) { 1162 data := &dataForTests{} // no variables available; we only need literals here 1163 scope := &Scope{ 1164 Data: data, 1165 BaseDir: "./testdata/functions-test", // for the functions that read from the filesystem 1166 } 1167 prepareScope(t, scope) 1168 1169 expr, parseDiags := hclsyntax.ParseExpression([]byte(test.src), "test.hcl", hcl.Pos{Line: 1, Column: 1}) 1170 if parseDiags.HasErrors() { 1171 for _, diag := range parseDiags { 1172 t.Error(diag.Error()) 1173 } 1174 return 1175 } 1176 1177 got, diags := scope.EvalExpr(expr, cty.DynamicPseudoType) 1178 if diags.HasErrors() { 1179 for _, diag := range diags { 1180 t.Errorf("%s: %s", diag.Description().Summary, diag.Description().Detail) 1181 } 1182 return 1183 } 1184 1185 if !test.want.RawEquals(got) { 1186 t.Errorf("wrong result\nexpr: %s\ngot: %#v\nwant: %#v", test.src, got, test.want) 1187 } 1188 }) 1189 } 1190 }) 1191 } 1192 } 1193 1194 const ( 1195 CipherBase64 = "eczGaDhXDbOFRZGhjx2etVzWbRqWDlmq0bvNt284JHVbwCgObiuyX9uV0LSAMY707IEgMkExJqXmsB4OWKxvB7epRB9G/3+F+pcrQpODlDuL9oDUAsa65zEpYF0Wbn7Oh7nrMQncyUPpyr9WUlALl0gRWytOA23S+y5joa4M34KFpawFgoqTu/2EEH4Xl1zo+0fy73fEto+nfkUY+meuyGZ1nUx/+DljP7ZqxHBFSlLODmtuTMdswUbHbXbWneW51D7Jm7xB8nSdiA2JQNK5+Sg5x8aNfgvFTt/m2w2+qpsyFa5Wjeu6fZmXSl840CA07aXbk9vN4I81WmJyblD/ZA==" 1196 PrivateKey = ` 1197 -----BEGIN RSA PRIVATE KEY----- 1198 MIIEowIBAAKCAQEAgUElV5mwqkloIrM8ZNZ72gSCcnSJt7+/Usa5G+D15YQUAdf9 1199 c1zEekTfHgDP+04nw/uFNFaE5v1RbHaPxhZYVg5ZErNCa/hzn+x10xzcepeS3KPV 1200 Xcxae4MR0BEegvqZqJzN9loXsNL/c3H/B+2Gle3hTxjlWFb3F5qLgR+4Mf4ruhER 1201 1v6eHQa/nchi03MBpT4UeJ7MrL92hTJYLdpSyCqmr8yjxkKJDVC2uRrr+sTSxfh7 1202 r6v24u/vp/QTmBIAlNPgadVAZw17iNNb7vjV7Gwl/5gHXonCUKURaV++dBNLrHIZ 1203 pqcAM8wHRph8mD1EfL9hsz77pHewxolBATV+7QIDAQABAoIBAC1rK+kFW3vrAYm3 1204 +8/fQnQQw5nec4o6+crng6JVQXLeH32qXShNf8kLLG/Jj0vaYcTPPDZw9JCKkTMQ 1205 0mKj9XR/5DLbBMsV6eNXXuvJJ3x4iKW5eD9WkLD4FKlNarBRyO7j8sfPTqXW7uat 1206 NxWdFH7YsSRvNh/9pyQHLWA5OituidMrYbc3EUx8B1GPNyJ9W8Q8znNYLfwYOjU4 1207 Wv1SLE6qGQQH9Q0WzA2WUf8jklCYyMYTIywAjGb8kbAJlKhmj2t2Igjmqtwt1PYc 1208 pGlqbtQBDUiWXt5S4YX/1maIQ/49yeNUajjpbJiH3DbhJbHwFTzP3pZ9P9GHOzlG 1209 kYR+wSECgYEAw/Xida8kSv8n86V3qSY/I+fYQ5V+jDtXIE+JhRnS8xzbOzz3v0WS 1210 Oo5H+o4nJx5eL3Ghb3Gcm0Jn46dHrxinHbm+3RjXv/X6tlbxIYjRSQfHOTSMCTvd 1211 qcliF5vC6RCLXuc7R+IWR1Ky6eDEZGtrvt3DyeYABsp9fRUFR/6NluUCgYEAqNsw 1212 1aSl7WJa27F0DoJdlU9LWerpXcazlJcIdOz/S9QDmSK3RDQTdqfTxRmrxiYI9LEs 1213 mkOkvzlnnOBMpnZ3ZOU5qIRfprecRIi37KDAOHWGnlC0EWGgl46YLb7/jXiWf0AG 1214 Y+DfJJNd9i6TbIDWu8254/erAS6bKMhW/3q7f2kCgYAZ7Id/BiKJAWRpqTRBXlvw 1215 BhXoKvjI2HjYP21z/EyZ+PFPzur/lNaZhIUlMnUfibbwE9pFggQzzf8scM7c7Sf+ 1216 mLoVSdoQ/Rujz7CqvQzi2nKSsM7t0curUIb3lJWee5/UeEaxZcmIufoNUrzohAWH 1217 BJOIPDM4ssUTLRq7wYM9uQKBgHCBau5OP8gE6mjKuXsZXWUoahpFLKwwwmJUp2vQ 1218 pOFPJ/6WZOlqkTVT6QPAcPUbTohKrF80hsZqZyDdSfT3peFx4ZLocBrS56m6NmHR 1219 UYHMvJ8rQm76T1fryHVidz85g3zRmfBeWg8yqT5oFg4LYgfLsPm1gRjOhs8LfPvI 1220 OLlRAoGBAIZ5Uv4Z3s8O7WKXXUe/lq6j7vfiVkR1NW/Z/WLKXZpnmvJ7FgxN4e56 1221 RXT7GwNQHIY8eDjDnsHxzrxd+raOxOZeKcMHj3XyjCX3NHfTscnsBPAGYpY/Wxzh 1222 T8UYnFu6RzkixElTf2rseEav7rkdKkI3LAeIZy7B0HulKKsmqVQ7 1223 -----END RSA PRIVATE KEY----- 1224 ` 1225 Poem = `Fleas: 1226 Adam 1227 Had'em 1228 1229 E.E. Cummings` 1230 )