...

Source file src/sigs.k8s.io/controller-runtime/pkg/internal/testing/process/bin_path_finder_test.go

Documentation: sigs.k8s.io/controller-runtime/pkg/internal/testing/process

     1  /*
     2  Copyright 2021 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 process
    18  
    19  import (
    20  	"os"
    21  
    22  	. "github.com/onsi/ginkgo/v2"
    23  	. "github.com/onsi/gomega"
    24  )
    25  
    26  var _ = Describe("BinPathFinder", func() {
    27  	var prevAssetPath string
    28  	BeforeEach(func() {
    29  		prevAssetPath = os.Getenv(EnvAssetsPath)
    30  		Expect(os.Unsetenv(EnvAssetsPath)).To(Succeed())
    31  		Expect(os.Unsetenv(EnvAssetOverridePrefix + "_SOME_FAKE")).To(Succeed())
    32  		Expect(os.Unsetenv(EnvAssetOverridePrefix + "OTHERFAKE")).To(Succeed())
    33  	})
    34  	AfterEach(func() {
    35  		if prevAssetPath != "" {
    36  			Expect(os.Setenv(EnvAssetsPath, prevAssetPath)).To(Succeed())
    37  		}
    38  	})
    39  	Context("when individual overrides are present", func() {
    40  		BeforeEach(func() {
    41  			Expect(os.Setenv(EnvAssetOverridePrefix+"OTHERFAKE", "/other/path")).To(Succeed())
    42  			Expect(os.Setenv(EnvAssetOverridePrefix+"_SOME_FAKE", "/some/path")).To(Succeed())
    43  			// set the global path to make sure we don't prefer it
    44  			Expect(os.Setenv(EnvAssetsPath, "/global/path")).To(Succeed())
    45  		})
    46  
    47  		It("should prefer individual overrides, using them unmodified", func() {
    48  			Expect(BinPathFinder("otherfake", "/hardcoded/path")).To(Equal("/other/path"))
    49  		})
    50  
    51  		It("should convert lowercase to uppercase, remove leading numbers, and replace punctuation with underscores when resolving the env var name", func() {
    52  			Expect(BinPathFinder("123.some-fake", "/hardcoded/path")).To(Equal("/some/path"))
    53  		})
    54  	})
    55  
    56  	Context("when individual overrides are missing but the global override is present", func() {
    57  		BeforeEach(func() {
    58  			Expect(os.Setenv(EnvAssetsPath, "/global/path")).To(Succeed())
    59  		})
    60  		It("should prefer the global override, appending the name to that path", func() {
    61  			Expect(BinPathFinder("some-fake", "/hardcoded/path")).To(Equal("/global/path/some-fake"))
    62  		})
    63  	})
    64  
    65  	Context("when an asset directory is given and no overrides are present", func() {
    66  		It("should use the asset directory, appending the name to that path", func() {
    67  			Expect(BinPathFinder("some-fake", "/hardcoded/path")).To(Equal("/hardcoded/path/some-fake"))
    68  		})
    69  	})
    70  
    71  	Context("when no path configuration is given", func() {
    72  		It("should just use the default path", func() {
    73  			Expect(BinPathFinder("some-fake", "")).To(Equal("/usr/local/kubebuilder/bin/some-fake"))
    74  		})
    75  	})
    76  })
    77  

View as plain text