Skip to content

Commit

Permalink
add validation tests for member cluster service
Browse files Browse the repository at this point in the history
  • Loading branch information
jamyct committed Nov 19, 2024
1 parent 326ac8a commit 05ae294
Show file tree
Hide file tree
Showing 9 changed files with 505 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ spec:
required:
- spec
type: object
x-kubernetes-validations:
- message: metadata.name max length is 63
rule: size(self.metadata.name) < 64
served: true
storage: true
subresources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ spec:
x-kubernetes-list-type: map
type: object
type: object
x-kubernetes-validations:
- message: metadata.name max length is 63
rule: size(self.metadata.name) < 64
served: true
storage: true
subresources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ spec:
type: string
type: object
type: object
x-kubernetes-validations:
- message: metadata.name max length is 63
rule: size(self.metadata.name) < 64
served: true
storage: true
subresources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ spec:
required:
- spec
type: object
x-kubernetes-validations:
- message: metadata.name max length is 63
rule: size(self.metadata.name) < 64
served: true
storage: true
subresources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ spec:
required:
- spec
type: object
x-kubernetes-validations:
- message: metadata.name max length is 63
rule: size(self.metadata.name) < 64
served: true
storage: true
subresources:
Expand Down
3 changes: 3 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ rules:
- multiclusterservices
- serviceexports
- serviceimports
- trafficmanagerbackends
- trafficmanagerprofiles
verbs:
- create
Expand All @@ -86,6 +87,7 @@ rules:
- multiclusterservices/status
- serviceexports/status
- serviceimports/status
- trafficmanagerbackends/status
- trafficmanagerprofiles/status
verbs:
- get
Expand All @@ -96,6 +98,7 @@ rules:
resources:
- multiclusterservices/finalizers
- serviceimports/finalizers
- trafficmanagerbackends/finalizers
- trafficmanagerprofiles/finalizers
verbs:
- get
Expand Down
185 changes: 185 additions & 0 deletions test/apis/v1alpha1/api_validation_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package v1alpha1

import (
"errors"
"fmt"
"reflect"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

v1alpha1 "go.goms.io/fleet-networking/api/v1alpha1"
)

var _ = Describe("Test cluster v1 API validation", func() {
Context("Test MemberClusterService API validation - invalid cases", func() {
It("should deny creating API with invalid name size", func() {
var name = "abcdef-123456789-123456789-123456789-123456789-123456789-123456789-123456789"
// Create the API.
memberClusterServiceName := &v1alpha1.MultiClusterService{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1alpha1.MultiClusterServiceSpec{
ServiceImport: v1alpha1.ServiceImportRef{
Name: "service-import-1",
},
},
}
By(fmt.Sprintf("expecting denial of CREATE API %s", name))
var err = hubClient.Create(ctx, memberClusterServiceName)
var statusErr *k8serrors.StatusError
Expect(errors.As(err, &statusErr)).To(BeTrue(), fmt.Sprintf("Create API call produced error %s. Error type wanted is %s.", reflect.TypeOf(err), reflect.TypeOf(&k8serrors.StatusError{})))
Expect(statusErr.Status().Message).Should(ContainSubstring("metadata.name max length is 63"))
})

It("should deny creating API with invalid name starting with non-alphanumeric character", func() {
var name = "-abcdef-123456789-123456789-123456789-123456789-123456789"
// Create the API.
memberClusterServiceName := &v1alpha1.MultiClusterService{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1alpha1.MultiClusterServiceSpec{
ServiceImport: v1alpha1.ServiceImportRef{
Name: "service-import-name",
},
},
}
By(fmt.Sprintf("expecting denial of CREATE API %s", name))
err := hubClient.Create(ctx, memberClusterServiceName)
var statusErr *k8serrors.StatusError
Expect(errors.As(err, &statusErr)).To(BeTrue(), fmt.Sprintf("Create API call produced error %s. Error type wanted is %s.", reflect.TypeOf(err), reflect.TypeOf(&k8serrors.StatusError{})))
Expect(statusErr.Status().Message).Should(ContainSubstring("a lowercase RFC 1123 subdomain"))
})

It("should deny creating API with invalid name ending with non-alphanumeric character", func() {
var name = "abcdef-123456789-123456789-123456789-123456789-123456789-"
// Create the API.
memberClusterServiceName := &v1alpha1.MultiClusterService{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1alpha1.MultiClusterServiceSpec{
ServiceImport: v1alpha1.ServiceImportRef{
Name: "service-import-name",
},
},
}
By(fmt.Sprintf("expecting denial of CREATE API %s", name))
err := hubClient.Create(ctx, memberClusterServiceName)
var statusErr *k8serrors.StatusError
Expect(errors.As(err, &statusErr)).To(BeTrue(), fmt.Sprintf("Create API call produced error %s. Error type wanted is %s.", reflect.TypeOf(err), reflect.TypeOf(&k8serrors.StatusError{})))
Expect(statusErr.Status().Message).Should(ContainSubstring("a lowercase RFC 1123 subdomain"))
})

It("should deny creating API with invalid name containing character that is not alphanumeric and not -", func() {
var name = "a_bcdef-123456789-123456789-123456789-123456789-123456789-123456789-123456789"
// Create the API.
memberClusterServiceName := &v1alpha1.MultiClusterService{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1alpha1.MultiClusterServiceSpec{
ServiceImport: v1alpha1.ServiceImportRef{
Name: "service-import-name",
},
},
}
By(fmt.Sprintf("expecting denial of CREATE API %s", name))
err := hubClient.Create(ctx, memberClusterServiceName)
var statusErr *k8serrors.StatusError
Expect(errors.As(err, &statusErr)).To(BeTrue(), fmt.Sprintf("Create API call produced error %s. Error type wanted is %s.", reflect.TypeOf(err), reflect.TypeOf(&k8serrors.StatusError{})))
Expect(statusErr.Status().Message).Should(ContainSubstring("a lowercase RFC 1123 subdomain"))
})
})

Context("Test Member Cluster Service creation API validation - valid cases", func() {
It("should allow creating API with valid name size", func() {
var name = "abc-123456789-123456789-123456789-123456789-123456789-123456789"
// Create the API.
memberClusterServiceName := &v1alpha1.MultiClusterService{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1alpha1.MultiClusterServiceSpec{
ServiceImport: v1alpha1.ServiceImportRef{
Name: "service-import-name",
},
},
}
Expect(hubClient.Create(ctx, memberClusterServiceName)).Should(Succeed())
Expect(hubClient.Delete(ctx, memberClusterServiceName)).Should(Succeed())
})

It("should allow creating API with valid name starting with alphabet character", func() {
var name = "abc-123456789"
// Create the API.
memberClusterServiceName := &v1alpha1.MultiClusterService{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1alpha1.MultiClusterServiceSpec{
ServiceImport: v1alpha1.ServiceImportRef{
Name: "service-import-name",
},
},
}
Expect(hubClient.Create(ctx, memberClusterServiceName)).Should(Succeed())
Expect(hubClient.Delete(ctx, memberClusterServiceName)).Should(Succeed())
})

It("should allow creating API with valid name starting with numeric character", func() {
var name = "123-123456789"
// Create the API.
memberClusterServiceName := &v1alpha1.MultiClusterService{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1alpha1.MultiClusterServiceSpec{
ServiceImport: v1alpha1.ServiceImportRef{
Name: "service-import-name",
},
},
}
Expect(hubClient.Create(ctx, memberClusterServiceName)).Should(Succeed())
Expect(hubClient.Delete(ctx, memberClusterServiceName)).Should(Succeed())
})

It("should allow creating API with valid name ending with alphabet character", func() {
var name = "123456789-abc"
// Create the API.
memberClusterServiceName := &v1alpha1.MultiClusterService{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1alpha1.MultiClusterServiceSpec{
ServiceImport: v1alpha1.ServiceImportRef{
Name: "service-import-name",
},
},
}
Expect(hubClient.Create(ctx, memberClusterServiceName)).Should(Succeed())
Expect(hubClient.Delete(ctx, memberClusterServiceName)).Should(Succeed())
})

It("should allow creating API with valid name ending with numeric character", func() {
var name = "123456789-123"
// Create the API.
memberClusterServiceName := &v1alpha1.MultiClusterService{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1alpha1.MultiClusterServiceSpec{
ServiceImport: v1alpha1.ServiceImportRef{
Name: "service-import-name",
},
},
}
Expect(hubClient.Create(ctx, memberClusterServiceName)).Should(Succeed())
Expect(hubClient.Delete(ctx, memberClusterServiceName)).Should(Succeed())
})
})
})
88 changes: 88 additions & 0 deletions test/apis/v1alpha1/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package v1alpha1

