| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package execplugin_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"path/filepath" |
| 8 |
"runtime" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
fLdr "sigs.k8s.io/kustomize/api/internal/loader" |
| 14 |
. "sigs.k8s.io/kustomize/api/internal/plugins/execplugin" |
| 15 |
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader" |
| 16 |
"sigs.k8s.io/kustomize/api/internal/plugins/utils" |
| 17 |
"sigs.k8s.io/kustomize/api/provider" |
| 18 |
"sigs.k8s.io/kustomize/api/resmap" |
| 19 |
"sigs.k8s.io/kustomize/api/types" |
| 20 |
"sigs.k8s.io/kustomize/kyaml/filesys" |
| 21 |
) |
| 22 |
|
| 23 |
const ( |
| 24 |
expectedLargeConfigMap = `{"apiVersion":"v1","data":{"password":"password","username":"user"},"kind":"ConfigMap",` + |
| 25 |
`"metadata":{"annotations":{"internal.config.kubernetes.io/generatorBehavior":"unspecified",` + |
| 26 |
`"internal.config.kubernetes.io/needsHashSuffix":"enabled"},"name":"example-configmap-test"}}` |
| 27 |
) |
| 28 |
|
| 29 |
func TestExecPluginConfig(t *testing.T) { |
| 30 |
fSys := filesys.MakeFsInMemory() |
| 31 |
err := fSys.WriteFile("sed-input.txt", []byte(` |
| 32 |
s/$FOO/foo/g |
| 33 |
s/$BAR/bar baz/g |
| 34 |
\ \ \ |
| 35 |
`)) |
| 36 |
require.NoError(t, err) |
| 37 |
ldr, err := fLdr.NewLoader( |
| 38 |
fLdr.RestrictionRootOnly, filesys.Separator, fSys) |
| 39 |
if err != nil { |
| 40 |
t.Fatal(err) |
| 41 |
} |
| 42 |
pvd := provider.NewDefaultDepProvider() |
| 43 |
rf := resmap.NewFactory(pvd.GetResourceFactory()) |
| 44 |
pluginConfig, err := rf.RF().FromMap( |
| 45 |
map[string]interface{}{ |
| 46 |
"apiVersion": "someteam.example.com/v1", |
| 47 |
"kind": "SedTransformer", |
| 48 |
"metadata": map[string]interface{}{ |
| 49 |
"name": "some-random-name", |
| 50 |
}, |
| 51 |
"argsOneLiner": "one two 'foo bar'", |
| 52 |
"argsFromFile": "sed-input.txt", |
| 53 |
}) |
| 54 |
if err != nil { |
| 55 |
t.Fatalf("failed to writes the data to a file: %v", err) |
| 56 |
} |
| 57 |
|
| 58 |
pluginConfig.RemoveBuildAnnotations() |
| 59 |
pc := types.DisabledPluginConfig() |
| 60 |
loader := pLdr.NewLoader(pc, rf, fSys) |
| 61 |
pluginPath, err := loader.AbsolutePluginPath(pluginConfig.OrgId()) |
| 62 |
if err != nil { |
| 63 |
t.Fatalf("unexpected err: %v", err) |
| 64 |
} |
| 65 |
p := NewExecPlugin(pluginPath) |
| 66 |
// Not checking to see if the plugin is executable, |
| 67 |
// because this test does not run it. |
| 68 |
// This tests only covers sending configuration |
| 69 |
// to the plugin wrapper object and confirming |
| 70 |
// that it's properly prepared for execution. |
| 71 |
|
| 72 |
yaml, err := pluginConfig.AsYAML() |
| 73 |
if err != nil { |
| 74 |
t.Fatalf("unexpected err: %v", err) |
| 75 |
} |
| 76 |
err = p.Config( |
| 77 |
resmap.NewPluginHelpers(ldr, pvd.GetFieldValidator(), rf, pc), |
| 78 |
yaml) |
| 79 |
require.NoError(t, err) |
| 80 |
|
| 81 |
expected := "someteam.example.com/v1/sedtransformer/SedTransformer" |
| 82 |
if !strings.HasSuffix(p.Path(), expected) { |
| 83 |
t.Fatalf("expected suffix '%s', got '%s'", expected, p.Path()) |
| 84 |
} |
| 85 |
|
| 86 |
expected = `apiVersion: someteam.example.com/v1 |
| 87 |
argsFromFile: sed-input.txt |
| 88 |
argsOneLiner: one two 'foo bar' |
| 89 |
kind: SedTransformer |
| 90 |
metadata: |
| 91 |
name: some-random-name |
| 92 |
` |
| 93 |
if expected != string(p.Cfg()) { |
| 94 |
t.Fatalf("expected cfg '%s', got '%s'", expected, string(p.Cfg())) |
| 95 |
} |
| 96 |
if len(p.Args()) != 6 { |
| 97 |
t.Fatalf("unexpected arg len %d, %#v", len(p.Args()), p.Args()) |
| 98 |
} |
| 99 |
if p.Args()[0] != "one" || |
| 100 |
p.Args()[1] != "two" || |
| 101 |
p.Args()[2] != "foo bar" || |
| 102 |
p.Args()[3] != "s/$FOO/foo/g" || |
| 103 |
p.Args()[4] != "s/$BAR/bar baz/g" || |
| 104 |
p.Args()[5] != "\\ \\ \\ " { |
| 105 |
t.Fatalf("unexpected arg array: %#v", p.Args()) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
func TestExecPlugin_ErrIfNotExecutable(t *testing.T) { |
| 110 |
if runtime.GOOS == "windows" { |
| 111 |
t.Skipf("always returns nil on Windows") |
| 112 |
} |
| 113 |
|
| 114 |
srcRoot, err := utils.DeterminePluginSrcRoot(filesys.MakeFsOnDisk()) |
| 115 |
if err != nil { |
| 116 |
t.Error(err) |
| 117 |
} |
| 118 |
|
| 119 |
// Test unexecutable plugin |
| 120 |
unexecutablePlugin := filepath.Join( |
| 121 |
srcRoot, "builtin", "", "secretgenerator", "SecretGenerator.so") |
| 122 |
p := NewExecPlugin(unexecutablePlugin) |
| 123 |
err = p.ErrIfNotExecutable() |
| 124 |
if err == nil { |
| 125 |
t.Fatalf("unexpected err: %v", err) |
| 126 |
} |
| 127 |
|
| 128 |
// Test executable plugin |
| 129 |
executablePlugin := filepath.Join( |
| 130 |
srcRoot, "someteam.example.com", "v1", "bashedconfigmap", "BashedConfigMap") |
| 131 |
p = NewExecPlugin(executablePlugin) |
| 132 |
err = p.ErrIfNotExecutable() |
| 133 |
if err != nil { |
| 134 |
t.Fatalf("unexpected err: %v", err) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
// TestExecPluginLarge loads PluginConfigs of various (large) sizes. It tests if the env variable is kept below the |
| 139 |
// maximum of 131072 bytes. |
| 140 |
func TestExecPluginLarge(t *testing.T) { |
| 141 |
// Skip this test on windows. |
| 142 |
if runtime.GOOS == "windows" { |
| 143 |
t.Skipf("always returns nil on Windows") |
| 144 |
} |
| 145 |
|
| 146 |
// Add executable plugin. |
| 147 |
srcRoot, err := utils.DeterminePluginSrcRoot(filesys.MakeFsOnDisk()) |
| 148 |
if err != nil { |
| 149 |
t.Error(err) |
| 150 |
} |
| 151 |
executablePlugin := filepath.Join( |
| 152 |
srcRoot, "someteam.example.com", "v1", "bashedconfigmap", "BashedConfigMap") |
| 153 |
p := NewExecPlugin(executablePlugin) |
| 154 |
err = p.ErrIfNotExecutable() |
| 155 |
if err != nil { |
| 156 |
t.Fatalf("unexpected err: %v", err) |
| 157 |
} |
| 158 |
|
| 159 |
// Create a fake filesystem. |
| 160 |
fSys := filesys.MakeFsInMemory() |
| 161 |
|
| 162 |
// Load plugin config. |
| 163 |
ldr, err := fLdr.NewLoader( |
| 164 |
fLdr.RestrictionRootOnly, filesys.Separator, fSys) |
| 165 |
if err != nil { |
| 166 |
t.Fatal(err) |
| 167 |
} |
| 168 |
pvd := provider.NewDefaultDepProvider() |
| 169 |
rf := resmap.NewFactory(pvd.GetResourceFactory()) |
| 170 |
pc := types.DisabledPluginConfig() |
| 171 |
|
| 172 |
// Test for various lengths. 131071 is the max length that we can have for any given env var in Bytes. |
| 173 |
tcs := []struct { |
| 174 |
length int |
| 175 |
char rune |
| 176 |
}{ |
| 177 |
{1000, 'a'}, |
| 178 |
{131071, 'a'}, |
| 179 |
{131072, 'a'}, |
| 180 |
{200000, 'a'}, |
| 181 |
{131071, '安'}, |
| 182 |
{131074, '安'}, |
| 183 |
} |
| 184 |
for _, tc := range tcs { |
| 185 |
t.Logf("Testing with an env var length of %d and character %c", tc.length, tc.char) |
| 186 |
pluginConfig, err := rf.RF().FromBytes(buildLargePluginConfig(tc.length, tc.char)) |
| 187 |
if err != nil { |
| 188 |
t.Fatalf("unexpected err: %v", err) |
| 189 |
} |
| 190 |
yaml, err := pluginConfig.AsYAML() |
| 191 |
if err != nil { |
| 192 |
t.Fatalf("unexpected err: %v", err) |
| 193 |
} |
| 194 |
err = p.Config(resmap.NewPluginHelpers(ldr, pvd.GetFieldValidator(), rf, pc), yaml) |
| 195 |
if err != nil { |
| 196 |
t.Fatalf("unexpected err: %v", err) |
| 197 |
} |
| 198 |
resMap, err := p.Generate() |
| 199 |
if err != nil { |
| 200 |
t.Fatalf("unexpected err: %v", err) |
| 201 |
} |
| 202 |
rNodeSlices := resMap.ToRNodeSlice() |
| 203 |
for _, rNodeSlice := range rNodeSlices { |
| 204 |
json, err := rNodeSlice.MarshalJSON() |
| 205 |
if err != nil { |
| 206 |
t.Fatalf("unexpected err: %v", err) |
| 207 |
} |
| 208 |
if string(json) != expectedLargeConfigMap { |
| 209 |
t.Fatalf("expected generated JSON to match %q, but got %q instead", |
| 210 |
expectedLargeConfigMap, string(json)) |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
// buildLargePluginConfig builds a plugin configuration of length: length - len("KUSTOMIZE_PLUGIN_CONFIG_STRING=") |
| 217 |
// This allows us to create an environment variable KUSTOMIZE_PLUGIN_CONFIG_STRING=<plugin content> with the exact |
| 218 |
// length that's provided in the length parameter. Used as a helper for TestExecPluginLarge. |
| 219 |
func buildLargePluginConfig(length int, char rune) []byte { |
| 220 |
length -= len("KUSTOMIZE_PLUGIN_CONFIG_STRING=") |
| 221 |
|
| 222 |
var sb strings.Builder |
| 223 |
sb.WriteString("apiVersion: someteam.example.com/v1\n") |
| 224 |
sb.WriteString("kind: BashedConfigMap\n") |
| 225 |
sb.WriteString("metadata:\n") |
| 226 |
sb.WriteString(" name: some-random-name\n") |
| 227 |
sb.WriteString("argsOneLiner: \"user password\"\n") |
| 228 |
sb.WriteString("customArg: ") |
| 229 |
|
| 230 |
// Now, fill up parameter customArg: until we reach the desired length. Account for the fact that runes can be |
| 231 |
// 1 to 4 Bytes each. |
| 232 |
upperBound := length - sb.Len() |
| 233 |
for i := 0; i < upperBound-len(string(char)); i += len(string(char)) { |
| 234 |
sb.WriteRune(char) |
| 235 |
} |
| 236 |
return []byte(sb.String()) |
| 237 |
} |