...

Source file src/sigs.k8s.io/kustomize/api/testutils/kusttest/plugintestenv.go

Documentation: sigs.k8s.io/kustomize/api/testutils/kusttest

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package kusttest_test
     5  
     6  import (
     7  	"os"
     8  	"testing"
     9  
    10  	"sigs.k8s.io/kustomize/api/internal/plugins/compiler"
    11  	"sigs.k8s.io/kustomize/api/internal/plugins/utils"
    12  	"sigs.k8s.io/kustomize/api/konfig"
    13  	"sigs.k8s.io/kustomize/kyaml/filesys"
    14  )
    15  
    16  // pluginTestEnv manages compiling plugins for tests.
    17  // It manages a Go plugin compiler, and sets/resets shell env vars as needed.
    18  type pluginTestEnv struct {
    19  	t          *testing.T
    20  	compiler   *compiler.Compiler
    21  	pluginRoot string
    22  	oldXdg     string
    23  	wasSet     bool
    24  }
    25  
    26  // newPluginTestEnv returns a new instance of pluginTestEnv.
    27  func newPluginTestEnv(t *testing.T) *pluginTestEnv {
    28  	t.Helper()
    29  	return &pluginTestEnv{t: t}
    30  }
    31  
    32  // set creates a test environment.
    33  // Uses a filesystem on disk for compilation (or copying) of
    34  // plugin code - this FileSystem has nothing to do with
    35  // the FileSystem used for loading config yaml in the tests.
    36  func (x *pluginTestEnv) set() *pluginTestEnv {
    37  	var err error
    38  	x.pluginRoot, err = utils.DeterminePluginSrcRoot(filesys.MakeFsOnDisk())
    39  	if err != nil {
    40  		x.t.Error(err)
    41  	}
    42  	x.compiler = compiler.NewCompiler(x.pluginRoot)
    43  	x.setEnv()
    44  	return x
    45  }
    46  
    47  // reset restores the environment to pre-test state.
    48  func (x *pluginTestEnv) reset() {
    49  	// Calling Cleanup forces recompilation in a test file with multiple
    50  	// calls to MakeEnhancedHarness - so leaving it out.  Your .gitignore
    51  	// should ignore .so files anyway.
    52  	// x.compiler.Cleanup()
    53  	x.resetEnv()
    54  }
    55  
    56  // prepareGoPlugin compiles a Go plugin, leaving the newly
    57  // created object code alongside the src code.
    58  func (x *pluginTestEnv) prepareGoPlugin(g, v, k string) {
    59  	x.compiler.SetGVK(g, v, k)
    60  	err := x.compiler.Compile()
    61  	if err != nil {
    62  		x.t.Errorf("compile failed: %v", err)
    63  	}
    64  }
    65  
    66  func (x *pluginTestEnv) prepareExecPlugin(_, _, _ string) {
    67  	// Do nothing.  At one point this method
    68  	// copied the exec plugin directory to a temp dir
    69  	// and ran it from there.  Left as a hook.
    70  }
    71  
    72  func (x *pluginTestEnv) setEnv() {
    73  	x.oldXdg, x.wasSet = os.LookupEnv(konfig.KustomizePluginHomeEnv)
    74  	os.Setenv(konfig.KustomizePluginHomeEnv, x.pluginRoot)
    75  }
    76  
    77  func (x *pluginTestEnv) resetEnv() {
    78  	if x.wasSet {
    79  		os.Setenv(konfig.KustomizePluginHomeEnv, x.oldXdg)
    80  	} else {
    81  		os.Unsetenv(konfig.KustomizePluginHomeEnv)
    82  	}
    83  }
    84  

View as plain text