Skip to content

Commit 3682cfb

Browse files
committed
add first basic tests for basic object synchronization
On-behalf-of: @SAP [email protected]
1 parent c513691 commit 3682cfb

File tree

4 files changed

+375
-1
lines changed

4 files changed

+375
-1
lines changed

hack/ci/run-e2e-tests.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export AGENT_BINARY="$(realpath _build/api-syncagent)"
8080

8181
# time to run the tests
8282
echodate "Running e2e tests…"
83-
(set -x; go test -tags e2e -timeout 2h -v ./test/e2e/...)
83+
WHAT="${WHAT:-./test/e2e/...}"
84+
(set -x; go test -tags e2e -timeout 2h -v $WHAT)
8485

8586
echodate "Done. :-)"

test/e2e/sync/primary_test.go

Lines changed: 334 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,334 @@
1+
//go:build e2e
2+
3+
/*
4+
Copyright 2025 The KCP Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package sync
20+
21+
import (
22+
"context"
23+
"errors"
24+
"fmt"
25+
"strings"
26+
"testing"
27+
"time"
28+
29+
"github.com/go-logr/logr"
30+
"github.com/kcp-dev/logicalcluster/v3"
31+
32+
syncagentv1alpha1 "github.com/kcp-dev/api-syncagent/sdk/apis/syncagent/v1alpha1"
33+
"github.com/kcp-dev/api-syncagent/test/utils"
34+
35+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
36+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
37+
"k8s.io/apimachinery/pkg/runtime"
38+
"k8s.io/apimachinery/pkg/runtime/schema"
39+
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
40+
"k8s.io/apimachinery/pkg/types"
41+
"k8s.io/apimachinery/pkg/util/wait"
42+
yamlutil "k8s.io/apimachinery/pkg/util/yaml"
43+
ctrlruntime "sigs.k8s.io/controller-runtime"
44+
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
45+
"sigs.k8s.io/controller-runtime/pkg/kontext"
46+
)
47+
48+
func TestSyncSimpleObject(t *testing.T) {
49+
const (
50+
apiExportName = "kcp.example.com"
51+
orgWorkspace = "sync-simple"
52+
)
53+
54+
ctx := context.Background()
55+
ctrlruntime.SetLogger(logr.Discard())
56+
57+
// setup a test environment in kcp
58+
orgKubconfig := utils.CreateOrganization(t, ctx, orgWorkspace, apiExportName)
59+
60+
// start a service cluster
61+
envtestKubeconfig, envtestClient, _ := utils.RunEnvtest(t, []string{
62+
"test/crds/crontab.yaml",
63+
})
64+
65+
// publish Crontabs and Backups
66+
t.Logf("Publishing CRDs…")
67+
prCrontabs := &syncagentv1alpha1.PublishedResource{
68+
ObjectMeta: metav1.ObjectMeta{
69+
Name: "publish-crontabs",
70+
},
71+
Spec: syncagentv1alpha1.PublishedResourceSpec{
72+
Resource: syncagentv1alpha1.SourceResourceDescriptor{
73+
APIGroup: "example.com",
74+
Version: "v1",
75+
Kind: "CronTab",
76+
},
77+
// These rules make finding the local object easier, but should not be used in production.
78+
Naming: &syncagentv1alpha1.ResourceNaming{
79+
Name: "$remoteName",
80+
Namespace: "synced-$remoteNamespace",
81+
},
82+
},
83+
}
84+
85+
if err := envtestClient.Create(ctx, prCrontabs); err != nil {
86+
t.Fatalf("Failed to create PublishedResource: %v", err)
87+
}
88+
89+
// start the agent in the background to update the APIExport with the CronTabs API
90+
utils.RunAgent(ctx, t, "bob", orgKubconfig, envtestKubeconfig, apiExportName)
91+
92+
// wait until the API is available
93+
teamCtx := kontext.WithCluster(ctx, logicalcluster.Name(fmt.Sprintf("root:%s:team-1", orgWorkspace)))
94+
kcpClient := utils.GetKcpAdminClusterClient(t)
95+
utils.WaitForBoundAPI(t, teamCtx, kcpClient, schema.GroupVersionResource{
96+
Group: apiExportName,
97+
Version: "v1",
98+
Resource: "crontabs",
99+
})
100+
101+
// create a Crontab object in a team workspace
102+
t.Log("Creating CronTab in kcp…")
103+
crontab := yamlToUnstructured(t, `
104+
apiVersion: kcp.example.com/v1
105+
kind: CronTab
106+
metadata:
107+
namespace: default
108+
name: my-crontab
109+
spec:
110+
cronSpec: '* * *'
111+
image: ubuntu:latest
112+
`)
113+
114+
if err := kcpClient.Create(teamCtx, crontab); err != nil {
115+
t.Fatalf("Failed to create CronTab in kcp: %v", err)
116+
}
117+
118+
// wait for the agent to sync the object down into the service cluster
119+
120+
t.Logf("Wait for CronTab to be synced…")
121+
copy := &unstructured.Unstructured{}
122+
copy.SetAPIVersion("example.com/v1")
123+
copy.SetKind("CronTab")
124+
125+
err := wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 1*time.Minute, false, func(ctx context.Context) (done bool, err error) {
126+
copyKey := types.NamespacedName{Namespace: "synced-default", Name: "my-crontab"}
127+
return envtestClient.Get(ctx, copyKey, copy) == nil, nil
128+
})
129+
if err != nil {
130+
t.Fatalf("Failed to wait for object to be synced down: %v", err)
131+
}
132+
}
133+
134+
func TestLocalChangesAreKept(t *testing.T) {
135+
const (
136+
apiExportName = "kcp.example.com"
137+
orgWorkspace = "sync-undo-local-changes"
138+
)
139+
140+
ctx := context.Background()
141+
ctrlruntime.SetLogger(logr.Discard())
142+
143+
// setup a test environment in kcp
144+
orgKubconfig := utils.CreateOrganization(t, ctx, orgWorkspace, apiExportName)
145+
146+
// start a service cluster
147+
envtestKubeconfig, envtestClient, _ := utils.RunEnvtest(t, []string{
148+
"test/crds/crontab.yaml",
149+
})
150+
151+
// publish Crontabs and Backups
152+
t.Logf("Publishing CRDs…")
153+
prCrontabs := &syncagentv1alpha1.PublishedResource{
154+
ObjectMeta: metav1.ObjectMeta{
155+
Name: "publish-crontabs",
156+
},
157+
Spec: syncagentv1alpha1.PublishedResourceSpec{
158+
Resource: syncagentv1alpha1.SourceResourceDescriptor{
159+
APIGroup: "example.com",
160+
Version: "v1",
161+
Kind: "CronTab",
162+
},
163+
// These rules make finding the local object easier, but should not be used in production.
164+
Naming: &syncagentv1alpha1.ResourceNaming{
165+
Name: "$remoteName",
166+
Namespace: "synced-$remoteNamespace",
167+
},
168+
},
169+
}
170+
171+
if err := envtestClient.Create(ctx, prCrontabs); err != nil {
172+
t.Fatalf("Failed to create PublishedResource: %v", err)
173+
}
174+
175+
// start the agent in the background to update the APIExport with the CronTabs API
176+
utils.RunAgent(ctx, t, "bob", orgKubconfig, envtestKubeconfig, apiExportName)
177+
178+
// wait until the API is available
179+
teamCtx := kontext.WithCluster(ctx, logicalcluster.Name(fmt.Sprintf("root:%s:team-1", orgWorkspace)))
180+
kcpClient := utils.GetKcpAdminClusterClient(t)
181+
utils.WaitForBoundAPI(t, teamCtx, kcpClient, schema.GroupVersionResource{
182+
Group: apiExportName,
183+
Version: "v1",
184+
Resource: "crontabs",
185+
})
186+
187+
// create a Crontab object in a team workspace
188+
t.Log("Creating CronTab in kcp…")
189+
crontab := yamlToUnstructured(t, `
190+
apiVersion: kcp.example.com/v1
191+
kind: CronTab
192+
metadata:
193+
namespace: default
194+
name: my-crontab
195+
spec:
196+
cronSpec: '* * *'
197+
image: ubuntu:latest
198+
`)
199+
200+
if err := kcpClient.Create(teamCtx, crontab); err != nil {
201+
t.Fatalf("Failed to create CronTab in kcp: %v", err)
202+
}
203+
204+
// wait for the agent to sync the object down into the service cluster
205+
206+
t.Logf("Wait for CronTab to be synced…")
207+
copyKey := types.NamespacedName{Namespace: "synced-default", Name: "my-crontab"}
208+
209+
copy := &unstructured.Unstructured{}
210+
copy.SetAPIVersion("example.com/v1")
211+
copy.SetKind("CronTab")
212+
213+
err := wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 1*time.Minute, false, func(ctx context.Context) (done bool, err error) {
214+
return envtestClient.Get(ctx, copyKey, copy) == nil, nil
215+
})
216+
if err != nil {
217+
t.Fatalf("Failed to wait for object to be synced down: %v", err)
218+
}
219+
220+
// make some changes on the service cluster; this is usually an external operator doing some
221+
// defaulting, maybe even a mutation webhook
222+
t.Logf("Modifying local object…")
223+
newCronSpec := "this-should-not-be-reverted"
224+
unstructured.SetNestedField(copy.Object, newCronSpec, "spec", "cronSpec")
225+
226+
if err := envtestClient.Update(ctx, copy); err != nil {
227+
t.Fatalf("Failed to update synced object in service cluster: %v", err)
228+
}
229+
230+
// make some changes in kcp, these should be applied to the local object without overwriting the cronSpec
231+
232+
// refresh the current object state
233+
if err := kcpClient.Get(teamCtx, ctrlruntimeclient.ObjectKeyFromObject(crontab), crontab); err != nil {
234+
t.Fatalf("Failed to create CronTab in kcp: %v", err)
235+
}
236+
237+
newImage := "new-value"
238+
unstructured.SetNestedField(crontab.Object, newImage, "spec", "image")
239+
240+
t.Logf("Modifying object in kcp…")
241+
if err := kcpClient.Update(teamCtx, crontab); err != nil {
242+
t.Fatalf("Failed to update source object in kcp: %v", err)
243+
}
244+
245+
// wait for the agent to sync again
246+
t.Logf("Waiting for the agent to sync again…")
247+
err = wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 1*time.Minute, false, func(ctx context.Context) (done bool, err error) {
248+
if err := envtestClient.Get(ctx, copyKey, copy); err != nil {
249+
return false, err
250+
}
251+
252+
value, existing, err := unstructured.NestedString(copy.Object, "spec", "cronSpec")
253+
if err != nil {
254+
return false, err
255+
}
256+
257+
if !existing {
258+
return false, errors.New("field does not exist in object anymore, this should not have happened")
259+
}
260+
261+
if value != newCronSpec {
262+
return false, fmt.Errorf("cronSpec was reverted back to %q, should still be %q", value, newCronSpec)
263+
}
264+
265+
value, existing, err = unstructured.NestedString(copy.Object, "spec", "image")
266+
if err != nil {
267+
return false, err
268+
}
269+
270+
if !existing {
271+
return false, errors.New("field does not exist in object anymore, this should not have happened")
272+
}
273+
274+
return value == newImage, nil
275+
})
276+
if err != nil {
277+
t.Fatalf("Failed to wait for object to be synced: %v", err)
278+
}
279+
280+
// Now we actually change the cronSpec in kcp, and this change _must_ make it to the service cluster.
281+
t.Logf("Modify object in kcp again…")
282+
283+
if err := kcpClient.Get(teamCtx, ctrlruntimeclient.ObjectKeyFromObject(crontab), crontab); err != nil {
284+
t.Fatalf("Failed to create CronTab in kcp: %v", err)
285+
}
286+
287+
kcpNewCronSpec := "users-new-desired-cronspec"
288+
unstructured.SetNestedField(crontab.Object, kcpNewCronSpec, "spec", "cronSpec")
289+
290+
if err := kcpClient.Update(teamCtx, crontab); err != nil {
291+
t.Fatalf("Failed to update source object in kcp: %v", err)
292+
}
293+
294+
// wait for the agent to sync again
295+
t.Logf("Waiting for the agent to sync again…")
296+
err = wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 1*time.Minute, false, func(ctx context.Context) (done bool, err error) {
297+
if err := envtestClient.Get(ctx, copyKey, copy); err != nil {
298+
return false, err
299+
}
300+
301+
value, existing, err := unstructured.NestedString(copy.Object, "spec", "cronSpec")
302+
if err != nil {
303+
return false, err
304+
}
305+
306+
if !existing {
307+
return false, errors.New("field does not exist in object anymore, this should not have happened")
308+
}
309+
310+
return value == kcpNewCronSpec, nil
311+
})
312+
if err != nil {
313+
t.Fatalf("Failed to wait for object to be synced: %v", err)
314+
}
315+
}
316+
317+
func yamlToUnstructured(t *testing.T, data string) *unstructured.Unstructured {
318+
t.Helper()
319+
320+
decoder := yamlutil.NewYAMLOrJSONDecoder(strings.NewReader(data), 100)
321+
322+
var rawObj runtime.RawExtension
323+
if err := decoder.Decode(&rawObj); err != nil {
324+
t.Fatalf("Failed to decode: %v", err)
325+
}
326+
327+
obj, _, err := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(rawObj.Raw, nil, nil)
328+
unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
329+
if err != nil {
330+
t.Fatal(err)
331+
}
332+
333+
return &unstructured.Unstructured{Object: unstructuredMap}
334+
}

test/utils/process.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func RunAgent(
8888
"--kcp-kubeconfig", kcpKubeconfig,
8989
"--namespace", "kube-system",
9090
"--log-format", "Console",
91+
"--log-debug=true",
9192
"--health-address", "0",
9293
"--metrics-address", "0",
9394
}

0 commit comments

Comments
 (0)