Skip to content

Commit

Permalink
fix: script and command should set the cwd (#437)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly authored Nov 17, 2023
1 parent a87ceec commit 39b1671
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
5 changes: 4 additions & 1 deletion pkg/runner/operations/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import (

type operation struct {
command v1alpha1.Command
basePath string
namespace string
}

func New(command v1alpha1.Command, namespace string) operations.Operation {
func New(command v1alpha1.Command, basePath string, namespace string) operations.Operation {
return &operation{
command: command,
basePath: basePath,
namespace: namespace,
}
}
Expand Down Expand Up @@ -53,6 +55,7 @@ func (o *operation) Exec(ctx context.Context) (_err error) {
// TODO
// env = append(env, fmt.Sprintf("KUBECONFIG=%s/bin/:%s", cwd, os.Getenv("PATH")))
cmd.Env = env
cmd.Dir = o.basePath
logger.Log(logging.Command, logging.RunStatus, color.BoldFgCyan)
cmd.Stdout = &output.Stdout
cmd.Stderr = &output.Stderr
Expand Down
12 changes: 12 additions & 0 deletions pkg/runner/operations/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func Test_operationCommand(t *testing.T) {
tests := []struct {
name string
command v1alpha1.Command
basePath string
namespace string
wantErr bool
}{{
Expand Down Expand Up @@ -42,12 +43,23 @@ func Test_operationCommand(t *testing.T) {
},
namespace: "test-namespace",
wantErr: false,
}, {
name: "Test base path",
command: v1alpha1.Command{
Entrypoint: "cat",
Args: []string{"operation.go"},
SkipLogOutput: true,
},
basePath: "..",
namespace: "test-namespace",
wantErr: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := logging.IntoContext(context.TODO(), &tlogging.FakeLogger{})
operation := operation{
command: tt.command,
basePath: tt.basePath,
namespace: tt.namespace,
}
err := operation.Exec(ctx)
Expand Down
5 changes: 4 additions & 1 deletion pkg/runner/operations/script/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import (

type operation struct {
script v1alpha1.Script
basePath string
namespace string
}

func New(script v1alpha1.Script, namespace string) operations.Operation {
func New(script v1alpha1.Script, basePath string, namespace string) operations.Operation {
return &operation{
script: script,
basePath: basePath,
namespace: namespace,
}
}
Expand Down Expand Up @@ -52,6 +54,7 @@ func (o *operation) Exec(ctx context.Context) (_err error) {
// TODO
// env = append(env, fmt.Sprintf("KUBECONFIG=%s/bin/:%s", cwd, os.Getenv("PATH")))
cmd.Env = env
cmd.Dir = o.basePath
logger.Log(logging.Script, logging.RunStatus, color.BoldFgCyan)
cmd.Stdout = &output.Stdout
cmd.Stderr = &output.Stderr
Expand Down
11 changes: 11 additions & 0 deletions pkg/runner/operations/script/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func Test_operationScript(t *testing.T) {
tests := []struct {
name string
script v1alpha1.Script
basePath string
namespace string
wantErr bool
}{{
Expand All @@ -40,12 +41,22 @@ func Test_operationScript(t *testing.T) {
},
namespace: "test-namespace",
wantErr: false,
}, {
name: "Test base path",
script: v1alpha1.Script{
Content: "cat operation.go",
SkipLogOutput: true,
},
basePath: "..",
namespace: "test-namespace",
wantErr: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := logging.IntoContext(context.TODO(), &tlogging.FakeLogger{})
operation := operation{
script: tt.script,
basePath: tt.basePath,
namespace: tt.namespace,
}
err := operation.Exec(ctx)
Expand Down
4 changes: 2 additions & 2 deletions pkg/runner/processors/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (p *stepProcessor) assertOperation(ctx context.Context, op v1alpha1.Assert,
func (p *stepProcessor) commandOperation(ctx context.Context, exec v1alpha1.Command, to *metav1.Duration) operation {
return operation{
timeout: timeout.Get(timeout.DefaultExecTimeout, p.config.Timeouts.Exec, p.test.Spec.Timeouts.Exec, p.step.Spec.Timeouts.Exec, to),
operation: opcommand.New(exec, p.namespacer.GetNamespace()),
operation: opcommand.New(exec, p.test.BasePath, p.namespacer.GetNamespace()),
}
}

Expand Down Expand Up @@ -322,7 +322,7 @@ func (p *stepProcessor) errorOperation(ctx context.Context, op v1alpha1.Error, t
func (p *stepProcessor) scriptOperation(ctx context.Context, exec v1alpha1.Script, to *metav1.Duration) operation {
return operation{
timeout: timeout.Get(timeout.DefaultExecTimeout, p.config.Timeouts.Exec, p.test.Spec.Timeouts.Exec, p.step.Spec.Timeouts.Exec, to),
operation: opscript.New(exec, p.namespacer.GetNamespace()),
operation: opscript.New(exec, p.test.BasePath, p.namespacer.GetNamespace()),
}
}

Expand Down

0 comments on commit 39b1671

Please sign in to comment.