<commit_before>package executor_test

import (
	"archive/tar"
	"fmt"
	"io"
	"io/ioutil"
	"net"
	"os"
	"time"

	"code.cloudfoundry.org/bbs/models"
	"code.cloudfoundry.org/clock"
	loggingclient "code.cloudfoundry.org/diego-logging-client"
	"code.cloudfoundry.org/durationjson"
	"code.cloudfoundry.org/executor"
	"code.cloudfoundry.org/executor/gardenhealth"
	executorinit "code.cloudfoundry.org/executor/initializer"
	"code.cloudfoundry.org/lager"
	"code.cloudfoundry.org/lager/lagertest"
	"github.com/cloudfoundry-incubator/cf-test-helpers/generator"
	uuid "github.com/nu7hatch/gouuid"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"
	"github.com/tedsuo/ifrit"
	"github.com/tedsuo/ifrit/ginkgomon"
	"github.com/tedsuo/ifrit/grouper"

	"code.cloudfoundry.org/garden"
)

var _ = Describe("Executor/Garden", func() {
	const pruningInterval = durationjson.Duration(500 * time.Millisecond)
	var (
		executorClient          executor.Client
		process                 ifrit.Process
		runner                  ifrit.Runner
		gardenCapacity          garden.Capacity
		exportNetworkEnvVars    bool
		cachePath               string
		config                  executorinit.ExecutorConfig
		logger                  lager.Logger
		ownerName               string
		gardenHealthcheckRootFS string

		expectedDiskCapacityMB int
	)

	BeforeEach(func() {
		gardenHealthcheckRootFS = ""
		config = executorinit.DefaultConfiguration
		config.GardenNetwork = "tcp"
		config.GardenAddr = componentMaker.Addresses.GardenLinux
		config.ReservedExpirationTime = pruningInterval
		config.ContainerReapInterval = pruningInterval
		config.HealthyMonitoringInterval = durationjson.Duration(time.Second)
		config.UnhealthyMonitoringInterval = durationjson.Duration(100 * time.Millisecond)
		config.GardenHealthcheckProcessPath = "/bin/sh"
		config.GardenHealthcheckProcessArgs = []string{"-c", "echo", "checking health"}
		config.GardenHealthcheckProcessUser = "vcap"
		config.MaxCacheSizeInBytes = 1 * 1024 * 1024

		var err error
		cachePath, err = ioutil.TempDir("", "executor-tmp")
		Expect(err).NotTo(HaveOccurred())

		ownerName = "executor" + generator.RandomName()

		config.CachePath = cachePath
		config.ContainerOwnerName = ownerName
	})

	JustBeforeEach(func() {
		var err error

		config.ExportNetworkEnvVars = exportNetworkEnvVars

		logger = lagertest.NewTestLogger("test")
		var executorMembers grouper.Members
		metronClient, err := loggingclient.NewIngressClient(loggingclient.Config{})
		Expect(err).NotTo(HaveOccurred())

		executorClient, executorMembers, err = executorinit.Initialize(logger, config, gardenHealthcheckRootFS, metronClient, clock.NewClock())
		Expect(err).NotTo(HaveOccurred())
		runner = grouper.NewParallel(os.Kill, executorMembers)

		gardenCapacity, err = gardenClient.Capacity()
		Expect(err).NotTo(HaveOccurred())

		expectedDiskCapacityMB = int((gardenCapacity.DiskInBytes - config.MaxCacheSizeInBytes) / 1024 / 1024)
	})

	AfterEach(func() {
		if executorClient != nil {
			executorClient.Cleanup(logger)
		}

		if process != nil {
			ginkgomon.Interrupt(process)
		}

		os.RemoveAll(cachePath)
	})

	generateGuid := func() string {<commit_msg>Reorder Cleanup of executor to after process shutdown<commit_after>package executor_test

import (
	"archive/tar"
	"fmt"
	"io"
	"io/ioutil"
	"net"
	"os"
	"time"

	"code.cloudfoundry.org/bbs/models"
	"code.cloudfoundry.org/clock"
	loggingclient "code.cloudfoundry.org/diego-logging-client"
	"code.cloudfoundry.org/durationjson"
	"code.cloudfoundry.org/executor"
	"code.cloudfoundry.org/executor/gardenhealth"
	executorinit "code.cloudfoundry.org/executor/initializer"
	"code.cloudfoundry.org/lager"
	"code.cloudfoundry.org/lager/lagertest"
	"github.com/cloudfoundry-incubator/cf-test-helpers/generator"
	uuid "github.com/nu7hatch/gouuid"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"
	"github.com/tedsuo/ifrit"
	"github.com/tedsuo/ifrit/ginkgomon"
	"github.com/tedsuo/ifrit/grouper"

	"code.cloudfoundry.org/garden"
)

var _ = Describe("Executor/Garden", func() {
	const pruningInterval = durationjson.Duration(500 * time.Millisecond)
	var (
		executorClient          executor.Client
		process                 ifrit.Process
		runner                  ifrit.Runner
		gardenCapacity          garden.Capacity
		exportNetworkEnvVars    bool
		cachePath               string
		config                  executorinit.ExecutorConfig
		logger                  lager.Logger
		ownerName               string
		gardenHealthcheckRootFS string

		expectedDiskCapacityMB int
	)

	BeforeEach(func() {
		gardenHealthcheckRootFS = ""
		config = executorinit.DefaultConfiguration
		config.GardenNetwork = "tcp"
		config.GardenAddr = componentMaker.Addresses.GardenLinux
		config.ReservedExpirationTime = pruningInterval
		config.ContainerReapInterval = pruningInterval
		config.HealthyMonitoringInterval = durationjson.Duration(time.Second)
		config.UnhealthyMonitoringInterval = durationjson.Duration(100 * time.Millisecond)
		config.GardenHealthcheckProcessPath = "/bin/sh"
		config.GardenHealthcheckProcessArgs = []string{"-c", "echo", "checking health"}
		config.GardenHealthcheckProcessUser = "vcap"
		config.MaxCacheSizeInBytes = 1 * 1024 * 1024

		var err error
		cachePath, err = ioutil.TempDir("", "executor-tmp")
		Expect(err).NotTo(HaveOccurred())

		ownerName = "executor" + generator.RandomName()

		config.CachePath = cachePath
		config.ContainerOwnerName = ownerName
	})

	JustBeforeEach(func() {
		var err error

		config.ExportNetworkEnvVars = exportNetworkEnvVars

		logger = lagertest.NewTestLogger("test")
		var executorMembers grouper.Members
		metronClient, err := loggingclient.NewIngressClient(loggingclient.Config{})
		Expect(err).NotTo(HaveOccurred())

		executorClient, executorMembers, err = executorinit.Initialize(logger, config, gardenHealthcheckRootFS, metronClient, clock.NewClock())
		Expect(err).NotTo(HaveOccurred())
		runner = grouper.NewParallel(os.Kill, executorMembers)

		gardenCapacity, err = gardenClient.Capacity()
		Expect(err).NotTo(HaveOccurred())

		expectedDiskCapacityMB = int((gardenCapacity.DiskInBytes - config.MaxCacheSizeInBytes) / 1024 / 1024)
	})

	AfterEach(func() {
		if process != nil {
			ginkgomon.Interrupt(process)
		}

		if executorClient != nil {
			executorClient.Cleanup(logger)
		}

		os.RemoveAll(cachePath)
	})

	generateGuid := func() string {
{'fraction_non_alphanumeric': 0.10883084577114428, 'fraction_numerical': 0.007462686567164179, 'mean_word_length': 4.281609195402299, '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': '905804', 'n_tokens_mistral': 2300, 'n_tokens_neox': 2116, 'n_words': 317}