/* * mockFlannelNetwork creates fake flannel network status file and configuration pointing to this network. * We won't have connectivity, but we could check if: netName was correct and if default gateway was set. */ func mockFlannelNetwork(t *testing.T, ctx *testutils.RktRunCtx) (string, networkTemplateT, error) { // write fake flannel info subnetPath := filepath.Join(ctx.DataDir(), "subnet.env") file, err := os.Create(subnetPath) if err != nil { return "", networkTemplateT{}, err } mockedFlannel := strings.Join([]string{ "FLANNEL_NETWORK=11.11.0.0/16", "FLANNEL_SUBNET=11.11.3.1/24", "FLANNEL_MTU=1472", "FLANNEL_IPMASQ=true", }, "\n") if _, err = file.WriteString(mockedFlannel); err != nil { return "", networkTemplateT{}, err } file.Close() // write net config for "flannel" based network ntFlannel := networkTemplateT{ Name: "rkt.kubernetes.io", Type: "flannel", SubnetFile: subnetPath, Delegate: &delegateTemplateT{ IsDefaultGateway: true, }, } netdir := prepareTestNet(t, ctx, ntFlannel) return netdir, ntFlannel, nil }
func unmountPod(t *testing.T, ctx *testutils.RktRunCtx, uuid string, rmNetns bool) { podDir := filepath.Join(ctx.DataDir(), "pods", "run", uuid) stage1MntPath := filepath.Join(podDir, "stage1", "rootfs") stage2MntPath := filepath.Join(stage1MntPath, "opt", "stage2", "rkt-inspect", "rootfs") netnsPath := filepath.Join(podDir, "netns") podNetNSPathBytes, err := ioutil.ReadFile(netnsPath) if err != nil { t.Fatalf(`cannot read "netns" stage1: %v`, err) } podNetNSPath := string(podNetNSPathBytes) if err := syscall.Unmount(stage2MntPath, 0); err != nil { t.Fatalf("cannot umount stage2: %v", err) } if err := syscall.Unmount(stage1MntPath, 0); err != nil { t.Fatalf("cannot umount stage1: %v", err) } if err := syscall.Unmount(podNetNSPath, 0); err != nil { t.Fatalf("cannot umount pod netns: %v", err) } if rmNetns { _ = os.RemoveAll(podNetNSPath) } }
func getPodDir(t *testing.T, ctx *testutils.RktRunCtx, podID string) string { podsDir := path.Join(ctx.DataDir(), "pods") dirs, err := ioutil.ReadDir(podsDir) if err != nil { t.Fatalf("Unexpected error: %v", err) } for _, dir := range dirs { podDir := path.Join(podsDir, dir.Name(), podID) if _, err := os.Stat(podDir); err == nil { return podDir } } t.Fatalf("Failed to find pod directory for pod %q", podID) return "" }
func getTreeStoreIDs(ctx *testutils.RktRunCtx) (map[string]struct{}, error) { treeStoreIDs := map[string]struct{}{} ls, err := ioutil.ReadDir(filepath.Join(ctx.DataDir(), "cas", "tree")) if err != nil { if os.IsNotExist(err) { return treeStoreIDs, nil } return nil, fmt.Errorf("cannot read treestore directory: %v", err) } for _, p := range ls { if p.IsDir() { id := filepath.Base(p.Name()) treeStoreIDs[id] = struct{}{} } } return treeStoreIDs, nil }
func podsRemaining(t *testing.T, ctx *testutils.RktRunCtx) []os.FileInfo { gcDirs := []string{ filepath.Join(ctx.DataDir(), "pods", "exited-garbage"), filepath.Join(ctx.DataDir(), "pods", "prepared"), filepath.Join(ctx.DataDir(), "pods", "garbage"), filepath.Join(ctx.DataDir(), "pods", "run"), } var remainingPods []os.FileInfo for _, dir := range gcDirs { pods, err := ioutil.ReadDir(dir) if err != nil { t.Fatalf("cannot read gc directory %q: %v", dir, err) } remainingPods = append(remainingPods, pods...) } return remainingPods }
func preparePidFileRace(t *testing.T, ctx *testutils.RktRunCtx, sleepImage string) (*gexpect.ExpectSubprocess, *gexpect.ExpectSubprocess, string, string) { // Start the pod runCmd := fmt.Sprintf("%s --debug --insecure-options=image run --mds-register=false --interactive %s", ctx.Cmd(), sleepImage) runChild := spawnOrFail(t, runCmd) if err := expectWithOutput(runChild, "Enter text:"); err != nil { t.Fatalf("Waited for the prompt but not found: %v", err) } // Check the ppid file is really created cmd := fmt.Sprintf(`%s list --full|grep running`, ctx.Cmd()) output, err := exec.Command("/bin/sh", "-c", cmd).CombinedOutput() if err != nil { t.Fatalf("Couldn't list the pods: %v", err) } UUID := strings.Split(string(output), "\t")[0] pidFileName := filepath.Join(ctx.DataDir(), "pods/run", UUID, "ppid") if _, err := os.Stat(pidFileName); err != nil { t.Fatalf("Pid file missing: %v", err) } // Temporarily move the ppid file away pidFileNameBackup := pidFileName + ".backup" if err := os.Rename(pidFileName, pidFileNameBackup); err != nil { t.Fatalf("Cannot move ppid file away: %v", err) } // Start the "enter" command without the pidfile enterCmd := fmt.Sprintf("%s --debug enter %s /inspect --print-msg=RktEnterWorksFine", ctx.Cmd(), UUID) t.Logf("%s", enterCmd) enterChild := spawnOrFail(t, enterCmd) // Enter should be able to wait until the ppid file appears time.Sleep(1 * time.Second) return runChild, enterChild, pidFileName, pidFileNameBackup }
// waitPodReady waits for the pod supervisor to get ready, busy-looping until `timeout` // while waiting for it. It returns the pod UUID or an error on failure. func waitPodReady(ctx *testutils.RktRunCtx, uuidFile string, timeout time.Duration) (string, error) { var podUUID []byte var err error interval := 500 * time.Millisecond elapsed := time.Duration(0) for elapsed < timeout { time.Sleep(interval) elapsed += interval podUUID, err = ioutil.ReadFile(uuidFile) if err == nil { break } } if err != nil { return "", fmt.Errorf("Can't read pod UUID: %v", err) } // wait for the pod supervisor to be ready podReadyFile := filepath.Join(ctx.DataDir(), "pods", "run", string(podUUID), "stage1/rootfs/rkt/supervisor-status") target := "" elapsed = time.Duration(0) for elapsed < timeout { time.Sleep(interval) elapsed += interval target, err = os.Readlink(podReadyFile) if err == nil && target == "ready" { break } } if err != nil || target != "ready" { return "", fmt.Errorf("Pod failed to become ready while checking %q", podReadyFile) } return string(podUUID), nil }