-
Notifications
You must be signed in to change notification settings - Fork 85
/
ksops_test.go
155 lines (141 loc) · 3.13 KB
/
ksops_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2019 viaduct.ai
// SPDX-License-Identifier: Apache-2.0
package main_test
import (
"bytes"
"os"
"os/exec"
"path"
"strings"
"testing"
)
func runKSOPSPluginIntegrationTest(t *testing.T, testDir string, kustomizeVersion string) {
want, err := os.ReadFile(path.Join(testDir, "want.yaml"))
if err != nil {
t.Fatalf("error readding expected resources file %s: %v", path.Join(testDir, "want.yaml"), err)
}
arg := []string{"build"}
flags := []string{"--enable-alpha-plugins", "--enable-exec"}
if kustomizeVersion == "v3" {
flags = []string{"--enable_alpha_plugins"}
}
arg = append(arg, flags...)
arg = append(arg, testDir)
cmd := exec.Command("kustomize", arg...)
out := bytes.Buffer{}
cmd.Stdout = &out
cmd.Stderr = &out
cmd.Run()
got := out.Bytes()
if !bytes.Equal(want, got) {
t.Errorf("wanted %s. got %s", want, got)
}
}
func TestKSOPSPluginInstallation(t *testing.T) {
tests := []struct {
name string
dir string
}{
{
name: "Legacy Single Resource",
dir: "test/legacy/single",
},
{
name: "Legacy Multiple Resources",
dir: "test/legacy/multiple",
},
{
name: "Legacy Hash Suffix",
dir: "test/legacy/hash",
},
{
name: "Legacy Replace Behavior",
dir: "test/legacy/behaviors",
},
{
name: "Legacy From File",
dir: "test/legacy/file",
},
{
name: "Legacy From Binary File",
dir: "test/legacy/binaryfile",
},
{
name: "Legacy From Envs",
dir: "test/legacy/envs",
},
{
name: "Legacy Override Key",
dir: "test/legacy/override",
},
{
name: "Legacy Secret Metadata",
dir: "test/legacy/metadata",
},
{
name: "KRM Single Resource",
dir: "test/krm/single",
},
{
name: "KRM Multiple Resources",
dir: "test/krm/multiple",
},
{
name: "KRM Hash Suffix",
dir: "test/krm/hash",
},
{
name: "KRM Replace Behavior",
dir: "test/krm/behaviors",
},
{
name: "KRM From File",
dir: "test/krm/file",
},
{
name: "KRM From Binary File",
dir: "test/krm/binaryfile",
},
{
name: "KRM From Envs",
dir: "test/krm/envs",
},
{
name: "KRM Override Key",
dir: "test/krm/override",
},
{
name: "KRM Secret Metadata",
dir: "test/krm/metadata",
},
}
// run kustomize version to validate installation
// and get kustomize version
cmd := exec.Command("kustomize", "version")
stdOut := bytes.Buffer{}
stdErr := bytes.Buffer{}
cmd.Stdout = &stdOut
cmd.Stderr = &stdErr
err := cmd.Run()
if err != nil {
t.Errorf("error running 'kustomize version': verify kustomize is installed: %v", err)
return
}
if stdErr.String() != "" {
t.Errorf("error running 'kustomize version': verify kustomize is installed: %s", stdErr.String())
return
}
// assume v4 (latest at time of writing)
kustomizeVersion := "v4"
// get std out and strip "ksops.v" string to simplify version matching
versionOutput := strings.ReplaceAll(stdOut.String(), "ksops.v", "")
if strings.Contains(versionOutput, "v3") {
t.Log("detected kustomize v3")
kustomizeVersion = "v3"
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
runKSOPSPluginIntegrationTest(t, tc.dir, kustomizeVersion)
})
}
}