-
Notifications
You must be signed in to change notification settings - Fork 104
/
render_test.go
228 lines (184 loc) · 7.01 KB
/
render_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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package __render_command_test
import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
const (
defaultExpectedDir = "default"
pinnedExpectedDir = "pinned"
bootstrapExpectedDir = "bootstrap"
noRefExpectedDir = "no-ref"
)
var (
assetsOutDir string
assetsInDirs []string
ppDir string
testDataPath string
defaultPinnedDir string
snoLegacyPinnedDir string
bootstrapPPDir string
extraMCPDir string
)
var _ = Describe("render command e2e test", func() {
BeforeEach(func() {
assetsOutDir = createTempAssetsDir()
assetsInDir := filepath.Join(workspaceDir, "test", "e2e", "performanceprofile", "cluster-setup", "base", "performance")
bootstrapPPDir = filepath.Join(workspaceDir, "test", "e2e", "performanceprofile", "cluster-setup", "bootstrap-cluster", "performance")
extraMCPDir = filepath.Join(workspaceDir, "test", "e2e", "performanceprofile", "cluster-setup", "bootstrap-cluster", "extra-mcp")
ppDir = filepath.Join(workspaceDir, "test", "e2e", "performanceprofile", "cluster-setup", "manual-cluster", "performance")
defaultPinnedDir = filepath.Join(workspaceDir, "test", "e2e", "performanceprofile", "cluster-setup", "pinned-cluster", "default")
snoLegacyPinnedDir = filepath.Join(workspaceDir, "test", "e2e", "performanceprofile", "cluster-setup", "pinned-cluster", "single-node-legacy")
testDataPath = filepath.Join(workspaceDir, "test", "e2e", "performanceprofile", "testdata")
assetsInDirs = []string{assetsInDir, ppDir}
})
Context("With a single performance-profile", func() {
It("Gets cli args and produces the expected components to output directory", func() {
cmdline := []string{
filepath.Join(binPath, "cluster-node-tuning-operator"),
"render",
"--asset-input-dir", strings.Join(assetsInDirs, ","),
"--asset-output-dir", assetsOutDir,
}
fmt.Fprintf(GinkgoWriter, "running: %v\n", cmdline)
cmd := exec.Command(cmdline[0], cmdline[1:]...)
runAndCompare(cmd, defaultExpectedDir)
})
It("Gets environment variables and produces the expected components to output directory", func() {
cmdline := []string{
filepath.Join(binPath, "cluster-node-tuning-operator"),
"render",
}
fmt.Fprintf(GinkgoWriter, "running: %v\n", cmdline)
cmd := exec.Command(cmdline[0], cmdline[1:]...)
cmd.Env = append(cmd.Env,
fmt.Sprintf("ASSET_INPUT_DIR=%s", strings.Join(assetsInDirs, ",")),
fmt.Sprintf("ASSET_OUTPUT_DIR=%s", assetsOutDir),
)
runAndCompare(cmd, defaultExpectedDir)
})
It("Must fail to restore legacy and wrong legacy owner reference if uid is missing", func() {
cmdline := []string{
filepath.Join(binPath, "cluster-node-tuning-operator"),
"render",
"--asset-input-dir", strings.Join(assetsInDirs, ","),
"--asset-output-dir", assetsOutDir,
"--owner-ref", "k8s",
}
fmt.Fprintf(GinkgoWriter, "running: %v\n", cmdline)
cmd := exec.Command(cmdline[0], cmdline[1:]...)
_, err := cmd.Output()
Expect(err).To(HaveOccurred(), logStderr(err))
})
It("Must not set any owner reference if disabled explicitely", func() {
cmdline := []string{
filepath.Join(binPath, "cluster-node-tuning-operator"),
"render",
"--asset-input-dir", strings.Join(assetsInDirs, ","),
"--asset-output-dir", assetsOutDir,
"--owner-ref", "none",
}
fmt.Fprintf(GinkgoWriter, "running: %v\n", cmdline)
cmd := exec.Command(cmdline[0], cmdline[1:]...)
cmd.Env = append(cmd.Env,
fmt.Sprintf("ASSET_INPUT_DIR=%s", strings.Join(assetsInDirs, ",")),
fmt.Sprintf("ASSET_OUTPUT_DIR=%s", assetsOutDir),
)
runAndCompare(cmd, noRefExpectedDir)
})
})
Context("With pinned cluster resources", func() {
It("Given default pinned infrastructure status, should render cpu partitioning configs", func() {
cmdline := []string{
filepath.Join(binPath, "cluster-node-tuning-operator"),
"render",
"--asset-input-dir", defaultPinnedDir,
"--asset-output-dir", assetsOutDir,
}
fmt.Fprintf(GinkgoWriter, "running: %v\n", cmdline)
cmd := exec.Command(cmdline[0], cmdline[1:]...)
runAndCompare(cmd, pinnedExpectedDir)
})
It("Given legacy SNO pinned infrastructure status, should render cpu partitioning configs", func() {
cmdline := []string{
filepath.Join(binPath, "cluster-node-tuning-operator"),
"render",
"--asset-input-dir", snoLegacyPinnedDir,
"--asset-output-dir", assetsOutDir,
}
fmt.Fprintf(GinkgoWriter, "running: %v\n", cmdline)
cmd := exec.Command(cmdline[0], cmdline[1:]...)
runAndCompare(cmd, pinnedExpectedDir)
})
})
Context("With no MCPs manifest resources during bootstrap", func() {
It("should render PerformanceProfile with default", func() {
bootstrapPPDirs := []string{bootstrapPPDir, defaultPinnedDir}
cmdline := []string{
filepath.Join(binPath, "cluster-node-tuning-operator"),
"render",
"--asset-input-dir", strings.Join(bootstrapPPDirs, ","),
"--asset-output-dir", assetsOutDir,
}
fmt.Fprintf(GinkgoWriter, "running: %v\n", cmdline)
cmd := exec.Command(cmdline[0], cmdline[1:]...)
runAndCompare(cmd, path.Join(bootstrapExpectedDir, "no-mcp"))
})
})
Context("With extra MCP manifest resources during bootstrap", func() {
It("should render PerformanceProfile with default", func() {
bootstrapPPDirs := []string{bootstrapPPDir, defaultPinnedDir, extraMCPDir}
cmdline := []string{
filepath.Join(binPath, "cluster-node-tuning-operator"),
"render",
"--asset-input-dir", strings.Join(bootstrapPPDirs, ","),
"--asset-output-dir", assetsOutDir,
}
fmt.Fprintf(GinkgoWriter, "running: %v\n", cmdline)
cmd := exec.Command(cmdline[0], cmdline[1:]...)
runAndCompare(cmd, path.Join(bootstrapExpectedDir, "extra-mcp"))
})
})
AfterEach(func() {
cleanArtifacts()
})
})
func createTempAssetsDir() string {
assets, err := os.MkdirTemp("", "assets")
Expect(err).ToNot(HaveOccurred())
fmt.Printf("assets` output dir at: %q\n", assets)
return assets
}
func cleanArtifacts() {
os.RemoveAll(assetsOutDir)
}
func runAndCompare(cmd *exec.Cmd, dir string) {
_, err := cmd.Output()
Expect(err).ToNot(HaveOccurred(), logStderr(err))
outputAssetsFiles, err := os.ReadDir(assetsOutDir)
Expect(err).ToNot(HaveOccurred())
refPath := filepath.Join(testDataPath, "render-expected-output", dir)
fmt.Fprintf(GinkgoWriter, "reference data at: %q\n", refPath)
for _, f := range outputAssetsFiles {
refData, err := os.ReadFile(filepath.Join(refPath, f.Name()))
Expect(err).ToNot(HaveOccurred())
data, err := os.ReadFile(filepath.Join(assetsOutDir, f.Name()))
Expect(err).ToNot(HaveOccurred())
diff, err := getFilesDiff(data, refData)
Expect(err).ToNot(HaveOccurred())
Expect(diff).To(BeZero(), "rendered %s file is not identical to its reference file; diff: %v",
f.Name(),
diff)
}
}
func logStderr(err error) string {
if exitErr, ok := err.(*exec.ExitError); ok {
return fmt.Sprintf("error running the command: [[%s]]", exitErr.Stderr)
}
return fmt.Sprintf("error running the command: [[%s]]", err)
}