func readFile(container garden.Container, dstPath, fileName, user string) garden.Process { filePath := filepath.Join(dstPath, fileName) process, err := container.Run(garden.ProcessSpec{ Path: "cat", Args: []string{filePath}, User: user, }, garden.ProcessIO{}) Expect(err).ToNot(HaveOccurred()) return process }
func runCommand(container garden.Container, path string, args []string) { proc, err := container.Run( garden.ProcessSpec{ Path: path, Args: args, }, ginkgoIO) Expect(err).NotTo(HaveOccurred()) exitCode, err := proc.Wait() Expect(err).NotTo(HaveOccurred()) Expect(exitCode).To(Equal(0)) }
func listenInContainer(container garden.Container, containerPort uint32) error { _, err := container.Run(garden.ProcessSpec{ User: "******", Path: "sh", Args: []string{"-c", fmt.Sprintf("echo %d | nc -l -p %d", containerPort, containerPort)}, }, garden.ProcessIO{ Stdout: GinkgoWriter, Stderr: GinkgoWriter, }) Expect(err).ToNot(HaveOccurred()) return err }
func createUser(container garden.Container, username string) { if container == nil { return } process, err := container.Run(garden.ProcessSpec{ User: "******", Path: "sh", Args: []string{"-c", fmt.Sprintf("id -u %s || adduser -D %s", username, username)}, }, garden.ProcessIO{ Stdout: GinkgoWriter, Stderr: GinkgoWriter, }) Expect(err).ToNot(HaveOccurred()) Expect(process.Wait()).To(Equal(0)) }
func writeFile(container garden.Container, dstPath, user string) garden.Process { // try to write a new file filePath := filepath.Join(dstPath, "checkFileAccess-file") process, err := container.Run(garden.ProcessSpec{ Path: "touch", Args: []string{filePath}, User: user, }, garden.ProcessIO{ Stderr: GinkgoWriter, Stdout: GinkgoWriter, }) Expect(err).ToNot(HaveOccurred()) return process }
func ethInterfaceName(container garden.Container) string { buffer := gbytes.NewBuffer() proc, err := container.Run( garden.ProcessSpec{ Path: "sh", Args: []string{"-c", "ifconfig | grep 'Ethernet' | cut -f 1 -d ' '"}, User: "******", }, garden.ProcessIO{ Stdout: buffer, Stderr: GinkgoWriter, }, ) Expect(err).NotTo(HaveOccurred()) Expect(proc.Wait()).To(Equal(0)) contIfaceName := string(buffer.Contents()) // g3-abc-1 return contIfaceName[:len(contIfaceName)-2] + "0" // g3-abc-0 }
func checkConnection(container garden.Container, ip string, port int) error { process, err := container.Run(garden.ProcessSpec{ User: "******", Path: "sh", Args: []string{"-c", fmt.Sprintf("echo hello | nc -w1 %s %d", ip, port)}, }, garden.ProcessIO{Stdout: GinkgoWriter, Stderr: GinkgoWriter}) if err != nil { return err } exitCode, err := process.Wait() if err != nil { return err } if exitCode == 0 { return nil } else { return fmt.Errorf("Request failed. Process exited with code %d", exitCode) } }
"docker:///cfgarden/empty:v0.1.0", fmt.Sprintf("non-quotaed-container-%d", GinkgoParallelNode()), })) }) It("executes plugin create as the container user", func() { maxId := uint32(sysinfo.Min(sysinfo.MustGetMaxValidUID(), sysinfo.MustGetMaxValidGID())) whoamiOutput, err := ioutil.ReadFile(filepath.Join(imagePath, "create-whoami")) Expect(err).ToNot(HaveOccurred()) Expect(string(whoamiOutput)).To(ContainSubstring(fmt.Sprintf("%d - %d", maxId, maxId))) }) It("loads the image.json env variables", func() { buffer := gbytes.NewBuffer() process, err := container.Run(garden.ProcessSpec{ Path: "/env", Dir: "/", }, garden.ProcessIO{Stdout: buffer, Stderr: buffer}) Expect(err).NotTo(HaveOccurred()) exitCode, err := process.Wait() Expect(err).NotTo(HaveOccurred()) Expect(exitCode).To(BeZero()) Eventually(buffer).Should(gbytes.Say("BLA=BLE")) Eventually(buffer).Should(gbytes.Say("HELLO=world")) }) It("executes plugin delete with the correct args", func() { Expect(client.Destroy(container.Handle())).To(Succeed()) userArgsStr, err := ioutil.ReadFile(filepath.Join(imagePath, "delete-args")) Expect(err).ToNot(HaveOccurred())
BeforeEach(func() { client = startGarden() var err error container, err = client.Create(garden.ContainerSpec{}) Expect(err).NotTo(HaveOccurred()) }) DescribeTable("contains the correct values", func(user, path string, env []string) { out := gbytes.NewBuffer() proc, err := container.Run( garden.ProcessSpec{ Path: "sh", Args: []string{"-c", "echo $PATH"}, User: user, Env: env, }, garden.ProcessIO{ Stdout: io.MultiWriter(GinkgoWriter, out), Stderr: io.MultiWriter(GinkgoWriter, out), }) Expect(err).NotTo(HaveOccurred()) exitCode, err := proc.Wait() Expect(err).NotTo(HaveOccurred()) Expect(exitCode).To(Equal(0)) Expect(out).To(gbytes.Say(path)) }, Entry("for a non-root user", "alice", `^/usr/local/bin:/usr/bin:/bin\n$`, []string{}),
Networks: []garden.IPRange{ garden.IPRangeFromIP(net.ParseIP("8.8.8.8")), }, }) info, err := container.Info() Expect(err).NotTo(HaveOccurred()) externalIP = info.ExternalIP interfacePrefix = info.Properties["kawasaki.iptable-prefix"] out := gbytes.NewBuffer() existingProc, err = container.Run( garden.ProcessSpec{ Path: "/bin/sh", Args: []string{"-c", "while true; do echo hello; sleep 1; done;"}, }, garden.ProcessIO{ Stdout: io.MultiWriter(GinkgoWriter, out), Stderr: io.MultiWriter(GinkgoWriter, out), }) Expect(err).NotTo(HaveOccurred()) if gracefulShutdown { Expect(client.Stop()).To(Succeed()) } else { Expect(client.Kill()).To(MatchError("exit status 137")) } if len(restartArgs) == 0 { restartArgs = args }
return process, nil } spec := garden.ProcessSpec{ Path: "some-script", } stdout := gbytes.NewBuffer() stderr := gbytes.NewBuffer() processIO := garden.ProcessIO{ Stdout: stdout, Stderr: stderr, } process, err := container.Run(spec, processIO) Ω(err).ShouldNot(HaveOccurred()) ranHandle, ranSpec, ranIO := fakeConnection.RunArgsForCall(0) Ω(ranHandle).Should(Equal("some-handle")) Ω(ranSpec).Should(Equal(spec)) Ω(ranIO).Should(Equal(processIO)) Ω(process.ID()).Should(Equal("process-handle")) status, err := process.Wait() Ω(err).ShouldNot(HaveOccurred()) Ω(status).Should(Equal(123)) Eventually(stdout).Should(gbytes.Say("stdout data")) Eventually(stderr).Should(gbytes.Say("stderr data"))
Expect(container.StreamIn(garden.StreamInSpec{ Path: "/root/test", User: "******", TarStream: tarStream, })).To(Succeed()) Expect(container).To(HaveFile("/root/test/some-temp-dir")) Expect(container).To(HaveFile("/root/test/some-temp-dir/some-temp-file")) }) }) Describe("StreamOut", func() { BeforeEach(func() { process, err := container.Run(garden.ProcessSpec{ User: "******", Path: "sh", Args: []string{"-c", "mkdir -p /root/documents/some/reports && echo hello > /root/documents/some/reports/test"}, }, ginkgoIO) Expect(err).NotTo(HaveOccurred()) statusCode, err := process.Wait() Expect(err).NotTo(HaveOccurred()) Expect(statusCode).To(Equal(0)) }) It("should stream out the files", func() { tarStream, err := container.StreamOut(garden.StreamOutSpec{ Path: "/root/documents/some/reports", User: "******", })
It("should create a depot subdirectory based on the container handle", func() { Expect(container.Handle()).NotTo(BeEmpty()) Expect(filepath.Join(client.DepotDir, container.Handle())).To(BeADirectory()) Expect(filepath.Join(client.DepotDir, container.Handle(), "config.json")).To(BeARegularFile()) }) It("should lookup the right container", func() { lookupContainer, lookupError := client.Lookup(container.Handle()) Expect(lookupError).NotTo(HaveOccurred()) Expect(lookupContainer).To(Equal(container)) }) It("should not leak pipes", func() { process, err := container.Run(garden.ProcessSpec{Path: "echo", Args: []string{"hello"}}, garden.ProcessIO{}) Expect(err).NotTo(HaveOccurred()) Expect(process.Wait()).To(Equal(0)) Expect(client.Destroy(container.Handle())).To(Succeed()) container = nil // avoid double-destroying Eventually(func() int { return numPipes(client.Pid) }).Should(Equal(initialPipes)) }) It("should not leak sockets", func() { Expect(client.Destroy(container.Handle())).To(Succeed()) container = nil // avoid double-destroying Eventually(func() int { return numOpenSockets(client.Pid) }).Should(Equal(initialSockets))
if fuseRootfs == "" { Skip("GARDEN_FUSE_TEST_ROOTFS not defined, skipping") } var err error client = startGarden() container, err = client.Create(garden.ContainerSpec{ RootFSPath: fuseRootfs, Privileged: true, }) Expect(err).NotTo(HaveOccurred()) }) AfterEach(func() { Expect(client.DestroyAndStop()).To(Succeed()) }) It("can mount fuse in the container", func() { stdout := gbytes.NewBuffer() sess, err := container.Run(garden.ProcessSpec{ Path: "sh", Args: []string{ "-c", `mkdir -p /tmp/fusetest && /usr/bin/hellofs /tmp/fusetest; cat /tmp/fusetest/hello`, }, }, garden.ProcessIO{Stdout: io.MultiWriter(stdout, GinkgoWriter), Stderr: GinkgoWriter}) Expect(err).NotTo(HaveOccurred()) Expect(sess.Wait()).To(Equal(0)) Expect(stdout).To(gbytes.Say("Hello World!")) }) })
container, err = client.Create(garden.ContainerSpec{ Network: containerNetwork, Properties: extraProperties, }) Expect(err).NotTo(HaveOccurred()) }) AfterEach(func() { Expect(client.DestroyAndStop()).To(Succeed()) }) It("should have a loopback interface", func() { buffer := gbytes.NewBuffer() proc, err := container.Run( garden.ProcessSpec{ Path: "ifconfig", User: "******", }, garden.ProcessIO{Stdout: io.MultiWriter(GinkgoWriter, buffer), Stderr: GinkgoWriter}, ) Expect(err).NotTo(HaveOccurred()) Expect(proc.Wait()).To(Equal(0)) Expect(buffer).To(gbytes.Say("lo")) }) It("should have a (dynamically assigned) IP address", func() { buffer := gbytes.NewBuffer() proc, err := container.Run( garden.ProcessSpec{ Path: "ifconfig", User: "******", }, garden.ProcessIO{Stdout: io.MultiWriter(GinkgoWriter, buffer), Stderr: io.MultiWriter(GinkgoWriter, buffer)},