forked from open-cluster-management-io/sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
103 lines (90 loc) · 2.82 KB
/
Copy pathclient_test.go
File metadata and controls
103 lines (90 loc) · 2.82 KB
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
package addon
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/cloudevents/sdk-go/v2/protocol/gochan"
jsonpatch "github.com/evanphx/json-patch/v5"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions"
"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/statushash"
"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/store"
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/clients"
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/options/fake"
)
func TestPatch(t *testing.T) {
cases := []struct {
name string
clusterName string
addon *addonapiv1alpha1.ManagedClusterAddOn
patch []byte
}{
{
name: "patch addon",
clusterName: "cluster1",
addon: &addonapiv1alpha1.ManagedClusterAddOn{
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "cluster1"},
},
patch: func() []byte {
old := &addonapiv1alpha1.ManagedClusterAddOn{
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "cluster1"},
}
oldData, err := json.Marshal(old)
if err != nil {
t.Error(err)
}
new := old.DeepCopy()
new.Status = addonapiv1alpha1.ManagedClusterAddOnStatus{
Namespace: "install",
}
newData, err := json.Marshal(new)
if err != nil {
t.Error(err)
}
patchBytes, err := jsonpatch.CreateMergePatch(oldData, newData)
if err != nil {
t.Error(err)
}
return patchBytes
}(),
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
watcherStore := store.NewAgentInformerWatcherStore[*addonapiv1alpha1.ManagedClusterAddOn]()
ceClientOpt := fake.NewAgentOptions(gochan.New(), nil, c.clusterName, c.clusterName+"agent")
ceClient, err := clients.NewCloudEventAgentClient(
context.Background(),
ceClientOpt,
store.NewAgentWatcherStoreLister(watcherStore),
statushash.StatusHash,
NewManagedClusterAddOnCodec())
if err != nil {
t.Error(err)
}
addonClientSet := &AddonClientSetWrapper{&AddonV1Alpha1ClientWrapper{
NewManagedClusterAddOnClient(ceClient, watcherStore),
}}
addonInformerFactory := addoninformers.NewSharedInformerFactory(addonClientSet, time.Minute*10)
informer := addonInformerFactory.Addon().V1alpha1().ManagedClusterAddOns().Informer()
store := informer.GetStore()
if err := store.Add(c.addon); err != nil {
t.Error(err)
}
watcherStore.SetInformer(informer)
if _, err = addonClientSet.AddonV1alpha1().ManagedClusterAddOns(c.clusterName).Patch(
context.Background(),
c.addon.Name,
types.MergePatchType,
c.patch,
metav1.PatchOptions{},
"status",
); err != nil {
t.Error(err)
}
})
}
}