-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbuiltin_test.go
233 lines (185 loc) · 4.34 KB
/
builtin_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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package tachyon
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func inTmp(blk func()) {
dir, err := os.Getwd()
if err != nil {
panic(err)
}
tmpDir := filepath.Join("test", fmt.Sprintf("builtin-test-%d", os.Getpid()))
os.Mkdir(tmpDir, 0755)
os.Chdir(tmpDir)
defer os.RemoveAll(tmpDir)
defer os.Chdir(dir)
blk()
}
var testData = []byte("test")
var testData2 = []byte("foobar")
func TestCopySimple(t *testing.T) {
inTmp(func() {
ioutil.WriteFile("a.txt", testData, 0644)
res, err := RunAdhocTask("copy", "src=a.txt dest=b.txt")
if err != nil {
panic(err)
}
if !res.Changed {
t.Errorf("The copy didn't change anything")
}
data, err := ioutil.ReadFile("b.txt")
if err != nil {
panic(err)
}
if !bytes.Equal(testData, data) {
t.Errorf("The copy didn't move the righte bytes")
}
})
}
func TestCopyFailsOnMissingSrc(t *testing.T) {
inTmp(func() {
_, err := RunAdhocTask("copy", "src=a.txt dest=b.txt")
if err == nil {
t.Errorf("Copy did not fail")
}
})
}
func TestCopyShowsNoChangeWhenFilesTheSame(t *testing.T) {
inTmp(func() {
ioutil.WriteFile("a.txt", testData, 0644)
ioutil.WriteFile("b.txt", testData, 0644)
res, err := RunAdhocTask("copy", "src=a.txt dest=b.txt")
if err != nil {
panic(err)
}
if res.Changed {
t.Errorf("The copy changed something incorrectly")
}
if res.Data["md5sum"].Read().(string) == "" {
t.Errorf("md5sum not returned")
}
})
}
func TestCopyMakesFileInDir(t *testing.T) {
inTmp(func() {
ioutil.WriteFile("a.txt", testData, 0644)
os.Mkdir("b", 0755)
res, err := RunAdhocTask("copy", "src=a.txt dest=b")
if err != nil {
panic(err)
}
if !res.Changed {
t.Errorf("The copy didn't change anything")
}
data, err := ioutil.ReadFile("b/a.txt")
if err != nil {
panic(err)
}
if !bytes.Equal(testData, data) {
t.Errorf("The copy didn't move the righte bytes")
}
})
}
func TestCopyRemovesALink(t *testing.T) {
inTmp(func() {
ioutil.WriteFile("a.txt", testData, 0644)
ioutil.WriteFile("c.txt", testData2, 0644)
os.Symlink("c.txt", "b.txt")
res, err := RunAdhocTask("copy", "src=a.txt dest=b.txt")
if err != nil {
panic(err)
}
if !res.Changed {
t.Errorf("The copy didn't change anything")
}
stat, err := os.Stat("b.txt")
if !stat.Mode().IsRegular() {
t.Errorf("copy didn't remove the link")
}
data, err := ioutil.ReadFile("b.txt")
if err != nil {
panic(err)
}
if !bytes.Equal(testData, data) {
t.Errorf("The copy didn't move the righte bytes")
}
data, err = ioutil.ReadFile("c.txt")
if err != nil {
panic(err)
}
if !bytes.Equal(testData2, data) {
t.Errorf("c.txt was overriden improperly")
}
})
}
func TestCopyPreservesMode(t *testing.T) {
inTmp(func() {
ioutil.WriteFile("a.txt", testData, 0755)
_, err := RunAdhocTask("copy", "src=a.txt dest=b.txt")
if err != nil {
panic(err)
}
stat, err := os.Stat("b.txt")
if err != nil {
panic(err)
}
if stat.Mode().Perm() != 0755 {
t.Errorf("Copy didn't preserve the perms")
}
})
}
func TestCommand(t *testing.T) {
res, err := RunAdhocTask("command", "date")
if err != nil {
panic(err)
}
if !res.Changed {
t.Errorf("changed not properly set")
}
if res.Data["rc"].Read().(int) != 0 {
t.Errorf("return code not captured")
}
if res.Data["stdout"].Read().(string) == "" {
t.Errorf("stdout was not captured: '%s'", res.Data["stdout"].Read())
}
}
func TestShell(t *testing.T) {
res, err := RunAdhocTask("shell", "echo \"hello dear\"")
if err != nil {
panic(err)
}
if res.Data["rc"].Read().(int) != 0 {
t.Errorf("return code not captured")
}
if res.Data["stdout"].Read().(string) != "hello dear" {
t.Errorf("stdout was not captured: '%s'", res.Data["stdout"].Read())
}
}
func TestShellSeesNonZeroRC(t *testing.T) {
res, err := RunAdhocTask("shell", "exit 1")
if err != nil {
panic(err)
}
if res.Data["rc"].Read().(int) != 1 {
t.Errorf("return code not captured")
}
if res.Data["stdout"].Read().(string) != "" {
t.Errorf("stdout was not captured")
}
}
func TestScriptExecutesRelative(t *testing.T) {
res, err := RunAdhocTask("script", "test/test_script.sh")
if err != nil {
panic(err)
}
if res.Data["rc"].Read().(int) != 0 {
t.Errorf("return code not captured")
}
if res.Data["stdout"].Read().(string) != "hello script" {
t.Errorf("stdout was not captured")
}
}