Skip to content

Commit da328f9

Browse files
committed
actually test something useful
On-behalf-of: @SAP [email protected]
1 parent d29d82d commit da328f9

File tree

4 files changed

+113
-36
lines changed

4 files changed

+113
-36
lines changed

test/crds/crontab.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# sourced from https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
name: crontabs.example.com
6+
spec:
7+
group: example.com
8+
scope: Namespaced
9+
names:
10+
plural: crontabs
11+
singular: crontab
12+
kind: CronTab
13+
shortNames:
14+
- ct
15+
versions:
16+
- name: v1
17+
served: true
18+
storage: true
19+
schema:
20+
openAPIV3Schema:
21+
type: object
22+
properties:
23+
spec:
24+
type: object
25+
properties:
26+
cronSpec:
27+
type: string
28+
image:
29+
type: string
30+
replicas:
31+
type: integer

test/e2e/apiresourceschema/apiresourceschema_test.go

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,85 @@ import (
2525

2626
"github.com/go-logr/logr"
2727

28+
syncagentv1alpha1 "github.com/kcp-dev/api-syncagent/sdk/apis/syncagent/v1alpha1"
2829
"github.com/kcp-dev/api-syncagent/test/utils"
2930

31+
kcpapisv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha1"
32+
33+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34+
"k8s.io/apimachinery/pkg/types"
35+
"k8s.io/apimachinery/pkg/util/wait"
3036
ctrlruntime "sigs.k8s.io/controller-runtime"
3137
)
3238

3339
func TestARSAreCreated(t *testing.T) {
40+
const (
41+
apiExportName = "example.com"
42+
)
43+
3444
ctx := context.Background()
3545
ctrlruntime.SetLogger(logr.Discard())
3646

3747
// setup a test environment in kcp
38-
fooKubconfig := utils.CreateHomeWorkspace(t, ctx, "foo", "my-api")
48+
orgKubconfig := utils.CreateOrganization(t, ctx, "ars-are-created", apiExportName)
3949

4050
// start a service cluster
41-
envtestKubeconfig, _, _ := utils.RunEnvtest(t)
42-
43-
t.Run("my subtest", func(t *testing.T) {
44-
utils.RunAgent(
45-
ctx,
46-
t,
47-
"bob",
48-
fooKubconfig,
49-
envtestKubeconfig,
50-
"my-api",
51-
)
52-
53-
time.Sleep(5 * time.Second)
51+
envtestKubeconfig, envtestClient, _ := utils.RunEnvtest(t, []string{
52+
"test/crds/crontab.yaml",
53+
})
54+
55+
// publish Crontabs
56+
t.Logf("Publishing CronTabs…")
57+
pr := &syncagentv1alpha1.PublishedResource{
58+
ObjectMeta: metav1.ObjectMeta{
59+
Name: "publish-crontabs",
60+
},
61+
Spec: syncagentv1alpha1.PublishedResourceSpec{
62+
Resource: syncagentv1alpha1.SourceResourceDescriptor{
63+
APIGroup: "example.com",
64+
Version: "v1",
65+
Kind: "CronTab",
66+
},
67+
},
68+
}
69+
70+
if err := envtestClient.Create(ctx, pr); err != nil {
71+
t.Fatalf("Failed to create PublishedResource: %v", err)
72+
}
73+
74+
// let the agent do its thing
75+
utils.RunAgent(ctx, t, "bob", orgKubconfig, envtestKubeconfig, apiExportName)
76+
time.Sleep(5 * time.Second)
77+
78+
// wait for the APIExport to be updated
79+
t.Logf("Waiting for APIExport to be updated…")
80+
orgClient := utils.GetClient(t, orgKubconfig)
81+
apiExportKey := types.NamespacedName{Name: apiExportName}
82+
83+
var arsName string
84+
err := wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 1*time.Minute, false, func(ctx context.Context) (done bool, err error) {
85+
apiExport := &kcpapisv1alpha1.APIExport{}
86+
err = orgClient.Get(ctx, apiExportKey, apiExport)
87+
if err != nil {
88+
return false, err
89+
}
90+
91+
if len(apiExport.Spec.LatestResourceSchemas) == 0 {
92+
return false, nil
93+
}
94+
95+
arsName = apiExport.Spec.LatestResourceSchemas[0]
96+
97+
return true, nil
5498
})
99+
if err != nil {
100+
t.Fatalf("Failed to wait for APIExport to be updated: %v", err)
101+
}
55102

103+
// check the APIResourceSchema
104+
ars := &kcpapisv1alpha1.APIResourceSchema{}
105+
err = orgClient.Get(ctx, types.NamespacedName{Name: arsName}, ars)
106+
if err != nil {
107+
t.Fatalf("APIResourceSchema does not exist: %v", err)
108+
}
56109
}

test/utils/fixtures.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
"sigs.k8s.io/controller-runtime/pkg/kontext"
3939
)
4040

