Skip to content

Commit 1cf9c8b

Browse files
committed
add test for prepScript to ensure the cleanup was invoked
1 parent f8b40a5 commit 1cf9c8b

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

pkg/runner/commands.go

+2
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ func (ec *execCmd) prepScript(ctx context.Context, s string, r io.Reader) (cmd,
455455
// remove local copy of the script after upload or in case of error
456456
if err := os.Remove(tmp.Name()); err != nil {
457457
log.Printf("[WARN] can't remove local temp script %s: %v", tmp.Name(), err)
458+
} else {
459+
log.Printf("[DEBUG] removed local temp script %s", tmp.Name())
458460
}
459461
}()
460462
// make the script executable locally, upload preserves the permissions

pkg/runner/commands_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -680,3 +680,68 @@ func Test_execCmdWithTmp(t *testing.T) {
680680
assert.Contains(t, wr.String(), fmt.Sprintf("cannot access '%s'", tmpPath))
681681
})
682682
}
683+
684+
func Test_execCmd_prepScript(t *testing.T) {
685+
testingHostAndPort, teardown := startTestContainer(t)
686+
defer teardown()
687+
688+
ctx := context.Background()
689+
logs := executor.MakeLogs(false, false, nil)
690+
connector, connErr := executor.NewConnector("testdata/test_ssh_key", time.Second*10, logs)
691+
require.NoError(t, connErr)
692+
sess, errSess := connector.Connect(ctx, testingHostAndPort, "my-host", "test")
693+
require.NoError(t, errSess)
694+
695+
t.Run("single line command, no temp files", func(t *testing.T) {
696+
ec := execCmd{exec: sess, tsk: &config.Task{Name: "test"}}
697+
cmd, script, teardown, err := ec.prepScript(ctx, "echo hello", nil)
698+
require.NoError(t, err)
699+
assert.Equal(t, "echo hello", cmd)
700+
assert.Empty(t, script)
701+
assert.Nil(t, teardown)
702+
})
703+
704+
t.Run("multiline script with temp files", func(t *testing.T) {
705+
input := "#!/bin/sh\necho 'line 1'\necho 'line 2'\n"
706+
ec := execCmd{exec: sess, tsk: &config.Task{Name: "test"}}
707+
708+
// capture log output
709+
var buf bytes.Buffer
710+
log.SetOutput(&buf)
711+
defer log.SetOutput(os.Stdout)
712+
713+
cmd, _, teardown, err := ec.prepScript(ctx, "", bytes.NewBufferString(input))
714+
require.NoError(t, err)
715+
require.NotNil(t, teardown)
716+
defer teardown()
717+
718+
// verify command format
719+
assert.Contains(t, cmd, "/bin/sh -c /tmp/.spot-")
720+
721+
// verify script content matches input
722+
remotePath := strings.Split(cmd, " ")[2]
723+
out, err := sess.Run(ctx, fmt.Sprintf("cat %s", remotePath), nil)
724+
require.NoError(t, err)
725+
assert.Equal(t, input, strings.Join(out, "\n")+"\n")
726+
727+
// verify local temp file was removed (check logs)
728+
assert.Contains(t, buf.String(), "[DEBUG] removed local temp script")
729+
})
730+
731+
t.Run("failed upload cleanup", func(t *testing.T) {
732+
// create invalid executor that will fail on upload
733+
invalidSess := &executor.Remote{} // This will fail on upload
734+
735+
// capture log output
736+
var buf bytes.Buffer
737+
log.SetOutput(&buf)
738+
defer log.SetOutput(os.Stdout)
739+
740+
ec := execCmd{exec: invalidSess, tsk: &config.Task{Name: "test"}}
741+
_, _, _, err := ec.prepScript(ctx, "", bytes.NewBufferString("echo test"))
742+
require.Error(t, err)
743+
744+
// verify local temp file was removed even on error (check logs)
745+
assert.Contains(t, buf.String(), "[DEBUG] removed local temp script")
746+
})
747+
}

0 commit comments

Comments
 (0)