-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathdocker_test.go
More file actions
161 lines (128 loc) · 3.89 KB
/
docker_test.go
File metadata and controls
161 lines (128 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package shim
import (
"context"
"encoding/hex"
"math/rand"
"os"
"runtime"
"sync"
"testing"
"time"
"github.com/docker/docker/api/types/mount"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestDocker_SSHServer pulls ubuntu image (without sshd), installs openssh-server and exits
// Basically, it indirectly tests a shell script generated by getSSHShellCommands
func TestDocker_SSHServer(t *testing.T) {
if testing.Short() || (os.Getenv("CI") == "true" && runtime.GOOS == "darwin") {
t.Skip()
}
t.Parallel()
params := &dockerParametersMock{
commands: []string{"/usr/sbin/sshd -V 2>&1 | grep OpenSSH"},
sshShellCommands: true,
runnerDir: t.TempDir(),
}
timeout := 180 // seconds
ctx, cancel := context.WithTimeout(t.Context(), time.Duration(timeout)*time.Second)
defer cancel()
dockerRunner, err := NewDockerRunner(ctx, params)
require.NoError(t, err)
taskConfig := createTaskConfig(t)
defer dockerRunner.Remove(t.Context(), taskConfig.ID)
assert.NoError(t, dockerRunner.Submit(ctx, taskConfig))
assert.NoError(t, dockerRunner.Run(ctx, taskConfig.ID))
}
func TestDocker_ShmNoexecByDefault(t *testing.T) {
if testing.Short() || (os.Getenv("CI") == "true" && runtime.GOOS == "darwin") {
t.Skip()
}
t.Parallel()
params := &dockerParametersMock{
commands: []string{"mount | grep '/dev/shm .*size=65536k' | grep noexec"},
runnerDir: t.TempDir(),
}
timeout := 180 // seconds
ctx, cancel := context.WithTimeout(t.Context(), time.Duration(timeout)*time.Second)
defer cancel()
dockerRunner, err := NewDockerRunner(ctx, params)
require.NoError(t, err)
taskConfig := createTaskConfig(t)
defer dockerRunner.Remove(t.Context(), taskConfig.ID)
assert.NoError(t, dockerRunner.Submit(ctx, taskConfig))
assert.NoError(t, dockerRunner.Run(ctx, taskConfig.ID))
}
func TestDocker_ShmExecIfSizeSpecified(t *testing.T) {
if testing.Short() || (os.Getenv("CI") == "true" && runtime.GOOS == "darwin") {
t.Skip()
}
t.Parallel()
params := &dockerParametersMock{
commands: []string{"mount | grep '/dev/shm .*size=1024k' | grep -v noexec"},
runnerDir: t.TempDir(),
}
timeout := 180 // seconds
ctx, cancel := context.WithTimeout(t.Context(), time.Duration(timeout)*time.Second)
defer cancel()
dockerRunner, err := NewDockerRunner(ctx, params)
require.NoError(t, err)
taskConfig := createTaskConfig(t)
taskConfig.ShmSize = 1024 * 1024
defer dockerRunner.Remove(t.Context(), taskConfig.ID)
assert.NoError(t, dockerRunner.Submit(ctx, taskConfig))
assert.NoError(t, dockerRunner.Run(ctx, taskConfig.ID))
}
/* Mocks */
type dockerParametersMock struct {
commands []string
sshShellCommands bool
runnerDir string
}
func (c *dockerParametersMock) DockerPrivileged() bool {
return false
}
func (c *dockerParametersMock) DockerPJRTDevice() string {
return ""
}
func (c *dockerParametersMock) DockerShellCommands(authorizedKeys []string, runnerHttpAddress string) []string {
commands := make([]string, 0)
if c.sshShellCommands {
commands = append(commands, getSSHShellCommands()...)
}
commands = append(commands, c.commands...)
return commands
}
func (c *dockerParametersMock) DockerPorts() []int {
return []int{}
}
func (c *dockerParametersMock) RunnerHTTPPort() int {
return 10999
}
func (c *dockerParametersMock) DockerMounts(string) ([]mount.Mount, error) {
return nil, nil
}
func (c *dockerParametersMock) MakeRunnerDir(string) (string, error) {
return c.runnerDir, nil
}
/* Utilities */
var (
randSrc = rand.New(rand.NewSource(time.Now().UnixNano()))
randMu = sync.Mutex{}
)
func generateID(t *testing.T) string {
const idLen = 16
b := make([]byte, idLen/2)
randMu.Lock()
defer randMu.Unlock()
_, err := randSrc.Read(b)
require.Nil(t, err)
return hex.EncodeToString(b)[:idLen]
}
func createTaskConfig(t *testing.T) TaskConfig {
return TaskConfig{
ID: generateID(t),
Name: t.Name(),
ImageName: "ubuntu",
}
}