<commit_before>package garden_runner

import (
	"fmt"
	"io/ioutil"
	"net"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"

	"github.com/onsi/ginkgo/config"
	"github.com/vito/cmdtest"
	"github.com/vito/gordon"
)

type GardenRunner struct {
	Remote string

	Network string
	Addr    string

	DepotPath     string
	RootPath      string
	RootFSPath    string
	SnapshotsPath string

	gardenBin string
	gardenCmd *exec.Cmd

	tmpdir string
}

func New(rootPath, rootFSPath, remote string) (*GardenRunner, error) {
	tmpdir, err := ioutil.TempDir(os.TempDir(), "garden-temp-socker")
	if err != nil {
		return nil, err
	}

	runner := &GardenRunner{
		Remote:     remote,
		Network:    "unix",
		Addr:       filepath.Join(tmpdir, "warden.sock"),
		RootPath:   rootPath,
		RootFSPath: rootFSPath,
	}

	return runner, runner.Prepare()
}

func (r *GardenRunner) cmd(command string, argv ...string) *exec.Cmd {
	if r.Remote == "" {
		return exec.Command(command, argv...)
	} else {
		args := []string{
			"-tt", "-l", "root", r.Remote,
			"shopt -s huponexit; " + command,
		}
		args = append(args, argv...)

		return exec.Command("ssh", args...)
	}
}

func (r *GardenRunner) Prepare() error {
	r.tmpdir = fmt.Sprintf("/tmp/garden-%d-%d", time.Now().UnixNano(), config.GinkgoConfig.ParallelNode)
	err := r.cmd("mkdir", r.tmpdir).Run()
	if err != nil {
		return err
	}

	compiled, err := cmdtest.Build("github.com/pivotal-cf-experimental/garden")
	if err != nil {
		return err
	}

	r.gardenBin = compiled

	r.DepotPath = filepath.Join(r.tmpdir, "containers")
	err = r.cmd("mkdir", "-m", "0755", r.DepotPath).Run()
	if err != nil {
		return err
	}

	r.SnapshotsPath = filepath.Join(r.tmpdir, "snapshots")
	return r.cmd("mkdir", r.SnapshotsPath).Run()
}

func (r *GardenRunner) Start(argv ...string) error {
	gardenArgs := argv
	gardenArgs = append(
		gardenArgs,
		"--listenNetwork", r.Network,
		"--listenAddr", r.Addr,
		"--root", r.RootPath,
		"--depot", r.DepotPath,
		"--rootfs", r.RootFSPath,
		"--snapshots", r.SnapshotsPath,
		"--debug",
		"--disableQuotas",
	)

	garden := r.cmd(r.gardenBin, gardenArgs...)

	garden.Stdout = os.Stdout
	garden.Stderr = os.Stderr

	err := garden.Start()
	if err != nil {
		return err
	}

	started := make(chan bool, 1)
	stop := make(chan bool, 1)<commit_msg>use cmdtest for running garden server to ensure .Wait is called<commit_after>package garden_runner

import (
	"fmt"
	"io/ioutil"
	"net"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"

	"github.com/cloudfoundry/gunk/runner_support"
	"github.com/onsi/ginkgo/config"
	"github.com/vito/cmdtest"
	"github.com/vito/gordon"
)

type GardenRunner struct {
	Remote string

	Network string
	Addr    string

	DepotPath     string
	RootPath      string
	RootFSPath    string
	SnapshotsPath string

	gardenBin string
	gardenCmd *exec.Cmd

	tmpdir string
}

func New(rootPath, rootFSPath, remote string) (*GardenRunner, error) {
	tmpdir, err := ioutil.TempDir(os.TempDir(), "garden-temp-socker")
	if err != nil {
		return nil, err
	}

	runner := &GardenRunner{
		Remote:     remote,
		Network:    "unix",
		Addr:       filepath.Join(tmpdir, "warden.sock"),
		RootPath:   rootPath,
		RootFSPath: rootFSPath,
	}

	return runner, runner.Prepare()
}

func (r *GardenRunner) cmd(command string, argv ...string) *exec.Cmd {
	if r.Remote == "" {
		return exec.Command(command, argv...)
	} else {
		args := []string{
			"-tt", "-l", "root", r.Remote,
			"shopt -s huponexit; " + command,
		}
		args = append(args, argv...)

		return exec.Command("ssh", args...)
	}
}

func (r *GardenRunner) Prepare() error {
	r.tmpdir = fmt.Sprintf("/tmp/garden-%d-%d", time.Now().UnixNano(), config.GinkgoConfig.ParallelNode)
	err := r.cmd("mkdir", r.tmpdir).Run()
	if err != nil {
		return err
	}

	compiled, err := cmdtest.Build("github.com/pivotal-cf-experimental/garden")
	if err != nil {
		return err
	}

	r.gardenBin = compiled

	r.DepotPath = filepath.Join(r.tmpdir, "containers")
	err = r.cmd("mkdir", "-m", "0755", r.DepotPath).Run()
	if err != nil {
		return err
	}

	r.SnapshotsPath = filepath.Join(r.tmpdir, "snapshots")
	return r.cmd("mkdir", r.SnapshotsPath).Run()
}

func (r *GardenRunner) Start(argv ...string) error {
	gardenArgs := argv
	gardenArgs = append(
		gardenArgs,
		"--listenNetwork", r.Network,
		"--listenAddr", r.Addr,
		"--root", r.RootPath,
		"--depot", r.DepotPath,
		"--rootfs", r.RootFSPath,
		"--snapshots", r.SnapshotsPath,
		"--debug",
		"--disableQuotas",
	)

	garden := r.cmd(r.gardenBin, gardenArgs...)

	garden.Stdout = os.Stdout
	garden.Stderr = os.Stderr

	_, err := cmdtest.StartWrapped(garden, runner_support.TeeIfVerbose, runner_support.TeeIfVerbose)
	if err != nil {
		return err
	}

	started := make(chan bool, 1)
	stop := make(chan bool, 1)
{'fraction_non_alphanumeric': 0.16610455311973019, 'fraction_numerical': 0.002529510961214165, 'mean_word_length': 3.480642115203022, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7309052', 'n_tokens_mistral': 1958, 'n_tokens_neox': 1840, 'n_words': 398}