-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker_test.go
229 lines (198 loc) · 6.7 KB
/
checker_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
229
package oachecker
import (
"os"
"path/filepath"
"testing"
)
// TestCheckChart tests the CheckChart function
func TestCheckChart(t *testing.T) {
// Test with valid chart path
err := CheckChart("testdata/firefox")
if err != nil {
t.Errorf("CheckChart failed with valid chart path: %v", err)
}
}
// TestCheckManifest tests the CheckManifest function
func TestCheckManifest(t *testing.T) {
// Get a valid app configuration
cfg, err := GetAppConfiguration("testdata/firefox")
if err != nil {
t.Fatalf("Failed to get app configuration: %v", err)
}
// Test with valid chart path and configuration
err = CheckManifest("testdata/firefox", cfg)
if err != nil {
t.Errorf("CheckManifest failed with valid inputs: %v", err)
}
// Test with invalid configuration (modify a required field)
invalidCfg := *cfg
invalidCfg.ConfigVersion = ""
err = CheckManifest("testdata/firefox", &invalidCfg)
if err == nil {
t.Error("CheckManifest should fail with invalid configuration")
}
}
// TestLint tests the Lint function
func TestLint(t *testing.T) {
// Test with valid chart path and default options
err := Lint("testdata/firefox", DefaultLintOptions())
if err != nil {
t.Errorf("Lint failed with valid chart path and default options: %v", err)
}
// Test with custom options
options := DefaultLintOptions().
WithOwner("test-owner").
WithAdmin("test-owner").
WithCustomValidator(func(oacPath string, cfg *AppConfiguration) error {
return nil // Always pass
})
err = Lint("testdata/firefox", options)
if err != nil {
t.Errorf("Lint failed with valid chart path and custom options: %v", err)
}
// Test with skipped manifest check
options = DefaultLintOptions().SkipManifest()
err = Lint("testdata/firefox", options)
if err != nil {
t.Errorf("Lint failed with skipped manifest check: %v", err)
}
// Test with skipped resource check
options = DefaultLintOptions().SkipResources()
err = Lint("testdata/firefox", options)
if err != nil {
t.Errorf("Lint failed with skipped resource check: %v", err)
}
// Test with invalid chart path
err = Lint("testdata/nonexistent", DefaultLintOptions())
if err == nil {
t.Error("Lint should fail with invalid chart path")
}
}
// TestLintWithDefaultOptions tests the LintWithDefaultOptions function
func TestLintWithDefaultOptions(t *testing.T) {
// Test with valid chart path
err := LintWithDefaultOptions("testdata/firefox")
if err != nil {
t.Errorf("LintWithDefaultOptions failed with valid chart path: %v", err)
}
// Test with invalid chart path
err = LintWithDefaultOptions("testdata/nonexistent")
if err == nil {
t.Error("LintWithDefaultOptions should fail with invalid chart path")
}
}
// TestLintWithSameOwnerAdmin tests the LintWithSameOwnerAdmin function
func TestLintWithSameOwnerAdmin(t *testing.T) {
// Test with valid chart path and owner/admin
err := LintWithSameOwnerAdmin("testdata/firefox", "test-owner-admin")
if err != nil {
t.Errorf("LintWithSameOwnerAdmin failed with valid inputs: %v", err)
}
// Test with invalid chart path
err = LintWithSameOwnerAdmin("testdata/nonexistent", "test-owner-admin")
if err == nil {
t.Error("LintWithSameOwnerAdmin should fail with invalid chart path")
}
}
// TestLintWithDifferentOwnerAdmin tests the LintWithDifferentOwnerAdmin function
func TestLintWithDifferentOwnerAdmin(t *testing.T) {
// Test with valid chart path and different owner/admin
err := LintWithDifferentOwnerAdmin("testdata/firefox", "test-owner", "test-admin")
if err != nil {
t.Errorf("LintWithDifferentOwnerAdmin failed with valid inputs: %v", err)
}
// Test with invalid chart path
err = LintWithDifferentOwnerAdmin("testdata/nonexistent", "test-owner", "test-admin")
if err == nil {
t.Error("LintWithDifferentOwnerAdmin should fail with invalid chart path")
}
}
// TestLintOptions tests the LintOptions methods
func TestLintOptions(t *testing.T) {
// Test DefaultLintOptions
options := DefaultLintOptions()
if options.Owner != "" || options.Admin != "" || options.SkipManifestCheck || options.SkipResourceCheck || len(options.CustomValidators) != 0 {
t.Error("DefaultLintOptions returned unexpected values")
}
// Test WithOwner
options = DefaultLintOptions().WithOwner("test-owner")
if options.Owner != "test-owner" {
t.Errorf("WithOwner failed, expected 'test-owner', got '%s'", options.Owner)
}
// Test WithAdmin
options = DefaultLintOptions().WithAdmin("test-admin")
if options.Admin != "test-admin" {
t.Errorf("WithAdmin failed, expected 'test-admin', got '%s'", options.Admin)
}
// Test WithSameOwnerAndAdmin
options = DefaultLintOptions().WithSameOwnerAndAdmin("test-both")
if options.Owner != "test-both" || options.Admin != "test-both" {
t.Errorf("WithSameOwnerAndAdmin failed, expected both 'test-both', got Owner: '%s', Admin: '%s'", options.Owner, options.Admin)
}
// Test WithCustomValidator
customValidator := func(oacPath string, cfg *AppConfiguration) error {
return nil
}
options = DefaultLintOptions().WithCustomValidator(customValidator)
if len(options.CustomValidators) != 1 {
t.Errorf("WithCustomValidator failed, expected 1 validator, got %d", len(options.CustomValidators))
}
// Test WithAppDataValidator
options = DefaultLintOptions()
options.WithAppDataValidator()
if len(options.CustomValidators) != 1 {
t.Errorf("WithAppDataValidator failed, expected 1 validator, got %d", len(options.CustomValidators))
}
// Test SkipManifest
options = DefaultLintOptions().SkipManifest()
if !options.SkipManifestCheck {
t.Error("SkipManifest failed, SkipManifestCheck should be true")
}
// Test SkipResources
options = DefaultLintOptions().SkipResources()
if !options.SkipResourceCheck {
t.Error("SkipResources failed, SkipResourceCheck should be true")
}
}
// Helper function to create a temporary test chart
func createTempTestChart(t *testing.T) string {
tempDir, err := os.MkdirTemp("", "chart-test-*")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
// Copy the test chart to the temp directory
err = copyDir("testdata/firefox", tempDir)
if err != nil {
os.RemoveAll(tempDir)
t.Fatalf("Failed to copy test chart: %v", err)
}
return tempDir
}
// Helper function to copy a directory recursively
func copyDir(src, dst string) error {
entries, err := os.ReadDir(src)
if err != nil {
return err
}
for _, entry := range entries {
srcPath := filepath.Join(src, entry.Name())
dstPath := filepath.Join(dst, entry.Name())
if entry.IsDir() {
if err := os.MkdirAll(dstPath, 0755); err != nil {
return err
}
if err := copyDir(srcPath, dstPath); err != nil {
return err
}
} else {
data, err := os.ReadFile(srcPath)
if err != nil {
return err
}
if err := os.WriteFile(dstPath, data, 0644); err != nil {
return err
}
}
}
return nil
}