-
Notifications
You must be signed in to change notification settings - Fork 1
/
testutil.go
407 lines (353 loc) · 11.6 KB
/
testutil.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package testutil
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
goruntime "runtime"
"strconv"
"sync/atomic"
"testing"
"time"
"github.com/go-logr/logr"
"github.com/go-logr/logr/testr"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
apiv1 "github.com/Azure/eno/api/v1"
testv1 "github.com/Azure/eno/internal/controllers/reconciliation/fixtures/v1"
"github.com/Azure/eno/internal/manager"
krmv1 "github.com/Azure/eno/pkg/krm/functions/api/v1"
)
func NewClient(t testing.TB) client.Client {
return NewClientWithInterceptors(t, nil)
}
func NewClientWithInterceptors(t testing.TB, ict *interceptor.Funcs) client.Client {
scheme := runtime.NewScheme()
require.NoError(t, apiv1.SchemeBuilder.AddToScheme(scheme))
require.NoError(t, corev1.SchemeBuilder.AddToScheme(scheme))
builder := fake.NewClientBuilder().
WithScheme(scheme).
WithStatusSubresource(&apiv1.ResourceSlice{}, &apiv1.Composition{})
if ict != nil {
builder.WithInterceptorFuncs(*ict)
}
return builder.Build()
}
func NewReadOnlyClient(t testing.TB, objs ...runtime.Object) client.Client {
scheme := runtime.NewScheme()
require.NoError(t, apiv1.SchemeBuilder.AddToScheme(scheme))
require.NoError(t, corev1.SchemeBuilder.AddToScheme(scheme))
builder := fake.NewClientBuilder().
WithScheme(scheme).
WithRuntimeObjects(objs...).
WithStatusSubresource(&apiv1.ResourceSlice{}, &apiv1.Composition{})
builder.WithInterceptorFuncs(interceptor.Funcs{
Create: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.CreateOption) error {
return errors.New("no writes allowed")
},
Update: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.UpdateOption) error {
return errors.New("no writes allowed")
},
Patch: func(ctx context.Context, client client.WithWatch, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
return errors.New("no writes allowed")
},
Delete: func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteOption) error {
return errors.New("no writes allowed")
},
})
return builder.Build()
}
func NewContext(t *testing.T) context.Context {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(func() {
cancel()
})
return logr.NewContext(ctx, testr.NewWithOptions(t, testr.Options{Verbosity: 2}))
}
type TestManagerOption func(*manager.Options)
func (o TestManagerOption) apply(opts *manager.Options) {
o(opts)
}
func WithPodNamespace(ns string) TestManagerOption {
return TestManagerOption(func(o *manager.Options) {
o.SynthesizerPodNamespace = ns
})
}
func WithCompositionNamespace(ns string) TestManagerOption {
return TestManagerOption(func(o *manager.Options) {
o.CompositionNamespace = ns
})
}
// NewManager starts one or two envtest environments depending on the env.
// This should work seamlessly when run locally assuming binaries have been fetched with setup-envtest.
// In CI the second environment is used to compatibility test against a matrix of k8s versions.
// This compatibility testing is tightly coupled to the github action and not expected to work locally.
func NewManager(t *testing.T, testOpts ...TestManagerOption) *Manager {
t.Parallel()
_, b, _, _ := goruntime.Caller(0)
root := filepath.Join(filepath.Dir(b), "..", "..")
testCrdDir := filepath.Join(root, "internal", "controllers", "reconciliation", "fixtures", "v1", "config", "crd")
env := &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join(root, "api", "v1", "config", "crd"),
testCrdDir,
},
ErrorIfCRDPathMissing: true,
AttachControlPlaneOutput: os.Getenv("ACTIONS_RUNNER_DEBUG") != "" || os.Getenv("ACTIONS_STEP_DEBUG") != "",
// We can't use KUBEBUILDER_ASSETS when also setting DOWNSTREAM_KUBEBUILDER_ASSETS
// because the envvar overrides BinaryAssetsDirectory
BinaryAssetsDirectory: os.Getenv("UPSTREAM_KUBEBUILDER_ASSETS"),
}
t.Cleanup(func() {
err := env.Stop()
if err != nil {
panic(err)
}
})
var cfg *rest.Config
var err error
for i := 0; i < 2; i++ {
cfg, err = env.Start()
if err != nil {
t.Logf("failed to start test environment: %s", err)
continue
}
break
}
require.NoError(t, err)
options := &manager.Options{
Rest: cfg,
HealthProbeAddr: "127.0.0.1:0",
MetricsAddr: "127.0.0.1:0",
SynthesizerPodNamespace: "default",
CompositionNamespace: "default",
CompositionSelector: labels.Everything(),
}
for _, o := range testOpts {
o.apply(options)
}
mgr, err := manager.NewTest(logr.FromContextOrDiscard(NewContext(t)), options)
require.NoError(t, err)
require.NoError(t, testv1.SchemeBuilder.AddToScheme(mgr.GetScheme())) // test-specific CRDs
newFakePodRuntime(t, mgr)
m := &Manager{
Manager: mgr,
RestConfig: cfg,
DownstreamRestConfig: cfg, // possible override below
DownstreamClient: mgr.GetClient(),
DownstreamEnv: env,
}
dir := os.Getenv("DOWNSTREAM_KUBEBUILDER_ASSETS")
if dir == "" {
return m // only one env needed
}
version, _ := strconv.Atoi(os.Getenv("DOWNSTREAM_VERSION_MINOR"))
downstreamEnv := &envtest.Environment{
BinaryAssetsDirectory: dir,
ErrorIfCRDPathMissing: true,
AttachControlPlaneOutput: os.Getenv("ACTIONS_RUNNER_DEBUG ") != "" || os.Getenv("ACTIONS_STEP_DEBUG ") != "",
}
// Only newer clusters can use envtest to install CRDs
if version >= 21 {
t.Logf("managing downstream cluster CRD with envtest because version >= 21")
downstreamEnv.CRDDirectoryPaths = append(downstreamEnv.CRDDirectoryPaths, testCrdDir)
}
// k8s <1.13 will not start if these flags are set
if version < 13 {
conf := downstreamEnv.ControlPlane.GetAPIServer().Configure()
conf.Disable("service-account-signing-key-file")
conf.Disable("service-account-issuer")
}
t.Cleanup(func() {
err := downstreamEnv.Stop()
if err != nil {
panic(err)
}
})
for i := 0; i < 2; i++ {
m.DownstreamRestConfig, err = downstreamEnv.Start()
if err != nil {
t.Logf("failed to start downstream test environment: %s", err)
continue
}
break
}
require.NoError(t, err)
m.DownstreamEnv = downstreamEnv
m.DownstreamClient, err = client.New(m.DownstreamRestConfig, client.Options{Scheme: mgr.GetScheme()})
require.NoError(t, err)
// Log apiserver version
disc, err := discovery.NewDiscoveryClientForConfig(m.DownstreamRestConfig)
if err == nil {
version, err := disc.ServerVersion()
if err == nil {
t.Logf("downstream control plane version: %s", version.String())
}
}
// We install old (v1beta1) CRDs ourselves because envtest assumes v1
if version < 21 {
t.Logf("managing downstream cluster CRD ourselves (not with envtest) because version < 21")
raw, err := os.ReadFile(filepath.Join(root, "internal", "controllers", "reconciliation", "fixtures", "v1", "config", "enotest.azure.io_testresources-old.yaml"))
require.NoError(t, err)
res := &unstructured.Unstructured{}
require.NoError(t, yaml.Unmarshal(raw, res))
cli, err := client.New(m.DownstreamRestConfig, client.Options{})
require.NoError(t, err)
require.NoError(t, cli.Create(context.Background(), res))
}
return m
}
type Manager struct {
ctrl.Manager
RestConfig *rest.Config
DownstreamRestConfig *rest.Config // may or may not == RestConfig
DownstreamClient client.Client // may or may not == Manager.GetClient()
DownstreamEnv *envtest.Environment
}
func (m *Manager) Start(t *testing.T) {
go func() {
err := m.Manager.Start(NewContext(t))
if err != nil {
// can't t.Fail here since we're in a different goroutine
panic(fmt.Sprintf("error while starting manager: %s", err))
}
}()
t.Logf("warming caches")
m.Manager.GetCache().WaitForCacheSync(context.Background())
t.Logf("warmed caches")
}
func (m *Manager) GetCurrentResourceSlices(ctx context.Context) ([]*apiv1.ResourceSlice, error) {
cli := m.Manager.GetAPIReader()
comps := &apiv1.CompositionList{}
err := cli.List(ctx, comps)
if err != nil {
return nil, err
}
if l := len(comps.Items); l != 1 {
return nil, fmt.Errorf("expected one composition, found %d", l)
}
if comps.Items[0].Status.CurrentSynthesis.Synthesized == nil {
return nil, fmt.Errorf("composition is still being synthesized")
}
synthesis := comps.Items[0].Status.CurrentSynthesis
if synthesis == nil {
return nil, fmt.Errorf("synthesis hasn't completed yet")
}
returns := make([]*apiv1.ResourceSlice, len(synthesis.ResourceSlices))
for i, ref := range synthesis.ResourceSlices {
slice := &apiv1.ResourceSlice{}
slice.Name = ref.Name
slice.Namespace = comps.Items[0].Namespace
returns[i] = slice
err = cli.Get(ctx, client.ObjectKeyFromObject(slice), slice)
if err != nil {
return nil, err
}
}
return returns, nil
}
var Backoff = wait.Backoff{
Steps: 10,
Duration: 10 * time.Millisecond,
Factor: 2.0,
Jitter: 0.1,
Cap: time.Minute,
}
func Eventually(t testing.TB, fn func() bool) {
t.Helper()
SomewhatEventually(t, time.Second*15, fn)
}
func SomewhatEventually(t testing.TB, dur time.Duration, fn func() bool) {
t.Helper()
start := time.Now()
for {
if time.Since(start) > dur {
t.Fatalf("timeout while waiting for condition")
return
}
if fn() {
return
}
time.Sleep(time.Millisecond * 100)
}
}
// newFakePodRuntime marks pod containers as running without actually doing anything.
// Allows fake synthesis using the exec controller, which waits until synthesizer containers are running.
func newFakePodRuntime(t testing.TB, mgr ctrl.Manager) {
cli := mgr.GetClient()
podCtrl := reconcile.Func(func(ctx context.Context, r reconcile.Request) (reconcile.Result, error) {
pod := &corev1.Pod{}
err := cli.Get(ctx, r.NamespacedName, pod)
if err != nil {
return reconcile.Result{}, client.IgnoreNotFound(err)
}
if len(pod.Status.ContainerStatuses) > 0 {
return reconcile.Result{}, nil
}
pod.Status.ContainerStatuses = []corev1.ContainerStatus{{
State: corev1.ContainerState{
Running: &corev1.ContainerStateRunning{},
},
}}
err = cli.Status().Update(ctx, pod)
if err != nil {
return reconcile.Result{}, err
}
logr.FromContextOrDiscard(ctx).Info("added container running state")
return reconcile.Result{}, nil
})
_, err := ctrl.NewControllerManagedBy(mgr).
For(&corev1.Pod{}).
Build(podCtrl)
require.NoError(t, err)
}
type ExecConn struct {
Hook func(s *apiv1.Synthesizer) []client.Object
PodHook func(p *corev1.Pod)
Calls atomic.Int64
}
func (e *ExecConn) Synthesize(ctx context.Context, syn *apiv1.Synthesizer, pod *corev1.Pod, input []byte) (io.Reader, error) {
defer e.Calls.Add(1)
objs := []client.Object{}
if e.Hook != nil {
objs = e.Hook(syn)
}
if e.PodHook != nil {
e.PodHook(pod)
}
outObjs := []*unstructured.Unstructured{}
for _, o := range objs {
m, err := runtime.DefaultUnstructuredConverter.ToUnstructured(o)
if err != nil {
return nil, err
}
outObjs = append(outObjs, &unstructured.Unstructured{Object: m})
}
rl := &krmv1.ResourceList{
Kind: krmv1.ResourceListKind,
APIVersion: krmv1.SchemeGroupVersion.String(),
Items: outObjs,
}
js, err := json.Marshal(rl)
if err != nil {
return nil, err
}
return bytes.NewBuffer(js), nil
}