-
Notifications
You must be signed in to change notification settings - Fork 718
/
handler_integration_test.go
82 lines (70 loc) · 2.8 KB
/
handler_integration_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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
// +build integration
package watches_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/operator"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/watches"
"github.com/elastic/cloud-on-k8s/pkg/utils/k8s"
"github.com/elastic/cloud-on-k8s/pkg/utils/test"
)
func TestMain(m *testing.M) {
test.RunWithK8s(m)
}
// TestDynamicEnqueueRequest tests the integration between a DynamicEnqueueRequest watch and
// a manager + controller, with a test k8s environment.
// The test just checks that everything fits together and reconciliations are correctly triggered
// from the EventHandler. More detailed behaviour is tested in `handler_test.go`.
func TestDynamicEnqueueRequest(t *testing.T) {
eventHandler := watches.NewDynamicEnqueueRequest()
// create a controller that watches secrets and enqueues requests into a chan
requests := make(chan reconcile.Request)
addToManager := func(mgr manager.Manager, params operator.Parameters) error {
reconcileFunc := reconcile.Func(func(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
requests <- req
return reconcile.Result{}, nil
})
ctrl, err := controller.New("test-reconciler", mgr, controller.Options{Reconciler: reconcileFunc})
require.NoError(t, err)
require.NoError(t, ctrl.Watch(&source.Kind{Type: &corev1.Secret{}}, eventHandler))
return nil
}
c, stop := test.StartManager(t, addToManager, operator.Parameters{})
defer stop()
// Fixtures
watched := types.NamespacedName{
Namespace: "default",
Name: "watched1",
}
testObj := &corev1.Secret{
ObjectMeta: k8s.ToObjectMeta(watched),
}
watching := types.NamespacedName{
Namespace: "default",
Name: "watcher",
}
// Create the object before registering any watches
assert.NoError(t, c.Create(context.Background(), testObj))
// Add a named watch for the first object
assert.NoError(t, eventHandler.AddHandler(watches.NamedWatch{
Watched: []types.NamespacedName{watched},
Watcher: watching,
Name: "test-watch-1",
}))
// Update the first object and expect a reconcile request
testLabels := map[string]string{"test": "label"}
testObj.Labels = testLabels
require.NoError(t, c.Update(context.Background(), testObj))
require.Equal(t, watching, (<-requests).NamespacedName)
}