|
| 1 | +/* |
| 2 | +Copyright © contributors to CloudNativePG, established as |
| 3 | +CloudNativePG a Series of LF Projects, LLC. |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +
|
| 17 | +SPDX-License-Identifier: Apache-2.0 |
| 18 | +*/ |
| 19 | + |
| 20 | +package scheme |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + |
| 25 | + cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1" |
| 26 | + . "github.com/onsi/ginkgo/v2" |
| 27 | + . "github.com/onsi/gomega" |
| 28 | + "github.com/spf13/viper" |
| 29 | + "k8s.io/apimachinery/pkg/runtime" |
| 30 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 31 | +) |
| 32 | + |
| 33 | +var _ = Describe("AddCNPGToScheme", func() { |
| 34 | + var s *runtime.Scheme |
| 35 | + |
| 36 | + BeforeEach(func() { |
| 37 | + s = runtime.NewScheme() |
| 38 | + }) |
| 39 | + |
| 40 | + AfterEach(func() { |
| 41 | + viper.Reset() |
| 42 | + }) |
| 43 | + |
| 44 | + It("should register CNPG types under the default group and version", func() { |
| 45 | + AddCNPGToScheme(context.Background(), s) |
| 46 | + |
| 47 | + gvks, _, err := s.ObjectKinds(&cnpgv1.Cluster{}) |
| 48 | + Expect(err).NotTo(HaveOccurred()) |
| 49 | + Expect(gvks).To(ContainElement(schema.GroupVersionKind{ |
| 50 | + Group: cnpgv1.SchemeGroupVersion.Group, |
| 51 | + Version: cnpgv1.SchemeGroupVersion.Version, |
| 52 | + Kind: "Cluster", |
| 53 | + })) |
| 54 | + }) |
| 55 | + |
| 56 | + It("should register Backup and ScheduledBackup under the default group", func() { |
| 57 | + AddCNPGToScheme(context.Background(), s) |
| 58 | + |
| 59 | + gvks, _, err := s.ObjectKinds(&cnpgv1.Backup{}) |
| 60 | + Expect(err).NotTo(HaveOccurred()) |
| 61 | + Expect(gvks).To(ContainElement(HaveField("Group", cnpgv1.SchemeGroupVersion.Group))) |
| 62 | + |
| 63 | + gvks, _, err = s.ObjectKinds(&cnpgv1.ScheduledBackup{}) |
| 64 | + Expect(err).NotTo(HaveOccurred()) |
| 65 | + Expect(gvks).To(ContainElement(HaveField("Group", cnpgv1.SchemeGroupVersion.Group))) |
| 66 | + }) |
| 67 | + |
| 68 | + It("should register CNPG types under a custom group", func() { |
| 69 | + viper.Set("custom-cnpg-group", "mycompany.io") |
| 70 | + |
| 71 | + AddCNPGToScheme(context.Background(), s) |
| 72 | + |
| 73 | + gvks, _, err := s.ObjectKinds(&cnpgv1.Cluster{}) |
| 74 | + Expect(err).NotTo(HaveOccurred()) |
| 75 | + Expect(gvks).To(ContainElement(schema.GroupVersionKind{ |
| 76 | + Group: "mycompany.io", |
| 77 | + Version: cnpgv1.SchemeGroupVersion.Version, |
| 78 | + Kind: "Cluster", |
| 79 | + })) |
| 80 | + // The default group must not be registered |
| 81 | + Expect(s.Recognizes(schema.GroupVersionKind{ |
| 82 | + Group: cnpgv1.SchemeGroupVersion.Group, |
| 83 | + Version: cnpgv1.SchemeGroupVersion.Version, |
| 84 | + Kind: "Cluster", |
| 85 | + })).To(BeFalse()) |
| 86 | + }) |
| 87 | + |
| 88 | + It("should register CNPG types under a custom version", func() { |
| 89 | + viper.Set("custom-cnpg-version", "v2") |
| 90 | + |
| 91 | + AddCNPGToScheme(context.Background(), s) |
| 92 | + |
| 93 | + gvks, _, err := s.ObjectKinds(&cnpgv1.Cluster{}) |
| 94 | + Expect(err).NotTo(HaveOccurred()) |
| 95 | + Expect(gvks).To(ContainElement(schema.GroupVersionKind{ |
| 96 | + Group: cnpgv1.SchemeGroupVersion.Group, |
| 97 | + Version: "v2", |
| 98 | + Kind: "Cluster", |
| 99 | + })) |
| 100 | + }) |
| 101 | + |
| 102 | + It("should register CNPG types under both a custom group and custom version", func() { |
| 103 | + viper.Set("custom-cnpg-group", "mycompany.io") |
| 104 | + viper.Set("custom-cnpg-version", "v2") |
| 105 | + |
| 106 | + AddCNPGToScheme(context.Background(), s) |
| 107 | + |
| 108 | + gvks, _, err := s.ObjectKinds(&cnpgv1.Cluster{}) |
| 109 | + Expect(err).NotTo(HaveOccurred()) |
| 110 | + Expect(gvks).To(ContainElement(schema.GroupVersionKind{ |
| 111 | + Group: "mycompany.io", |
| 112 | + Version: "v2", |
| 113 | + Kind: "Cluster", |
| 114 | + })) |
| 115 | + }) |
| 116 | +}) |
0 commit comments