summaryrefslogtreecommitdiff
path: root/daemon/runtime_unix_test.go
blob: 570d3536505c3d4b84ffe92545f7090f8713ef0f (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//go:build !windows
// +build !windows

package daemon

import (
	"os"
	"path/filepath"
	"testing"

	"github.com/containerd/containerd/plugin"
	v2runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/daemon/config"
	"github.com/docker/docker/errdefs"
)

func TestInitRuntimes_InvalidConfigs(t *testing.T) {
	cases := []struct {
		name      string
		runtime   types.Runtime
		expectErr string
	}{
		{
			name:      "Empty",
			expectErr: "either a runtimeType or a path must be configured",
		},
		{
			name:      "ArgsOnly",
			runtime:   types.Runtime{Args: []string{"foo", "bar"}},
			expectErr: "either a runtimeType or a path must be configured",
		},
		{
			name:      "OptionsOnly",
			runtime:   types.Runtime{Options: map[string]interface{}{"hello": "world"}},
			expectErr: "either a runtimeType or a path must be configured",
		},
		{
			name:      "PathAndType",
			runtime:   types.Runtime{Path: "/bin/true", Type: "io.containerd.runsc.v1"},
			expectErr: "cannot configure both",
		},
		{
			name:      "PathAndOptions",
			runtime:   types.Runtime{Path: "/bin/true", Options: map[string]interface{}{"a": "b"}},
			expectErr: "options cannot be used with a path runtime",
		},
		{
			name:      "TypeAndArgs",
			runtime:   types.Runtime{Type: "io.containerd.runsc.v1", Args: []string{"--version"}},
			expectErr: "args cannot be used with a runtimeType runtime",
		},
		{
			name: "PathArgsOptions",
			runtime: types.Runtime{
				Path:    "/bin/true",
				Args:    []string{"--version"},
				Options: map[string]interface{}{"hmm": 3},
			},
			expectErr: "options cannot be used with a path runtime",
		},
		{
			name: "TypeOptionsArgs",
			runtime: types.Runtime{
				Type:    "io.containerd.kata.v2",
				Options: map[string]interface{}{"a": "b"},
				Args:    []string{"--help"},
			},
			expectErr: "args cannot be used with a runtimeType runtime",
		},
		{
			name: "PathArgsTypeOptions",
			runtime: types.Runtime{
				Path:    "/bin/true",
				Args:    []string{"foo"},
				Type:    "io.containerd.runsc.v1",
				Options: map[string]interface{}{"a": "b"},
			},
			expectErr: "cannot configure both",
		},
	}

	for _, tt := range cases {
		t.Run(tt.name, func(t *testing.T) {
			cfg, err := config.New()
			assert.NilError(t, err)
			d := &Daemon{configStore: cfg}
			d.configStore.Root = t.TempDir()
			assert.Assert(t, os.Mkdir(filepath.Join(d.configStore.Root, "runtimes"), 0700))

			err = d.initRuntimes(map[string]types.Runtime{"myruntime": tt.runtime})
			assert.Check(t, is.ErrorContains(err, tt.expectErr))
		})
	}
}

func TestGetRuntime(t *testing.T) {
	// Configured runtimes can have any arbitrary name, including names
	// which would not be allowed as implicit runtime names. Explicit takes
	// precedence over implicit.
	const configuredRtName = "my/custom.runtime.v1"
	configuredRuntime := types.Runtime{Path: "/bin/true"}

	const rtWithArgsName = "withargs"
	rtWithArgs := types.Runtime{
		Path: "/bin/false",
		Args: []string{"--version"},
	}

	const shimWithOptsName = "shimwithopts"
	shimWithOpts := types.Runtime{
		Type:    plugin.RuntimeRuncV2,
		Options: map[string]interface{}{"IoUid": 42},
	}

	const shimAliasName = "wasmedge"
	shimAlias := types.Runtime{Type: "io.containerd.wasmedge.v1"}

	const configuredShimByPathName = "shimwithpath"
	configuredShimByPath := types.Runtime{Type: "/path/to/my/shim"}

	cfg, err := config.New()
	assert.NilError(t, err)

	d := &Daemon{configStore: cfg}
	d.configStore.Root = t.TempDir()
	assert.Assert(t, os.Mkdir(filepath.Join(d.configStore.Root, "runtimes"), 0700))
	d.configStore.Runtimes = map[string]types.Runtime{
		configuredRtName:         configuredRuntime,
		rtWithArgsName:           rtWithArgs,
		shimWithOptsName:         shimWithOpts,
		shimAliasName:            shimAlias,
		configuredShimByPathName: configuredShimByPath,
	}
	configureRuntimes(d.configStore)
	assert.Assert(t, d.loadRuntimes())

	stockRuntime, ok := d.configStore.Runtimes[config.StockRuntimeName]
	assert.Assert(t, ok, "stock runtime could not be found (test needs to be updated)")

	configdOpts := *stockRuntime.ShimConfig.Opts.(*v2runcoptions.Options)
	configdOpts.BinaryName = configuredRuntime.Path

	for _, tt := range []struct {
		name, runtime string
		wantShim      string
		wantOpts      interface{}
	}{
		{
			name:     "StockRuntime",
			runtime:  config.StockRuntimeName,
			wantShim: stockRuntime.ShimConfig.Binary,
			wantOpts: stockRuntime.ShimConfig.Opts,
		},
		{
			name:     "ShimName",
			runtime:  "io.containerd.my-shim.v42",
			wantShim: "io.containerd.my-shim.v42",
		},
		{
			// containerd is pretty loose about the format of runtime names. Perhaps too
			// loose. The only requirements are that the name contain a dot and (depending
			// on the containerd version) not start with a dot. It does not enforce any
			// particular format of the dot-delimited components of the name.
			name:     "VersionlessShimName",
			runtime:  "io.containerd.my-shim",
			wantShim: "io.containerd.my-shim",
		},
		{
			name:    "IllformedShimName",
			runtime: "myshim",
		},
		{
			name:    "EmptyString",
			runtime: "",
		},
		{
			name:    "PathToShim",
			runtime: "/path/to/runc",
		},
		{
			name:    "PathToShimName",
			runtime: "/path/to/io.containerd.runc.v2",
		},
		{
			name:    "RelPathToShim",
			runtime: "my/io.containerd.runc.v2",
		},
		{
			name:     "ConfiguredRuntime",
			runtime:  configuredRtName,
			wantShim: stockRuntime.ShimConfig.Binary,
			wantOpts: &configdOpts,
		},
		{
			name:     "RuntimeWithArgs",
			runtime:  rtWithArgsName,
			wantShim: stockRuntime.ShimConfig.Binary,
			wantOpts: defaultV2ShimConfig(
				d.configStore,
				d.rewriteRuntimePath(
					rtWithArgsName,
					rtWithArgs.Path,
					rtWithArgs.Args)).Opts,
		},
		{
			name:     "ShimWithOpts",
			runtime:  shimWithOptsName,
			wantShim: shimWithOpts.Type,
			wantOpts: &v2runcoptions.Options{IoUid: 42},
		},
		{
			name:     "ShimAlias",
			runtime:  shimAliasName,
			wantShim: shimAlias.Type,
		},
		{
			name:     "ConfiguredShimByPath",
			runtime:  configuredShimByPathName,
			wantShim: configuredShimByPath.Type,
		},
	} {
		tt := tt
		t.Run(tt.name, func(t *testing.T) {
			gotShim, gotOpts, err := d.getRuntime(tt.runtime)
			assert.Check(t, is.Equal(gotShim, tt.wantShim))
			assert.Check(t, is.DeepEqual(gotOpts, tt.wantOpts))
			if tt.wantShim != "" {
				assert.Check(t, err)
			} else {
				assert.Check(t, errdefs.IsInvalidParameter(err))
			}
		})
	}
}