|
| 1 | +package controllers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + . "github.com/onsi/ginkgo/v2" |
| 7 | + . "github.com/onsi/gomega" |
| 8 | + operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | +) |
| 11 | + |
| 12 | +func operator(spec operatorsv1alpha1.OperatorSpec) *operatorsv1alpha1.Operator { |
| 13 | + return &operatorsv1alpha1.Operator{ |
| 14 | + ObjectMeta: metav1.ObjectMeta{ |
| 15 | + Name: "test-operator", |
| 16 | + }, |
| 17 | + Spec: spec, |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +var _ = Describe("Operator Spec Validations", func() { |
| 22 | + var ( |
| 23 | + ctx context.Context |
| 24 | + cancel context.CancelFunc |
| 25 | + ) |
| 26 | + BeforeEach(func() { |
| 27 | + ctx, cancel = context.WithCancel(context.Background()) |
| 28 | + }) |
| 29 | + AfterEach(func() { |
| 30 | + cancel() |
| 31 | + }) |
| 32 | + It("should fail if the spec is empty", func() { |
| 33 | + err := cl.Create(ctx, operator(operatorsv1alpha1.OperatorSpec{})) |
| 34 | + Expect(err).To(HaveOccurred()) |
| 35 | + Expect(err.Error()).To(ContainSubstring("spec.packageName in body should match '^[a-z0-9]+(-[a-z0-9]+)*$'")) |
| 36 | + }) |
| 37 | + It("should fail if package name length is greater than 48 characters", func() { |
| 38 | + err := cl.Create(ctx, operator(operatorsv1alpha1.OperatorSpec{ |
| 39 | + PackageName: "this-is-a-really-long-package-name-that-is-greater-than-48-characters", |
| 40 | + })) |
| 41 | + Expect(err).To(HaveOccurred()) |
| 42 | + Expect(err.Error()).To(ContainSubstring("Too long: may not be longer than 48")) |
| 43 | + }) |
| 44 | + It("should fail if version is valid semver but length is greater than 64 characters", func() { |
| 45 | + err := cl.Create(ctx, operator(operatorsv1alpha1.OperatorSpec{ |
| 46 | + PackageName: "package", |
| 47 | + Version: "1234567890.1234567890.12345678901234567890123456789012345678901234", |
| 48 | + })) |
| 49 | + Expect(err).To(HaveOccurred()) |
| 50 | + Expect(err.Error()).To(ContainSubstring("Too long: may not be longer than 64")) |
| 51 | + }) |
| 52 | + It("should fail if an invalid semver range is given", func() { |
| 53 | + err := cl.Create(ctx, operator(operatorsv1alpha1.OperatorSpec{ |
| 54 | + PackageName: "package", |
| 55 | + Version: "this-is-not-a-valid-semver", |
| 56 | + })) |
| 57 | + |
| 58 | + Expect(err).To(HaveOccurred()) |
| 59 | + Expect(err.Error()).To(ContainSubstring("spec.version in body should match '^(\\|\\||\\s+)?([\\s~^><=]*)v?(\\d+)(\\.(\\d+))?(\\.(\\d+))?(\\-(.+))?$")) |
| 60 | + }) |
| 61 | +}) |
0 commit comments