|
| 1 | +/* |
| 2 | +Copyright (c) Microsoft Corporation. |
| 3 | +Licensed under the MIT license. |
| 4 | +*/ |
| 5 | +package v1 |
| 6 | + |
| 7 | +import ( |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "reflect" |
| 11 | + |
| 12 | + . "github.com/onsi/ginkgo/v2" |
| 13 | + . "github.com/onsi/gomega" |
| 14 | + rbacv1 "k8s.io/api/rbac/v1" |
| 15 | + k8serrors "k8s.io/apimachinery/pkg/api/errors" |
| 16 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 17 | + |
| 18 | + clusterv1 "go.goms.io/fleet/apis/cluster/v1" |
| 19 | +) |
| 20 | + |
| 21 | +var _ = Describe("Test cluster v1 API validation", func() { |
| 22 | + Context("Test MemberCluster API validation - invalid cases", func() { |
| 23 | + It("should deny creating API with invalid name size", func() { |
| 24 | + var name = "abcdef-123456789-123456789-123456789-123456789-123456789-123456789-123456789" |
| 25 | + // Create the API. |
| 26 | + memberClusterName := &clusterv1.MemberCluster{ |
| 27 | + ObjectMeta: metav1.ObjectMeta{ |
| 28 | + Name: name, |
| 29 | + }, |
| 30 | + Spec: clusterv1.MemberClusterSpec{ |
| 31 | + Identity: rbacv1.Subject{ |
| 32 | + Name: "fleet-member-agent-cluster-1", |
| 33 | + Kind: "ServiceAccount", |
| 34 | + Namespace: "fleet-system", |
| 35 | + APIGroup: "", |
| 36 | + }, |
| 37 | + }, |
| 38 | + } |
| 39 | + By(fmt.Sprintf("expecting denial of CREATE API %s", name)) |
| 40 | + var err = hubClient.Create(ctx, memberClusterName) |
| 41 | + var statusErr *k8serrors.StatusError |
| 42 | + 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{}))) |
| 43 | + Expect(statusErr.Status().Message).Should(ContainSubstring("metadata.name max length is 63")) |
| 44 | + }) |
| 45 | + |
| 46 | + It("should deny creating API with invalid name starting with non-alphanumeric character", func() { |
| 47 | + var name = "-abcdef-123456789-123456789-123456789-123456789-123456789" |
| 48 | + // Create the API. |
| 49 | + memberClusterName := &clusterv1.MemberCluster{ |
| 50 | + ObjectMeta: metav1.ObjectMeta{ |
| 51 | + Name: name, |
| 52 | + }, |
| 53 | + Spec: clusterv1.MemberClusterSpec{ |
| 54 | + Identity: rbacv1.Subject{ |
| 55 | + Name: "fleet-member-agent-cluster-1", |
| 56 | + Kind: "ServiceAccount", |
| 57 | + Namespace: "fleet-system", |
| 58 | + APIGroup: "", |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | + By(fmt.Sprintf("expecting denial of CREATE API %s", name)) |
| 63 | + err := hubClient.Create(ctx, memberClusterName) |
| 64 | + var statusErr *k8serrors.StatusError |
| 65 | + 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{}))) |
| 66 | + Expect(statusErr.Status().Message).Should(ContainSubstring("a lowercase RFC 1123 subdomain")) |
| 67 | + }) |
| 68 | + |
| 69 | + It("should deny creating API with invalid name ending with non-alphanumeric character", func() { |
| 70 | + var name = "abcdef-123456789-123456789-123456789-123456789-123456789-" |
| 71 | + // Create the API. |
| 72 | + memberClusterName := &clusterv1.MemberCluster{ |
| 73 | + ObjectMeta: metav1.ObjectMeta{ |
| 74 | + Name: name, |
| 75 | + }, |
| 76 | + Spec: clusterv1.MemberClusterSpec{ |
| 77 | + Identity: rbacv1.Subject{ |
| 78 | + Name: "fleet-member-agent-cluster-1", |
| 79 | + Kind: "ServiceAccount", |
| 80 | + Namespace: "fleet-system", |
| 81 | + APIGroup: "", |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | + By(fmt.Sprintf("expecting denial of CREATE API %s", name)) |
| 86 | + err := hubClient.Create(ctx, memberClusterName) |
| 87 | + var statusErr *k8serrors.StatusError |
| 88 | + 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{}))) |
| 89 | + Expect(statusErr.Status().Message).Should(ContainSubstring("a lowercase RFC 1123 subdomain")) |
| 90 | + }) |
| 91 | + |
| 92 | + It("should deny creating API with invalid name containing character that is not alphanumeric and not -", func() { |
| 93 | + var name = "a_bcdef-123456789-123456789-123456789-123456789-123456789-123456789-123456789" |
| 94 | + // Create the API. |
| 95 | + memberClusterName := &clusterv1.MemberCluster{ |
| 96 | + ObjectMeta: metav1.ObjectMeta{ |
| 97 | + Name: name, |
| 98 | + }, |
| 99 | + Spec: clusterv1.MemberClusterSpec{ |
| 100 | + Identity: rbacv1.Subject{ |
| 101 | + Name: "fleet-member-agent-cluster-1", |
| 102 | + Kind: "ServiceAccount", |
| 103 | + Namespace: "fleet-system", |
| 104 | + APIGroup: "", |
| 105 | + }, |
| 106 | + }, |
| 107 | + } |
| 108 | + By(fmt.Sprintf("expecting denial of CREATE API %s", name)) |
| 109 | + err := hubClient.Create(ctx, memberClusterName) |
| 110 | + var statusErr *k8serrors.StatusError |
| 111 | + 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{}))) |
| 112 | + Expect(statusErr.Status().Message).Should(ContainSubstring("a lowercase RFC 1123 subdomain")) |
| 113 | + }) |
| 114 | + }) |
| 115 | + |
| 116 | + Context("Test Member Cluster creation API validation - valid cases", func() { |
| 117 | + It("should allow creating API with valid name size", func() { |
| 118 | + var name = "abc-123456789-123456789-123456789-123456789-123456789-123456789" |
| 119 | + // Create the API. |
| 120 | + memberClusterName := &clusterv1.MemberCluster{ |
| 121 | + ObjectMeta: metav1.ObjectMeta{ |
| 122 | + Name: name, |
| 123 | + }, |
| 124 | + Spec: clusterv1.MemberClusterSpec{ |
| 125 | + Identity: rbacv1.Subject{ |
| 126 | + Name: "fleet-member-agent-cluster-1", |
| 127 | + Kind: "ServiceAccount", |
| 128 | + Namespace: "fleet-system", |
| 129 | + APIGroup: "", |
| 130 | + }, |
| 131 | + }, |
| 132 | + } |
| 133 | + Expect(hubClient.Create(ctx, memberClusterName)).Should(Succeed()) |
| 134 | + Expect(hubClient.Delete(ctx, memberClusterName)).Should(Succeed()) |
| 135 | + }) |
| 136 | + |
| 137 | + It("should allow creating API with valid name starting with alphabet character", func() { |
| 138 | + var name = "abc-123456789" |
| 139 | + // Create the API. |
| 140 | + memberClusterName := &clusterv1.MemberCluster{ |
| 141 | + ObjectMeta: metav1.ObjectMeta{ |
| 142 | + Name: name, |
| 143 | + }, |
| 144 | + Spec: clusterv1.MemberClusterSpec{ |
| 145 | + Identity: rbacv1.Subject{ |
| 146 | + Name: "fleet-member-agent-cluster-1", |
| 147 | + Kind: "ServiceAccount", |
| 148 | + Namespace: "fleet-system", |
| 149 | + APIGroup: "", |
| 150 | + }, |
| 151 | + }, |
| 152 | + } |
| 153 | + Expect(hubClient.Create(ctx, memberClusterName)).Should(Succeed()) |
| 154 | + Expect(hubClient.Delete(ctx, memberClusterName)).Should(Succeed()) |
| 155 | + }) |
| 156 | + |
| 157 | + It("should allow creating API with valid name starting with numeric character", func() { |
| 158 | + var name = "123-123456789" |
| 159 | + // Create the API. |
| 160 | + memberClusterName := &clusterv1.MemberCluster{ |
| 161 | + ObjectMeta: metav1.ObjectMeta{ |
| 162 | + Name: name, |
| 163 | + }, |
| 164 | + Spec: clusterv1.MemberClusterSpec{ |
| 165 | + Identity: rbacv1.Subject{ |
| 166 | + Name: "fleet-member-agent-cluster-1", |
| 167 | + Kind: "ServiceAccount", |
| 168 | + Namespace: "fleet-system", |
| 169 | + APIGroup: "", |
| 170 | + }, |
| 171 | + }, |
| 172 | + } |
| 173 | + Expect(hubClient.Create(ctx, memberClusterName)).Should(Succeed()) |
| 174 | + Expect(hubClient.Delete(ctx, memberClusterName)).Should(Succeed()) |
| 175 | + }) |
| 176 | + |
| 177 | + It("should allow creating API with valid name ending with alphabet character", func() { |
| 178 | + var name = "123456789-abc" |
| 179 | + // Create the API. |
| 180 | + memberClusterName := &clusterv1.MemberCluster{ |
| 181 | + ObjectMeta: metav1.ObjectMeta{ |
| 182 | + Name: name, |
| 183 | + }, |
| 184 | + Spec: clusterv1.MemberClusterSpec{ |
| 185 | + Identity: rbacv1.Subject{ |
| 186 | + Name: "fleet-member-agent-cluster-1", |
| 187 | + Kind: "ServiceAccount", |
| 188 | + Namespace: "fleet-system", |
| 189 | + APIGroup: "", |
| 190 | + }, |
| 191 | + }, |
| 192 | + } |
| 193 | + Expect(hubClient.Create(ctx, memberClusterName)).Should(Succeed()) |
| 194 | + Expect(hubClient.Delete(ctx, memberClusterName)).Should(Succeed()) |
| 195 | + }) |
| 196 | + |
| 197 | + It("should allow creating API with valid name ending with numeric character", func() { |
| 198 | + var name = "123456789-123" |
| 199 | + // Create the API. |
| 200 | + memberClusterName := &clusterv1.MemberCluster{ |
| 201 | + ObjectMeta: metav1.ObjectMeta{ |
| 202 | + Name: name, |
| 203 | + }, |
| 204 | + Spec: clusterv1.MemberClusterSpec{ |
| 205 | + Identity: rbacv1.Subject{ |
| 206 | + Name: "fleet-member-agent-cluster-1", |
| 207 | + Kind: "ServiceAccount", |
| 208 | + Namespace: "fleet-system", |
| 209 | + APIGroup: "", |
| 210 | + }, |
| 211 | + }, |
| 212 | + } |
| 213 | + Expect(hubClient.Create(ctx, memberClusterName)).Should(Succeed()) |
| 214 | + Expect(hubClient.Delete(ctx, memberClusterName)).Should(Succeed()) |
| 215 | + }) |
| 216 | + }) |
| 217 | +}) |
0 commit comments