Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
jamyct committed Dec 3, 2024
1 parent e9e8a85 commit 5b4bb9f
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 4 deletions.
4 changes: 0 additions & 4 deletions test/apis/v1alpha1/api_validation_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/*
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
*/
package v1alpha1

import (
Expand Down
4 changes: 4 additions & 0 deletions test/apis/v1alpha1/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ var _ = BeforeSuite(func() {
// Start the cluster.
hubTestEnv = &envtest.Environment{
CRDDirectoryPaths: []string{
<<<<<<< HEAD

Check failure on line 47 in test/apis/v1alpha1/suite_test.go

View workflow job for this annotation

GitHub Actions / Lint

syntax error: unexpected <<, expected expression

Check failure on line 47 in test/apis/v1alpha1/suite_test.go

View workflow job for this annotation

GitHub Actions / Lint

expected operand, found '<<' (typecheck)

Check failure on line 47 in test/apis/v1alpha1/suite_test.go

View workflow job for this annotation

GitHub Actions / unit-integration-tests

expected operand, found '<<'
filepath.Join("..", "..", "..", "config", "crd", "bases"),

Check failure on line 48 in test/apis/v1alpha1/suite_test.go

View workflow job for this annotation

GitHub Actions / Lint

syntax error: unexpected ) in composite literal; possibly missing comma or }
=======
filepath.Join("..", "..", "..", "..", "config", "crd", "bases"),
>>>>>>> ad659a0 (add validation tests for member cluster service)

Check failure on line 51 in test/apis/v1alpha1/suite_test.go

View workflow job for this annotation

GitHub Actions / Lint

syntax error: unexpected cluster, expected {

Check failure on line 51 in test/apis/v1alpha1/suite_test.go

View workflow job for this annotation

GitHub Actions / Lint

missing ',' in composite literal (typecheck)

Check failure on line 51 in test/apis/v1alpha1/suite_test.go

View workflow job for this annotation

GitHub Actions / unit-integration-tests

missing ',' in composite literal
},

Check failure on line 52 in test/apis/v1alpha1/suite_test.go

View workflow job for this annotation

GitHub Actions / Lint

syntax error: unexpected }, expected expression
ErrorIfCRDPathMissing: true,
}

Check failure on line 54 in test/apis/v1alpha1/suite_test.go

View workflow job for this annotation

GitHub Actions / Lint

syntax error: unexpected }, expected { after for clause (typecheck)
Expand Down
214 changes: 214 additions & 0 deletions test/e2e/resource_validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/*
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
*/
package e2e

import (
"errors"
"fmt"

"k8s.io/apimachinery/pkg/types"

"reflect"

rbacv1 "k8s.io/api/rbac/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = Describe("Resource validation tests for Member Cluster", func() {
It("should deny creating API with invalid name size", func() {
var name = "abcdef-123456789-123456789-123456789-123456789-123456789-123456789-123456789"
// Create the API.
memberClusterName := ServiceExport{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
By(fmt.Sprintf("expecting denial of CREATE API %s", name))
err := hubClient.Create(ctx, memberClusterName)
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 allow creating API with valid name size", func() {
var name = "abc-123456789-123456789-123456789-123456789-123456789-123456789"
// Create the API.
memberClusterName := &clusterv1beta1.MemberCluster{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: clusterv1beta1.MemberClusterSpec{
Identity: rbacv1.Subject{
Name: "fleet-member-agent-cluster-1",
Kind: "ServiceAccount",
Namespace: "fleet-system",
APIGroup: "",
},
},
}
Expect(impersonateHubClient.Create(ctx, memberClusterName)).Should(Succeed())
Expect(impersonateHubClient.Get(ctx, types.NamespacedName{Name: memberClusterName.Name}, memberClusterName)).Should(Succeed())
Expect(impersonateHubClient.Delete(ctx, memberClusterName)).Should(Succeed())
ensureMemberClusterAndRelatedResourcesDeletion(name)
})

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.
memberClusterName := &clusterv1beta1.MemberCluster{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: clusterv1beta1.MemberClusterSpec{
Identity: rbacv1.Subject{
Name: "fleet-member-agent-cluster-1",
Kind: "ServiceAccount",
Namespace: "fleet-system",
APIGroup: "",
},
},
}
By(fmt.Sprintf("expecting denial of CREATE API %s", name))
err := hubClient.Create(ctx, memberClusterName)
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 must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')"))
})

It("should allow creating API with valid name starting with alphabet character", func() {
var name = "abc-123456789"
// Create the API.
memberClusterName := &clusterv1beta1.MemberCluster{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: clusterv1beta1.MemberClusterSpec{
Identity: rbacv1.Subject{
Name: "fleet-member-agent-cluster-1",
Kind: "ServiceAccount",
Namespace: "fleet-system",
APIGroup: "",
},
},
}
Expect(impersonateHubClient.Create(ctx, memberClusterName)).Should(Succeed())
Expect(impersonateHubClient.Get(ctx, types.NamespacedName{Name: memberClusterName.Name}, memberClusterName)).Should(Succeed())
Expect(impersonateHubClient.Delete(ctx, memberClusterName)).Should(Succeed())
ensureMemberClusterAndRelatedResourcesDeletion(name)
})

It("should allow creating API with valid name starting with numeric character", func() {
var name = "123-123456789"
// Create the API.
memberClusterName := &clusterv1beta1.MemberCluster{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: clusterv1beta1.MemberClusterSpec{
Identity: rbacv1.Subject{
Name: "fleet-member-agent-cluster-1",
Kind: "ServiceAccount",
Namespace: "fleet-system",
APIGroup: "",
},
},
}
Expect(impersonateHubClient.Create(ctx, memberClusterName)).Should(Succeed())
Expect(impersonateHubClient.Get(ctx, types.NamespacedName{Name: memberClusterName.Name}, memberClusterName)).Should(Succeed())
Expect(impersonateHubClient.Delete(ctx, memberClusterName)).Should(Succeed())
ensureMemberClusterAndRelatedResourcesDeletion(name)
})

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.
memberClusterName := &clusterv1beta1.MemberCluster{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: clusterv1beta1.MemberClusterSpec{
Identity: rbacv1.Subject{
Name: "fleet-member-agent-cluster-1",
Kind: "ServiceAccount",
Namespace: "fleet-system",
APIGroup: "",
},
},
}
By(fmt.Sprintf("expecting denial of CREATE API %s", name))
err := hubClient.Create(ctx, memberClusterName)
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 must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')"))
})

