|
| 1 | +package resource_test |
| 2 | + |
| 3 | +import ( |
| 4 | + . "github.com/onsi/ginkgo" |
| 5 | + . "github.com/onsi/gomega" |
| 6 | + |
| 7 | + . "sigs.k8s.io/kubebuilder/pkg/scaffold/resource" |
| 8 | +) |
| 9 | + |
| 10 | +var _ = Describe("Resource", func() { |
| 11 | + Describe("scaffolding an API", func() { |
| 12 | + It("should succeed if the Resource is valid", func() { |
| 13 | + instance := &Resource{Group: "crew", Version: "v1", Kind: "FirstMate"} |
| 14 | + Expect(instance.Validate()).To(Succeed()) |
| 15 | + }) |
| 16 | + |
| 17 | + It("should fail if the Group is not specified", func() { |
| 18 | + instance := &Resource{Version: "v1", Kind: "FirstMate"} |
| 19 | + Expect(instance.Validate()).NotTo(Succeed()) |
| 20 | + Expect(instance.Validate().Error()).To(ContainSubstring("group cannot be empty")) |
| 21 | + }) |
| 22 | + |
| 23 | + It("should fail if the Group is not all lowercase", func() { |
| 24 | + instance := &Resource{Group: "Crew", Version: "v1", Kind: "FirstMate"} |
| 25 | + Expect(instance.Validate()).NotTo(Succeed()) |
| 26 | + Expect(instance.Validate().Error()).To(ContainSubstring("group name is invalid: ([a DNS-1123 subdomain must consist of lower case alphanumeric characters")) |
| 27 | + }) |
| 28 | + |
| 29 | + It("should fail if the Group contains non-alpha characters", func() { |
| 30 | + instance := &Resource{Group: "crew1*?", Version: "v1", Kind: "FirstMate"} |
| 31 | + Expect(instance.Validate()).NotTo(Succeed()) |
| 32 | + Expect(instance.Validate().Error()).To(ContainSubstring("group name is invalid: ([a DNS-1123 subdomain must consist of lower case alphanumeric characters")) |
| 33 | + }) |
| 34 | + |
| 35 | + It("should fail if the Version is not specified", func() { |
| 36 | + instance := &Resource{Group: "crew", Kind: "FirstMate"} |
| 37 | + Expect(instance.Validate()).NotTo(Succeed()) |
| 38 | + Expect(instance.Validate().Error()).To(ContainSubstring("version cannot be empty")) |
| 39 | + }) |
| 40 | + |
| 41 | + It("should fail if the Version does not match the version format", func() { |
| 42 | + instance := &Resource{Group: "crew", Version: "1", Kind: "FirstMate"} |
| 43 | + Expect(instance.Validate()).NotTo(Succeed()) |
| 44 | + Expect(instance.Validate().Error()).To(ContainSubstring( |
| 45 | + `version must match ^v\d+(alpha\d+|beta\d+)?$ (was 1)`)) |
| 46 | + |
| 47 | + instance = &Resource{Group: "crew", Version: "1beta1", Kind: "FirstMate"} |
| 48 | + Expect(instance.Validate()).NotTo(Succeed()) |
| 49 | + Expect(instance.Validate().Error()).To(ContainSubstring( |
| 50 | + `version must match ^v\d+(alpha\d+|beta\d+)?$ (was 1beta1)`)) |
| 51 | + |
| 52 | + instance = &Resource{Group: "crew", Version: "a1beta1", Kind: "FirstMate"} |
| 53 | + Expect(instance.Validate()).NotTo(Succeed()) |
| 54 | + Expect(instance.Validate().Error()).To(ContainSubstring( |
| 55 | + `version must match ^v\d+(alpha\d+|beta\d+)?$ (was a1beta1)`)) |
| 56 | + |
| 57 | + instance = &Resource{Group: "crew", Version: "v1beta", Kind: "FirstMate"} |
| 58 | + Expect(instance.Validate()).NotTo(Succeed()) |
| 59 | + Expect(instance.Validate().Error()).To(ContainSubstring( |
| 60 | + `version must match ^v\d+(alpha\d+|beta\d+)?$ (was v1beta)`)) |
| 61 | + |
| 62 | + instance = &Resource{Group: "crew", Version: "v1beta1alpha1", Kind: "FirstMate"} |
| 63 | + Expect(instance.Validate()).NotTo(Succeed()) |
| 64 | + Expect(instance.Validate().Error()).To(ContainSubstring( |
| 65 | + `version must match ^v\d+(alpha\d+|beta\d+)?$ (was v1beta1alpha1)`)) |
| 66 | + }) |
| 67 | + |
| 68 | + It("should fail if the Kind is not specified", func() { |
| 69 | + instance := &Resource{Group: "crew", Version: "v1"} |
| 70 | + Expect(instance.Validate()).NotTo(Succeed()) |
| 71 | + Expect(instance.Validate().Error()).To(ContainSubstring("kind cannot be empty")) |
| 72 | + }) |
| 73 | + |
| 74 | + It("should fail if the Kind is not pascal cased", func() { |
| 75 | + // Base case |
| 76 | + instance := &Resource{Group: "crew", Kind: "FirstMate", Version: "v1"} |
| 77 | + Expect(instance.Validate()).To(Succeed()) |
| 78 | + |
| 79 | + // Can't detect this case :( |
| 80 | + instance = &Resource{Group: "crew", Kind: "Firstmate", Version: "v1"} |
| 81 | + Expect(instance.Validate()).To(Succeed()) |
| 82 | + |
| 83 | + instance = &Resource{Group: "crew", Kind: "firstMate", Version: "v1"} |
| 84 | + Expect(instance.Validate()).NotTo(Succeed()) |
| 85 | + Expect(instance.Validate().Error()).To(ContainSubstring( |
| 86 | + `kind must be PascalCase (expected FirstMate was firstMate)`)) |
| 87 | + |
| 88 | + instance = &Resource{Group: "crew", Kind: "firstmate", Version: "v1"} |
| 89 | + Expect(instance.Validate()).NotTo(Succeed()) |
| 90 | + Expect(instance.Validate().Error()).To(ContainSubstring( |
| 91 | + `kind must be PascalCase (expected Firstmate was firstmate)`)) |
| 92 | + }) |
| 93 | + |
| 94 | + It("should default the Resource by pluralizing the Kind", func() { |
| 95 | + instance := &Resource{Group: "crew", Kind: "FirstMate", Version: "v1"} |
| 96 | + Expect(instance.Validate()).To(Succeed()) |
| 97 | + Expect(instance.Resource).To(Equal("firstmates")) |
| 98 | + |
| 99 | + instance = &Resource{Group: "crew", Kind: "Fish", Version: "v1"} |
| 100 | + Expect(instance.Validate()).To(Succeed()) |
| 101 | + Expect(instance.Resource).To(Equal("fish")) |
| 102 | + |
| 103 | + instance = &Resource{Group: "crew", Kind: "Helmswoman", Version: "v1"} |
| 104 | + Expect(instance.Validate()).To(Succeed()) |
| 105 | + Expect(instance.Resource).To(Equal("helmswomen")) |
| 106 | + }) |
| 107 | + |
| 108 | + It("should allow Cat as a Kind", func() { |
| 109 | + instance := &Resource{Group: "crew", Kind: "Cat", Version: "v1"} |
| 110 | + Expect(instance.Validate()).To(Succeed()) |
| 111 | + Expect(instance.Resource).To(Equal("cats")) |
| 112 | + }) |
| 113 | + |
| 114 | + It("should allow hyphens in group names", func() { |
| 115 | + instance := &Resource{Group: "my-project", Kind: "Cat", Version: "v1"} |
| 116 | + Expect(instance.Validate()).To(Succeed()) |
| 117 | + Expect(instance.GroupImportSafe).To(Equal("myproject")) |
| 118 | + }) |
| 119 | + |
| 120 | + It("should keep the Resource if specified", func() { |
| 121 | + instance := &Resource{Group: "crew", Kind: "FirstMate", Version: "v1", Resource: "myresource"} |
| 122 | + Expect(instance.Validate()).To(Succeed()) |
| 123 | + Expect(instance.Resource).To(Equal("myresource")) |
| 124 | + }) |
| 125 | + }) |
| 126 | +}) |
0 commit comments