diff --git a/pkg/runner/runner_test.go b/pkg/runner/runner_test.go index 0ca4c438..a04c35f4 100644 --- a/pkg/runner/runner_test.go +++ b/pkg/runner/runner_test.go @@ -352,6 +352,29 @@ func TestProcess_Run(t *testing.T) { assert.NotContains(t, outWriter.String(), ` > good command 2`) assert.NotContains(t, outWriter.String(), ` > good command 3`) }) + + t.Run("script with task-level user", func(t *testing.T) { + testingHostAndPort, teardown := startTestContainerWithCustomUser(t, "test2") + defer teardown() + conf, err := config.New("testdata/conf.yml", nil, nil) + require.NoError(t, err) + + p := Process{ + Concurrency: 1, + Connector: connector, + Playbook: conf, + Logs: logs, + } + + outWriter := &bytes.Buffer{} + log.SetOutput(io.MultiWriter(outWriter, os.Stderr)) + + _, err = p.Run(ctx, "with_task_user", testingHostAndPort) + assert.NoError(t, err) + assert.Contains(t, outWriter.String(), ` > good command 1`) + assert.Contains(t, outWriter.String(), `(test2)`) + assert.Contains(t, outWriter.String(), ` > test2`) + }) } func TestProcess_RunWithSudo(t *testing.T) { @@ -1056,6 +1079,10 @@ func TestGen(t *testing.T) { } func startTestContainer(t *testing.T) (hostAndPort string, teardown func()) { + return startTestContainerWithCustomUser(t, "test") +} + +func startTestContainerWithCustomUser(t *testing.T, user string) (hostAndPort string, teardown func()) { ctx := context.Background() pubKey, err := os.ReadFile("testdata/test_ssh_key.pub") require.NoError(t, err) @@ -1069,7 +1096,7 @@ func startTestContainer(t *testing.T) (hostAndPort string, teardown func()) { }, Env: map[string]string{ "PUBLIC_KEY": string(pubKey), - "USER_NAME": "test", + "USER_NAME": user, "TZ": "Etc/UTC", "SUDO_ACCESS": "true", }, diff --git a/pkg/runner/testdata/conf.yml b/pkg/runner/testdata/conf.yml index a51c70f2..851af39a 100644 --- a/pkg/runner/testdata/conf.yml +++ b/pkg/runner/testdata/conf.yml @@ -196,3 +196,11 @@ tasks: echo "on exit called for copy. task: ${SPOT_TASK}, host: ${SPOT_REMOTE_HOST}, error: ${SPOT_ERROR}" rm -fv /tmp/conf-blah.yml + - name: with_task_user + user: test2 + commands: + - name: some command + script: | + echo good command 1 + echo $(id -a) + echo $SPOT_REMOTE_USER