|
| 1 | +// Copyright 2021 The Operator-SDK Authors |
| 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 v1 |
| 16 | + |
| 17 | +import ( |
| 18 | + . "github.com/onsi/ginkgo" |
| 19 | + . "github.com/onsi/gomega" |
| 20 | + "github.com/spf13/pflag" |
| 21 | + "sigs.k8s.io/kubebuilder/v3/pkg/config" |
| 22 | + "sigs.k8s.io/kubebuilder/v3/pkg/machinery" |
| 23 | + "sigs.k8s.io/kubebuilder/v3/pkg/model/resource" |
| 24 | +) |
| 25 | + |
| 26 | +var _ = Describe("v1", func() { |
| 27 | + var ( |
| 28 | + testAPISubcommand createAPISubcommand |
| 29 | + ) |
| 30 | + |
| 31 | + BeforeEach(func() { |
| 32 | + testAPISubcommand = createAPISubcommand{} |
| 33 | + }) |
| 34 | + |
| 35 | + Describe("UpdateResource", func() { |
| 36 | + It("verify that resource fields were set", func() { |
| 37 | + testAPIOptions := &createAPIOptions{ |
| 38 | + CRDVersion: "testVersion", |
| 39 | + Namespaced: true, |
| 40 | + } |
| 41 | + updateTestResource := resource.Resource{} |
| 42 | + testAPIOptions.UpdateResource(&updateTestResource) |
| 43 | + Expect(updateTestResource.API.CRDVersion).To(Equal(testAPIOptions.CRDVersion)) |
| 44 | + Expect(updateTestResource.API.Namespaced).To(Equal(testAPIOptions.Namespaced)) |
| 45 | + Expect(updateTestResource.Path).To(Equal("")) |
| 46 | + Expect(updateTestResource.Controller).To(BeFalse()) |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + Describe("BindFlags", func() { |
| 51 | + It("should set SortFlags to false", func() { |
| 52 | + flagTest := pflag.NewFlagSet("testFlag", -1) |
| 53 | + testAPISubcommand.BindFlags(flagTest) |
| 54 | + Expect(flagTest.SortFlags).To(BeFalse()) |
| 55 | + Expect(testAPISubcommand.options.CRDVersion).To(Equal("v1")) |
| 56 | + Expect(testAPISubcommand.options.Namespaced).To(BeTrue()) |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + Describe("InjectConfig", func() { |
| 61 | + It("should set config", func() { |
| 62 | + testConfig, _ := config.New(config.Version{Number: 3}) |
| 63 | + err := testAPISubcommand.InjectConfig(testConfig) |
| 64 | + Expect(testAPISubcommand.config).To(Equal(testConfig)) |
| 65 | + Expect(err).To(BeNil()) |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + Describe("Run", func() { |
| 70 | + It("should return nil", func() { |
| 71 | + Expect(testAPISubcommand.Run(machinery.Filesystem{})).To(BeNil()) |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + Describe("Validate", func() { |
| 76 | + It("should return nil", func() { |
| 77 | + Expect(testAPISubcommand.Validate()).To(BeNil()) |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + Describe("PostScaffold", func() { |
| 82 | + It("should return nil", func() { |
| 83 | + Expect(testAPISubcommand.PostScaffold()).To(BeNil()) |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + Describe("InjectResource", func() { |
| 88 | + It("verify that wrong GVKs fail", func() { |
| 89 | + failAPISubcommand := &createAPISubcommand{} |
| 90 | + failResource := resource.Resource{ |
| 91 | + GVK: resource.GVK{ |
| 92 | + Group: "Fail-Test-Group", |
| 93 | + Version: "test-version", |
| 94 | + Kind: "test-kind", |
| 95 | + }, |
| 96 | + Plural: "test-plural", |
| 97 | + } |
| 98 | + |
| 99 | + groupErr := failAPISubcommand.InjectResource(&failResource) |
| 100 | + Expect(failAPISubcommand.resource, failResource) |
| 101 | + Expect(groupErr).To(HaveOccurred()) |
| 102 | + |
| 103 | + failResource.GVK.Group = "test-group" |
| 104 | + versionErr := failAPISubcommand.InjectResource(&failResource) |
| 105 | + Expect(versionErr).To(HaveOccurred()) |
| 106 | + |
| 107 | + failResource.GVK.Version = "v1" |
| 108 | + kindError := failAPISubcommand.InjectResource(&failResource) |
| 109 | + Expect(kindError).To(HaveOccurred()) |
| 110 | + }) |
| 111 | + |
| 112 | + It("verify that a correct GVK succeeds", func() { |
| 113 | + testResource := resource.Resource{ |
| 114 | + GVK: resource.GVK{ |
| 115 | + Group: "test-group", |
| 116 | + Version: "v1", |
| 117 | + Kind: "Test-Kind", |
| 118 | + }, |
| 119 | + Plural: "test-plural", |
| 120 | + } |
| 121 | + |
| 122 | + testConfig, _ := config.New(config.Version{Number: 3}) |
| 123 | + testAPISubcommand.InjectConfig(testConfig) |
| 124 | + noErr := testAPISubcommand.InjectResource(&testResource) |
| 125 | + Expect(testAPISubcommand.resource, testResource) |
| 126 | + Expect(noErr).To(BeNil()) |
| 127 | + }) |
| 128 | + }) |
| 129 | +}) |
0 commit comments