|
| 1 | +package command_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "io/ioutil" |
| 7 | + "path/filepath" |
| 8 | + "time" |
| 9 | + |
| 10 | + "code.cloudfoundry.org/dontpanic/collectors/command" |
| 11 | + . "github.com/onsi/ginkgo" |
| 12 | + . "github.com/onsi/gomega" |
| 13 | + "github.com/onsi/gomega/gbytes" |
| 14 | +) |
| 15 | + |
| 16 | +var _ = Describe("Command Runner", func() { |
| 17 | + var ( |
| 18 | + cmd string |
| 19 | + ctx context.Context |
| 20 | + cancel context.CancelFunc |
| 21 | + dstPath string |
| 22 | + stdout io.Writer |
| 23 | + filename string |
| 24 | + |
| 25 | + err error |
| 26 | + ) |
| 27 | + |
| 28 | + BeforeEach(func() { |
| 29 | + ctx = context.TODO() |
| 30 | + dstPath, err = ioutil.TempDir("", "") |
| 31 | + Expect(err).NotTo(HaveOccurred()) |
| 32 | + stdout = gbytes.NewBuffer() |
| 33 | + filename = "hello" |
| 34 | + }) |
| 35 | + |
| 36 | + JustBeforeEach(func() { |
| 37 | + err = command.New(cmd, filename).Run(ctx, dstPath, stdout) |
| 38 | + }) |
| 39 | + |
| 40 | + When("cmd is a simple executable", func() { |
| 41 | + BeforeEach(func() { |
| 42 | + cmd = "echo hello world" |
| 43 | + }) |
| 44 | + |
| 45 | + It("returns the byte output", func() { |
| 46 | + Expect(err).NotTo(HaveOccurred()) |
| 47 | + Expect(stdout).To(gbytes.Say("hello world\n")) |
| 48 | + |
| 49 | + fileContents, err := ioutil.ReadFile(filepath.Join(dstPath, filename)) |
| 50 | + Expect(err).NotTo(HaveOccurred()) |
| 51 | + Expect(fileContents).To(Equal([]byte("hello world\n"))) |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + When("cmd is a pipeline", func() { |
| 56 | + BeforeEach(func() { |
| 57 | + cmd = "seq 2 10 | wc -l" |
| 58 | + }) |
| 59 | + |
| 60 | + It("executes the pipeline", func() { |
| 61 | + Expect(err).NotTo(HaveOccurred()) |
| 62 | + Expect(stdout).To(gbytes.Say("9")) |
| 63 | + |
| 64 | + fileContents, err := ioutil.ReadFile(filepath.Join(dstPath, filename)) |
| 65 | + Expect(err).NotTo(HaveOccurred()) |
| 66 | + Expect(fileContents).To(Equal([]byte("9\n"))) |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + When("cmd exceeds time limit", func() { |
| 71 | + BeforeEach(func() { |
| 72 | + ctx, cancel = context.WithTimeout(context.Background(), time.Millisecond) |
| 73 | + cmd = "sleep 1" |
| 74 | + }) |
| 75 | + |
| 76 | + AfterEach(func() { |
| 77 | + cancel() |
| 78 | + }) |
| 79 | + |
| 80 | + It("times out", func() { |
| 81 | + Expect(err).To(Equal(context.DeadlineExceeded)) |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + When("command fails and has stdout and stderr", func() { |
| 86 | + BeforeEach(func() { |
| 87 | + cmd = "echo foo; echo bar >&2; exit 1" |
| 88 | + }) |
| 89 | + |
| 90 | + It("returns error containing bar", func() { |
| 91 | + Expect(err).To(MatchError(ContainSubstring("bar"))) |
| 92 | + Expect(filepath.Join(dstPath, filename)).NotTo(BeAnExistingFile()) |
| 93 | + Expect(stdout).NotTo(gbytes.Say("foo")) |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | +}) |
0 commit comments