Skip to content

Commit

Permalink
actually test something useful
Browse files Browse the repository at this point in the history
On-behalf-of: @SAP [email protected]
  • Loading branch information
xrstf committed Feb 12, 2025
1 parent d29d82d commit a8e24a1
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 36 deletions.
31 changes: 31 additions & 0 deletions test/crds/crontab.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# sourced from https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: crontabs.example.com
spec:
group: example.com
scope: Namespaced
names:
plural: crontabs
singular: crontab
kind: CronTab
shortNames:
- ct
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
cronSpec:
type: string
image:
type: string
replicas:
type: integer
80 changes: 66 additions & 14 deletions test/e2e/apiresourceschema/apiresourceschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,84 @@ import (

"github.com/go-logr/logr"

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

kcpapisv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha1"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
ctrlruntime "sigs.k8s.io/controller-runtime"
)

func TestARSAreCreated(t *testing.T) {
const (
apiExportName = "example.com"
)

ctx := context.Background()
ctrlruntime.SetLogger(logr.Discard())

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

// start a service cluster
envtestKubeconfig, _, _ := utils.RunEnvtest(t)

t.Run("my subtest", func(t *testing.T) {
utils.RunAgent(
ctx,
t,
"bob",
fooKubconfig,
envtestKubeconfig,
"my-api",
)

time.Sleep(5 * time.Second)
envtestKubeconfig, envtestClient, _ := utils.RunEnvtest(t, []string{
"test/crds/crontab.yaml",
})

// publish Crontabs
t.Logf("Publishing CronTabs…")
pr := &syncagentv1alpha1.PublishedResource{
ObjectMeta: metav1.ObjectMeta{
Name: "publish-crontabs",
},
Spec: syncagentv1alpha1.PublishedResourceSpec{
Resource: syncagentv1alpha1.SourceResourceDescriptor{
APIGroup: "example.com",
Version: "v1",
Kind: "CronTab",
},
},
}

if err := envtestClient.Create(ctx, pr); err != nil {
t.Fatalf("Failed to create PublishedResource: %v", err)
}

// let the agent do its thing
utils.RunAgent(ctx, t, "bob", orgKubconfig, envtestKubeconfig, apiExportName)

// wait for the APIExport to be updated
t.Logf("Waiting for APIExport to be updated…")
orgClient := utils.GetClient(t, orgKubconfig)
apiExportKey := types.NamespacedName{Name: apiExportName}

var arsName string
err := wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 1*time.Minute, false, func(ctx context.Context) (done bool, err error) {
apiExport := &kcpapisv1alpha1.APIExport{}
err = orgClient.Get(ctx, apiExportKey, apiExport)
if err != nil {
return false, err
}

if len(apiExport.Spec.LatestResourceSchemas) == 0 {
return false, nil
}

arsName = apiExport.Spec.LatestResourceSchemas[0]

return true, nil
})
if err != nil {
t.Fatalf("Failed to wait for APIExport to be updated: %v", err)
}

// check the APIResourceSchema
ars := &kcpapisv1alpha1.APIResourceSchema{}
err = orgClient.Get(ctx, types.NamespacedName{Name: arsName}, ars)
if err != nil {
t.Fatalf("APIResourceSchema does not exist: %v", err)
}
}
12 changes: 6 additions & 6 deletions test/utils/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/kontext"
)

func CreateHomeWorkspace(
func CreateOrganization(
t *testing.T,
ctx context.Context,
workspaceName logicalcluster.Name,
Expand All @@ -53,10 +53,10 @@ func CreateHomeWorkspace(
}

// setup workspaces
homeClusterName := CreateWorkspace(t, ctx, kcpClient, "root", workspaceName)
orgClusterName := CreateWorkspace(t, ctx, kcpClient, "root", workspaceName)

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

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

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

return CreateKcpAgentKubeconfig(t, fmt.Sprintf("/clusters/%s", homeClusterName))
return CreateKcpAgentKubeconfig(t, fmt.Sprintf("/clusters/%s", orgClusterName))
}

func CreateWorkspace(t *testing.T, ctx context.Context, client ctrlruntimeclient.Client, parent logicalcluster.Name, workspaceName logicalcluster.Name) logicalcluster.Name {
Expand Down
25 changes: 9 additions & 16 deletions test/utils/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ import (
"strings"
"testing"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"k8s.io/client-go/scale/scheme"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
)
Expand Down Expand Up @@ -121,16 +118,20 @@ func RunAgent(
return cancelAndWait
}

func RunEnvtest(t *testing.T) (string, ctrlruntimeclient.Client, context.CancelFunc) {
func RunEnvtest(t *testing.T, extraCRDs []string) (string, ctrlruntimeclient.Client, context.CancelFunc) {
t.Helper()

rootDirectory := requiredEnv(t, "ROOT_DIRECTORY")

testEnv := &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join(rootDirectory, "deploy/crd/kcp.io")},
ErrorIfCRDPathMissing: true,
}

rootDirectory := requiredEnv(t, "ROOT_DIRECTORY")
extraCRDs = append(extraCRDs, "deploy/crd/kcp.io")

for _, extra := range extraCRDs {
testEnv.CRDDirectoryPaths = append(testEnv.CRDDirectoryPaths, filepath.Join(rootDirectory, extra))
}

_, err := testEnv.Start()
if err != nil {
t.Fatalf("Failed to start envtest: %v", err)
Expand All @@ -141,16 +142,8 @@ func RunEnvtest(t *testing.T) (string, ctrlruntimeclient.Client, context.CancelF
t.Fatal(err)
}

sc := runtime.NewScheme()
if err := scheme.AddToScheme(sc); err != nil {
t.Fatal(err)
}
if err := corev1.AddToScheme(sc); err != nil {
t.Fatal(err)
}

client, err := ctrlruntimeclient.New(adminRestConfig, ctrlruntimeclient.Options{
Scheme: sc,
Scheme: newScheme(t),
})
if err != nil {
t.Fatalf("Failed to create client: %v", err)
Expand Down

0 comments on commit a8e24a1

Please sign in to comment.