41-
func CreateHomeWorkspace(
41+
func CreateOrganization(
4242
t *testing.T,
4343
ctx context.Context,
4444
workspaceName logicalcluster.Name,
@@ -53,10 +53,10 @@ func CreateHomeWorkspace(
5353
}
5454

5555
// setup workspaces
56-
homeClusterName := CreateWorkspace(t, ctx, kcpClient, "root", workspaceName)
56+
orgClusterName := CreateWorkspace(t, ctx, kcpClient, "root", workspaceName)
5757

5858
// grant access and allow the agent to resolve its own workspace path
59-
homeCtx := kontext.WithCluster(ctx, homeClusterName)
59+
homeCtx := kontext.WithCluster(ctx, orgClusterName)
6060
GrantWorkspaceAccess(t, homeCtx, kcpClient, string(workspaceName), agent, rbacv1.PolicyRule{
6161
APIGroups: []string{"core.kcp.io"},
6262
Resources: []string{"logicalclusters"},
@@ -66,8 +66,8 @@ func CreateHomeWorkspace(
6666

6767
// add some consumer workspaces
6868
teamClusters := []logicalcluster.Name{
69-
CreateWorkspace(t, ctx, kcpClient, homeClusterName, "team-1"),
70-
CreateWorkspace(t, ctx, kcpClient, homeClusterName, "team-2"),
69+
CreateWorkspace(t, ctx, kcpClient, orgClusterName, "team-1"),
70+
CreateWorkspace(t, ctx, kcpClient, orgClusterName, "team-2"),
7171
}
7272

7373
// setup the APIExport and wait for it to be ready
@@ -79,7 +79,7 @@ func CreateHomeWorkspace(
7979
BindToAPIExport(t, teamCtx, kcpClient, apiExport)
8080
}
8181

82-
return CreateKcpAgentKubeconfig(t, fmt.Sprintf("/clusters/%s", homeClusterName))
82+
return CreateKcpAgentKubeconfig(t, fmt.Sprintf("/clusters/%s", orgClusterName))
8383
}
8484

8585
func CreateWorkspace(t *testing.T, ctx context.Context, client ctrlruntimeclient.Client, parent logicalcluster.Name, workspaceName logicalcluster.Name) logicalcluster.Name {

test/utils/process.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ import (
2626
"strings"
2727
"testing"
2828

29-
corev1 "k8s.io/api/core/v1"
30-
"k8s.io/apimachinery/pkg/runtime"
3129
"k8s.io/client-go/rest"
32-
"k8s.io/client-go/scale/scheme"
3330
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
3431
"sigs.k8s.io/controller-runtime/pkg/envtest"
3532
)
@@ -121,16 +118,20 @@ func RunAgent(
121118
return cancelAndWait
122119
}
123120

124-
func RunEnvtest(t *testing.T) (string, ctrlruntimeclient.Client, context.CancelFunc) {
121+
func RunEnvtest(t *testing.T, extraCRDs []string) (string, ctrlruntimeclient.Client, context.CancelFunc) {
125122
t.Helper()
126123

127-
rootDirectory := requiredEnv(t, "ROOT_DIRECTORY")
128-
129124
testEnv := &envtest.Environment{
130-
CRDDirectoryPaths: []string{filepath.Join(rootDirectory, "deploy/crd/kcp.io")},
131125
ErrorIfCRDPathMissing: true,
132126
}
133127

128+
rootDirectory := requiredEnv(t, "ROOT_DIRECTORY")
129+
extraCRDs = append(extraCRDs, "deploy/crd/kcp.io")
130+
131+
for _, extra := range extraCRDs {
132+
testEnv.CRDDirectoryPaths = append(testEnv.CRDDirectoryPaths, filepath.Join(rootDirectory, extra))
133+
}
134+
134135
_, err := testEnv.Start()
135136
if err != nil {
136137
t.Fatalf("Failed to start envtest: %v", err)
@@ -141,16 +142,8 @@ func RunEnvtest(t *testing.T) (string, ctrlruntimeclient.Client, context.CancelF
141142
t.Fatal(err)
142143
}
143144

144-
sc := runtime.NewScheme()
145-
if err := scheme.AddToScheme(sc); err != nil {
146-
t.Fatal(err)
147-
}
148-
if err := corev1.AddToScheme(sc); err != nil {
149-
t.Fatal(err)
150-
}
151-
152145
client, err := ctrlruntimeclient.New(adminRestConfig, ctrlruntimeclient.Options{
153-
Scheme: sc,
146+
Scheme: newScheme(t),
154147
})
155148
if err != nil {
156149
t.Fatalf("Failed to create client: %v", err)

0 commit comments

Comments
 (0)