Skip to content

Commit f976355

Browse files
authored
[shim] Fix DockerRunner tests (#3429)
* TestDocker_SSHServer - sshd check added * TestDocker_SSHServerConnect - removed, as getSSHShellCommands does not start sshd since #3421 The test should have failed since that PR, but, as there was no proper assertion, the test just slept for 180 seconds instead, reporting a false-positive result Part-of: #3419
1 parent ef0d8a7 commit f976355

File tree

1 file changed

+16
-91
lines changed

1 file changed

+16
-91
lines changed

runner/internal/shim/docker_test.go

Lines changed: 16 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ import (
55
"encoding/hex"
66
"math/rand"
77
"os"
8-
"os/exec"
98
"runtime"
10-
"strconv"
119
"strings"
1210
"sync"
13-
"sync/atomic"
1411
"testing"
1512
"time"
1613

@@ -20,90 +17,33 @@ import (
2017
)
2118

2219
// TestDocker_SSHServer pulls ubuntu image (without sshd), installs openssh-server and exits
20+
// Basically, it indirectly tests a shell script generated by getSSHShellCommands
2321
func TestDocker_SSHServer(t *testing.T) {
2422
if testing.Short() || (os.Getenv("CI") == "true" && runtime.GOOS == "darwin") {
2523
t.Skip()
2624
}
2725
t.Parallel()
2826

2927
params := &dockerParametersMock{
30-
commands: []string{"echo 1"},
31-
sshPort: nextPort(),
32-
runnerDir: t.TempDir(),
28+
commands: []string{"/usr/sbin/sshd -V 2>&1 | grep OpenSSH"},
29+
sshShellCommands: true,
30+
runnerDir: t.TempDir(),
3331
}
3432

3533
timeout := 180 // seconds
36-
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
34+
ctx, cancel := context.WithTimeout(t.Context(), time.Duration(timeout)*time.Second)
3735
defer cancel()
3836

3937
dockerRunner, err := NewDockerRunner(ctx, params)
4038
require.NoError(t, err)
4139

4240
taskConfig := createTaskConfig(t)
43-
defer dockerRunner.Remove(context.Background(), taskConfig.ID)
41+
defer dockerRunner.Remove(t.Context(), taskConfig.ID)
4442

4543
assert.NoError(t, dockerRunner.Submit(ctx, taskConfig))
4644
assert.NoError(t, dockerRunner.Run(ctx, taskConfig.ID))
4745
}
4846

49-
// TestDocker_SSHServerConnect pulls ubuntu image (without sshd), installs openssh-server and tries to connect via SSH
50-
func TestDocker_SSHServerConnect(t *testing.T) {
51-
if testing.Short() || (os.Getenv("CI") == "true" && runtime.GOOS == "darwin") {
52-
t.Skip()
53-
}
54-
t.Parallel()
55-
56-
tempDir := t.TempDir()
57-
require.NoError(t, exec.CommandContext(t.Context(), "ssh-keygen", "-t", "rsa", "-b", "2048", "-f", tempDir+"/id_rsa", "-q", "-N", "").Run())
58-
publicBytes, err := os.ReadFile(tempDir + "/id_rsa.pub")
59-
require.NoError(t, err)
60-
61-
params := &dockerParametersMock{
62-
commands: []string{"sleep 5"},
63-
sshPort: nextPort(),
64-
publicSSHKey: string(publicBytes),
65-
runnerDir: t.TempDir(),
66-
}
67-
68-
timeout := 180 // seconds
69-
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
70-
defer cancel()
71-
72-
dockerRunner, err := NewDockerRunner(ctx, params)
73-
require.NoError(t, err)
74-
75-
var wg sync.WaitGroup
76-
wg.Add(1)
77-
go func() {
78-
defer wg.Done()
79-
taskConfig := createTaskConfig(t)
80-
defer dockerRunner.Remove(context.Background(), taskConfig.ID)
81-
82-
assert.NoError(t, dockerRunner.Submit(ctx, taskConfig))
83-
assert.NoError(t, dockerRunner.Run(ctx, taskConfig.ID))
84-
}()
85-
86-
for i := 0; i < timeout; i++ {
87-
cmd := exec.CommandContext(
88-
t.Context(),
89-
"ssh",
90-
"-F", "none",
91-
"-o", "StrictHostKeyChecking=no",
92-
"-o", "UserKnownHostsFile=/dev/null",
93-
"-i", tempDir+"/id_rsa",
94-
"-p", strconv.Itoa(params.sshPort),
95-
"root@localhost", "whoami",
96-
)
97-
output, err := cmd.Output()
98-
if err == nil {
99-
assert.Equal(t, "root\n", string(output))
100-
break
101-
}
102-
time.Sleep(time.Second) // 1 attempt per second
103-
}
104-
wg.Wait()
105-
}
106-
10747
func TestDocker_ShmNoexecByDefault(t *testing.T) {
10848
if testing.Short() || (os.Getenv("CI") == "true" && runtime.GOOS == "darwin") {
10949
t.Skip()
@@ -116,14 +56,14 @@ func TestDocker_ShmNoexecByDefault(t *testing.T) {
11656
}
11757

11858
timeout := 180 // seconds
119-
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
59+
ctx, cancel := context.WithTimeout(t.Context(), time.Duration(timeout)*time.Second)
12060
defer cancel()
12161

12262
dockerRunner, err := NewDockerRunner(ctx, params)
12363
require.NoError(t, err)
12464

12565
taskConfig := createTaskConfig(t)
126-
defer dockerRunner.Remove(context.Background(), taskConfig.ID)
66+
defer dockerRunner.Remove(t.Context(), taskConfig.ID)
12767

12868
assert.NoError(t, dockerRunner.Submit(ctx, taskConfig))
12969
assert.NoError(t, dockerRunner.Run(ctx, taskConfig.ID))
@@ -141,15 +81,15 @@ func TestDocker_ShmExecIfSizeSpecified(t *testing.T) {
14181
}
14282

14383
timeout := 180 // seconds
144-
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
84+
ctx, cancel := context.WithTimeout(t.Context(), time.Duration(timeout)*time.Second)
14585
defer cancel()
14686

14787
dockerRunner, err := NewDockerRunner(ctx, params)
14888
require.NoError(t, err)
14989

15090
taskConfig := createTaskConfig(t)
15191
taskConfig.ShmSize = 1024 * 1024
152-
defer dockerRunner.Remove(context.Background(), taskConfig.ID)
92+
defer dockerRunner.Remove(t.Context(), taskConfig.ID)
15393

15494
assert.NoError(t, dockerRunner.Submit(ctx, taskConfig))
15595
assert.NoError(t, dockerRunner.Run(ctx, taskConfig.ID))
@@ -158,11 +98,9 @@ func TestDocker_ShmExecIfSizeSpecified(t *testing.T) {
15898
/* Mocks */
15999

160100
type dockerParametersMock struct {
161-
// If sshPort is not set (equals zero), sshd won't be started.
162-
commands []string
163-
sshPort int
164-
publicSSHKey string
165-
runnerDir string
101+
commands []string
102+
sshShellCommands bool
103+
runnerDir string
166104
}
167105

168106
func (c *dockerParametersMock) DockerPrivileged() bool {
@@ -174,24 +112,17 @@ func (c *dockerParametersMock) DockerPJRTDevice() string {
174112
}
175113

176114
func (c *dockerParametersMock) DockerShellCommands(publicKeys []string) []string {
177-
userPublicKey := c.publicSSHKey
178-
if len(publicKeys) > 0 {
179-
userPublicKey = strings.Join(publicKeys, "\n")
180-
}
115+
userPublicKey := strings.Join(publicKeys, "\n")
181116
commands := make([]string, 0)
182-
if c.sshPort != 0 {
117+
if c.sshShellCommands {
183118
commands = append(commands, getSSHShellCommands(userPublicKey)...)
184119
}
185120
commands = append(commands, c.commands...)
186121
return commands
187122
}
188123

189124
func (c *dockerParametersMock) DockerPorts() []int {
190-
ports := make([]int, 0)
191-
if c.sshPort != 0 {
192-
ports = append(ports, c.sshPort)
193-
}
194-
return ports
125+
return []int{}
195126
}
196127

197128
func (c *dockerParametersMock) DockerMounts(string) ([]mount.Mount, error) {
@@ -204,12 +135,6 @@ func (c *dockerParametersMock) MakeRunnerDir(string) (string, error) {
204135

205136
/* Utilities */
206137

207-
var portNumber int32 = 10000
208-
209-
func nextPort() int {
210-
return int(atomic.AddInt32(&portNumber, 1))
211-
}
212-
213138
var (
214139
randSrc = rand.New(rand.NewSource(time.Now().UnixNano()))
215140
randMu = sync.Mutex{}

0 commit comments

Comments
 (0)