mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-30 09:51:23 +00:00
move check for working dir for exec functions
This commit is contained in:
@@ -5,6 +5,7 @@ package container
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
runtimeexec "sigs.k8s.io/kustomize/kyaml/fn/runtime/exec"
|
||||
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
|
||||
@@ -139,19 +140,27 @@ func (c Filter) GetExit() error {
|
||||
}
|
||||
|
||||
func (c *Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
c.setupExec()
|
||||
if err := c.setupExec(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.Exec.Filter(nodes)
|
||||
}
|
||||
|
||||
func (c *Filter) setupExec() {
|
||||
func (c *Filter) setupExec() error {
|
||||
// don't init 2x
|
||||
if c.Exec.Path != "" {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.Exec.WorkingDir = wd
|
||||
|
||||
path, args := c.getCommand()
|
||||
c.Exec.Path = path
|
||||
c.Exec.Args = args
|
||||
return nil
|
||||
}
|
||||
|
||||
// getArgs returns the command + args to run to spawn the container
|
||||
|
||||
@@ -6,6 +6,8 @@ package container
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/require"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -128,7 +130,7 @@ metadata:
|
||||
instance := NewContainer(tt.containerSpec, tt.UIDGID)
|
||||
instance.Exec.FunctionConfig = cfg
|
||||
instance.Env = append(instance.Env, "KYAML_TEST=FOO")
|
||||
instance.setupExec()
|
||||
assert.NoError(t, instance.setupExec())
|
||||
|
||||
tt.expectedArgs = append(tt.expectedArgs,
|
||||
runtimeutil.NewContainerEnvFromStringSlice(instance.Env).GetDockerFlags()...)
|
||||
@@ -173,6 +175,8 @@ metadata:
|
||||
instance.Exec.FunctionConfig = cfg
|
||||
instance.Exec.Path = "sed"
|
||||
instance.Exec.Args = []string{"s/Deployment/StatefulSet/g"}
|
||||
instance.Exec.WorkingDir = getWorkingDir(t)
|
||||
|
||||
output, err := instance.Filter(input)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
@@ -219,6 +223,7 @@ func TestFilter_ExitCode(t *testing.T) {
|
||||
instance := Filter{}
|
||||
instance.Exec.Path = "/not/real/command"
|
||||
instance.Exec.DeferFailure = true
|
||||
instance.Exec.WorkingDir = getWorkingDir(t)
|
||||
_, err := instance.Filter(nil)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
@@ -231,3 +236,9 @@ func TestFilter_ExitCode(t *testing.T) {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func getWorkingDir(t *testing.T) string {
|
||||
wd, err := os.Getwd()
|
||||
require.NoError(t, err)
|
||||
return wd
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user