It("should allow creating API with valid name ending with alphabet character", func() {
var name = "123456789-abc"
// Create the API.
memberClusterName := &clusterv1beta1.MemberCluster{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: clusterv1beta1.MemberClusterSpec{
Identity: rbacv1.Subject{
Name: "fleet-member-agent-cluster-1",
Kind: "ServiceAccount",
Namespace: "fleet-system",
APIGroup: "",
},
},
}
Expect(impersonateHubClient.Create(ctx, memberClusterName)).Should(Succeed())
Expect(impersonateHubClient.Get(ctx, types.NamespacedName{Name: memberClusterName.Name}, memberClusterName)).Should(Succeed())
Expect(impersonateHubClient.Delete(ctx, memberClusterName)).Should(Succeed())
ensureMemberClusterAndRelatedResourcesDeletion(name)
})

It("should allow creating API with valid name ending with numeric character", func() {
var name = "123456789-123"
// Create the API.
memberClusterName := &clusterv1beta1.MemberCluster{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: clusterv1beta1.MemberClusterSpec{
Identity: rbacv1.Subject{
Name: "fleet-member-agent-cluster-1",
Kind: "ServiceAccount",
Namespace: "fleet-system",
APIGroup: "",
},
},
}
Expect(impersonateHubClient.Create(ctx, memberClusterName)).Should(Succeed())
Expect(impersonateHubClient.Get(ctx, types.NamespacedName{Name: memberClusterName.Name}, memberClusterName)).Should(Succeed())
Expect(impersonateHubClient.Delete(ctx, memberClusterName)).Should(Succeed())
ensureMemberClusterAndRelatedResourcesDeletion(name)
})

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.
memberClusterName := &clusterv1beta1.MemberCluster{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: clusterv1beta1.MemberClusterSpec{
Identity: rbacv1.Subject{
Name: "fleet-member-agent-cluster-1",
Kind: "ServiceAccount",
Namespace: "fleet-system",
APIGroup: "",
},
},
}
By(fmt.Sprintf("expecting denial of CREATE API %s", name))
err := hubClient.Create(ctx, memberClusterName)
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 must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')"))
})
})

0 comments on commit 5b4bb9f

Please sign in to comment.