-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathvagrant_test.go
102 lines (81 loc) · 2.09 KB
/
vagrant_test.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// +build vagrant
package remotessh
import (
"os"
"strings"
"sync"
. "testing"
. "github.com/contiv/check"
log "github.com/Sirupsen/logrus"
)
type vagrantTestSuite struct {
vagrant Testbed
}
var _ = Suite(&vagrantTestSuite{})
func TestVagrant(t *T) {
if os.Getenv("HOST_TEST") != "" {
os.Exit(0)
}
log.SetLevel(log.DebugLevel)
TestingT(t)
}
func (v *vagrantTestSuite) SetUpSuite(c *C) {
vagrant := &Vagrant{}
c.Assert(vagrant.Setup(false, []string{}, 3), IsNil)
v.vagrant = vagrant
}
func (v *vagrantTestSuite) TestRunEnv(c *C) {
vagrant := &Vagrant{}
c.Assert(vagrant.Setup(false, []string{"MYENV=foo"}, 3), IsNil)
outChan := make(chan string, 3)
c.Assert(vagrant.IterateNodes(func(node TestbedNode) error {
out, err := node.RunCommandWithOutput("echo $MYENV")
outChan <- out
return err
}), IsNil)
for x := 0; x < 3; x++ {
c.Assert(strings.TrimSpace(<-outChan), Equals, "foo")
}
}
func (v *vagrantTestSuite) TestSetupInvalidArgs(c *C) {
vagrant := &Vagrant{}
c.Assert(vagrant.Setup(1, "foo"), ErrorMatches, "unexpected args to Setup.*Expected:.*Received:.*")
}
func (v *vagrantTestSuite) TestRun(c *C) {
for _, node := range v.vagrant.GetNodes() {
c.Assert(node.RunCommand("ls"), IsNil)
}
for _, node := range v.vagrant.GetNodes() {
c.Assert(node.RunCommand("exit 1"), NotNil)
}
}
func (v *vagrantTestSuite) TestRunWithOutput(c *C) {
for _, node := range v.vagrant.GetNodes() {
out, err := node.RunCommandWithOutput("whoami")
c.Assert(err, IsNil)
c.Assert(strings.TrimSpace(out), Equals, "vagrant")
}
for _, node := range v.vagrant.GetNodes() {
_, err := node.RunCommandWithOutput("exit 1")
c.Assert(err, NotNil)
}
}
func (v *vagrantTestSuite) TestIterateNodes(c *C) {
mutex := &sync.Mutex{}
var i int
c.Assert(v.vagrant.IterateNodes(func(node TestbedNode) error {
mutex.Lock()
i++
mutex.Unlock()
return node.RunCommand("exit 0")
}), IsNil)
c.Assert(i, Equals, 3)
i = 0
c.Assert(v.vagrant.IterateNodes(func(node TestbedNode) error {
mutex.Lock()
i++
mutex.Unlock()
return node.RunCommand("exit 1")
}), NotNil)
c.Assert(i, Equals, 3)
}