Skip to content

Commit

Permalink
add a test for user name set on task level
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Apr 22, 2024
1 parent e08ad86 commit ac11d63
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
29 changes: 28 additions & 1 deletion pkg/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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",
},
Expand Down
8 changes: 8 additions & 0 deletions pkg/runner/testdata/conf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ac11d63

Please sign in to comment.