-
Notifications
You must be signed in to change notification settings - Fork 0
/
Checksumer_test.go
190 lines (170 loc) · 4.38 KB
/
Checksumer_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main_test
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sigs.k8s.io/kustomize/v3/pkg/kusttest"
"sigs.k8s.io/kustomize/v3/pkg/loader"
"sigs.k8s.io/kustomize/v3/pkg/pgmconfig"
"sigs.k8s.io/kustomize/v3/pkg/plugins"
"strings"
"testing"
)
// Start "sigs.k8s.io/kustomize/v3/pkg/plugins/test" override
// We need EnvForTest "workDir" attribute to be accessible
// EnvForTest manages the plugin test environment.
// It sets/resets XDG_CONFIG_HOME, makes/removes a temp objRoot.
type EnvForTest struct {
t *testing.T
compiler *plugins.Compiler
workDir string
oldXdg string
wasSet bool
}
func NewEnvForTest(t *testing.T) *EnvForTest {
return &EnvForTest{t: t}
}
func (x *EnvForTest) Set() *EnvForTest {
x.createWorkDir()
x.compiler = x.makeCompiler()
x.setEnv()
return x
}
func (x *EnvForTest) Reset() {
x.resetEnv()
x.removeWorkDir()
}
func (x *EnvForTest) BuildGoPlugin(g, v, k string) {
err := x.compiler.Compile(g, v, k)
if err != nil {
x.t.Errorf("compile failed: %v", err)
}
}
func (x *EnvForTest) BuildExecPlugin(g, v, k string) {
lowK := strings.ToLower(k)
obj := filepath.Join(x.compiler.ObjRoot(), g, v, lowK, k)
src := filepath.Join(x.compiler.SrcRoot(), g, v, lowK, k)
if err := os.MkdirAll(filepath.Dir(obj), 0755); err != nil {
x.t.Errorf("error making directory: %s", filepath.Dir(obj))
}
cmd := exec.Command("cp", src, obj)
cmd.Env = os.Environ()
if err := cmd.Run(); err != nil {
x.t.Errorf("error copying %s to %s: %v", src, obj, err)
}
}
func (x *EnvForTest) makeCompiler() *plugins.Compiler {
// The plugin loader wants to find object code under
// $XDG_CONFIG_HOME/kustomize/plugins
// and the compiler writes object code to
// $objRoot
// so set things up accordingly.
objRoot := filepath.Join(
x.workDir, pgmconfig.ProgramName, pgmconfig.PluginRoot)
err := os.MkdirAll(objRoot, os.ModePerm)
if err != nil {
x.t.Error(err)
}
srcRoot, err := plugins.DefaultSrcRoot()
if err != nil {
x.t.Error(err)
}
return plugins.NewCompiler(srcRoot, objRoot)
}
func (x *EnvForTest) createWorkDir() {
var err error
x.workDir, err = ioutil.TempDir("", "kustomize-plugin-tests")
if err != nil {
x.t.Errorf("failed to make work dir: %v", err)
}
}
func (x *EnvForTest) removeWorkDir() {
err := os.RemoveAll(x.workDir)
if err != nil {
x.t.Errorf(
"removing work dir: %s %v", x.workDir, err)
}
}
func (x *EnvForTest) setEnv() {
x.oldXdg, x.wasSet = os.LookupEnv(pgmconfig.XDG_CONFIG_HOME)
os.Setenv(pgmconfig.XDG_CONFIG_HOME, x.workDir)
}
func (x *EnvForTest) resetEnv() {
if x.wasSet {
os.Setenv(pgmconfig.XDG_CONFIG_HOME, x.oldXdg)
} else {
os.Unsetenv(pgmconfig.XDG_CONFIG_HOME)
}
}
// End plugins_test override
func createFileWithinTestEnv(path string, content string) error {
err := os.MkdirAll(filepath.Dir(path), os.FileMode(int(0750)))
if err != nil {
return err
}
err = ioutil.WriteFile(path, []byte(content), os.FileMode(int(0640)))
return err
}
func TestTransformer(t *testing.T) {
tc := NewEnvForTest(t).Set()
defer tc.Reset()
kustomizeRootPath := tc.workDir + "/app"
err := os.MkdirAll(kustomizeRootPath, os.FileMode(int(0750)))
if err != nil {
panic(err)
}
tc.BuildGoPlugin("github.com/oboukili", "", "Checksumer")
th := kusttest_test.NewKustTestHarnessFull(t, kustomizeRootPath, loader.RestrictionNone, plugins.ActivePluginConfig())
err = createFileWithinTestEnv(kustomizeRootPath+"/../test/foobarbaz", "foobarbaz")
if err != nil {
panic(err)
}
err = createFileWithinTestEnv(kustomizeRootPath+"/../test/bar", "bar")
if err != nil {
panic(err)
}
err = createFileWithinTestEnv(kustomizeRootPath+"/../test/sub/foo", "foo")
if err != nil {
panic(err)
}
rm := th.LoadAndRunTransformer(`
apiVersion: github.com/oboukili
kind: Checksumer
metadata:
name: notImportantHere
files:
- key: directory
path: ../test
recurse: false
- key: directoryRecursive
path: ../test
recurse: true
- key: file
path: ../test/foobarbaz
fieldSpecs:
- path: metadata/annotations
create: true
`, `
apiVersion: v1
kind: Service
metadata:
name: myService
spec:
ports:
- port: 7002
`)
th.AssertActualEqualsExpected(rm, `
apiVersion: v1
kind: Service
metadata:
annotations:
directory: 7567aa21664edd14a9eeac26efd9c07a7eed914e
directoryRecursive: 01cbe21c720fb38989f494dfcc3c4635c156bc04
file: 5f5513f8822fdbe5145af33b64d8d970dcf95c6e
name: myService
spec:
ports:
- port: 7002
`)
}