This repository has been archived by the owner on Dec 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_test.go
246 lines (223 loc) · 5.31 KB
/
import_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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package pk8s
import (
"io"
"net/http"
"os"
"strings"
"testing"
)
func TestImporterRead(t *testing.T) {
tests := []struct {
name string
path string
setupMock func()
cleanupMock func()
wantData string
wantErr bool
}{
{
name: "Custom name",
path: "custom:=testfile.yaml",
setupMock: func() {
os.WriteFile("testfile.yaml", []byte("test data"), 0o644)
},
cleanupMock: func() {
os.Remove("testfile.yaml")
},
wantData: "test data",
wantErr: false,
},
{
name: "Stdin input",
path: "-",
setupMock: func() {
oldStdin := os.Stdin
r, w, _ := os.Pipe()
os.Stdin = r
go func() {
w.Write([]byte("stdin data"))
w.Close()
}()
t.Cleanup(func() { os.Stdin = oldStdin })
},
wantData: "stdin data",
wantErr: false,
},
{
name: "HTTP link",
path: "http://example.com/data",
setupMock: func() {
oldClient := http.DefaultClient
http.DefaultClient = &http.Client{
Transport: &mockTransport{
response: &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader("http data")),
},
},
}
t.Cleanup(func() { http.DefaultClient = oldClient })
},
wantData: "http data",
wantErr: false,
},
{
name: "File path",
path: "testfile.yaml",
setupMock: func() {
os.WriteFile("testfile.yaml", []byte("file data"), 0o644)
},
cleanupMock: func() {
os.Remove("testfile.yaml")
},
wantData: "file data",
wantErr: false,
},
{
name: "Non-existent file",
path: "nonexistent.yaml",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.setupMock != nil {
tt.setupMock()
}
if tt.cleanupMock != nil {
defer tt.cleanupMock()
}
i := &importer{}
gotData, err := i.Read(tt.path)
if (err != nil) != tt.wantErr {
t.Errorf("importer.Read() error = %v, wantErr %v", err, tt.wantErr)
return
}
if string(gotData) != tt.wantData {
t.Errorf("importer.Read() = %v, want %v", string(gotData), tt.wantData)
}
})
}
}
// mockTransport is a mock for http.RoundTripper
type mockTransport struct {
response *http.Response
}
func (m *mockTransport) RoundTrip(*http.Request) (*http.Response, error) {
return m.response, nil
}
func TestImporterImport(t *testing.T) {
crdYAML := `
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: testcrds.example.com
spec:
group: example.com
names:
kind: TestCRD
listKind: TestCRDList
plural: testcrds
singular: testcrd
scope: Namespaced
versions:
- name: v1
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
field1:
type: string
field2:
type: integer
`
importer := NewImporter(&ImporterConfig{Overwrite: true})
err := importer.Import([]byte(crdYAML))
if err != nil {
t.Fatalf("Failed to import CRD: %v", err)
}
_, err = os.Stat("imports/example_com/test_crd.go")
if os.IsNotExist(err) {
t.Errorf("Expected file was not created")
}
// TODO: verifying the content of the created file
}
func TestHasCustomName(t *testing.T) {
tests := []struct {
input string
wantName string
wantValue string
wantOk bool
}{
{"custom:=value", "custom", "value", true},
{"normal_input", "", "", false},
{"custom:=", "custom", "", true},
}
for _, tt := range tests {
gotName, gotValue, gotOk := hasCustomName(tt.input)
if gotName != tt.wantName || gotValue != tt.wantValue || gotOk != tt.wantOk {
t.Errorf("hasCustomName(%q) = (%q, %q, %v), want (%q, %q, %v)",
tt.input, gotName, gotValue, gotOk, tt.wantName, tt.wantValue, tt.wantOk)
}
}
}
func TestIsHTTPLink(t *testing.T) {
tests := []struct {
input string
want bool
}{
{"http://example.com", true},
{"https://example.com", true},
{"ftp://example.com", false},
{"not_a_link", false},
}
for _, tt := range tests {
got := isHTTPLink(tt.input)
if got != tt.want {
t.Errorf("isHTTPLink(%q) = %v, want %v", tt.input, got, tt.want)
}
}
}
func TestGetGoType(t *testing.T) {
tests := []struct {
name string
prop Property
want string
}{
{"String", Property{Type: "string"}, "string"},
{"Integer", Property{Type: "integer"}, "int"},
{"Boolean", Property{Type: "boolean"}, "bool"},
{"Array", Property{Type: "array", Items: &Property{Type: "string"}}, "[]string"},
{"Object", Property{Type: "object", Properties: map[string]Property{}}, "map[string]interface{}"},
{"CustomObject", Property{Type: "object", Properties: map[string]Property{"field": {}}}, "CustomObject"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := getGoType(tt.name, tt.prop)
if got != tt.want {
t.Errorf("getGoType() = %v, want %v", got, tt.want)
}
})
}
}
func TestGenerateStructs(t *testing.T) {
prop := Property{
Type: "object",
Properties: map[string]Property{
"field1": {Type: "string"},
//"field2": {Type: "integer"},
},
}
var file File
generateStructs(&file, "TestStruct", prop)
expected := `type TestStruct struct {
Field1 *string ` + "`json:\"field1,omitempty\"`" + `
}
`
if file.String() != expected {
t.Errorf("generateStructs() generated incorrect struct:\n%s\nExpected:\n%s", file.String(), expected)
}
}