Skip to content

Commit c705798

Browse files
yulianedyalkovaKieron Brownegcapizzi
committed
Refactoring
Co-authored-by: Kieron Browne <[email protected]> Co-authored-by: Julia Nedialkova <[email protected]> Co-authored-by: Giuseppe Capizzi <[email protected]>
1 parent 4918a1b commit c705798

17 files changed

+575
-680
lines changed

bpmchecker/bpmchecker.go

-31
This file was deleted.

bpmchecker/bpmchecker_suite_test.go

-13
This file was deleted.

bpmchecker/bpmchecker_test.go

-87
This file was deleted.

collectors/command/command.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package command
2+
3+
import (
4+
"context"
5+
"errors"
6+
"io"
7+
"io/ioutil"
8+
"os"
9+
"os/exec"
10+
"path/filepath"
11+
)
12+
13+
type Collector struct {
14+
cmd string
15+
filename string
16+
}
17+
18+
func New(cmd, filename string) Collector {
19+
return Collector{cmd: cmd, filename: filename}
20+
}
21+
22+
func (c Collector) Run(ctx context.Context, destPath string, stdout io.Writer) error {
23+
cmd := exec.CommandContext(ctx, "sh", "-c", c.cmd)
24+
out, err := cmd.Output()
25+
26+
if ctx.Err() == context.DeadlineExceeded {
27+
return ctx.Err()
28+
}
29+
30+
if exitErr, ok := err.(*exec.ExitError); ok {
31+
return errors.New(string(exitErr.Stderr))
32+
}
33+
34+
if err != nil {
35+
return err
36+
}
37+
38+
outPath := filepath.Join(destPath, c.filename)
39+
40+
err = os.MkdirAll(filepath.Dir(outPath), 0755)
41+
if err != nil {
42+
return err
43+
}
44+
45+
err = ioutil.WriteFile(outPath, out, 0644)
46+
if err != nil {
47+
return err
48+
}
49+
50+
_, err = stdout.Write(out)
51+
if err != nil {
52+
return err
53+
}
54+
55+
return nil
56+
}

collectors/command/command_test.go

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)