|
1 | 1 | package validators_test
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - . "github.com/onsi/ginkgo/v2" |
5 |
| - . "github.com/onsi/gomega" |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
6 | 7 |
|
7 | 8 | "github.com/operator-framework/operator-controller/api/v1alpha1"
|
8 | 9 | "github.com/operator-framework/operator-controller/internal/controllers/validators"
|
9 | 10 | )
|
10 | 11 |
|
11 |
| -var _ = Describe("Validators", func() { |
12 |
| - Describe("ValidateOperatorSpec", func() { |
13 |
| - It("should not return an error for valid SemVer", func() { |
14 |
| - operator := &v1alpha1.Operator{ |
15 |
| - Spec: v1alpha1.OperatorSpec{ |
16 |
| - Version: "1.2.3", |
17 |
| - }, |
18 |
| - } |
19 |
| - err := validators.ValidateOperatorSpec(operator) |
20 |
| - Expect(err).NotTo(HaveOccurred()) |
21 |
| - }) |
22 |
| - |
23 |
| - It("should return an error for invalid SemVer", func() { |
24 |
| - operator := &v1alpha1.Operator{ |
25 |
| - Spec: v1alpha1.OperatorSpec{ |
26 |
| - Version: "invalid-semver", |
27 |
| - }, |
28 |
| - } |
29 |
| - err := validators.ValidateOperatorSpec(operator) |
30 |
| - Expect(err).To(HaveOccurred()) |
31 |
| - }) |
| 12 | +var semVers = []struct { |
| 13 | + data string |
| 14 | + comment string |
| 15 | + result bool |
| 16 | +}{ |
| 17 | + // list of valid semvers |
| 18 | + {"1.2.3", "simple semver", true}, |
| 19 | + {"", "empty semver", true}, |
| 20 | + {"1.2.3-alpha.1+metadata", "semver with pre-release and metadata", true}, |
| 21 | + {"1.2.3-alpha-beta", "semver with pre-release", true}, |
| 22 | + {">=1.2.3", ">= operator", true}, |
| 23 | + {"=>1.2.3", "=> operator", true}, |
| 24 | + {">= 1.2.3", ">= operator with space", true}, |
| 25 | + {">=v1.2.3", ">= operator with 'v' prefix", true}, |
| 26 | + {">= v1.2.3", ">= operator with space and 'v' prefix", true}, |
| 27 | + {"<=1.2.3", "<= operator", true}, |
| 28 | + {"=<1.2.3", "=< operator", true}, |
| 29 | + {"=1.2.3", "= operator", true}, |
| 30 | + {"!=1.2.3", "!= operator", true}, |
| 31 | + {"<1.2.3", "< operator", true}, |
| 32 | + {">1.2.3", "> operator", true}, |
| 33 | + {"~1.2.2", "~ operator", true}, |
| 34 | + {"~>1.2.3", "~> operator", true}, |
| 35 | + {"^1.2.3", "^ operator", true}, |
| 36 | + {"v1.2.3", "with 'v' prefix", true}, |
| 37 | + {"1.x", "with lower-case y-stream", true}, |
| 38 | + {"1.X", "with upper-case Y-stream", true}, |
| 39 | + {"1.*", "with asterisk y-stream", true}, |
| 40 | + {"1.2.x", "with lower-case z-stream", true}, |
| 41 | + {"1.2.X", "with upper-case Z-stream", true}, |
| 42 | + {"1.2.*", "with asterisk z-stream", true}, |
| 43 | + {">=1.2.3 <2.3.4", "multiple operators space-separated", true}, |
| 44 | + {">=1.2.3,<2.3.4", "multiple operators comma-separated", true}, |
| 45 | + {">=1.2.3, <2.3.4", "multiple operators comma-and-space-separated", true}, |
| 46 | + {"<1.2.3||>2.3.4", "multiple operators OR-separated", true}, |
| 47 | + {"<1.2.3|| >2.3.4", "multiple operarors OR-and-space-separated", true}, |
| 48 | + {"<1.2.3 ||>2.3.4", "multiple operators space-and-OR-separated", true}, |
| 49 | + {"<1.2.3 || >2.3.4", "multiple operators space-and-OR-and-space-separated", true}, |
| 50 | + {">1.0.0,<1.2.3 || >2.1.0", "multiple operators with comma and OR separation", true}, |
| 51 | + {"<1.2.3-abc >2.3.4-def", "multiple operators with pre-release data", true}, |
| 52 | + {"<1.2.3-abc+def >2.3.4-ghi+jkl", "multiple operators with pre-release and metadata", true}, |
| 53 | + // list of invalid semvers |
| 54 | + {"invalid-semver", "invalid characters", false}, |
| 55 | + {"1.2.3.4", "too many components", false}, |
| 56 | + {"1.2.3-beta!", "invalid character in pre-release", false}, |
| 57 | + {"1.2.3.alpha", "invalid pre-release/4th component", false}, |
| 58 | + {"1..2.3", "extra dot", false}, |
| 59 | + {"1.2.3-pre+bad_metadata", "invalid metadata", false}, |
| 60 | + {"1.2.-3", "negative component", false}, |
| 61 | + {".1.2.3", "leading dot", false}, |
| 62 | + {"<<1.2.3", "invalid << operator", false}, |
| 63 | + {">>1.2.3", "invalid >> operator", false}, |
| 64 | + {">~1.2.3", "invalid >~ operator", false}, |
| 65 | + {"==1.2.3", "invalid == operator, valid for blang", false}, |
| 66 | + {"=!1.2.3", "invalid =! operator", false}, |
| 67 | + {"!1.2.3", "invalid ! operator, valid for blang", false}, |
| 68 | + {"1.Y", "invalid y-stream wildcard", false}, |
| 69 | + {">1.2.3 && <2.3.4", "invalid AND separator", false}, |
| 70 | + {">1.2.3;<2.3.4", "invalid semicolon separator", false}, |
| 71 | + // Invalid semvers that get past simple validation - THESE NEED TO BE TESTED SEPARATELY |
| 72 | + {"1.02.3", "leading zero in y-stream - FALSE POSITIVE", true}, |
| 73 | + {"1.2.03", "leading zero in z-stream - FALSE POSITIVE", true}, |
| 74 | + {"1.2.3 - 2.3.4", "unsupported hyphen (range) operator - FALSE POSITIVE", true}, |
| 75 | +} |
32 | 76 |
|
33 |
| - It("should not return an error for empty SemVer", func() { |
| 77 | +func TestValidateOperatorSpecSemVer(t *testing.T) { |
| 78 | + t.Parallel() |
| 79 | + for _, s := range semVers { |
| 80 | + d := s |
| 81 | + t.Run(d.comment, func(t *testing.T) { |
| 82 | + t.Parallel() |
34 | 83 | operator := &v1alpha1.Operator{
|
35 | 84 | Spec: v1alpha1.OperatorSpec{
|
36 |
| - Version: "", |
| 85 | + Version: d.data, |
37 | 86 | },
|
38 | 87 | }
|
39 |
| - err := validators.ValidateOperatorSpec(operator) |
40 |
| - Expect(err).NotTo(HaveOccurred()) |
41 |
| - }) |
42 |
| - |
43 |
| - It("should not return an error for valid SemVer with pre-release and metadata", func() { |
44 |
| - operator := &v1alpha1.Operator{ |
45 |
| - Spec: v1alpha1.OperatorSpec{ |
46 |
| - Version: "1.2.3-alpha.1+metadata", |
47 |
| - }, |
48 |
| - } |
49 |
| - err := validators.ValidateOperatorSpec(operator) |
50 |
| - Expect(err).NotTo(HaveOccurred()) |
51 |
| - }) |
52 |
| - |
53 |
| - It("should not return an error for valid SemVer with pre-release", func() { |
54 |
| - operator := &v1alpha1.Operator{ |
55 |
| - Spec: v1alpha1.OperatorSpec{ |
56 |
| - Version: "1.2.3-alpha-beta", |
57 |
| - }, |
| 88 | + if d.result { |
| 89 | + require.NoError(t, validators.ValidateOperatorSpec(operator)) |
| 90 | + } else { |
| 91 | + require.Error(t, validators.ValidateOperatorSpec(operator)) |
58 | 92 | }
|
59 |
| - err := validators.ValidateOperatorSpec(operator) |
60 |
| - Expect(err).NotTo(HaveOccurred()) |
61 | 93 | })
|
62 |
| - }) |
63 |
| -}) |
| 94 | + } |
| 95 | +} |
0 commit comments