sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/model/resource/gvk_test.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package resource 18 19 import ( 20 "strings" 21 22 . "github.com/onsi/ginkgo/v2" 23 . "github.com/onsi/gomega" 24 ) 25 26 var _ = Describe("GVK", func() { 27 const ( 28 group = "group" 29 domain = "my.domain" 30 version = "v1" 31 kind = "Kind" 32 ) 33 34 gvk := GVK{Group: group, Domain: domain, Version: version, Kind: kind} 35 36 Context("Validate", func() { 37 It("should succeed for a valid GVK", func() { 38 Expect(gvk.Validate()).To(Succeed()) 39 }) 40 41 DescribeTable("should fail for invalid GVKs", 42 func(gvk GVK) { Expect(gvk.Validate()).NotTo(Succeed()) }, 43 // Ensure that the rest of the fields are valid to check each part 44 Entry("Group (uppercase)", GVK{Group: "Group", Domain: domain, Version: version, Kind: kind}), 45 Entry("Group (non-alpha characters)", GVK{Group: "_*?", Domain: domain, Version: version, Kind: kind}), 46 Entry("Domain (uppercase)", GVK{Group: group, Domain: "Domain", Version: version, Kind: kind}), 47 Entry("Domain (non-alpha characters)", GVK{Group: group, Domain: "_*?", Version: version, Kind: kind}), 48 Entry("Group and Domain (empty)", GVK{Group: "", Domain: "", Version: version, Kind: kind}), 49 Entry("Version (empty)", GVK{Group: group, Domain: domain, Version: "", Kind: kind}), 50 Entry("Version (starts with number)", GVK{Group: group, Domain: domain, Version: "1", Kind: kind}), 51 Entry("Version (uppercase)", GVK{Group: group, Domain: domain, Version: "V1", Kind: kind}), 52 Entry("Version (non-alpha characters)", GVK{Group: group, Domain: domain, Version: "_*?", Kind: kind}), 53 Entry("Kind (empty)", GVK{Group: group, Domain: domain, Version: version, Kind: ""}), 54 Entry("Kind (whitespaces)", GVK{Group: group, Domain: domain, Version: version, Kind: "Ki nd"}), 55 Entry("Kind (lowercase)", GVK{Group: group, Domain: domain, Version: version, Kind: "kind"}), 56 Entry("Kind (starts with number)", GVK{Group: group, Domain: domain, Version: version, Kind: "1Kind"}), 57 Entry("Kind (ends with `-`)", GVK{Group: group, Domain: domain, Version: version, Kind: "Kind-"}), 58 Entry("Kind (non-alpha characters)", GVK{Group: group, Domain: domain, Version: version, Kind: "_*?"}), 59 Entry("Kind (too long)", 60 GVK{Group: group, Domain: domain, Version: version, Kind: strings.Repeat("a", 64)}), 61 ) 62 }) 63 64 Context("QualifiedGroup", func() { 65 DescribeTable("should return the correct string", 66 func(gvk GVK, qualifiedGroup string) { Expect(gvk.QualifiedGroup()).To(Equal(qualifiedGroup)) }, 67 Entry("fully qualified resource", gvk, group+"."+domain), 68 Entry("empty group name", GVK{Domain: domain, Version: version, Kind: kind}, domain), 69 Entry("empty domain", GVK{Group: group, Version: version, Kind: kind}, group), 70 ) 71 }) 72 73 Context("IsEqualTo", func() { 74 It("should return true for the same resource", func() { 75 Expect(gvk.IsEqualTo(GVK{Group: group, Domain: domain, Version: version, Kind: kind})).To(BeTrue()) 76 }) 77 78 DescribeTable("should return false for different resources", 79 func(other GVK) { Expect(gvk.IsEqualTo(other)).To(BeFalse()) }, 80 Entry("different kind", GVK{Group: group, Domain: domain, Version: version, Kind: "Kind2"}), 81 Entry("different version", GVK{Group: group, Domain: domain, Version: "v2", Kind: kind}), 82 Entry("different domain", GVK{Group: group, Domain: "other.domain", Version: version, Kind: kind}), 83 Entry("different group", GVK{Group: "group2", Domain: domain, Version: version, Kind: kind}), 84 ) 85 }) 86 })