|
| 1 | +// Copyright (C) 2015 The Gravitee team (http://gravitee.io) |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package subscription |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/gravitee-io/gravitee-kubernetes-operator/api/model/refs" |
| 19 | + "github.com/gravitee-io/gravitee-kubernetes-operator/api/v1alpha1" |
| 20 | + "github.com/gravitee-io/gravitee-kubernetes-operator/internal/admission/subscription" |
| 21 | + . "github.com/onsi/ginkgo/v2" |
| 22 | + . "github.com/onsi/gomega" |
| 23 | +) |
| 24 | + |
| 25 | +func contextRef(namespace, name string) *refs.NamespacedName { |
| 26 | + return &refs.NamespacedName{Namespace: namespace, Name: name} |
| 27 | +} |
| 28 | + |
| 29 | +// A contextRef is optional on API definitions, so it reaches validation as an interface |
| 30 | +// wrapping a nil pointer. Every combination must yield a verdict rather than a panic. |
| 31 | +var _ = Describe("Validating context refs", func() { |
| 32 | + DescribeTable("subscribing an application to an API", |
| 33 | + func(apiContext, appContext *refs.NamespacedName, expectRejection bool) { |
| 34 | + api := &v1alpha1.ApiV4Definition{Spec: v1alpha1.ApiV4DefinitionSpec{Context: apiContext}} |
| 35 | + app := &v1alpha1.Application{Spec: v1alpha1.ApplicationSpec{Context: appContext}} |
| 36 | + |
| 37 | + err := subscription.ValidateContextRefs(api, app) |
| 38 | + |
| 39 | + if expectRejection { |
| 40 | + Expect(err).ToNot(BeNil()) |
| 41 | + return |
| 42 | + } |
| 43 | + Expect(err).To(BeNil()) |
| 44 | + }, |
| 45 | + Entry("is accepted when both reference the same context", |
| 46 | + contextRef("default", "dev-ctx"), contextRef("default", "dev-ctx"), false), |
| 47 | + Entry("is rejected when the context names differ", |
| 48 | + contextRef("default", "prod-ctx"), contextRef("default", "dev-ctx"), true), |
| 49 | + Entry("is rejected when the context namespaces differ", |
| 50 | + contextRef("other", "dev-ctx"), contextRef("default", "dev-ctx"), true), |
| 51 | + Entry("is rejected when the API has no context", |
| 52 | + nil, contextRef("default", "dev-ctx"), true), |
| 53 | + Entry("is rejected when the application has no context", |
| 54 | + contextRef("default", "dev-ctx"), nil, true), |
| 55 | + Entry("is rejected when neither has a context", |
| 56 | + nil, nil, true), |
| 57 | + ) |
| 58 | +}) |
0 commit comments