Skip to content

Commit 9c79674

Browse files
committed
add basic e2e tests for AEES in the same cluster
On-behalf-of: @SAP [email protected]
1 parent 8308b2e commit 9c79674

File tree

2 files changed

+158
-1
lines changed

2 files changed

+158
-1
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
"testing"
24+
"time"
25+
26+
"github.com/go-logr/logr"
27+
"github.com/kcp-dev/logicalcluster/v3"
28+
29+
syncagentv1alpha1 "github.com/kcp-dev/api-syncagent/sdk/apis/syncagent/v1alpha1"
30+
"github.com/kcp-dev/api-syncagent/test/utils"
31+
32+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
34+
"k8s.io/apimachinery/pkg/runtime/schema"
35+
"k8s.io/apimachinery/pkg/types"
36+
"k8s.io/apimachinery/pkg/util/wait"
37+
ctrlruntime "sigs.k8s.io/controller-runtime"
38+
)
39+
40+
// TestAPIExportEndpointSlice is functionally equivalent to a simple sync test,
41+
// but is bootstrapping the agent using a AEES ref instead of an APIExport ref.
42+
func TestAPIExportEndpointSliceSameCluster(t *testing.T) {
43+
const (
44+
apiExportName = "kcp.example.com"
45+
kcpGroupName = "kcp.example.com"
46+
orgWorkspace = "endpointslice-same-cluster"
47+
)
48+
49+
ctx := t.Context()
50+
ctrlruntime.SetLogger(logr.Discard())
51+
52+
// setup a test environment in kcp
53+
orgKubconfig := utils.CreateOrganization(t, ctx, orgWorkspace, apiExportName)
54+
55+
// start a service cluster
56+
envtestKubeconfig, envtestClient, _ := utils.RunEnvtest(t, []string{
57+
"test/crds/crontab.yaml",
58+
})
59+
60+
// publish Crontabs and Backups
61+
t.Logf("Publishing CRDs…")
62+
prCrontabs := &syncagentv1alpha1.PublishedResource{
63+
ObjectMeta: metav1.ObjectMeta{
64+
Name: "publish-crontabs",
65+
},
66+
Spec: syncagentv1alpha1.PublishedResourceSpec{
67+
Resource: syncagentv1alpha1.SourceResourceDescriptor{
68+
APIGroup: "example.com",
69+
Version: "v1",
70+
Kind: "CronTab",
71+
},
72+
// These rules make finding the local object easier, but should not be used in production.
73+
Naming: &syncagentv1alpha1.ResourceNaming{
74+
Name: "{{ .Object.metadata.name }}",
75+
Namespace: "synced-{{ .Object.metadata.namespace }}",
76+
},
77+
Projection: &syncagentv1alpha1.ResourceProjection{
78+
Group: kcpGroupName,
79+
},
80+
},
81+
}
82+
83+
if err := envtestClient.Create(ctx, prCrontabs); err != nil {
84+
t.Fatalf("Failed to create PublishedResource: %v", err)
85+
}
86+
87+
// start the agent in the background to update the APIExport with the CronTabs API;
88+
// use the export's name because kcp created an endpoint slice of the same name
89+
utils.RunEndpointSliceAgent(ctx, t, "bob", orgKubconfig, envtestKubeconfig, apiExportName)
90+
91+
// wait until the API is available
92+
kcpClusterClient := utils.GetKcpAdminClusterClient(t)
93+
94+
teamClusterPath := logicalcluster.NewPath("root").Join(orgWorkspace).Join("team-1")
95+
teamClient := kcpClusterClient.Cluster(teamClusterPath)
96+
97+
utils.WaitForBoundAPI(t, ctx, teamClient, schema.GroupVersionResource{
98+
Group: kcpGroupName,
99+
Version: "v1",
100+
Resource: "crontabs",
101+
})
102+
103+
// create a Crontab object in a team workspace
104+
t.Log("Creating CronTab in kcp…")
105+
crontab := utils.YAMLToUnstructured(t, `
106+
apiVersion: kcp.example.com/v1
107+
kind: CronTab
108+
metadata:
109+
namespace: default
110+
name: my-crontab
111+
spec:
112+
cronSpec: '* * *'
113+
image: ubuntu:latest
114+
`)
115+
116+
if err := teamClient.Create(ctx, crontab); err != nil {
117+
t.Fatalf("Failed to create CronTab in kcp: %v", err)
118+
}
119+
120+
// wait for the agent to sync the object down into the service cluster
121+
122+
t.Logf("Wait for CronTab to be synced…")
123+
copy := &unstructured.Unstructured{}
124+
copy.SetAPIVersion("example.com/v1")
125+
copy.SetKind("CronTab")
126+
127+
err := wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 30*time.Second, false, func(ctx context.Context) (done bool, err error) {
128+
copyKey := types.NamespacedName{Namespace: "synced-default", Name: "my-crontab"}
129+
return envtestClient.Get(ctx, copyKey, copy) == nil, nil
130+
})
131+
if err != nil {
132+
t.Fatalf("Failed to wait for object to be synced down: %v", err)
133+
}
134+
}

test/utils/process.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,44 @@ func uniqueLogfile(t *testing.T, basename string) string {
6868
return fmt.Sprintf("%s_%02d.log", testName, counter)
6969
}
7070

71+
func RunEndpointSliceAgent(
72+
ctx context.Context,
73+
t *testing.T,
74+
name string,
75+
kcpKubeconfig string,
76+
localKubeconfig string,
77+
apiExportEndpointSlice string,
78+
) context.CancelFunc {
79+
return runAgent(ctx, t, name, kcpKubeconfig, localKubeconfig, "--apiexportendpointslice-ref", apiExportEndpointSlice)
80+
}
81+
7182
func RunAgent(
7283
ctx context.Context,
7384
t *testing.T,
7485
name string,
7586
kcpKubeconfig string,
7687
localKubeconfig string,
7788
apiExport string,
89+
) context.CancelFunc {
90+
return runAgent(ctx, t, name, kcpKubeconfig, localKubeconfig, "--apiexport-ref", apiExport)
91+
}
92+
93+
func runAgent(
94+
ctx context.Context,
95+
t *testing.T,
96+
name string,
97+
kcpKubeconfig string,
98+
localKubeconfig string,
99+
refFlag string,
100+
refValue string,
78101
) context.CancelFunc {
79102
t.Helper()
80103

81104
t.Logf("Running agent %q…", name)
82105

83106
args := []string{
84107
"--agent-name", name,
85-
"--apiexport-ref", apiExport,
108+
refFlag, refValue,
86109
"--enable-leader-election=false",
87110
"--kubeconfig", localKubeconfig,
88111
"--kcp-kubeconfig", kcpKubeconfig,

0 commit comments

Comments
 (0)