sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/cli/resource_test.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cli
    18  
    19  import (
    20  	. "github.com/onsi/ginkgo/v2"
    21  	. "github.com/onsi/gomega"
    22  
    23  	"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
    24  )
    25  
    26  var _ = Describe("resourceOptions", func() {
    27  	const (
    28  		group   = "crew"
    29  		domain  = "test.io"
    30  		version = "v1"
    31  		kind    = "FirstMate"
    32  	)
    33  
    34  	var (
    35  		fullGVK = resource.GVK{
    36  			Group:   group,
    37  			Domain:  domain,
    38  			Version: version,
    39  			Kind:    kind,
    40  		}
    41  		noDomainGVK = resource.GVK{
    42  			Group:   group,
    43  			Version: version,
    44  			Kind:    kind,
    45  		}
    46  		noGroupGVK = resource.GVK{
    47  			Domain:  domain,
    48  			Version: version,
    49  			Kind:    kind,
    50  		}
    51  	)
    52  
    53  	Context("validate", func() {
    54  		DescribeTable("should succeed for valid options",
    55  			func(options resourceOptions) { Expect(options.validate()).To(Succeed()) },
    56  			Entry("full GVK", resourceOptions{GVK: fullGVK}),
    57  			Entry("missing domain", resourceOptions{GVK: noDomainGVK}),
    58  			Entry("missing group", resourceOptions{GVK: noGroupGVK}),
    59  		)
    60  
    61  		DescribeTable("should fail for invalid options",
    62  			func(options resourceOptions) { Expect(options.validate()).NotTo(Succeed()) },
    63  			Entry("group flag captured another flag", resourceOptions{GVK: resource.GVK{Group: "--version"}}),
    64  			Entry("version flag captured another flag", resourceOptions{GVK: resource.GVK{Version: "--kind"}}),
    65  			Entry("kind flag captured another flag", resourceOptions{GVK: resource.GVK{Kind: "--group"}}),
    66  		)
    67  	})
    68  
    69  	Context("newResource", func() {
    70  		DescribeTable("should succeed if the Resource is valid",
    71  			func(options resourceOptions) {
    72  				Expect(options.validate()).To(Succeed())
    73  
    74  				resource := options.newResource()
    75  				Expect(resource.Validate()).To(Succeed())
    76  				Expect(resource.GVK.IsEqualTo(options.GVK)).To(BeTrue())
    77  				// Plural is checked in the next test
    78  				Expect(resource.Path).To(Equal(""))
    79  				Expect(resource.API).NotTo(BeNil())
    80  				Expect(resource.API.CRDVersion).To(Equal(""))
    81  				Expect(resource.API.Namespaced).To(BeFalse())
    82  				Expect(resource.Controller).To(BeFalse())
    83  				Expect(resource.Webhooks).NotTo(BeNil())
    84  				Expect(resource.Webhooks.WebhookVersion).To(Equal(""))
    85  				Expect(resource.Webhooks.Defaulting).To(BeFalse())
    86  				Expect(resource.Webhooks.Validation).To(BeFalse())
    87  				Expect(resource.Webhooks.Conversion).To(BeFalse())
    88  			},
    89  			Entry("full GVK", resourceOptions{GVK: fullGVK}),
    90  			Entry("missing domain", resourceOptions{GVK: noDomainGVK}),
    91  			Entry("missing group", resourceOptions{GVK: noGroupGVK}),
    92  		)
    93  
    94  		DescribeTable("should default the Plural by pluralizing the Kind",
    95  			func(kind, plural string) {
    96  				options := resourceOptions{GVK: resource.GVK{Group: group, Version: version, Kind: kind}}
    97  				Expect(options.validate()).To(Succeed())
    98  
    99  				resource := options.newResource()
   100  				Expect(resource.Validate()).To(Succeed())
   101  				Expect(resource.GVK.IsEqualTo(options.GVK)).To(BeTrue())
   102  				Expect(resource.Plural).To(Equal(plural))
   103  			},
   104  			Entry("for `FirstMate`", "FirstMate", "firstmates"),
   105  			Entry("for `Fish`", "Fish", "fish"),
   106  			Entry("for `Helmswoman`", "Helmswoman", "helmswomen"),
   107  		)
   108  	})
   109  })