1package houdini_test
2
3import (
4	"io"
5
6	"code.cloudfoundry.org/garden"
7
8	. "github.com/onsi/ginkgo"
9	. "github.com/onsi/gomega"
10)
11
12var _ = Describe("Container", func() {
13	var container garden.Container
14
15	BeforeEach(func() {
16		var err error
17		container, err = backend.Create(garden.ContainerSpec{})
18		Expect(err).ToNot(HaveOccurred())
19	})
20
21	AfterEach(func() {
22		err := backend.Destroy(container.Handle())
23		Expect(err).ToNot(HaveOccurred())
24	})
25
26	Describe("Streaming", func() {
27		Context("between containers", func() {
28			var destinationContainer garden.Container
29
30			BeforeEach(func() {
31				var err error
32				destinationContainer, err = backend.Create(garden.ContainerSpec{})
33				Expect(err).ToNot(HaveOccurred())
34			})
35
36			AfterEach(func() {
37				err := backend.Destroy(destinationContainer.Handle())
38				Expect(err).ToNot(HaveOccurred())
39			})
40
41			It("can transfer between containers", func() {
42				process, err := container.Run(garden.ProcessSpec{
43					Path: "sh",
44					Args: []string{
45						"-exc",
46						`
47							touch a
48							touch b
49							mkdir foo/
50							touch foo/in-foo-a
51							touch foo/in-foo-b
52						`,
53					},
54				}, garden.ProcessIO{
55					Stdout: GinkgoWriter,
56					Stderr: GinkgoWriter,
57				})
58				Expect(err).ToNot(HaveOccurred())
59				Expect(process.Wait()).To(Equal(0))
60
61				out, err := container.StreamOut(garden.StreamOutSpec{
62					Path: ".",
63				})
64				Expect(err).ToNot(HaveOccurred())
65
66				err = destinationContainer.StreamIn(garden.StreamInSpec{
67					Path:      ".",
68					TarStream: out,
69				})
70				Expect(err).ToNot(HaveOccurred())
71
72				nothing := make([]byte, 1)
73				n, err := out.Read(nothing)
74				Expect(n).To(Equal(0))
75				Expect(err).To(Equal(io.EOF))
76
77				checkTree, err := destinationContainer.Run(garden.ProcessSpec{
78					Path: "sh",
79					Args: []string{
80						"-exc",
81						`
82							find .
83							test -e a
84							test -e b
85							test -e foo/in-foo-a
86							test -e foo/in-foo-b
87						`,
88					},
89				}, garden.ProcessIO{
90					Stdout: GinkgoWriter,
91					Stderr: GinkgoWriter,
92				})
93				Expect(err).ToNot(HaveOccurred())
94				Expect(checkTree.Wait()).To(Equal(0))
95			})
96
97			It("treates a trailing slash as /.", func() {
98				process, err := container.Run(garden.ProcessSpec{
99					Path: "sh",
100					Args: []string{
101						"-exc",
102						`
103							touch a
104							touch b
105							mkdir foo/
106							touch foo/in-foo-a
107							touch foo/in-foo-b
108						`,
109					},
110				}, garden.ProcessIO{
111					Stdout: GinkgoWriter,
112					Stderr: GinkgoWriter,
113				})
114				Expect(err).ToNot(HaveOccurred())
115				Expect(process.Wait()).To(Equal(0))
116
117				out, err := container.StreamOut(garden.StreamOutSpec{
118					Path: "foo/",
119				})
120				Expect(err).ToNot(HaveOccurred())
121
122				err = destinationContainer.StreamIn(garden.StreamInSpec{
123					Path:      ".",
124					TarStream: out,
125				})
126				Expect(err).ToNot(HaveOccurred())
127
128				nothing := make([]byte, 1)
129				n, err := out.Read(nothing)
130				Expect(n).To(Equal(0))
131				Expect(err).To(Equal(io.EOF))
132
133				checkTree, err := destinationContainer.Run(garden.ProcessSpec{
134					Path: "sh",
135					Args: []string{
136						"-exc",
137						`
138							find .
139							test -e in-foo-a
140							test -e in-foo-b
141						`,
142					},
143				}, garden.ProcessIO{
144					Stdout: GinkgoWriter,
145					Stderr: GinkgoWriter,
146				})
147				Expect(err).ToNot(HaveOccurred())
148				Expect(checkTree.Wait()).To(Equal(0))
149			})
150		})
151	})
152})
153