Serge Bazanski | be538db | 2020-11-12 00:22:42 +0100 | [diff] [blame] | 1 | // Copyright 2017 The kubecfg authors |
| 2 | // |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | package utils |
| 17 | |
| 18 | import ( |
| 19 | "testing" |
| 20 | |
| 21 | jsonnet "github.com/google/go-jsonnet" |
| 22 | ) |
| 23 | |
| 24 | // check there is no err, and a == b. |
| 25 | func check(t *testing.T, err error, actual, expected string) { |
| 26 | if err != nil { |
| 27 | t.Errorf("Expected %q, got error: %q", expected, err.Error()) |
| 28 | } else if actual != expected { |
| 29 | t.Errorf("Expected %q, got %q", expected, actual) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func TestParseJson(t *testing.T) { |
| 34 | vm := jsonnet.MakeVM() |
| 35 | RegisterNativeFuncs(vm, NewIdentityResolver()) |
| 36 | |
| 37 | _, err := vm.EvaluateSnippet("failtest", `std.native("parseJson")("barf{")`) |
| 38 | if err == nil { |
| 39 | t.Errorf("parseJson succeeded on invalid json") |
| 40 | } |
| 41 | |
| 42 | x, err := vm.EvaluateSnippet("test", `std.native("parseJson")("null")`) |
| 43 | check(t, err, x, "null\n") |
| 44 | |
| 45 | x, err = vm.EvaluateSnippet("test", ` |
| 46 | local a = std.native("parseJson")('{"foo": 3, "bar": 4}'); |
| 47 | a.foo + a.bar`) |
| 48 | check(t, err, x, "7\n") |
| 49 | } |
| 50 | |
| 51 | func TestParseYaml(t *testing.T) { |
| 52 | vm := jsonnet.MakeVM() |
| 53 | RegisterNativeFuncs(vm, NewIdentityResolver()) |
| 54 | |
| 55 | _, err := vm.EvaluateSnippet("failtest", `std.native("parseYaml")("[barf")`) |
| 56 | if err == nil { |
| 57 | t.Errorf("parseYaml succeeded on invalid yaml") |
| 58 | } |
| 59 | |
| 60 | x, err := vm.EvaluateSnippet("test", `std.native("parseYaml")("")`) |
| 61 | check(t, err, x, "[ ]\n") |
| 62 | |
| 63 | x, err = vm.EvaluateSnippet("test", ` |
| 64 | local a = std.native("parseYaml")("foo:\n- 3\n- 4\n")[0]; |
| 65 | a.foo[0] + a.foo[1]`) |
| 66 | check(t, err, x, "7\n") |
| 67 | |
| 68 | x, err = vm.EvaluateSnippet("test", ` |
| 69 | local a = std.native("parseYaml")("---\nhello\n---\nworld"); |
| 70 | a[0] + a[1]`) |
| 71 | check(t, err, x, "\"helloworld\"\n") |
| 72 | } |
| 73 | |
| 74 | func TestRegexMatch(t *testing.T) { |
| 75 | vm := jsonnet.MakeVM() |
| 76 | RegisterNativeFuncs(vm, NewIdentityResolver()) |
| 77 | |
| 78 | _, err := vm.EvaluateSnippet("failtest", `std.native("regexMatch")("[f", "foo")`) |
| 79 | if err == nil { |
| 80 | t.Errorf("regexMatch succeeded with invalid regex") |
| 81 | } |
| 82 | |
| 83 | x, err := vm.EvaluateSnippet("test", `std.native("regexMatch")("foo.*", "seafood")`) |
| 84 | check(t, err, x, "true\n") |
| 85 | |
| 86 | x, err = vm.EvaluateSnippet("test", `std.native("regexMatch")("bar.*", "seafood")`) |
| 87 | check(t, err, x, "false\n") |
| 88 | } |
| 89 | |
| 90 | func TestRegexSubst(t *testing.T) { |
| 91 | vm := jsonnet.MakeVM() |
| 92 | RegisterNativeFuncs(vm, NewIdentityResolver()) |
| 93 | |
| 94 | _, err := vm.EvaluateSnippet("failtest", `std.native("regexSubst")("[f",s "foo", "bar")`) |
| 95 | if err == nil { |
| 96 | t.Errorf("regexSubst succeeded with invalid regex") |
| 97 | } |
| 98 | |
| 99 | x, err := vm.EvaluateSnippet("test", `std.native("regexSubst")("a(x*)b", "-ab-axxb-", "T")`) |
| 100 | check(t, err, x, "\"-T-T-\"\n") |
| 101 | |
| 102 | x, err = vm.EvaluateSnippet("test", `std.native("regexSubst")("a(x*)b", "-ab-axxb-", "${1}W")`) |
| 103 | check(t, err, x, "\"-W-xxW-\"\n") |
| 104 | } |