Skip to content

Commit 81a6e52

Browse files
milantracygvisor-bot
authored andcommitted
Add test coverage for docker build command in gVisor.
PiperOrigin-RevId: 776326812
1 parent 957abbe commit 81a6e52

File tree

1 file changed

+102
-18
lines changed

1 file changed

+102
-18
lines changed

test/image/image_test.go

Lines changed: 102 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -405,50 +405,98 @@ func dockerInGvisorCapabilities() []string {
405405
}
406406

407407
func TestDockerOverlayWithHostNetwork(t *testing.T) {
408-
testDocker(t, true, true, false)
408+
if testutil.IsRunningWithHostNet() {
409+
t.Skip("docker doesn't work with hostinet")
410+
}
411+
ctx := context.Background()
412+
d := startDockerdInGvisor(ctx, t, true)
413+
defer d.CleanUp(ctx)
414+
testDockerBuild(ctx, t, d, true)
415+
testDockerRun(ctx, t, d, true, false)
409416
}
410417

411418
func TestPrivilegedDockerOverlayWithHostNetwork(t *testing.T) {
412-
testDocker(t, true, true, true)
419+
if testutil.IsRunningWithHostNet() {
420+
t.Skip("docker doesn't work with hostinet")
421+
}
422+
ctx := context.Background()
423+
d := startDockerdInGvisor(ctx, t, true)
424+
defer d.CleanUp(ctx)
425+
testDockerRun(ctx, t, d, true, true)
413426
}
414427

415428
func TestDockerOverlay(t *testing.T) {
416-
testDocker(t, true, false, false)
429+
if testutil.IsRunningWithHostNet() {
430+
t.Skip("docker doesn't work with hostinet")
431+
}
432+
ctx := context.Background()
433+
d := startDockerdInGvisor(ctx, t, true)
434+
defer d.CleanUp(ctx)
435+
testDockerBuild(ctx, t, d, false)
436+
testDockerRun(ctx, t, d, false, false)
417437
}
418438

419439
func TestPrivilegedDockerOverlay(t *testing.T) {
420-
testDocker(t, true, false, true)
440+
if testutil.IsRunningWithHostNet() {
441+
t.Skip("docker doesn't work with hostinet")
442+
}
443+
ctx := context.Background()
444+
d := startDockerdInGvisor(ctx, t, true)
445+
defer d.CleanUp(ctx)
446+
testDockerRun(ctx, t, d, false, true)
421447
}
422448

423449
func TestDockerWithHostNetwork(t *testing.T) {
424-
testDocker(t, false, true, false)
450+
if testutil.IsRunningWithHostNet() {
451+
t.Skip("docker doesn't work with hostinet")
452+
}
453+
ctx := context.Background()
454+
d := startDockerdInGvisor(ctx, t, false)
455+
defer d.CleanUp(ctx)
456+
testDockerBuild(ctx, t, d, true)
457+
testDockerRun(ctx, t, d, true, false)
425458
}
426459

427460
func TestPrivilegedDockerWithHostNetwork(t *testing.T) {
428-
testDocker(t, false, true, true)
461+
if testutil.IsRunningWithHostNet() {
462+
t.Skip("docker doesn't work with hostinet")
463+
}
464+
ctx := context.Background()
465+
d := startDockerdInGvisor(ctx, t, false)
466+
defer d.CleanUp(ctx)
467+
testDockerRun(ctx, t, d, true, true)
429468
}
430469

431470
func TestDocker(t *testing.T) {
471+
if testutil.IsRunningWithHostNet() {
472+
t.Skip("docker doesn't work with hostinet")
473+
}
474+
ctx := context.Background()
475+
d := startDockerdInGvisor(ctx, t, false)
476+
defer d.CleanUp(ctx)
477+
testDockerBuild(ctx, t, d, false)
432478
// Overlayfs can't be built on top of another overlayfs, so docket has
433479
// to fall back to the vfs driver.
434-
testDocker(t, false, false, false)
480+
testDockerRun(ctx, t, d, false, false)
435481
}
436482

