-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove ginkgo from internal/controller/validators
Fix #193 Added additional semver tests, and converted to a table. Signed-off-by: Todd Short <[email protected]>
- Loading branch information
Showing
2 changed files
with
84 additions
and
66 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,94 @@ | ||
package validators_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/operator-framework/operator-controller/api/v1alpha1" | ||
"github.com/operator-framework/operator-controller/internal/controllers/validators" | ||
) | ||
|
||
var _ = Describe("Validators", func() { | ||
Describe("ValidateOperatorSpec", func() { | ||
It("should not return an error for valid SemVer", func() { | ||
operator := &v1alpha1.Operator{ | ||
Spec: v1alpha1.OperatorSpec{ | ||
Version: "1.2.3", | ||
}, | ||
} | ||
err := validators.ValidateOperatorSpec(operator) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("should return an error for invalid SemVer", func() { | ||
operator := &v1alpha1.Operator{ | ||
Spec: v1alpha1.OperatorSpec{ | ||
Version: "invalid-semver", | ||
}, | ||
} | ||
err := validators.ValidateOperatorSpec(operator) | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
|
||
It("should not return an error for empty SemVer", func() { | ||
operator := &v1alpha1.Operator{ | ||
Spec: v1alpha1.OperatorSpec{ | ||
Version: "", | ||
}, | ||
} | ||
err := validators.ValidateOperatorSpec(operator) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
type testData struct { | ||
data string | ||
comment string | ||
result bool | ||
} | ||
|
||
It("should not return an error for valid SemVer with pre-release and metadata", func() { | ||
operator := &v1alpha1.Operator{ | ||
Spec: v1alpha1.OperatorSpec{ | ||
Version: "1.2.3-alpha.1+metadata", | ||
}, | ||
} | ||
err := validators.ValidateOperatorSpec(operator) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
var sm = []testData{ | ||
// list of valid semvers | ||
{"1.2.3", "simple semver", true}, | ||
{"", "empty semver", true}, | ||
{"1.2.3-alpha.1+metadata", "semver with pre-release and metadata", true}, | ||
{"1.2.3-alpha-beta", "semver with pre-release", true}, | ||
{">=1.2.3", ">= operator", true}, | ||
{"=>1.2.3", "=> operator", true}, | ||
{">= 1.2.3", ">= operator with space", true}, | ||
{">=v1.2.3", ">= operator with 'v' prefix", true}, | ||
{">= v1.2.3", ">= operator with space and 'v' prefix", true}, | ||
{"<=1.2.3", "<= operator", true}, | ||
{"=<1.2.3", "=< operator", true}, | ||
{"=1.2.3", "= operator", true}, | ||
{"!=1.2.3", "!= operator", true}, | ||
{"<1.2.3", "< operator", true}, | ||
{">1.2.3", "> operator", true}, | ||
{"~1.2.2", "~ operator", true}, | ||
{"~>1.2.3", "~> operator", true}, | ||
{"^1.2.3", "^ operator", true}, | ||
{"v1.2.3", "with 'v' prefix", true}, | ||
{"1.x", "with lower-case y-stream", true}, | ||
{"1.X", "with upper-case Y-stream", true}, | ||
{"1.*", "with asterisk y-stream", true}, | ||
{"1.2.x", "with lower-case z-stream", true}, | ||
{"1.2.X", "with upper-case Z-stream", true}, | ||
{"1.2.*", "with asterisk z-stream", true}, | ||
{">=1.2.3 <2.3.4", "multiple operators space-separated", true}, | ||
{">=1.2.3,<2.3.4", "multiple operators comma-separated", true}, | ||
{">=1.2.3, <2.3.4", "multiple operators comma-and-space-separated", true}, | ||
{"<1.2.3||>2.3.4", "multiple operators OR-separated", true}, | ||
{"<1.2.3|| >2.3.4", "multiple operarors OR-and-space-separated", true}, | ||
{"<1.2.3 ||>2.3.4", "multiple operators space-and-OR-separated", true}, | ||
{"<1.2.3 || >2.3.4", "multiple operators space-and-OR-and-space-separated", true}, | ||
{">1.0.0,<1.2.3 || >2.1.0", "multiple operators with comma and OR separation", true}, | ||
{"<1.2.3-abc >2.3.4-def", "multiple operators with pre-release data", true}, | ||
{"<1.2.3-abc+def >2.3.4-ghi+jkl", "multiple operators with pre-release and metadata", true}, | ||
// list of invalid semvers | ||
{"invalid-semver", "invalid characters", false}, | ||
{"1.2.3.4", "too many components", false}, | ||
{"1.2.3-beta!", "invalid character in pre-release", false}, | ||
{"1.2.3.alpha", "invalid pre-release/4th component", false}, | ||
{"1..2.3", "extra dot", false}, | ||
{"1.2.3-pre+bad_metadata", "invalid metadata", false}, | ||
{"1.2.-3", "negative component", false}, | ||
{".1.2.3", "leading dot", false}, | ||
{"<<1.2.3", "invalid << operator", false}, | ||
{">>1.2.3", "invalid >> operator", false}, | ||
{">~1.2.3", "invalid >~ operator", false}, | ||
{"==1.2.3", "invalid == operator, valid for blang", false}, | ||
{"=!1.2.3", "invalid =! operator", false}, | ||
{"!1.2.3", "invalid ! operator, valid for blang", false}, | ||
{"1.Y", "invalid y-stream wildcard", false}, | ||
{">1.2.3 && <2.3.4", "invalid AND separator", false}, | ||
{">1.2.3;<2.3.4", "invalid semicolon separator", false}, | ||
// Invalid semvers that get past simple validation - THESE NEED TO BE TESTED SEPARATELY | ||
{"1.02.3", "leading zero in y-stream - FALSE POSITIVE", true}, | ||
{"1.2.03", "leading zero in z-stream - FALSE POSITIVE", true}, | ||
{"1.2.3 - 2.3.4", "unsupported hyphen (range) operator - FALSE POSITIVE", true}, | ||
} | ||
|
||
It("should not return an error for valid SemVer with pre-release", func() { | ||
operator := &v1alpha1.Operator{ | ||
Spec: v1alpha1.OperatorSpec{ | ||
Version: "1.2.3-alpha-beta", | ||
}, | ||
} | ||
err := validators.ValidateOperatorSpec(operator) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
}) | ||
}) | ||
func TestValidateOperatorSpecSemVer(t *testing.T) { | ||
for _, d := range sm { | ||
t.Logf("Testing %s: %q", d.comment, d.data) | ||
operator := &v1alpha1.Operator{ | ||
Spec: v1alpha1.OperatorSpec{ | ||
Version: d.data, | ||
}, | ||
} | ||
if d.result { | ||
require.NoError(t, validators.ValidateOperatorSpec(operator)) | ||
} else { | ||
require.EqualError(t, validators.ValidateOperatorSpec(operator), fmt.Sprintf("invalid .spec.version: improper constraint: %s", d.data)) | ||
} | ||
} | ||
} |