sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/machinery/mixins_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 machinery
    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  type mockTemplate struct {
    27  	TemplateMixin
    28  	DomainMixin
    29  	RepositoryMixin
    30  	ProjectNameMixin
    31  	MultiGroupMixin
    32  	ComponentConfigMixin
    33  	BoilerplateMixin
    34  	ResourceMixin
    35  }
    36  
    37  type mockInserter struct {
    38  	// InserterMixin requires a different type because it collides with TemplateMixin
    39  	InserterMixin
    40  }
    41  
    42  var _ = Describe("TemplateMixin", func() {
    43  	const (
    44  		path           = "path/to/file.go"
    45  		ifExistsAction = SkipFile
    46  		body           = "content"
    47  	)
    48  
    49  	tmp := mockTemplate{
    50  		TemplateMixin: TemplateMixin{
    51  			PathMixin:           PathMixin{path},
    52  			IfExistsActionMixin: IfExistsActionMixin{ifExistsAction},
    53  			TemplateBody:        body,
    54  		},
    55  	}
    56  
    57  	Context("GetPath", func() {
    58  		It("should return the path", func() {
    59  			Expect(tmp.GetPath()).To(Equal(path))
    60  		})
    61  	})
    62  
    63  	Context("GetIfExistsAction", func() {
    64  		It("should return the if-exists action", func() {
    65  			Expect(tmp.GetIfExistsAction()).To(Equal(ifExistsAction))
    66  		})
    67  	})
    68  
    69  	Context("GetBody", func() {
    70  		It("should return the body", func() {
    71  			Expect(tmp.GetBody()).To(Equal(body))
    72  		})
    73  	})
    74  })
    75  
    76  var _ = Describe("InserterMixin", func() {
    77  	const path = "path/to/file.go"
    78  
    79  	tmp := mockInserter{
    80  		InserterMixin: InserterMixin{
    81  			PathMixin: PathMixin{path},
    82  		},
    83  	}
    84  
    85  	Context("GetPath", func() {
    86  		It("should return the path", func() {
    87  			Expect(tmp.GetPath()).To(Equal(path))
    88  		})
    89  	})
    90  
    91  	Context("GetIfExistsAction", func() {
    92  		It("should return overwrite file always", func() {
    93  			Expect(tmp.GetIfExistsAction()).To(Equal(OverwriteFile))
    94  		})
    95  	})
    96  })
    97  
    98  var _ = Describe("DomainMixin", func() {
    99  	const domain = "my.domain"
   100  
   101  	tmp := mockTemplate{}
   102  
   103  	Context("InjectDomain", func() {
   104  		It("should inject the provided domain", func() {
   105  			tmp.InjectDomain(domain)
   106  			Expect(tmp.Domain).To(Equal(domain))
   107  		})
   108  	})
   109  })
   110  
   111  var _ = Describe("RepositoryMixin", func() {
   112  	const repo = "test"
   113  
   114  	tmp := mockTemplate{}
   115  
   116  	Context("InjectRepository", func() {
   117  		It("should inject the provided repository", func() {
   118  			tmp.InjectRepository(repo)
   119  			Expect(tmp.Repo).To(Equal(repo))
   120  		})
   121  	})
   122  })
   123  
   124  var _ = Describe("ProjectNameMixin", func() {
   125  	const name = "my project"
   126  
   127  	tmp := mockTemplate{}
   128  
   129  	Context("InjectProjectName", func() {
   130  		It("should inject the provided project name", func() {
   131  			tmp.InjectProjectName(name)
   132  			Expect(tmp.ProjectName).To(Equal(name))
   133  		})
   134  	})
   135  })
   136  
   137  var _ = Describe("MultiGroupMixin", func() {
   138  	tmp := mockTemplate{}
   139  
   140  	Context("InjectMultiGroup", func() {
   141  		It("should inject the provided multi group flag", func() {
   142  			tmp.InjectMultiGroup(true)
   143  			Expect(tmp.MultiGroup).To(BeTrue())
   144  		})
   145  	})
   146  })
   147  
   148  var _ = Describe("ComponentConfigMixin", func() {
   149  	tmp := mockTemplate{}
   150  
   151  	Context("InjectComponentConfig", func() {
   152  		It("should inject the provided component config flag", func() {
   153  			tmp.InjectComponentConfig(true)
   154  			Expect(tmp.ComponentConfig).To(BeTrue())
   155  		})
   156  	})
   157  })
   158  
   159  var _ = Describe("BoilerplateMixin", func() {
   160  	const boilerplate = "Copyright"
   161  
   162  	tmp := mockTemplate{}
   163  
   164  	Context("InjectBoilerplate", func() {
   165  		It("should inject the provided boilerplate", func() {
   166  			tmp.InjectBoilerplate(boilerplate)
   167  			Expect(tmp.Boilerplate).To(Equal(boilerplate))
   168  		})
   169  	})
   170  })
   171  
   172  var _ = Describe("ResourceMixin", func() {
   173  	res := &resource.Resource{GVK: resource.GVK{
   174  		Group:   "group",
   175  		Domain:  "my.domain",
   176  		Version: "v1",
   177  		Kind:    "Kind",
   178  	}}
   179  
   180  	tmp := mockTemplate{}
   181  
   182  	Context("InjectResource", func() {
   183  		It("should inject the provided resource", func() {
   184  			tmp.InjectResource(res)
   185  			Expect(tmp.Resource.GVK.IsEqualTo(res.GVK)).To(BeTrue())
   186  		})
   187  	})
   188  })