import (
"context"
"flag"
"path/filepath"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog/v2"
"k8s.io/klog/v2/textlogger"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

v1alpha1 "go.goms.io/fleet-networking/api/v1alpha1"
)

var (
hubTestEnv *envtest.Environment
hubClient client.Client
ctx context.Context
cancel context.CancelFunc
)

func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)

RunSpecs(t, "ClusterResourcePlacement Controller Suite")
}

var _ = BeforeSuite(func() {
By("Setup klog")
fs := flag.NewFlagSet("klog", flag.ContinueOnError)
klog.InitFlags(fs)
Expect(fs.Parse([]string{"--v", "5", "-add_dir_header", "true"})).Should(Succeed())

ctx, cancel = context.WithCancel(context.TODO())

By("bootstrap the test environment")
// Start the cluster.
hubTestEnv = &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join("..", "..", "..", "..", "config", "crd", "bases"),
},
ErrorIfCRDPathMissing: true,
}
hubCfg, err := hubTestEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(hubCfg).NotTo(BeNil())

Expect(v1alpha1.AddToScheme(scheme.Scheme)).Should(Succeed())

klog.InitFlags(flag.CommandLine)
flag.Parse()
// Create the hub controller manager.
hubCtrlMgr, err := ctrl.NewManager(hubCfg, ctrl.Options{
Scheme: scheme.Scheme,
Metrics: metricsserver.Options{
BindAddress: "0",
},
Logger: textlogger.NewLogger(textlogger.NewConfig(textlogger.Verbosity(4))),
})
Expect(err).NotTo(HaveOccurred())

// Set up the client.
// The client must be one with cache (i.e. configured by the controller manager) to make
// use of the cache indexes.
hubClient = hubCtrlMgr.GetClient()
Expect(hubClient).NotTo(BeNil())

go func() {
defer GinkgoRecover()
err = hubCtrlMgr.Start(ctx)
Expect(err).ToNot(HaveOccurred(), "failed to start manager for hub")
}()
})

var _ = AfterSuite(func() {
defer klog.Flush()
cancel()

By("tearing down the test environment")
Expect(hubTestEnv.Stop()).Should(Succeed())
})
Loading

0 comments on commit 05ae294

Please sign in to comment.