-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathcontainer.go
74 lines (61 loc) · 1.67 KB
/
container.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package matchers
import (
"io"
"code.cloudfoundry.org/garden"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/types"
"fmt"
)
func HaveFile(expected interface{}) types.GomegaMatcher {
s, ok := expected.(string)
if !ok {
panic("HaveFile matcher expects a string")
}
return &haveFileMatcher{
expected: s,
}
}
type haveFileMatcher struct {
expected string
}
func (matcher *haveFileMatcher) Match(actual interface{}) (success bool, err error) {
container, ok := actual.(garden.Container)
if !ok {
return false, fmt.Errorf("HaveFile matcher expects an garden.Container")
}
out := gbytes.NewBuffer()
proc, err := container.Run(
garden.ProcessSpec{
Path: "ls",
Args: []string{matcher.expected},
},
garden.ProcessIO{
Stdout: io.MultiWriter(ginkgo.GinkgoWriter, out),
Stderr: io.MultiWriter(ginkgo.GinkgoWriter, out),
})
if err != nil {
return false, err
}
exitCode, err := proc.Wait()
if err != nil {
return false, err
}
if exitCode != 0 {
return false, nil
}
return true, nil
}
func (matcher *haveFileMatcher) FailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Container:\n\t%s\nDoes not have file:\n\t%s", matcher.containerHandle(actual), matcher.expected)
}
func (matcher *haveFileMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Container:\n\t%s\nHas file:\n\t%s\nWhen it shouldn't", matcher.containerHandle(actual), matcher.expected)
}
func (matcher *haveFileMatcher) containerHandle(actual interface{}) string {
container, ok := actual.(garden.Container)
if !ok {
panic("HaveFile matcher expects a string")
}
return container.Handle()
}