blob: ab6d60cf3cebd027b12937c7b871961d64673f42 [file] [log] [blame]
Serge Bazanskibe538db2020-11-12 00:22:42 +01001// 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
16package cmd
17
18import (
19 "bytes"
20 "encoding/json"
21 "os"
22 "path/filepath"
23 "reflect"
24 "testing"
25
26 "github.com/spf13/cobra"
27 "github.com/spf13/pflag"
28 "gopkg.in/yaml.v2"
29)
30
31func resetFlagsOf(cmd *cobra.Command) {
32 cmd.Flags().VisitAll(func(f *pflag.Flag) {
33 if sv, ok := f.Value.(pflag.SliceValue); ok {
34 sv.Replace(nil)
35 } else {
36 f.Value.Set(f.DefValue)
37 }
38 })
39}
40
41func cmdOutput(t *testing.T, args []string) string {
42 var buf bytes.Buffer
43 RootCmd.SetOutput(&buf)
44 defer RootCmd.SetOutput(nil)
45
46 t.Log("Running args", args)
47 RootCmd.SetArgs(args)
48 if err := RootCmd.Execute(); err != nil {
49 t.Fatal("command failed:", err)
50 }
51
52 return buf.String()
53}
54
55func TestShow(t *testing.T) {
56 formats := map[string]func(string) (interface{}, error){
57 "json": func(text string) (ret interface{}, err error) {
58 err = json.Unmarshal([]byte(text), &ret)
59 return
60 },
61 "yaml": func(text string) (ret interface{}, err error) {
62 err = yaml.Unmarshal([]byte(text), &ret)
63 return
64 },
65 }
66
67 // Use the fact that JSON is also valid YAML ..
68 expected := `
69{
70 "apiVersion": "v0alpha1",
71 "kind": "TestObject",
72 "nil": null,
73 "bool": true,
74 "number": 42,
75 "string": "bar",
76 "notAVal": "aVal",
77 "notAnotherVal": "aVal2",
78 "filevar": "foo\n",
79 "array": ["one", 2, [3]],
80 "object": {"foo": "bar"},
81 "extcode": {"foo": 1, "bar": "test"}
82}
83`
84
85 for format, parser := range formats {
86 expected, err := parser(expected)
87 if err != nil {
88 t.Fatalf("error parsing *expected* value: %v", err)
89 }
90
91 os.Setenv("anVar", "aVal2")
92 defer os.Unsetenv("anVar")
93
94 output := cmdOutput(t, []string{"show",
95 "-J", filepath.FromSlash("../testdata/lib"),
96 "-o", format,
97 filepath.FromSlash("../testdata/test.jsonnet"),
98 "-V", "aVar=aVal",
99 "-V", "anVar",
100 "--ext-str-file", "filevar=" + filepath.FromSlash("../testdata/extvar.file"),
101 "--ext-code", `extcode={foo: 1, bar: "test"}`,
102 })
103 defer resetFlagsOf(RootCmd)
104
105 t.Log("output is", output)
106 actual, err := parser(output)
107 if err != nil {
108 t.Errorf("error parsing output of format %s: %v", format, err)
109 } else if !reflect.DeepEqual(expected, actual) {
110 t.Errorf("format %s expected != actual: %s != %s", format, expected, actual)
111 }
112 }
113}
114
115func TestShowUsingExtVarFiles(t *testing.T) {
116 expectedText := `
117{
118 "apiVersion": "v1",
119 "kind": "ConfigMap",
120 "metadata": {
121 "name": "sink"
122 },
123 "data": {
124 "input": {
125 "greeting": "Hello!",
126 "helper": true,
127 "top": true
128 },
129 "var": "I'm a var!"
130 }
131}
132`
133 var expected interface{}
134 if err := json.Unmarshal([]byte(expectedText), &expected); err != nil {
135 t.Fatalf("error parsing *expected* value: %v", err)
136 }
137
138 cwd, err := os.Getwd()
139 if err != nil {
140 t.Fatalf("failed to get current working directory: %v", err)
141 }
142 if err := os.Chdir("../testdata/extvars/feed"); err != nil {
143 t.Fatalf("failed to change to target directory: %v", err)
144 }
145 defer os.Chdir(cwd)
146
147 output := cmdOutput(t, []string{"show",
148 "top.jsonnet",
149 "-o", "json",
150 "--tla-code-file", "input=input.jsonnet",
151 "--tla-code-file", "sink=sink.jsonnet",
152 "--ext-str-file", "filevar=var.txt",
153 })
154 defer resetFlagsOf(RootCmd)
155
156 t.Log("output is", output)
157 var actual interface{}
158 err = json.Unmarshal([]byte(output), &actual)
159 if err != nil {
160 t.Errorf("error parsing output: %v", err)
161 } else if !reflect.DeepEqual(expected, actual) {
162 t.Errorf("expected != actual: %s != %s", expected, actual)
163 }
164}