-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make more use of api-server validation
Signed-off-by: Erik Godding Boye <[email protected]>
- Loading branch information
Showing
7 changed files
with
120 additions
and
53 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Copyright 2021 The cert-manager Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package test | ||
|
||
import ( | ||
"context" | ||
"k8s.io/utils/ptr" | ||
|
||
"k8s.io/klog/v2/ktesting" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
trustapi "github.com/cert-manager/trust-manager/pkg/apis/trust/v1alpha1" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Bundle Validation", func() { | ||
var ( | ||
ctx context.Context | ||
cl client.Client | ||
bundle *trustapi.Bundle | ||
) | ||
BeforeEach(func() { | ||
_, ctx = ktesting.NewTestContext(GinkgoT()) | ||
|
||
var err error | ||
cl, err = client.New(env.Config, client.Options{ | ||
Scheme: trustapi.GlobalScheme, | ||
}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
bundle = &trustapi.Bundle{} | ||
bundle.GenerateName = "validation-" | ||
bundle.Spec.Sources = []trustapi.BundleSource{{ | ||
UseDefaultCAs: ptr.To(true), | ||
}} | ||
}) | ||
|
||
Context("Sources", func() { | ||
It("should require at least one source", func() { | ||
bundle.Spec.Sources = make([]trustapi.BundleSource, 0) | ||
|
||
expectedErr := "spec.sources: Invalid value: 0: spec.sources in body should have at least 1 items" | ||
Expect(cl.Create(ctx, bundle)).Should(MatchError(ContainSubstring(expectedErr))) | ||
}) | ||
}) | ||
|
||
Context("Target", func() { | ||
DescribeTable("should require at least one target configMap/secret", | ||
func(target trustapi.BundleTarget, wantErr bool) { | ||
bundle.Spec.Target = target | ||
if wantErr { | ||
expectedErr := "spec.target: Invalid value: \"object\": must define at least one target configMap/secret" | ||
Expect(cl.Create(ctx, bundle)).Should(MatchError(ContainSubstring(expectedErr))) | ||
} else { | ||
Expect(cl.Create(ctx, bundle)).To(Succeed()) | ||
} | ||
}, | ||
Entry("when none set", trustapi.BundleTarget{NamespaceSelector: &trustapi.NamespaceSelector{}}, true), | ||
Entry("when configMap set", trustapi.BundleTarget{ConfigMap: &trustapi.KeySelector{Key: "ca-bundle.crt"}}, false), | ||
Entry("when secret set", trustapi.BundleTarget{Secret: &trustapi.KeySelector{Key: "ca-bundle.crt"}}, false), | ||
Entry("when both set", trustapi.BundleTarget{ConfigMap: &trustapi.KeySelector{Key: "ca-bundle.crt"}, Secret: &trustapi.KeySelector{Key: "ca-bundle.crt"}}, false), | ||
) | ||
|
||
DescribeTable("should require target key", | ||
func(target trustapi.BundleTarget, expErr string) { | ||
bundle.Spec.Target = target | ||
Expect(cl.Create(ctx, bundle)).Should(MatchError(ContainSubstring(expErr))) | ||
}, | ||
Entry("for configMap", trustapi.BundleTarget{ConfigMap: &trustapi.KeySelector{}}, "spec.target.configMap.key: Invalid value: \"\": spec.target.configMap.key in body should be at least 1 chars long"), | ||
Entry("for secret", trustapi.BundleTarget{Secret: &trustapi.KeySelector{}}, "spec.target.secret.key: Invalid value: \"\": spec.target.secret.key in body should be at least 1 chars long"), | ||
) | ||
}) | ||
}) |