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

Code before:
	}

	if !strings.Contains(string(rootSnap), rootFile) {
		t.Fatalf("root container tar missing expected file %q: %s", rootFile, rootSnap)
	}
	if strings.Contains(string(rootSnap), subFile) {
		t.Fatalf("root container tar unexpectedly contains sub container file %q: %s", subFile, rootSnap)
	}
	if !strings.Contains(string(subSnap), subFile) {
		t.Fatalf("sub container tar missing expected file %q: %s", subFile, subSnap)
	}
	if strings.Contains(string(subSnap), rootFile) {
		t.Fatalf("sub container tar unexpectedly contains root container file %q: %s", rootFile, subSnap)
	}

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

	rootfsKey := specutils.AnnotationRootfsUpperTar
	restoreSpecs[0].Annotations[contNameKey] = rootContainerName
	restoreSpecs[0].Annotations[rootfsKey+"."+rootContainerName] = rootTar.Name()

	restoreSpecs[1].Annotations[contNameKey] = subContainerName
	restoreSpecs[1].Annotations[rootfsKey+"."+subContainerName] = subTar.Name()

	restoreContainers, restoreCleanup, err := startContainers(conf, restoreSpecs, restoreIDs)
	if err != nil {
		t.Fatalf("error starting containers with restored rootfs: %v", err)
	}
	defer restoreCleanup()

	if ws, err := execute(conf, restoreContainers[0], "/bin/sh", "-c", fmt.Sprintf("test -f /root-upper/%s", rootFile)); err != nil || ws != 0 {
		t.Fatalf("root container missing restored file %q, ws: %v, err: %v", rootFile, ws, err)
	}
	if ws, err := execute(conf, restoreContainers[0], "/bin/sh", "-c", fmt.Sprintf("test ! -e /sub-upper/%s", subFile)); err != nil || ws != 0 {
		t.Fatalf("root container unexpectedly has sub-container file %q, ws: %v, err: %v", subFile, ws, err)
	}
	if ws, err := execute(conf, restoreContainers[1], "/bin/sh", "-c", fmt.Sprintf("test -f /sub-upper/%s", subFile)); err != nil || ws != 0 {
		t.Fatalf("sub-container missing restored file %q, ws: %v, err: %v", subFile, ws, err)
	}
	if ws, err := execute(conf, restoreContainers[1], "/bin/sh", "-c", fmt.Sprintf("test ! -e /root-upper/%s", rootFile)); err != nil || ws != 0 {
		t.Fatalf("sub-container unexpectedly has root file %q, ws: %v, err: %v", rootFile, ws, err)
	}
}

func restoreContainers(conf *config.Config, specs []*specs.Spec, ids []string, imagePath string) ([]*Container, func(), error) {
	if len(conf.RootDir) == 0 {
		panic("conf.RootDir not set. Call testutil.SetupRootDir() to set.")
	}

Review comment (suggestion):
We should mimic what annotations would be like in k8s. Pod annotations will be applied to all containers alike. So both containers should have each other's rootfs annotation as well. So we test that k8s-like annotations.

Code after:
	}

	if !strings.Contains(string(rootSnap), rootFile) {
		t.Fatalf("root container tar missing expected file %q: %s", rootFile, rootSnap)
	}
	if strings.Contains(string(rootSnap), subFile) {
		t.Fatalf("root container tar unexpectedly contains sub container file %q: %s", subFile, rootSnap)
	}
	if !strings.Contains(string(subSnap), subFile) {
		t.Fatalf("sub container tar missing expected file %q: %s", subFile, subSnap)
	}
	if strings.Contains(string(subSnap), rootFile) {
		t.Fatalf("sub container tar unexpectedly contains root container file %q: %s", rootFile, subSnap)
	}

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

	// Mimic k8s pod annotations: all containers see all annotations.
	rootfsKey := specutils.AnnotationRootfsUpperTar
	rootRootfsAnnotation := rootfsKey + "." + rootContainerName
	subRootfsAnnotation := rootfsKey + "." + subContainerName

	restoreSpecs[0].Annotations[contNameKey] = rootContainerName
	restoreSpecs[0].Annotations[rootRootfsAnnotation] = rootTar.Name()
	restoreSpecs[0].Annotations[subRootfsAnnotation] = subTar.Name()

	restoreSpecs[1].Annotations[contNameKey] = subContainerName
	restoreSpecs[1].Annotations[rootRootfsAnnotation] = rootTar.Name()
	restoreSpecs[1].Annotations[subRootfsAnnotation] = subTar.Name()

	restoreContainers, restoreCleanup, err := startContainers(conf, restoreSpecs, restoreIDs)
	if err != nil {
		t.Fatalf("error starting containers with restored rootfs: %v", err)
	}
	defer restoreCleanup()

	if ws, err := execute(conf, restoreContainers[0], "/bin/sh", "-c", fmt.Sprintf("test -f /root-upper/%s", rootFile)); err != nil || ws != 0 {
		t.Fatalf("root container missing restored file %q, ws: %v, err: %v", rootFile, ws, err)
	}
	if ws, err := execute(conf, restoreContainers[0], "/bin/sh", "-c", fmt.Sprintf("test ! -e /sub-upper/%s", subFile)); err != nil || ws != 0 {
		t.Fatalf("root container unexpectedly has sub-container file %q, ws: %v, err: %v", subFile, ws, err)
	}
	if ws, err := execute(conf, restoreContainers[1], "/bin/sh", "-c", fmt.Sprintf("test -f /sub-upper/%s", subFile)); err != nil || ws != 0 {
		t.Fatalf("sub-container missing restored file %q, ws: %v, err: %v", subFile, ws, err)
	}
	if ws, err := execute(conf, restoreContainers[1], "/bin/sh", "-c", fmt.Sprintf("test ! -e /root-upper/%s", rootFile)); err != nil || ws != 0 {
		t.Fatalf("sub-container unexpectedly has root file %q, ws: %v, err: %v", rootFile, ws, err)
	}