Skip to content

Commit 9b4cc97

Browse files
committed
add basic e2e test
On-behalf-of: @SAP christoph.mewes@sap.com
1 parent 233d5c1 commit 9b4cc97

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//go:build e2e
2+
3+
/*
4+
Copyright 2026 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 cacheserver
20+
21+
import (
22+
"context"
23+
"fmt"
24+
"testing"
25+
"time"
26+
27+
"github.com/go-logr/logr"
28+
"github.com/kcp-dev/logicalcluster/v3"
29+
30+
corev1 "k8s.io/api/core/v1"
31+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32+
"k8s.io/apimachinery/pkg/types"
33+
ctrlruntime "sigs.k8s.io/controller-runtime"
34+
35+
operatorv1alpha1 "github.com/kcp-dev/kcp-operator/sdk/apis/operator/v1alpha1"
36+
"github.com/kcp-dev/kcp-operator/test/utils"
37+
)
38+
39+
func TestCacheWithRootShard(t *testing.T) {
40+
ctrlruntime.SetLogger(logr.Discard())
41+
42+
client := utils.GetKubeClient(t)
43+
ctx := context.Background()
44+
45+
// create namespace
46+
namespace := utils.CreateSelfDestructingNamespace(t, ctx, client, "cache-rootshard")
47+
48+
// deploy the cache server
49+
cacheServer := utils.DeployCacheServer(ctx, t, client, namespace.Name)
50+
51+
// deploy a root shard that uses our cache
52+
rootShard := utils.DeployRootShard(ctx, t, client, namespace.Name, "example.localhost", func(rs *operatorv1alpha1.RootShard) {
53+
rs.Spec.Cache.Reference = &corev1.LocalObjectReference{
54+
Name: cacheServer.Name,
55+
}
56+
})
57+
58+
// create a kubeconfig to access the root shard
59+
configSecretName := fmt.Sprintf("%s-shard-kubeconfig", rootShard.Name)
60+
61+
rsConfig := operatorv1alpha1.Kubeconfig{}
62+
rsConfig.Name = configSecretName
63+
rsConfig.Namespace = namespace.Name
64+
65+
rsConfig.Spec = operatorv1alpha1.KubeconfigSpec{
66+
Target: operatorv1alpha1.KubeconfigTarget{
67+
RootShardRef: &corev1.LocalObjectReference{
68+
Name: rootShard.Name,
69+
},
70+
},
71+
Username: "e2e",
72+
Validity: metav1.Duration{Duration: 2 * time.Hour},
73+
SecretRef: corev1.LocalObjectReference{
74+
Name: configSecretName,
75+
},
76+
Groups: []string{"system:masters"},
77+
}
78+
79+
t.Log("Creating kubeconfig for RootShard...")
80+
if err := client.Create(ctx, &rsConfig); err != nil {
81+
t.Fatal(err)
82+
}
83+
utils.WaitForObject(t, ctx, client, &corev1.Secret{}, types.NamespacedName{Namespace: rsConfig.Namespace, Name: rsConfig.Spec.SecretRef.Name})
84+
85+
t.Log("Connecting to RootShard...")
86+
rootShardClient := utils.ConnectWithKubeconfig(t, ctx, client, namespace.Name, rsConfig.Name, logicalcluster.NewPath("root"))
87+
88+
// proof of life: list something every logicalcluster in kcp has
89+
t.Log("Should be able to list Secrets.")
90+
secrets := &corev1.SecretList{}
91+
if err := rootShardClient.List(ctx, secrets); err != nil {
92+
t.Fatalf("Failed to list secrets in kcp: %v", err)
93+
}
94+
}

test/utils/deploy.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,43 @@ func DeployFrontProxy(ctx context.Context, t *testing.T, client ctrlruntimeclien
244244

245245
return frontProxy
246246
}
247+
248+
func DeployCacheServer(ctx context.Context, t *testing.T, client ctrlruntimeclient.Client, namespace string, patches ...func(*operatorv1alpha1.CacheServer)) operatorv1alpha1.CacheServer {
249+
t.Helper()
250+
251+
server := operatorv1alpha1.CacheServer{}
252+
server.Name = "kachy"
253+
server.Namespace = namespace
254+
255+
server.Spec = operatorv1alpha1.CacheServerSpec{
256+
Certificates: operatorv1alpha1.Certificates{
257+
IssuerRef: GetSelfSignedIssuerRef(),
258+
},
259+
}
260+
261+
if tag := getKcpTag(); tag != "" {
262+
server.Spec.Image = &operatorv1alpha1.ImageSpec{
263+
Tag: tag,
264+
}
265+
}
266+
267+
for _, patch := range patches {
268+
patch(&server)
269+
}
270+
271+
t.Logf("Creating CacheServer %s...", server.Name)
272+
if err := client.Create(ctx, &server); err != nil {
273+
t.Fatal(err)
274+
}
275+
276+
opts := []ctrlruntimeclient.ListOption{
277+
ctrlruntimeclient.InNamespace(server.Namespace),
278+
ctrlruntimeclient.MatchingLabels{
279+
"app.kubernetes.io/component": "cache-server",
280+
"app.kubernetes.io/instance": server.Name,
281+
},
282+
}
283+
WaitForPods(t, ctx, client, opts...)
284+
285+
return server
286+
}

0 commit comments

Comments
 (0)