func (step *taskStep) ensureBuildDirExists(container garden.Container) error { emptyTar := new(bytes.Buffer) err := tar.NewWriter(emptyTar).Close() if err != nil { return err } err = container.StreamIn(garden.StreamInSpec{ Path: step.artifactsRoot, TarStream: emptyTar, }) if err != nil { return err } return nil }
func createContainerDir(container garden.Container, dir string) error { emptyTar := new(bytes.Buffer) err := tar.NewWriter(emptyTar).Close() if err != nil { return err } err = container.StreamIn(garden.StreamInSpec{ Path: dir, TarStream: emptyTar, }) if err != nil { return err } return nil }
func StreamIn(c garden.Container) error { tarFile, err := os.Open("../../greenhouse-security-fixtures/output/SecurityFixtures.tgz") Expect(err).ShouldNot(HaveOccurred()) defer tarFile.Close() return c.StreamIn(garden.StreamInSpec{Path: "bin", TarStream: tarFile}) }
BeforeEach(func() { server.AppendHandlers( ghttp.CombineHandlers( ghttp.VerifyRequest("PUT", "/api/containers/containerhandle/files", "destination=a/path"), func(w http.ResponseWriter, req *http.Request) { body, err := ioutil.ReadAll(req.Body) req.Body.Close() Expect(err).ShouldNot(HaveOccurred()) Expect(string(body)).Should(Equal("stuff")) }, ), ) }) It("makes a call out to an external service", func() { err := container.StreamIn(garden.StreamInSpec{Path: "a/path", TarStream: strings.NewReader("stuff")}) Expect(err).NotTo(HaveOccurred()) Expect(server.ReceivedRequests()).Should(HaveLen(1)) }) }) Context("Http PUT failure request", func() { BeforeEach(func() { server.AppendHandlers( ghttp.CombineHandlers( ghttp.VerifyRequest("PUT", "/api/containers/containerhandle/files", "destination=a/path"), ghttp.RespondWith(500, ``), func(w http.ResponseWriter, req *http.Request) { req.Body.Close() }, ), )
c, err = client.Create(garden.ContainerSpec{}) Expect(err).ToNot(HaveOccurred()) }) AfterEach(func() { err := client.Destroy(c.Handle()) Expect(err).ShouldNot(HaveOccurred()) }) Describe("process", func() { It("pid is returned", func() { tarFile, err := os.Open("../bin/consume.tar") Expect(err).ShouldNot(HaveOccurred()) defer tarFile.Close() err = c.StreamIn(garden.StreamInSpec{Path: "bin", TarStream: tarFile}) Expect(err).ShouldNot(HaveOccurred()) buf := make([]byte, 0, 1024*1024) stdout := bytes.NewBuffer(buf) process, err := c.Run(garden.ProcessSpec{ Path: "bin/consume.exe", Args: []string{"64"}, }, garden.ProcessIO{Stdout: stdout}) Expect(err).ShouldNot(HaveOccurred()) Expect(process.ID()).ToNot(Equal("0")) }) It("can be signaled", func(done Done) { for _, f := range []string{"../bin/loop.tar", "../bin/launcher.tar"} {
Describe("StreamIn", func() { It("sends a stream in request", func() { fakeConnection.StreamInStub = func(handle string, spec garden.StreamInSpec) error { Ω(spec.Path).Should(Equal("to")) Ω(spec.User).Should(Equal("frank")) content, err := ioutil.ReadAll(spec.TarStream) Ω(err).ShouldNot(HaveOccurred()) Ω(string(content)).Should(Equal("stuff")) return nil } err := container.StreamIn(garden.StreamInSpec{ User: "******", Path: "to", TarStream: bytes.NewBufferString("stuff"), }) Ω(err).ShouldNot(HaveOccurred()) }) Context("when streaming in fails", func() { disaster := errors.New("oh no!") BeforeEach(func() { fakeConnection.StreamInReturns( disaster) }) It("returns the error", func() { err := container.StreamIn(garden.StreamInSpec{
Context("when the containerizer fails to run a process", func() { BeforeEach(func() { containerizer.RunReturns(nil, errors.New("lost my banana")) }) It("returns the error", func() { _, err := container.Run(garden.ProcessSpec{}, garden.ProcessIO{}) Expect(err).To(MatchError("lost my banana")) }) }) }) Describe("streaming files in to the container", func() { It("asks the containerizer to stream in the tar stream", func() { spec := garden.StreamInSpec{Path: "potato", User: "******", TarStream: gbytes.NewBuffer()} Expect(container.StreamIn(spec)).To(Succeed()) _, handle, specArg := containerizer.StreamInArgsForCall(0) Expect(handle).To(Equal("banana")) Expect(specArg).To(Equal(spec)) }) }) Describe("streaming files outside the container", func() { It("asks the containerizer to stream out the files", func() { spec := garden.StreamOutSpec{Path: "potato", User: "******"} _, err := container.StreamOut(spec) Expect(err).To(Succeed()) _, handle, specArg := containerizer.StreamOutArgsForCall(0) Expect(handle).To(Equal("banana"))
}) }) Describe("StreamIn", func() { It("sends a stream in request", func() { fakeConnection.StreamInStub = func(handle string, dst string, reader io.Reader) error { Ω(dst).Should(Equal("to")) content, err := ioutil.ReadAll(reader) Ω(err).ShouldNot(HaveOccurred()) Ω(string(content)).Should(Equal("stuff")) return nil } err := container.StreamIn("to", bytes.NewBufferString("stuff")) Ω(err).ShouldNot(HaveOccurred()) }) Context("when streaming in fails", func() { disaster := errors.New("oh no!") BeforeEach(func() { fakeConnection.StreamInReturns( disaster) }) It("returns the error", func() { err := container.StreamIn("to", nil) Ω(err).Should(Equal(disaster)) })
func StreamIn(c garden.Container) error { tarFile, err := os.Open("../bin/consume.tgz") Expect(err).ShouldNot(HaveOccurred()) defer tarFile.Close() return c.StreamIn(garden.StreamInSpec{Path: "bin", TarStream: tarFile}) }
}) }) }) }) Describe("streaming in", func() { It("streams the file in, waits for completion, and succeeds", func() { data := bytes.NewBufferString("chunk-1;chunk-2;chunk-3;") fakeContainer.StreamInStub = func(dest string, stream io.Reader) error { Ω(dest).Should(Equal("/dst/path")) Ω(ioutil.ReadAll(stream)).Should(Equal([]byte("chunk-1;chunk-2;chunk-3;"))) return nil } err := container.StreamIn("/dst/path", data) Ω(err).ShouldNot(HaveOccurred()) Ω(fakeContainer.StreamInCallCount()).Should(Equal(1)) }) itFailsWhenTheContainerIsNotFound(func() error { return container.StreamIn("/dst/path", nil) }) Context("when copying in to the container fails", func() { BeforeEach(func() { fakeContainer.StreamInReturns(errors.New("oh no!")) }) It("fails", func() {
out, err := container.StreamOut(garden.StreamOutSpec{ Path: ".", }) Expect(err).ToNot(HaveOccurred()) outBytes, err := ioutil.ReadAll(out) data := ioutil.NopCloser(bytes.NewReader(outBytes)) Expect(backend.Destroy(container.Handle())).To(Succeed()) destinationContainer, err = backend.Create(garden.ContainerSpec{}) Expect(err).ToNot(HaveOccurred()) err = destinationContainer.StreamIn(garden.StreamInSpec{ Path: ".", TarStream: data, }) Expect(err).ToNot(HaveOccurred()) nothing := make([]byte, 1) n, err := out.Read(nothing) Expect(n).To(Equal(0)) Expect(err).To(Equal(io.EOF)) checkTree, err := destinationContainer.Run(garden.ProcessSpec{ Path: "sh", Args: []string{ "-exc", ` find . test -e a
func StreamToDestination(c garden.Container, destPath string) error { tarFile, err := os.Open("../bin/consume.tar") Expect(err).ShouldNot(HaveOccurred()) defer tarFile.Close() return c.StreamIn(garden.StreamInSpec{Path: destPath, TarStream: tarFile}) }
Body: "some-body", }, }, ) tgz, err := os.Open(tgzPath) Expect(err).ToNot(HaveOccurred()) tarStream, err = gzip.NewReader(tgz) Expect(err).ToNot(HaveOccurred()) }) It("should stream in the files", func() { 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)