-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgossh_test.go
115 lines (103 loc) · 3.92 KB
/
gossh_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
103
104
105
106
107
108
109
110
111
112
113
114
115
package gossh_test
import (
. "github.com/dcapwell/gossh"
"github.com/dcapwell/gossh/workpool"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"log"
)
var _ = Describe("Gossh", func() {
Describe("multi hosts", func() {
ssh := NewSsh()
Describe("run multiple ssh commands concurrently", func() {
Context("with one host", func() {
Context("and valid command", func() {
rsp, err := ssh.Run([]string{"localhost"}, "date -u", Options{})
data := chanToSlize(rsp.Responses)
It("should succeed", func() {
Expect(err).To(BeNil())
})
It("with one result", func() {
Expect(data).To(HaveLen(1))
Expect(data[0].Hostname).To(Equal("localhost"))
Expect(data[0].Response.Code).To(Equal(workpool.SUCCESS))
Expect(data[0].Response.Stdout).To(ContainSubstring("UTC"))
})
})
Context("with invalid command", func() {
rsp, err := ssh.Run([]string{"localhost"}, "thiscmdreallyshouldntexist", Options{})
data := chanToSlize(rsp.Responses)
It("should not return error", func() {
Expect(err).To(BeNil())
})
It("should have only one response", func() {
Expect(data).To(HaveLen(1))
})
It("should have response from host", func() {
localRsp := data[0]
Expect(localRsp.Hostname).To(Equal("localhost"))
})
It("should have failed", func() {
localRsp := data[0]
Expect(localRsp.Response).ShouldNot(BeNil())
Expect(localRsp.Response.Code).To(BeGreaterThan(0))
})
})
})
Context("with mulitple hosts", func() {
Context("with valid command", func() {
rsp, err := ssh.Run([]string{"localhost", "localhost"}, "date -u", Options{})
data := chanToSlize(rsp.Responses)
It("should not return error", func() {
Expect(err).To(BeNil())
})
It("should have only two response", func() {
Expect(data).To(HaveLen(2))
})
It("should have response from host", func() {
localRsp := data[0]
Expect(localRsp.Hostname).To(Equal("localhost"))
localRsp = data[1]
Expect(localRsp.Hostname).To(Equal("localhost"))
})
It("should have successed", func() {
Expect(data[0].Response.Code).To(Equal(workpool.SUCCESS))
Expect(data[0].Response.Stdout).To(ContainSubstring("UTC"))
Expect(data[1].Response.Code).To(Equal(workpool.SUCCESS))
Expect(data[1].Response.Stdout).To(ContainSubstring("UTC"))
})
})
Context("with invalid command", func() {
rsp, err := ssh.Run([]string{"localhost", "localhost"}, "thiscmdreallyshouldntexist", Options{})
data := chanToSlize(rsp.Responses)
log.Printf("Response from multi requests, single host, invalid cmd: %v\n", rsp)
It("should not return error", func() {
Expect(err).To(BeNil())
})
It("should have only two response", func() {
Expect(data).To(HaveLen(2))
})
It("should have response from host", func() {
Expect(data[0].Hostname).To(Equal("localhost"))
Expect(data[1].Hostname).To(Equal("localhost"))
})
It("should have failed", func() {
localRsp := data[0]
Expect(localRsp.Response).ShouldNot(BeNil())
Expect(localRsp.Response.Code).To(BeGreaterThan(0))
localRsp = data[1]
Expect(localRsp.Response).ShouldNot(BeNil())
Expect(localRsp.Response.Code).To(BeGreaterThan(0))
})
})
})
})
})
})
func chanToSlize(ch chan SshResponseContext) []SshResponseContext {
data := make([]SshResponseContext, 0)
for cxt := range ch {
data = append(data, cxt)
}
return data
}