sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/machinery/injector_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/config"
    24  	cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
    25  	"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
    26  )
    27  
    28  type templateBase struct {
    29  	path           string
    30  	ifExistsAction IfExistsAction
    31  }
    32  
    33  func (t templateBase) GetPath() string {
    34  	return t.path
    35  }
    36  
    37  func (t templateBase) GetIfExistsAction() IfExistsAction {
    38  	return t.ifExistsAction
    39  }
    40  
    41  type templateWithDomain struct {
    42  	templateBase
    43  	domain string
    44  }
    45  
    46  func (t *templateWithDomain) InjectDomain(domain string) {
    47  	t.domain = domain
    48  }
    49  
    50  type templateWithRepository struct {
    51  	templateBase
    52  	repository string
    53  }
    54  
    55  func (t *templateWithRepository) InjectRepository(repository string) {
    56  	t.repository = repository
    57  }
    58  
    59  type templateWithProjectName struct {
    60  	templateBase
    61  	projectName string
    62  }
    63  
    64  func (t *templateWithProjectName) InjectProjectName(projectName string) {
    65  	t.projectName = projectName
    66  }
    67  
    68  type templateWithMultiGroup struct {
    69  	templateBase
    70  	multiGroup bool
    71  }
    72  
    73  func (t *templateWithMultiGroup) InjectMultiGroup(multiGroup bool) {
    74  	t.multiGroup = multiGroup
    75  }
    76  
    77  type templateWithComponentConfig struct {
    78  	templateBase
    79  	componentConfig bool
    80  }
    81  
    82  func (t *templateWithComponentConfig) InjectComponentConfig(componentConfig bool) {
    83  	t.componentConfig = componentConfig
    84  }
    85  
    86  type templateWithBoilerplate struct {
    87  	templateBase
    88  	boilerplate string
    89  }
    90  
    91  func (t *templateWithBoilerplate) InjectBoilerplate(boilerplate string) {
    92  	t.boilerplate = boilerplate
    93  }
    94  
    95  type templateWithResource struct {
    96  	templateBase
    97  	resource *resource.Resource
    98  }
    99  
   100  func (t *templateWithResource) InjectResource(res *resource.Resource) {
   101  	t.resource = res
   102  }
   103  
   104  var _ = Describe("injector", func() {
   105  	tmp := templateBase{
   106  		path:           "my/path/to/file",
   107  		ifExistsAction: Error,
   108  	}
   109  
   110  	Context("injectInto", func() {
   111  		Context("Config", func() {
   112  			var c config.Config
   113  
   114  			BeforeEach(func() {
   115  				c = cfgv3.New()
   116  			})
   117  
   118  			Context("Domain", func() {
   119  				var template *templateWithDomain
   120  
   121  				BeforeEach(func() {
   122  					template = &templateWithDomain{templateBase: tmp}
   123  				})
   124  
   125  				It("should not inject anything if the config is nil", func() {
   126  					injector{}.injectInto(template)
   127  					Expect(template.domain).To(Equal(""))
   128  				})
   129  
   130  				It("should not inject anything if the config doesn't have a domain set", func() {
   131  					injector{config: c}.injectInto(template)
   132  					Expect(template.domain).To(Equal(""))
   133  				})
   134  
   135  				It("should inject if the config has a domain set", func() {
   136  					const domain = "my.domain"
   137  					Expect(c.SetDomain(domain)).To(Succeed())
   138  
   139  					injector{config: c}.injectInto(template)
   140  					Expect(template.domain).To(Equal(domain))
   141  				})
   142  			})
   143  
   144  			Context("Repository", func() {
   145  				var template *templateWithRepository
   146  
   147  				BeforeEach(func() {
   148  					template = &templateWithRepository{templateBase: tmp}
   149  				})
   150  
   151  				It("should not inject anything if the config is nil", func() {
   152  					injector{}.injectInto(template)
   153  					Expect(template.repository).To(Equal(""))
   154  				})
   155  
   156  				It("should not inject anything if the config doesn't have a repository set", func() {
   157  					injector{config: c}.injectInto(template)
   158  					Expect(template.repository).To(Equal(""))
   159  				})
   160  
   161  				It("should inject if the config has a repository set", func() {
   162  					const repo = "test"
   163  					Expect(c.SetRepository(repo)).To(Succeed())
   164  
   165  					injector{config: c}.injectInto(template)
   166  					Expect(template.repository).To(Equal(repo))
   167  				})
   168  			})
   169  
   170  			Context("Project name", func() {
   171  				var template *templateWithProjectName
   172  
   173  				BeforeEach(func() {
   174  					template = &templateWithProjectName{templateBase: tmp}
   175  				})
   176  
   177  				It("should not inject anything if the config is nil", func() {
   178  					injector{}.injectInto(template)
   179  					Expect(template.projectName).To(Equal(""))
   180  				})
   181  
   182  				It("should not inject anything if the config doesn't have a project name set", func() {
   183  					injector{config: c}.injectInto(template)
   184  					Expect(template.projectName).To(Equal(""))
   185  				})
   186  
   187  				It("should inject if the config has a project name set", func() {
   188  					const projectName = "my project"
   189  					Expect(c.SetProjectName(projectName)).To(Succeed())
   190  
   191  					injector{config: c}.injectInto(template)
   192  					Expect(template.projectName).To(Equal(projectName))
   193  				})
   194  			})
   195  
   196  			Context("Multi-group", func() {
   197  				var template *templateWithMultiGroup
   198  
   199  				BeforeEach(func() {
   200  					template = &templateWithMultiGroup{templateBase: tmp}
   201  				})
   202  
   203  				It("should not inject anything if the config is nil", func() {
   204  					injector{}.injectInto(template)
   205  					Expect(template.multiGroup).To(BeFalse())
   206  				})
   207  
   208  				It("should not set the flag if the config doesn't have the multi-group flag set", func() {
   209  					injector{config: c}.injectInto(template)
   210  					Expect(template.multiGroup).To(BeFalse())
   211  				})
   212  
   213  				It("should set the flag if the config has the multi-group flag set", func() {
   214  					Expect(c.SetMultiGroup()).To(Succeed())
   215  
   216  					injector{config: c}.injectInto(template)
   217  					Expect(template.multiGroup).To(BeTrue())
   218  				})
   219  			})
   220  
   221  			Context("Component config", func() {
   222  				var template *templateWithComponentConfig
   223  
   224  				BeforeEach(func() {
   225  					template = &templateWithComponentConfig{templateBase: tmp}
   226  				})
   227  
   228  				It("should not inject anything if the config is nil", func() {
   229  					injector{}.injectInto(template)
   230  					Expect(template.componentConfig).To(BeFalse())
   231  				})
   232  
   233  				It("should not set the flag if the config doesn't have the component config flag set", func() {
   234  					injector{config: c}.injectInto(template)
   235  					Expect(template.componentConfig).To(BeFalse())
   236  				})
   237  
   238  				It("should set the flag if the config has the component config flag set", func() {
   239  					Expect(c.SetComponentConfig()).To(Succeed())
   240  
   241  					injector{config: c}.injectInto(template)
   242  					Expect(template.componentConfig).To(BeTrue())
   243  				})
   244  			})
   245  		})
   246  
   247  		Context("Boilerplate", func() {
   248  			var template *templateWithBoilerplate
   249  
   250  			BeforeEach(func() {
   251  				template = &templateWithBoilerplate{templateBase: tmp}
   252  			})
   253  
   254  			It("should not inject anything if no boilerplate was set", func() {
   255  				injector{}.injectInto(template)
   256  				Expect(template.boilerplate).To(Equal(""))
   257  			})
   258  
   259  			It("should inject if the a boilerplate was set", func() {
   260  				const boilerplate = `Copyright "The Kubernetes Authors"`
   261  
   262  				injector{boilerplate: boilerplate}.injectInto(template)
   263  				Expect(template.boilerplate).To(Equal(boilerplate))
   264  			})
   265  		})
   266  
   267  		Context("Resource", func() {
   268  			var template *templateWithResource
   269  
   270  			BeforeEach(func() {
   271  				template = &templateWithResource{templateBase: tmp}
   272  			})
   273  
   274  			It("should not inject anything if the resource is nil", func() {
   275  				injector{}.injectInto(template)
   276  				Expect(template.resource).To(BeNil())
   277  			})
   278  
   279  			It("should inject if the config has a domain set", func() {
   280  				res := &resource.Resource{
   281  					GVK: resource.GVK{
   282  						Group:   "group",
   283  						Domain:  "my.domain",
   284  						Version: "v1",
   285  						Kind:    "Kind",
   286  					},
   287  				}
   288  
   289  				injector{resource: res}.injectInto(template)
   290  				Expect(template.resource).To(Equal(res))
   291  			})
   292  		})
   293  	})
   294  })