437483
func TestPrivilegedDocker(t *testing.T) {
438-
// Overlayfs can't be built on top of another overlayfs, so docket has
439-
// to fall back to the vfs driver.
440-
testDocker(t, false, false, true)
441-
}
442-
443-
func testDocker(t *testing.T, overlay, hostNetwork, startPrivilegedContainer bool) {
444484
if testutil.IsRunningWithHostNet() {
445485
t.Skip("docker doesn't work with hostinet")
446486
}
447487
ctx := context.Background()
448-
d := dockerutil.MakeContainerWithRuntime(ctx, t, "-docker")
488+
d := startDockerdInGvisor(ctx, t, true)
449489
defer d.CleanUp(ctx)
490+
// Overlayfs can't be built on top of another overlayfs, so docket has
491+
// to fall back to the vfs driver.
492+
testDockerRun(ctx, t, d, false, true)
493+
}
450494

451-
// Start the container.
495+
// The container returned by this function has to be cleaned up by the caller.
496+
func startDockerdInGvisor(ctx context.Context, t *testing.T, overlay bool) *dockerutil.Container {
497+
d := dockerutil.MakeContainerWithRuntime(ctx, t, "-docker")
498+
499+
// Start the container which starts dockerd.
452500
opts := dockerutil.RunOpts{
453501
Image: "basic/docker",
454502
CapAdd: dockerInGvisorCapabilities(),
@@ -477,27 +525,63 @@ func testDocker(t *testing.T, overlay, hostNetwork, startPrivilegedContainer boo
477525
}
478526
// Wait for the docker daemon.
479527
for i := 0; i < 10; i++ {
480-
output, err := d.Exec(ctx, dockerutil.ExecOpts{}, "docker", "info")
481-
t.Logf("== docker info ==\n%s", output)
528+
_, err := d.Exec(ctx, dockerutil.ExecOpts{}, "docker", "info")
482529
if err != nil {
483530
t.Logf("docker exec failed: %v", err)
484531
time.Sleep(5 * time.Second)
485532
continue
486533
}
487534
break
488535
}
536+
return d
537+
}
538+
539+
func testDockerRun(ctx context.Context, t *testing.T, d *dockerutil.Container, hostNetwork, startPrivilegedContainer bool) {
489540
cmd := []string{"docker", "run", "--rm"}
490541
if hostNetwork {
491542
cmd = append(cmd, "--network", "host")
492543
}
493544
if startPrivilegedContainer {
494545
cmd = append(cmd, "--privileged")
495546
}
496-
cmd = append(cmd, "alpine", "sh", "-c", "apk add curl && curl -h")
547+
cmd = append(cmd, "alpine", "sh", "-c", "apk add curl && apk info -d curl")
548+
execProc, err := d.ExecProcess(ctx, dockerutil.ExecOpts{}, cmd...)
549+
if err != nil {
550+
t.Fatalf("docker exec failed: %v", err)
551+
}
552+
output, err := execProc.Logs()
553+
if err != nil {
554+
t.Fatalf("docker logs failed: %v", err)
555+
}
556+
expectedOutput := "URL retrival utility and library"
557+
if !strings.Contains(output, expectedOutput) {
558+
t.Fatalf("docker didn't get output expected: %q, got: %q", expectedOutput, output)
559+
}
560+
}
561+
562+
func testDockerBuild(ctx context.Context, t *testing.T, d *dockerutil.Container, hostNetwork bool) {
563+
cmd := []string{"echo", "-e", "FROM alpine:3.19\nRUN apk add git", "|", "docker", "build"}
564+
if hostNetwork {
565+
cmd = append(cmd, "--network", "host")
566+
}
567+
imageName := "test_docker_build_in_gvisor"
568+
cmd = append(cmd, "-t", imageName, "-f", "-", ".")
497569
_, err := d.ExecProcess(ctx, dockerutil.ExecOpts{}, cmd...)
498570
if err != nil {
499571
t.Fatalf("docker exec failed: %v", err)
500572
}
573+
inspectImage, err := d.ExecProcess(ctx, dockerutil.ExecOpts{}, []string{"docker", "image", "inspect", imageName}...)
574+
if err != nil {
575+
t.Fatalf("docker exec failed: %v", err)
576+
}
577+
got, err := inspectImage.Logs()
578+
if err != nil {
579+
t.Fatalf("docker logs failed: %v", err)
580+
}
581+
output := imageName + ":latest"
582+
if !strings.Contains(got, output) {
583+
t.Fatalf("docker didn't get output expected: %q, got: %q", output, got)
584+
}
501585
}
502586

503587
func TestMain(m *testing.M) {

0 commit comments

Comments
 (0)