Skip to content

Commit c384984

Browse files
committed
add controller admission test to validate OpenAPI property constraint annotations
Signed-off-by: perdasilva <[email protected]>
1 parent 0c044fa commit c384984

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

api/v1alpha1/operator_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ type OperatorSpec struct {
3131
//+kubebuilder:validation:MaxLength:=64
3232
//+kubebuilder:validation:Pattern:=^(\|\||\s+)?([\s~^><=]*)v?(\d+)(\.(\d+))?(\.(\d+))?(\-(.+))?$
3333
//+kubebuilder:optional
34+
// Version is an optional is semver constraint on the package version. If not specified, the latest version available of the package will be installed.
35+
// If specified, the specific version of the package will be installed so long as it is available in any of the content sources available.
36+
// Examples:
37+
// - 1.2.3
38+
// - 1.0.0-alpha
39+
// - 1.0.0-rc.1
40+
//
41+
// For more information on semver, please see https://semver.org/
3442
Version string `json:"version,omitempty"`
3543
}
3644

config/crd/bases/operators.operatorframework.io_operators.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ spec:
4040
pattern: ^[a-z0-9]+(-[a-z0-9]+)*$
4141
type: string
4242
version:
43+
description: "Version is an optional is semver constraint on the package
44+
version. If not specified, the latest version available of the package
45+
will be installed. If specified, the specific version of the package
46+
will be installed so long as it is available in any of the content
47+
sources available. Examples: - 1.2.3 - 1.0.0-alpha - 1.0.0-rc.1
48+
\n For more information on semver, please see https://semver.org/"
4349
maxLength: 64
4450
pattern: ^(\|\||\s+)?([\s~^><=]*)v?(\d+)(\.(\d+))?(\.(\d+))?(\-(.+))?$
4551
type: string

controllers/admission_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
})

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/operator-framework/deppy v0.0.0-20230125110717-dc02e928470f
1111
github.com/operator-framework/operator-registry v1.26.2
1212
github.com/operator-framework/rukpak v0.11.0
13+
go.uber.org/zap v1.21.0
1314
golang.org/x/net v0.0.0-20220722155237-a158d28d115b
1415
google.golang.org/grpc v1.47.0
1516
k8s.io/api v0.25.0
@@ -70,7 +71,6 @@ require (
7071
github.com/spf13/pflag v1.0.5 // indirect
7172
go.uber.org/atomic v1.7.0 // indirect
7273
go.uber.org/multierr v1.6.0 // indirect
73-
go.uber.org/zap v1.21.0 // indirect
7474
golang.org/x/crypto v0.0.0-20220408190544-5352b0902921 // indirect
7575
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
7676
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect

0 commit comments

Comments
 (0)