<commit_before>package gqt_test

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"

	"code.cloudfoundry.org/garden"
	"code.cloudfoundry.org/guardian/gqt/runner"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Bind mount", func() {
	var (
		client    *runner.RunningGarden
		container garden.Container
		srcPath   string
		dstPath   string
	)

	Describe("When source is a file", func() {
		BeforeEach(func() {
			tempFile, err := ioutil.TempFile("/opt", "bindmount-srcpath-file-")
			Expect(err).NotTo(HaveOccurred())
			defer tempFile.Close()
			srcPath = tempFile.Name()
			Expect(os.Chmod(srcPath, 0777)).To(Succeed())
			dstPath = "/home/alice/afile"
		})

		JustBeforeEach(func() {
			var err error
			client = runner.Start(config)
			container, err = client.Create(
				garden.ContainerSpec{
					BindMounts: []garden.BindMount{{
						SrcPath: srcPath,
						DstPath: dstPath,
					}},
					Network: fmt.Sprintf("10.0.%d.0/24", GinkgoParallelNode()),
				})
			Expect(err).NotTo(HaveOccurred())
		})

		AfterEach(func() {
			Expect(client.DestroyAndStop()).To(Succeed())
			Expect(os.RemoveAll(srcPath)).To(Succeed())
		})

		It("allows all users to read files", func() {
			readProcess := containerReadFile(container, "/home/alice", "afile", "alice")
			Expect(readProcess.Wait()).To(Equal(0))
		})

		Context("and the file is a bind mount", func() {
			BeforeEach(func() {
				var cmd *exec.Cmd
				cmd = exec.Command("mount", "--bind", srcPath, srcPath)
				Expect(cmd.Run()).To(Succeed())
			})

			AfterEach(func() {
				unmount(srcPath)
			})

			It("allows all users to read files", func() {
				readProcess := containerReadFile(container, "/home/alice", "afile", "alice")
				Expect(readProcess.Wait()).To(Equal(0))
			})
		})
	})

	Describe("When source is a dir", func() {
		var (
			privilegedContainer bool
			tmpfsPath           string
			useSymlink          bool
			bindMountMode       garden.BindMountMode   // bm: RO or RW
			bindMountOrigin     garden.BindMountOrigin // bm: Container or Host
			mountOptions        []string

			// pre-existing file for permissions testing
			testFileName string
		)

		BeforeEach(func() {
			privilegedContainer = false
			container = nil
			tmpfsPath = ""
			srcPath = ""
			dstPath = ""
			bindMountMode = garden.BindMountModeRO
			bindMountOrigin = garden.BindMountOriginHost
			testFileName = ""
			mountOptions = []string{"--bind"}
			bindMountOrigin = garden.BindMountOriginHost
		})

		JustBeforeEach(func() {
			srcPath, tmpfsPath, testFileName = createTestHostDirAndTestFile(mountOptions, useSymlink)
			client = runner.Start(config)

			var err error
			container, err = client.Create(
				garden.ContainerSpec{
					Privileged: privilegedContainer,
					BindMounts: []garden.BindMount{{
						SrcPath: srcPath,
						DstPath: dstPath,
						Mode:    bindMountMode,
						Origin:  bindMountOrigin,
					}},
					Network: fmt.Sprintf("10.0.%d.0/24", GinkgoParallelNode()),
				})
			Expect(err).NotTo(HaveOccurred())
		})

		AfterEach(func() {
			Expect(client.DestroyAndStop()).To(Succeed())

			unmount(srcPath)
			Expect(os.RemoveAll(srcPath)).To(Succeed())<commit_msg>Reset useSymlink to false in bind mounts test<commit_after>package gqt_test

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"

	"code.cloudfoundry.org/garden"
	"code.cloudfoundry.org/guardian/gqt/runner"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Bind mount", func() {
	var (
		client    *runner.RunningGarden
		container garden.Container
		srcPath   string
		dstPath   string
	)

	Describe("When source is a file", func() {
		BeforeEach(func() {
			tempFile, err := ioutil.TempFile("/opt", "bindmount-srcpath-file-")
			Expect(err).NotTo(HaveOccurred())
			defer tempFile.Close()
			srcPath = tempFile.Name()
			Expect(os.Chmod(srcPath, 0777)).To(Succeed())
			dstPath = "/home/alice/afile"
		})

		JustBeforeEach(func() {
			var err error
			client = runner.Start(config)
			container, err = client.Create(
				garden.ContainerSpec{
					BindMounts: []garden.BindMount{{
						SrcPath: srcPath,
						DstPath: dstPath,
					}},
					Network: fmt.Sprintf("10.0.%d.0/24", GinkgoParallelNode()),
				})
			Expect(err).NotTo(HaveOccurred())
		})

		AfterEach(func() {
			Expect(client.DestroyAndStop()).To(Succeed())
			Expect(os.RemoveAll(srcPath)).To(Succeed())
		})

		It("allows all users to read files", func() {
			readProcess := containerReadFile(container, "/home/alice", "afile", "alice")
			Expect(readProcess.Wait()).To(Equal(0))
		})

		Context("and the file is a bind mount", func() {
			BeforeEach(func() {
				var cmd *exec.Cmd
				cmd = exec.Command("mount", "--bind", srcPath, srcPath)
				Expect(cmd.Run()).To(Succeed())
			})

			AfterEach(func() {
				unmount(srcPath)
			})

			It("allows all users to read files", func() {
				readProcess := containerReadFile(container, "/home/alice", "afile", "alice")
				Expect(readProcess.Wait()).To(Equal(0))
			})
		})
	})

	Describe("When source is a dir", func() {
		var (
			privilegedContainer bool
			tmpfsPath           string
			useSymlink          bool
			bindMountMode       garden.BindMountMode   // bm: RO or RW
			bindMountOrigin     garden.BindMountOrigin // bm: Container or Host
			mountOptions        []string

			// pre-existing file for permissions testing
			testFileName string
		)

		BeforeEach(func() {
			privilegedContainer = false
			container = nil
			tmpfsPath = ""
			srcPath = ""
			dstPath = ""
			bindMountMode = garden.BindMountModeRO
			bindMountOrigin = garden.BindMountOriginHost
			testFileName = ""
			useSymlink = false
			mountOptions = []string{"--bind"}
			bindMountOrigin = garden.BindMountOriginHost
		})

		JustBeforeEach(func() {
			srcPath, tmpfsPath, testFileName = createTestHostDirAndTestFile(mountOptions, useSymlink)
			client = runner.Start(config)

			var err error
			container, err = client.Create(
				garden.ContainerSpec{
					Privileged: privilegedContainer,
					BindMounts: []garden.BindMount{{
						SrcPath: srcPath,
						DstPath: dstPath,
						Mode:    bindMountMode,
						Origin:  bindMountOrigin,
					}},
					Network: fmt.Sprintf("10.0.%d.0/24", GinkgoParallelNode()),
				})
			Expect(err).NotTo(HaveOccurred())
		})

		AfterEach(func() {
			Expect(client.DestroyAndStop()).To(Succeed())

			unmount(srcPath)
			Expect(os.RemoveAll(srcPath)).To(Succeed())
{'fraction_non_alphanumeric': 0.1432356608478803, 'fraction_numerical': 0.005610972568578554, 'mean_word_length': 3.3712534059945503, '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': '317433', 'n_tokens_mistral': 2616, 'n_tokens_neox': 2164, 'n_words': 408}