Beispiel #1
0
func initDataset() error {
	for _, cmd := range [][]string{
		[]string{"jetpack", "init"},
		[]string{"make", "-C", filepath.Join(ImagesPath, "freebsd-base.release")},
		[]string{"make", "-C", filepath.Join(ImagesPath, "freebsd-base")},
		[]string{"make", "-C", filepath.Join(ImagesPath, "example.showenv")},
	} {
		cmd := run.Command(cmd[0], cmd[1:]...)
		if HasParam("verbose") {
			fmt.Println(cmd)
		}
		if err := cmd.Run(); err != nil {
			return err
		}
	}

	if ds, err := zfs.GetDataset(RootDatasetName); err != nil {
		return err
	} else {
		RootDataset = ds
	}

	if snap, err := RootDataset.Snapshot(snapshotName, "-r"); err != nil {
		return err
	} else {
		RootDatasetSnapshot = snap
	}

	return nil
}
Beispiel #2
0
func doRun(m *testing.M) int {
	// Parse flags and variables from command line
	for _, arg := range os.Args {
		if arg == "-test.v" {
			arg = "verbose"
		}
		if arg[0] == '-' {
			continue
		}
		if elts := strings.SplitN(arg, "=", 2); len(elts) == 1 {
			Params[elts[0]] = ""
		} else {
			Params[elts[0]] = elts[1]
		}
	}

	if err := os.Setenv("PATH", strings.Join([]string{BinPath, os.Getenv("PATH")}, ":")); err != nil {
		fmt.Fprintln(os.Stderr, "ERROR:", err)
	}

	var parentDataset *zfs.Dataset

	if parentName, ok := Params["dataset"]; !ok {
		fmt.Fprintln(os.Stderr, "ERROR: dataset parameter is not set")
		return 2
	} else {
		if ds, err := zfs.GetDataset(parentName); err != nil {
			fmt.Fprintln(os.Stderr, "ERROR:", err)
			return 2
		} else {
			parentDataset = ds
		}
	}

	if datadir, err := ioutil.TempDir(parentDataset.Mountpoint, "test."); err != nil {
		fmt.Fprintln(os.Stderr, "ERROR:", err)
		return 2
	} else {
		RootDatadir = datadir
	}

	ConfigPath = RootDatadir + ".conf"
	RootDatasetName = path.Join(parentDataset.Name, path.Base(RootDatadir))

	if !HasParam("keepfs") {
		defer os.RemoveAll(RootDatadir)
		defer os.Remove(ConfigPath)
	}

	if err := ioutil.WriteFile(ConfigPath,
		[]byte(fmt.Sprintf("root.zfs=%v\nroot.zfs.mountpoint=%v\n", RootDatasetName, RootDatadir)),
		0644,
	); err != nil {
		fmt.Fprintln(os.Stderr, "ERROR:", err)
		return 2
	}

	if err := os.Setenv("JETPACK_CONF", ConfigPath); err != nil {
		fmt.Fprintln(os.Stderr, "ERROR:", err)
		return 2
	}

	defer func() {
		if RootDataset == nil {
			if ds, err := zfs.GetDataset(RootDatasetName); err != nil {
				fmt.Fprintf(os.Stderr, "ERROR: RootDataset is nil, trying to get %#v: %v\n", RootDatasetName, err)
				return
			} else {
				RootDataset = ds
			}
		}
		if !HasParam("keepfs") {
			if err := RootDataset.Destroy("-r"); err != nil {
				fmt.Fprintln(os.Stderr, "ERROR:", err)
			}
		} else {
			fmt.Fprintln(os.Stderr, "*** Keeping", RootDataset)
		}
	}()

	// Fill dataset
	if err := prepareDataset(); err != nil {
		fmt.Fprintln(os.Stderr, "ERROR:", err)
		return 2
	} else if HasParam("verbose") {
		RootDataset.Zfs("list", "-r", "-tall", "-oname,mounted,mountpoint")
	}

	return m.Run()
}