forked from operator-framework/operator-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrd_test.go
250 lines (229 loc) · 6.75 KB
/
crd_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
247
248
249
250
// Copyright 2018 The Operator-SDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package scaffold
import (
"os"
"path/filepath"
"testing"
"github.com/operator-framework/operator-sdk/internal/scaffold/input"
testutil "github.com/operator-framework/operator-sdk/internal/scaffold/internal/testutil"
"github.com/operator-framework/operator-sdk/internal/util/diffutil"
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
"github.com/spf13/afero"
)
func TestCRDGoProject(t *testing.T) {
r, err := NewResource("cache.example.com/v1alpha1", "Memcached")
if err != nil {
t.Fatal(err)
}
s, buf := setupScaffoldAndWriter()
s.Fs = afero.NewMemMapFs()
cfg, err := setupTestFrameworkConfig()
if err != nil {
t.Fatal(err)
}
err = testutil.WriteOSPathToFS(afero.NewOsFs(), s.Fs, cfg.AbsProjectPath)
if err != nil {
t.Fatal(err)
}
// Must change directories since the test framework dir is a sub-module.
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer func() { _ = os.Chdir(wd) }()
if err = os.Chdir(cfg.AbsProjectPath); err != nil {
t.Fatal(err)
}
err = s.Execute(cfg, &CRD{Resource: r, IsOperatorGo: true})
if err != nil {
t.Fatalf("Failed to execute the scaffold: (%v)", err)
}
if crdGoExp != buf.String() {
diffs := diffutil.Diff(crdGoExp, buf.String())
t.Fatalf("Expected vs actual differs.\n%v", diffs)
}
}
const crdGoExp = `apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: memcacheds.cache.example.com
spec:
group: cache.example.com
names:
kind: Memcached
listKind: MemcachedList
plural: memcacheds
singular: memcached
scope: Namespaced
subresources:
status: {}
validation:
openAPIV3Schema:
description: Memcached is the Schema for the memcacheds API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: MemcachedSpec defines the desired state of Memcached
properties:
size:
description: Size is the size of the memcached deployment
format: int32
type: integer
required:
- size
type: object
status:
description: MemcachedStatus defines the observed state of Memcached
properties:
nodes:
description: Nodes are the names of the memcached pods
items:
type: string
type: array
required:
- nodes
type: object
type: object
version: v1alpha1
versions:
- name: v1alpha1
served: true
storage: true
`
func TestCRDNonGoProjectDefault(t *testing.T) {
s, buf := setupScaffoldAndWriter()
s.Fs = afero.NewMemMapFs()
r, err := NewResource(appApiVersion, appKind)
if err != nil {
t.Fatal(err)
}
crd := &CRD{Resource: r}
cfg := &input.Config{}
if err = s.Execute(cfg, crd); err != nil {
t.Fatalf("Failed to execute the scaffold: (%v)", err)
}
if crdNonGoDefaultExp != buf.String() {
diffs := diffutil.Diff(crdNonGoDefaultExp, buf.String())
t.Fatalf("Expected vs actual differs.\n%v", diffs)
}
}
// crdNonGoDefaultExp is the default non-go CRD. Non-go projects don't have the
// luxury of kubebuilder annotations.
const crdNonGoDefaultExp = `apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: appservices.app.example.com
spec:
group: app.example.com
names:
kind: AppService
listKind: AppServiceList
plural: appservices
singular: appservice
scope: Namespaced
subresources:
status: {}
validation:
openAPIV3Schema:
type: object
x-kubernetes-preserve-unknown-fields: true
versions:
- name: v1alpha1
served: true
storage: true
`
func TestCRDNonGoProjectCustom(t *testing.T) {
s, buf := setupScaffoldAndWriter()
s.Fs = afero.NewMemMapFs()
r, err := NewResource(appApiVersion, appKind)
if err != nil {
t.Fatal(err)
}
crd := &CRD{Resource: r}
i, err := crd.GetInput()
if err != nil {
t.Fatal(err)
}
cfg, err := setupTestFrameworkConfig()
if err != nil {
t.Fatal(err)
}
path := filepath.Join(cfg.AbsProjectPath, i.Path)
err = afero.WriteFile(s.Fs, path, []byte(crdNonGoCustomExp), fileutil.DefaultFileMode)
if err != nil {
t.Fatal(err)
}
if err = s.Execute(cfg, crd); err != nil {
t.Fatalf("Failed to execute the scaffold: (%v)", err)
}
if crdNonGoCustomExp != buf.String() {
diffs := diffutil.Diff(crdNonGoCustomExp, buf.String())
t.Fatalf("Expected vs actual differs.\n%v", diffs)
}
}
// crdNonGoCustomExp contains a simple validation block to make sure manually-added
// validation is not overwritten. Non-go projects don't have the luxury of
// kubebuilder annotations.
const crdNonGoCustomExp = `apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: appservices.app.example.com
spec:
group: app.example.com
names:
kind: AppService
listKind: AppServiceList
plural: appservices
singular: appservice
scope: Namespaced
version: v1alpha1
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
properties:
spec:
properties:
size:
format: int32
type: integer
required:
- size
type: object
status:
properties:
nodes:
items:
type: string
type: array
required:
- nodes
type: object
served: true
storage: true
subresources:
status: {}
`