sigs.k8s.io/kubebuilder/v3@v3.14.0/test/e2e/externalplugin/generate_test.go (about)

     1  /*
     2  Copyright 2023 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 externalplugin
    18  
    19  import (
    20  	"path/filepath"
    21  
    22  	pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
    23  
    24  	//nolint:golint
    25  	//nolint:revive
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  
    29  	//nolint:golint
    30  	//nolint:revive
    31  	//nolint:golint
    32  	//nolint:revive
    33  	"sigs.k8s.io/kubebuilder/v3/test/e2e/utils"
    34  )
    35  
    36  var _ = Describe("kubebuilder", func() {
    37  	Context("plugin sampleexternalplugin/v1", func() {
    38  		var (
    39  			kbc *utils.TestContext
    40  		)
    41  
    42  		BeforeEach(func() {
    43  			var err error
    44  			kbc, err = utils.NewTestContext(pluginutil.KubebuilderBinName, "GO111MODULE=on")
    45  			Expect(err).NotTo(HaveOccurred(), "Prepare NewTestContext should return no error.")
    46  			Expect(kbc.Prepare()).To(Succeed())
    47  		})
    48  
    49  		AfterEach(func() {
    50  			kbc.Destroy()
    51  		})
    52  
    53  		It("should generate a runnable project with sample external plugin", func() {
    54  			GenerateProject(kbc)
    55  		})
    56  
    57  	})
    58  })
    59  
    60  // GenerateProject implements a sampleexternalplugin/v1 external plugin project defined by a TestContext.
    61  func GenerateProject(kbc *utils.TestContext) {
    62  	var err error
    63  
    64  	By("initializing a project")
    65  	err = kbc.Init(
    66  		"--plugins", "sampleexternalplugin/v1",
    67  		"--domain", "sample.domain.com",
    68  	)
    69  	ExpectWithOffset(1, err).NotTo(HaveOccurred())
    70  
    71  	var initFileContentsTmpl = "A simple text file created with the `init` subcommand\nDOMAIN: sample.domain.com"
    72  	initFileContainsExpr, err := pluginutil.HasFileContentWith(
    73  		filepath.Join(kbc.Dir, "initFile.txt"), initFileContentsTmpl)
    74  	ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Check initFile.txt should return no error.")
    75  	ExpectWithOffset(1, initFileContainsExpr).To(BeTrue(), "The init file does not contain the expected expression.")
    76  
    77  	By("creating API definition")
    78  	err = kbc.CreateAPI(
    79  		"--plugins", "sampleexternalplugin/v1",
    80  		"--number=2",
    81  		"--group", kbc.Group,
    82  		"--version", kbc.Version,
    83  		"--kind", kbc.Kind,
    84  	)
    85  	ExpectWithOffset(1, err).NotTo(HaveOccurred())
    86  
    87  	var apiFileContentsTmpl = "A simple text file created with the `create api` subcommand\nNUMBER: 2"
    88  	apiFileContainsExpr, err := pluginutil.HasFileContentWith(
    89  		filepath.Join(kbc.Dir, "apiFile.txt"), apiFileContentsTmpl)
    90  	ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Check apiFile.txt should return no error.")
    91  	ExpectWithOffset(1, apiFileContainsExpr).To(BeTrue(), "The api file does not contain the expected expression.")
    92  
    93  	By("scaffolding webhook")
    94  	err = kbc.CreateWebhook(
    95  		"--plugins", "sampleexternalplugin/v1",
    96  		"--hooked",
    97  		"--group", kbc.Group,
    98  		"--version", kbc.Version,
    99  		"--kind", kbc.Kind,
   100  	)
   101  	ExpectWithOffset(1, err).NotTo(HaveOccurred())
   102  
   103  	var webhookFileContentsTmpl = "A simple text file created with the `create webhook` subcommand\nHOOKED!"
   104  	webhookFileContainsExpr, err := pluginutil.HasFileContentWith(
   105  		filepath.Join(kbc.Dir, "webhookFile.txt"), webhookFileContentsTmpl)
   106  	ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Check webhookFile.txt should return no error.")
   107  	ExpectWithOffset(1, webhookFileContainsExpr).To(BeTrue(), "The webhook file does not contain the expected expression.")
   108  }