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

Code before:
		}
	}

	return containers, cu.Release(), nil
}

func TestMultiContainerTarRootfsUpperLayer(t *testing.T) {
	conf := testutil.TestConfig(t)
	conf.Overlay2.Set("all:memory")

	rootDir, cleanup, err := testutil.SetupRootDir()
	if err != nil {
		t.Fatalf("error creating root dir: %v", err)
	}
	defer cleanup()
	conf.RootDir = rootDir

	specs, ids := createSpecs(sleepCmd, sleepCmd)
	for _, spec := range specs {
		spec.Root.Readonly = false
	}

	const rootContainerName = "root-container"
	const subContainerName = "sub-container"
	specs[0].Annotations["io.kubernetes.cri.container-name"] = rootContainerName
	specs[1].Annotations["io.kubernetes.cri.container-name"] = subContainerName

	containers, cleanupContainers, err := startContainers(conf, specs, ids)
	if err != nil {
		t.Fatalf("error starting containers: %v", err)
	}
	defer cleanupContainers()

	rootFile := "root-upper-file.txt"
	subFile := "sub-upper-file.txt"

	if ws, err := execute(conf, containers[0], "/bin/sh", "-c", fmt.Sprintf("mkdir -p /root-upper && echo root > /root-upper/%s", rootFile)); err != nil || ws != 0 {
		t.Fatalf("failed to create file in root container, ws: %v, err: %v", ws, err)
	}
	if ws, err := execute(conf, containers[1], "/bin/sh", "-c", fmt.Sprintf("mkdir -p /sub-upper && echo sub > /sub-upper/%s", subFile)); err != nil || ws != 0 {
		t.Fatalf("failed to create file in sub-container, ws: %v, err: %v", ws, err)
	}

	rootTar, err := os.CreateTemp(testutil.TmpDir(), "root-tar-*.tar")
	if err != nil {
		t.Fatalf("error creating temp file: %v", err)
	}
	defer os.Remove(rootTar.Name())

	subTar, err := os.CreateTemp(testutil.TmpDir(), "sub-tar-*.tar")
	if err != nil {

Review comment (suggestion):
maybe export this constant: https://github.com/google/gvisor/blob/77785a7efef6906001c53e59e1939f34285c1400/runsc/specutils/specutils.go#L53

And use it in the tests. `specutils` is already imported.

Code after:
		}
	}

	return containers, cu.Release(), nil
}

func TestMultiContainerTarRootfsUpperLayer(t *testing.T) {
	conf := testutil.TestConfig(t)
	conf.Overlay2.Set("all:memory")

	rootDir, cleanup, err := testutil.SetupRootDir()
	if err != nil {
		t.Fatalf("error creating root dir: %v", err)
	}
	defer cleanup()
	conf.RootDir = rootDir

	specs, ids := createSpecs(sleepCmd, sleepCmd)
	for _, spec := range specs {
		spec.Root.Readonly = false
	}

	const rootContainerName = "root-container"
	const subContainerName = "sub-container"
	contNameKey := specutils.ContainerdContainerNameAnnotation
	specs[0].Annotations[contNameKey] = rootContainerName
	specs[1].Annotations[contNameKey] = subContainerName

	containers, cleanupContainers, err := startContainers(conf, specs, ids)
	if err != nil {
		t.Fatalf("error starting containers: %v", err)
	}
	defer cleanupContainers()

	rootFile := "root-upper-file.txt"
	subFile := "sub-upper-file.txt"

	if ws, err := execute(conf, containers[0], "/bin/sh", "-c", fmt.Sprintf("mkdir -p /root-upper && echo root > /root-upper/%s", rootFile)); err != nil || ws != 0 {
		t.Fatalf("failed to create file in root container, ws: %v, err: %v", ws, err)
	}
	if ws, err := execute(conf, containers[1], "/bin/sh", "-c", fmt.Sprintf("mkdir -p /sub-upper && echo sub > /sub-upper/%s", subFile)); err != nil || ws != 0 {
		t.Fatalf("failed to create file in sub-container, ws: %v, err: %v", ws, err)
	}

	rootTar, err := os.CreateTemp(testutil.TmpDir(), "root-tar-*.tar")
	if err != nil {
		t.Fatalf("error creating temp file: %v", err)
	}
	defer os.Remove(rootTar.Name())

	subTar, err := os.CreateTemp(testutil.TmpDir(), "sub-tar-*.tar")