forked from helmfile/vals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vals_test.go
118 lines (91 loc) · 2.2 KB
/
vals_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package vals
import (
"bytes"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestExec(t *testing.T) {
dir := t.TempDir()
// should evaluate to "x: baz"
data := []byte("x: ref+echo://foo/bar/baz#/foo/bar")
require.NoError(t, os.WriteFile(filepath.Join(dir, "input.yaml"), data, 0644))
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
c := ExecConfig{
StreamYAML: dir,
Stdout: stdout,
Stderr: stderr,
}
err := Exec(map[string]interface{}{}, []string{"cat"}, c)
require.NoError(t, err)
require.Equal(t, "x: baz\n", stdout.String())
}
func TestEnv(t *testing.T) {
input := make(map[string]interface{})
input["var1"] = "ref+echo://value"
input["var2"] = "ref+echo://val;ue"
input["var3"] = "ref+echo://val'ue"
input["var4"] = "ref+echo://'value"
var expected = []string{
"var1=value",
"var2=val;ue",
"var3=val'ue",
"var4='value",
}
got, err := Env(input)
require.NoError(t, err)
sort.Strings(got)
require.Equal(t, expected, got)
}
func TestQuotedEnv(t *testing.T) {
input := make(map[string]interface{})
input["var1"] = "ref+echo://value"
input["var2"] = "ref+echo://val;ue"
input["var3"] = "ref+echo://val'ue"
input["var4"] = "ref+echo://'value"
var expected = []string{
"var1=value",
`var2='val;ue'`,
`var3='val'"'"'ue'`,
`var4=''"'"'value'`,
}
got, err := QuotedEnv(input)
require.NoError(t, err)
sort.Strings(got)
require.Equal(t, expected, got)
}
func TestEvalNodes(t *testing.T) {
var yamlDocs = `apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
---
apiVersion: v1
data:
username: ref+echo://secrets.enc.yaml
kind: Secret
`
var expected = `apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
---
apiVersion: v1
data:
username: secrets.enc.yaml
kind: Secret
`
tmpFile, err := os.CreateTemp("", "secrets.yaml")
defer os.Remove(tmpFile.Name())
require.NoError(t, err)
_, err = tmpFile.WriteString(yamlDocs)
require.NoError(t, err)
input, err := Inputs(tmpFile.Name())
require.NoError(t, err)
nodes, err := EvalNodes(input, Options{})
require.NoError(t, err)
buf := new(strings.Builder)
err = Output(buf, "", nodes)
require.NoError(t, err)
require.Equal(t, expected, buf.String())
}