...

Source file src/sigs.k8s.io/controller-runtime/pkg/internal/testing/process/bin_path_finder.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  	"path/filepath"
    22  	"regexp"
    23  	"strings"
    24  )
    25  
    26  const (
    27  	// EnvAssetsPath is the environment variable that stores the global test
    28  	// binary location override.
    29  	EnvAssetsPath = "KUBEBUILDER_ASSETS"
    30  	// EnvAssetOverridePrefix is the environment variable prefix for per-binary
    31  	// location overrides.
    32  	EnvAssetOverridePrefix = "TEST_ASSET_"
    33  	// AssetsDefaultPath is the default location to look for test binaries in,
    34  	// if no override was provided.
    35  	AssetsDefaultPath = "/usr/local/kubebuilder/bin"
    36  )
    37  
    38  // BinPathFinder finds the path to the given named binary, using the following locations
    39  // in order of precedence (highest first).  Notice that the various env vars only need
    40  // to be set -- the asset is not checked for existence on the filesystem.
    41  //
    42  // 1. TEST_ASSET_{tr/a-z-/A-Z_/} (if set; asset overrides -- EnvAssetOverridePrefix)
    43  // 1. KUBEBUILDER_ASSETS (if set; global asset path -- EnvAssetsPath)
    44  // 3. assetDirectory (if set; per-config asset directory)
    45  // 4. /usr/local/kubebuilder/bin (AssetsDefaultPath).
    46  func BinPathFinder(symbolicName, assetDirectory string) (binPath string) {
    47  	punctuationPattern := regexp.MustCompile("[^A-Z0-9]+")
    48  	sanitizedName := punctuationPattern.ReplaceAllString(strings.ToUpper(symbolicName), "_")
    49  	leadingNumberPattern := regexp.MustCompile("^[0-9]+")
    50  	sanitizedName = leadingNumberPattern.ReplaceAllString(sanitizedName, "")
    51  	envVar := EnvAssetOverridePrefix + sanitizedName
    52  
    53  	// TEST_ASSET_XYZ
    54  	if val, ok := os.LookupEnv(envVar); ok {
    55  		return val
    56  	}
    57  
    58  	// KUBEBUILDER_ASSETS
    59  	if val, ok := os.LookupEnv(EnvAssetsPath); ok {
    60  		return filepath.Join(val, symbolicName)
    61  	}
    62  
    63  	// assetDirectory
    64  	if assetDirectory != "" {
    65  		return filepath.Join(assetDirectory, symbolicName)
    66  	}
    67  
    68  	// default path
    69  	return filepath.Join(AssetsDefaultPath, symbolicName)
    70  }
    71  

View as plain text