<commit_before><commit_msg>Add integration test for run_script<commit_after>package integration_test

import (
	"path/filepath"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	"github.com/cloudfoundry/bosh-agent/agentclient"
	"github.com/cloudfoundry/bosh-agent/agentclient/applyspec"
	"github.com/cloudfoundry/bosh-agent/settings"
)

func setupDummyJob(agentClient agentclient.AgentClient) {
	err := testEnvironment.CreateSensitiveBlobFromAsset(filepath.Join("release", "jobs/foobar.tgz"), "abc0")
	Expect(err).NotTo(HaveOccurred())

	applySpec := applyspec.ApplySpec{
		ConfigurationHash: "fake-desired-config-hash",
		NodeID:            "node-id01-123f-r2344",
		AvailabilityZone:  "ex-az",
		Deployment:        "deployment-name",
		Name:              "instance-name",

		Job: applyspec.Job{
			Name: "foobar-ig",
			Templates: []applyspec.Blob{
				{Name: "foobar", SHA1: "b70d2e6fefb1ff48f33a1cb08a609f19dd0f2c7d", BlobstoreID: "abc0", Version: "1234"},
			},
		},

		RenderedTemplatesArchive: applyspec.RenderedTemplatesArchiveSpec{
			BlobstoreID: "abc0",
			SHA1:        "b70d2e6fefb1ff48f33a1cb08a609f19dd0f2c7d",
		},

		Packages: map[string]applyspec.Blob{},
	}

	err = agentClient.Apply(applySpec)
	Expect(err).NotTo(HaveOccurred())
}

var _ = Describe("run_script", func() {
	var (
		agentClient      agentclient.AgentClient
		registrySettings settings.Settings
	)

	BeforeEach(func() {
		err := testEnvironment.StopAgent()
		Expect(err).ToNot(HaveOccurred())

		err = testEnvironment.CleanupDataDir()
		Expect(err).ToNot(HaveOccurred())

		err = testEnvironment.CleanupLogFile()
		Expect(err).ToNot(HaveOccurred())

		err = testEnvironment.SetupConfigDrive()
		Expect(err).ToNot(HaveOccurred())

		err = testEnvironment.UpdateAgentConfig("config-drive-agent.json")
		Expect(err).ToNot(HaveOccurred())

		registrySettings = settings.Settings{
			AgentID: "fake-agent-id",

			// note that this SETS the username and password for HTTP message bus access
			Mbus: "https://mbus-user:mbus-pass@127.0.0.1:6868",

			Blobstore: settings.Blobstore{
				Type: "local",
				Options: map[string]interface{}{
					"blobstore_path": "/var/vcap/data/blobs",
				},
			},

			Disks: settings.Disks{
				Ephemeral: "/dev/sdh",
			},
		}

		err = testEnvironment.AttachDevice("/dev/sdh", 128, 2)
		Expect(err).ToNot(HaveOccurred())

		err = testEnvironment.StartRegistry(registrySettings)
		Expect(err).ToNot(HaveOccurred())

		err = testEnvironment.StartAgent()
		Expect(err).ToNot(HaveOccurred())

		agentClient, err = testEnvironment.StartAgentTunnel("mbus-user", "mbus-pass", 6868)
		Expect(err).NotTo(HaveOccurred())

		_, err = testEnvironment.RunCommand("sudo mkdir -p /var/vcap/data")
		Expect(err).NotTo(HaveOccurred())

		_, err = testEnvironment.RunCommand("sudo mkdir -p /var/vcap/data")
		Expect(err).NotTo(HaveOccurred())

		setupDummyJob(agentClient)
	})

	BeforeEach(func() {
		r, stderr, _, err := testEnvironment.RunCommand3("rm -f /var/vcap/data/foobar/output.txt")
		Expect(err).NotTo(HaveOccurred(), r, stderr)

		r, stderr, _, err = testEnvironment.RunCommand3("sudo mkdir -p /var/vcap/jobs/foobar/bin")
		Expect(err).NotTo(HaveOccurred(), r, stderr)

		r, err = testEnvironment.RunCommand("sudo mkdir -p /var/vcap/data/foobar")
		Expect(err).NotTo(HaveOccurred(), r)
	})

	AfterEach(func() {
		err := testEnvironment.StopAgentTunnel()
		Expect(err).NotTo(HaveOccurred())

		err = testEnvironment.StopAgent()
		Expect(err).NotTo(HaveOccurred())

		err = testEnvironment.DetachDevice("/dev/sdh")
		Expect(err).ToNot(HaveOccurred())
	})

	It("runs a custom script", func() {
		r, stderr, _, err := testEnvironment.RunCommand3(`sudo bash -c "echo '#!/bin/bash
		echo -n foobar > /var/vcap/data/foobar/output.txt' > /var/vcap/jobs/foobar/bin/custom-script"`)
		Expect(err).NotTo(HaveOccurred(), r, stderr)

		r, err = testEnvironment.RunCommand("sudo chmod +x /var/vcap/jobs/foobar/bin/custom-script")
		Expect(err).NotTo(HaveOccurred(), r)

		err = agentClient.RunScript("custom-script", map[string]interface{}{})
		Expect(err).NotTo(HaveOccurred())

		r, stderr, _, err = testEnvironment.RunCommand3("cat /var/vcap/data/foobar/output.txt")

		Expect(err).NotTo(HaveOccurred(), r, stderr)
		Expect(r).To(Equal("foobar"))
	})

	It("runs a custom script with variables", func() {
		r, stderr, _, err := testEnvironment.RunCommand3(`sudo bash -c "echo '#!/bin/bash
		echo -n \$FOO > /var/vcap/data/foobar/output.txt' > /var/vcap/jobs/foobar/bin/custom-script"`)
		Expect(err).NotTo(HaveOccurred(), r, stderr)

		r, err = testEnvironment.RunCommand("sudo chmod +x /var/vcap/jobs/foobar/bin/custom-script")
		Expect(err).NotTo(HaveOccurred(), r)

		options := make(map[string]interface{})
		options["env"] = map[string]string{
			"FOO": "bar",
		}
		err = agentClient.RunScript("custom-script", options)
		Expect(err).NotTo(HaveOccurred())

		r, stderr, _, err = testEnvironment.RunCommand3("cat /var/vcap/data/foobar/output.txt")

		Expect(err).NotTo(HaveOccurred(), r, stderr)
		Expect(r).To(Equal("bar"))
	})
})
{'fraction_non_alphanumeric': 0.16219972205677982, 'fraction_numerical': 0.01627953146714314, 'mean_word_length': 4.686230248306997, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '5875423', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1792, 'n_words': 273}