| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package krusty_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"sigs.k8s.io/kustomize/api/konfig" |
| 12 |
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" |
| 13 |
"sigs.k8s.io/kustomize/kyaml/filesys" |
| 14 |
) |
| 15 |
|
| 16 |
// The PrintPluginEnv plugin is a toy plugin that emits |
| 17 |
// its working directory and some environment variables, |
| 18 |
// to add regression protection to plugin loading logic. |
| 19 |
func TestPluginEnvironment(t *testing.T) { |
| 20 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 21 |
PrepExecPlugin( |
| 22 |
"someteam.example.com", "v1", "PrintPluginEnv") |
| 23 |
defer th.Reset() |
| 24 |
|
| 25 |
confirmBehavior( |
| 26 |
kusttest_test.MakeHarnessWithFs(t, filesys.MakeFsInMemory()), |
| 27 |
filesys.Separator) |
| 28 |
|
| 29 |
// On MacOS, $TMPDIR is by default set to /var/folders/…, with /var a |
| 30 |
// symlink to /private/var , which does not match our expectations |
| 31 |
dir, err := filepath.EvalSymlinks(t.TempDir()) |
| 32 |
if err != nil { |
| 33 |
t.Fatalf("err: %v", err) |
| 34 |
} |
| 35 |
|
| 36 |
confirmBehavior( |
| 37 |
kusttest_test.MakeHarnessWithFs(t, filesys.MakeFsOnDisk()), |
| 38 |
dir) |
| 39 |
} |
| 40 |
|
| 41 |
func confirmBehavior(th kusttest_test.Harness, dir string) { |
| 42 |
th.WriteK(dir, ` |
| 43 |
generators: |
| 44 |
- config.yaml |
| 45 |
`) |
| 46 |
th.WriteF(filepath.Join(dir, "config.yaml"), ` |
| 47 |
apiVersion: someteam.example.com/v1 |
| 48 |
kind: PrintPluginEnv |
| 49 |
metadata: |
| 50 |
name: irrelevantHere |
| 51 |
`) |
| 52 |
m := th.Run(dir, th.MakeOptionsPluginsEnabled()) |
| 53 |
|
| 54 |
pHome, ok := os.LookupEnv(konfig.KustomizePluginHomeEnv) |
| 55 |
if !ok { |
| 56 |
th.GetT().Fatalf( |
| 57 |
"expected env var '%s' to be defined", |
| 58 |
konfig.KustomizePluginHomeEnv) |
| 59 |
} |
| 60 |
|
| 61 |
th.AssertActualEqualsExpected(m, ` |
| 62 |
apiVersion: v1 |
| 63 |
env: |
| 64 |
kustomize_plugin_config_root: `+dir+` |
| 65 |
kustomize_plugin_home: `+pHome+` |
| 66 |
pwd: `+dir+` |
| 67 |
kind: GeneratedEnv |
| 68 |
metadata: |
| 69 |
name: hello |
| 70 |
`) |
| 71 |
} |