Skip to content

Commit c89438e

Browse files
committed
integration: add container start test using abs runtime path
Signed-off-by: Iceber Gu <[email protected]>
1 parent 97064b0 commit c89438e

File tree

2 files changed

+128
-1
lines changed

2 files changed

+128
-1
lines changed

integration/client/container_test.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424
"os"
2525
"path"
26+
"path/filepath"
2627
"runtime"
2728
"strings"
2829
"syscall"
@@ -39,9 +40,11 @@ import (
3940
"github.com/containerd/containerd/namespaces"
4041
"github.com/containerd/containerd/oci"
4142
"github.com/containerd/containerd/platforms"
43+
"github.com/containerd/containerd/plugin"
4244
gogotypes "github.com/containerd/containerd/protobuf/types"
4345
_ "github.com/containerd/containerd/runtime"
4446
"github.com/containerd/containerd/runtime/v2/runc/options"
47+
"github.com/containerd/continuity/fs"
4548
"github.com/containerd/go-runc"
4649
"github.com/containerd/typeurl/v2"
4750
specs "github.com/opencontainers/runtime-spec/specs-go"
@@ -173,6 +176,130 @@ func TestContainerStart(t *testing.T) {
173176
}
174177
}
175178

179+
func readShimPath(taskID string) (string, error) {
180+
runtime := fmt.Sprintf("%s.%s", plugin.RuntimePluginV2, "task")
181+
shimBinaryNamePath := filepath.Join(defaultState, runtime, testNamespace, taskID, "shim-binary-path")
182+
183+
shimPath, err := os.ReadFile(shimBinaryNamePath)
184+
if err != nil {
185+
return "", err
186+
}
187+
return string(shimPath), nil
188+
}
189+
190+
func copyShim(shimPath string) (string, error) {
191+
tempPath := filepath.Join(os.TempDir(), filepath.Base(shimPath))
192+
if err := fs.CopyFile(tempPath, shimPath); err != nil {
193+
return "", err
194+
}
195+
196+
fi, err := os.Stat(shimPath)
197+
if err != nil {
198+
return "", err
199+
}
200+
if err := os.Chmod(tempPath, fi.Mode().Perm()); err != nil {
201+
return "", err
202+
}
203+
204+
return tempPath, nil
205+
}
206+
207+
func TestContainerStartWithAbsRuntimePath(t *testing.T) {
208+
t.Parallel()
209+
210+
client, err := newClient(t, address)
211+
if err != nil {
212+
t.Fatal(err)
213+
}
214+
defer client.Close()
215+
216+
var (
217+
image Image
218+
ctx, cancel = testContext(t)
219+
id = t.Name()
220+
)
221+
defer cancel()
222+
223+
image, err = client.GetImage(ctx, testImage)
224+
if err != nil {
225+
t.Fatal(err)
226+
}
227+
container, err := client.NewContainer(ctx, id, WithNewSnapshot(id, image), WithNewSpec(oci.WithImageConfig(image), withExitStatus(7)))
228+
if err != nil {
229+
t.Fatal(err)
230+
}
231+
defer container.Delete(ctx, WithSnapshotCleanup)
232+
233+
// create a temp task to read the default shim path
234+
task, err := container.NewTask(ctx, empty())
235+
if err != nil {
236+
t.Fatal(err)
237+
}
238+
239+
defaultShimPath, err := readShimPath(task.ID())
240+
if err != nil {
241+
t.Fatal(err)
242+
}
243+
244+
// remove the temp task
245+
if _, err := task.Delete(ctx, WithProcessKill); err != nil {
246+
t.Fatal(err)
247+
}
248+
249+
tempShimPath, err := copyShim(defaultShimPath)
250+
if err != nil {
251+
t.Fatal(err)
252+
}
253+
defer os.Remove(tempShimPath)
254+
255+
task, err = container.NewTask(ctx, empty(), WithRuntimePath(tempShimPath))
256+
if err != nil {
257+
t.Fatal(err)
258+
}
259+
260+
shimPath, err := readShimPath(task.ID())
261+
if err != nil {
262+
t.Fatal(err)
263+
}
264+
if shimPath != tempShimPath {
265+
t.Fatalf("The task's shim path is %s, does not used the specified runtime path: %s", shimPath, tempShimPath)
266+
}
267+
268+
statusC, err := task.Wait(ctx)
269+
if err != nil {
270+
t.Fatal(err)
271+
}
272+
273+
if runtime.GOOS != "windows" {
274+
// task.Pid not implemented on Windows
275+
if pid := task.Pid(); pid < 1 {
276+
t.Errorf("invalid task pid %d", pid)
277+
}
278+
}
279+
280+
if err := task.Start(ctx); err != nil {
281+
t.Error(err)
282+
task.Delete(ctx)
283+
return
284+
}
285+
status := <-statusC
286+
code, _, err := status.Result()
287+
if err != nil {
288+
t.Fatal(err)
289+
}
290+
if code != 7 {
291+
t.Errorf("expected status 7 from wait but received %d", code)
292+
}
293+
294+
deleteStatus, err := task.Delete(ctx)
295+
if err != nil {
296+
t.Fatal(err)
297+
}
298+
if ec := deleteStatus.ExitCode(); ec != 7 {
299+
t.Errorf("expected status 7 from delete but received %d", ec)
300+
}
301+
}
302+
176303
func TestContainerOutput(t *testing.T) {
177304
t.Parallel()
178305

integration/client/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/Microsoft/hcsshim/test v0.0.0-20210408205431-da33ecd607e1
99
github.com/containerd/cgroups/v3 v3.0.1
1010
github.com/containerd/containerd v1.7.0-beta.0 // see replace; the actual version of containerd is replaced with the code at the root of this repository
11+
github.com/containerd/continuity v0.3.0
1112
github.com/containerd/go-runc v1.0.0
1213
github.com/containerd/ttrpc v1.2.1
1314
github.com/containerd/typeurl/v2 v2.1.0
@@ -27,7 +28,6 @@ require (
2728
github.com/cilium/ebpf v0.9.1 // indirect
2829
github.com/containerd/cgroups v1.1.0 // indirect
2930
github.com/containerd/console v1.0.3 // indirect
30-
github.com/containerd/continuity v0.3.0 // indirect
3131
github.com/containerd/fifo v1.1.0 // indirect
3232
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
3333
github.com/cyphar/filepath-securejoin v0.2.3 // indirect

0 commit comments

Comments
 (0)