Repository: google/gvisor
File: runsc/container/container_test.go (Go)

Code before:
		t.Fatalf("error creating temp file: %v", err)
	}
	defer os.Remove(tarFile1.Name())

	if err := cont.TarRootfsUpperLayer(tarFile1); err != nil {
		t.Fatalf("error serializing rootfs upper layer to tar: %v", err)
	}
	tarFile1.Close()

	// List the contents of the tar file using the tar command.
	snap1, err := exec.Command("tar", "-tvf", tarFile1.Name()).CombinedOutput()
	if err != nil {
		t.Fatalf("error listing contents of tar file: %v, output: %s", err, snap1)
	} else {
		t.Logf("contents of tar file: %s", snap1)
	}
	lineCounts := make(map[string]int)
	if err := processSnapBytes(snap1, lineCounts, 1); err != nil {
		t.Fatalf("error processing tar file: %v", err)
	}
	if len(lineCounts) <= 1 {
		t.Fatalf("unexpected number of lines in tar file: %d", len(lineCounts))
	}

	// Add the tar file to the spec annotations and create a new container.
	spec.Annotations["dev.gvisor.tar.rootfs.upper."+containerName] = tarFile1.Name()
	_, bundleDir, cleanup, err = testutil.SetupContainer(spec, conf)
	if err != nil {
		t.Fatalf("error setting up container: %v", err)
	}
	defer cleanup()

	args = Args{
		ID:        testutil.RandomContainerID(),
		Spec:      spec,
		BundleDir: bundleDir,
	}

	newCont, err := New(conf, args)
	if err != nil {
		t.Fatalf("error creating container: %v", err)
	}
	defer newCont.Destroy()
	if err := newCont.Start(conf); err != nil {
		t.Fatalf("error starting container: %v", err)
	}

	// Create a new temporary file to write the tar bytes to.
	tarFile2, err := os.CreateTemp(testutil.TmpDir(), "tarfile-*.tar")
	if err != nil {
		t.Fatalf("error creating temp file: %v", err)

Review comment (suggestion):
Export `dev.gvisor.tar.rootfs.upper` constant from specutils and use here and in the multi-container test.

Code after:
	defer os.Remove(tarFile1.Name())

	if err := cont.TarRootfsUpperLayer(tarFile1); err != nil {
		t.Fatalf("error serializing rootfs upper layer to tar: %v", err)
	}
	tarFile1.Close()

	// List the contents of the tar file using the tar command.
	snap1, err := exec.Command("tar", "-tvf", tarFile1.Name()).CombinedOutput()
	if err != nil {
		t.Fatalf("error listing contents of tar file: %v, output: %s", err, snap1)
	} else {
		t.Logf("contents of tar file: %s", snap1)
	}
	lineCounts := make(map[string]int)
	if err := processSnapBytes(snap1, lineCounts, 1); err != nil {
		t.Fatalf("error processing tar file: %v", err)
	}
	if len(lineCounts) <= 1 {
		t.Fatalf("unexpected number of lines in tar file: %d", len(lineCounts))
	}

	// Add the tar file to the spec annotations and create a new container.
	spec.Annotations["dev.gvisor.tar.rootfs.upper"] = tarFile1.Name()
	_, bundleDir, cleanup, err = testutil.SetupContainer(spec, conf)
	if err != nil {
		t.Fatalf("error setting up container: %v", err)
	}
	defer cleanup()

	args = Args{
		ID:        testutil.RandomContainerID(),
		Spec:      spec,
		BundleDir: bundleDir,
	}

	newCont, err := New(conf, args)
	if err != nil {
		t.Fatalf("error creating container: %v", err)
	}
	defer newCont.Destroy()
	if err := newCont.Start(conf); err != nil {
		t.Fatalf("error starting container: %v", err)
	}

	// Create a new temporary file to write the tar bytes to.
	tarFile2, err := os.CreateTemp(testutil.TmpDir(), "tarfile-*.tar")
	if err != nil {
		t.Fatalf("error creating temp file: %v", err)
	}
	defer os.Remove(tarFile2.Name())