forked from JordanICollier/cfbackup
/
nfs_test.go
214 lines (176 loc) · 5.36 KB
/
nfs_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package cfbackup_test
import (
"bytes"
"fmt"
"io"
"strings"
. "github.com/pivotalservices/cfbackup"
"github.com/pivotalservices/gtils/command"
"github.com/pivotalservices/gtils/mock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
)
var _ = Describe("nfs", func() {
Describe("Import NFS Restore", func() {
var (
nfs *NFSBackup
buffer *gbytes.Buffer
controlString string = "test of local file"
err error
)
BeforeEach(func() {
err = nil
})
AfterEach(func() {
err = nil
})
Context("successful call to import", func() {
BeforeEach(func() {
lf := strings.NewReader(controlString)
buffer = gbytes.NewBuffer()
nfs = getNfs(buffer, &SuccessMockNFSExecuter{})
err = nfs.Import(lf)
})
It("should return nil error", func() {
Ω(err).Should(BeNil())
})
It("should write the local file contents to the remote", func() {
Ω(buffer).Should(gbytes.Say(controlString))
})
})
Context("error on command execution", func() {
BeforeEach(func() {
lf := strings.NewReader(controlString)
buffer = gbytes.NewBuffer()
nfs = getNfs(buffer, &FailureMockNFSExecuter{})
err = nfs.Import(lf)
})
It("should return non-nil execution error", func() {
Ω(err).ShouldNot(BeNil())
Ω(err).Should(Equal(mockNfsCommandError))
})
It("should write the local file contents to the remote", func() {
Ω(buffer).Should(gbytes.Say(controlString))
})
})
Context("error on file upload", func() {
BeforeEach(func() {
buffer = gbytes.NewBuffer()
nfs = getNfs(buffer, &SuccessMockNFSExecuter{})
})
Context("Read failure", func() {
BeforeEach(func() {
lf := mock.NewReadWriteCloser(mock.ErrReadFailure, nil, nil)
err = nfs.Import(lf)
})
It("should return non-nil execution error", func() {
Ω(err).ShouldNot(BeNil())
Ω(err).Should(Equal(mock.ErrReadFailure))
})
It("should write the local file contents to the remote", func() {
Ω(buffer).ShouldNot(gbytes.Say(controlString))
})
})
Context("Writer related failure", func() {
Context("Write failure", func() {
BeforeEach(func() {
lf := mock.NewReadWriteCloser(nil, mock.ErrWriteFailure, nil)
nfs = getNfs(lf, &SuccessMockNFSExecuter{})
err = nfs.Import(lf)
})
It("should return non-nil execution error", func() {
Ω(err).ShouldNot(BeNil())
Ω(err).Should(Equal(mock.ErrWriteFailure))
})
It("should write the local file contents to the remote", func() {
Ω(buffer).ShouldNot(gbytes.Say(controlString))
})
})
Context("Close failure", func() {
BeforeEach(func() {
lf := mock.NewReadWriteCloser(nil, nil, mock.ErrCloseFailure)
nfs = getNfs(lf, &SuccessMockNFSExecuter{})
err = nfs.Import(lf)
})
It("should return non-nil execution error", func() {
Ω(err).ShouldNot(BeNil())
Ω(err).Should(Equal(io.ErrShortWrite))
})
It("should write the local file contents to the remote", func() {
Ω(buffer).ShouldNot(gbytes.Say(controlString))
})
})
})
})
})
Describe("NFSBackup", func() {
var nfs *NFSBackup
BeforeEach(func() {
nfs = &NFSBackup{}
})
Context("sucessfully calling Dump", func() {
BeforeEach(func() {
nfs.Caller = &SuccessMockNFSExecuter{}
})
It("Should return nil error and a success message in the writer", func() {
var b bytes.Buffer
err := nfs.Dump(&b)
Ω(err).Should(BeNil())
Ω(b.String()).Should(Equal(nfsSuccessString))
})
})
Context("failed calling Dump", func() {
BeforeEach(func() {
nfs.Caller = &FailureMockNFSExecuter{}
})
It("Should return non nil error and a failure output in the writer", func() {
var b bytes.Buffer
err := nfs.Dump(&b)
Ω(err).ShouldNot(BeNil())
Ω(b.String()).Should(Equal(nfsFailureString))
})
})
Describe("NewNFSBackup", func() {
Context("when executer is created successfully", func() {
var origExecuterFunction func(command.SshConfig) (command.Executer, error)
BeforeEach(func() {
origExecuterFunction = NfsNewRemoteExecuter
NfsNewRemoteExecuter = func(command.SshConfig) (command.Executer, error) {
return &SuccessMockNFSExecuter{}, nil
}
})
AfterEach(func() {
NfsNewRemoteExecuter = origExecuterFunction
})
It("should return a nil error and a non-nil NFSBackup object", func() {
n, err := NewNFSBackup("pass", "0.0.0.0", "")
Ω(err).Should(BeNil())
Ω(n).Should(BeAssignableToTypeOf(&NFSBackup{}))
Ω(n).ShouldNot(BeNil())
})
})
Context("when executer fails to be created properly", func() {
var origExecuterFunction func(command.SshConfig) (command.Executer, error)
BeforeEach(func() {
origExecuterFunction = NfsNewRemoteExecuter
NfsNewRemoteExecuter = func(command.SshConfig) (ce command.Executer, err error) {
ce = &FailureMockNFSExecuter{}
err = fmt.Errorf("we have an error")
return
}
})
AfterEach(func() {
NfsNewRemoteExecuter = origExecuterFunction
})
It("should return a nil error and a NFSBackup object that is nil", func() {
n, err := NewNFSBackup("pass", "0.0.0.0", "")
Ω(err).ShouldNot(BeNil())
Ω(n).Should(BeNil())
Ω(n).Should(BeAssignableToTypeOf(&NFSBackup{}))
Ω(n).Should(BeNil())
})
})
})
})
})