-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_test.go
172 lines (157 loc) · 3.96 KB
/
backup_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
package backive
import (
"fmt"
"os/exec"
"path"
"runtime"
"testing"
)
func getCurrentFilePath() string {
pc, file, line, ok := runtime.Caller(1)
fmt.Printf("pc: %d, file: %s, line: %d, ok: %v\n", pc, file, line, ok)
return file
}
func TestFindBackupsForDevice(t *testing.T) {
var testBackups = Backups{}
testBackups["backup1"] = &Backup{
Name: "backup1",
TargetDevice: "dev1",
}
testBackups["backup2"] = &Backup{
Name: "backup2",
TargetDevice: "dev1",
}
testBackups["backup3"] = &Backup{
Name: "backup3",
TargetDevice: "dev2",
}
var testDevice = Device{
Name: "dev1",
}
bkps, found := testBackups.FindBackupsForDevice(testDevice)
if !found {
t.Log("found has to be true")
t.Fail()
}
if len(bkps) != 2 {
t.Log("Length of the returned backup slice has to be 2")
t.Fail()
}
for _, b := range bkps {
if b.TargetDevice != testDevice.Name {
t.Log("All resulting elements of the returned slice have to have the questioned device as TargetDevice!")
t.Fail()
}
}
}
func TestCanRun(t *testing.T) {
var bkpTargetPathMissing = Backup{
Name: "targetPathMissing",
ScriptPath: "Somethingsomething",
}
err := bkpTargetPathMissing.CanRun()
if err == nil {
t.Log("Missing targetPath has to fail function 'CanRun()'")
t.Fail()
}
var bkpScriptPathMissing = Backup{
Name: "scriptPathMissing",
TargetPath: "somethingsomething",
}
err = bkpScriptPathMissing.CanRun()
if err == nil {
t.Logf("ScriptPath is %v", bkpScriptPathMissing.ScriptPath)
t.Log("Missing scriptPath has to fail function 'CanRun()'")
t.Fail()
}
var bkpFrequencyZero = Backup{
Name: "testFrequencyZero",
TargetPath: "somewhere",
ScriptPath: "somehwere_else",
Frequency: 0,
}
var bkpFrequencySeven = Backup{
Name: "testFrequencySeven",
TargetPath: "somewhere",
ScriptPath: "somewhere_else",
Frequency: 7,
}
database.Load()
runs.Load(database)
runs.RegisterRun(&bkpFrequencyZero)
err = bkpFrequencyZero.CanRun()
if err != nil {
t.Log("Frequency zero can be executed anytime.")
t.Fail()
}
if !bkpFrequencyZero.ShouldRun() {
t.Log("Frequency zero can be executed anytime, should run does not work.")
t.Fail()
}
runs.RegisterRun(&bkpFrequencySeven)
err = bkpFrequencySeven.CanRun()
if err == nil {
t.Log("Frequency 7 must give an error about not having reached the interval")
t.Fail()
}
}
func setupNewTestEnv(subdir string) {
file := getCurrentFilePath()
dir, _ := path.Split(file)
dir = path.Join(dir, "test", "_workarea", subdir)
config.Settings.SystemMountPoint = path.Join(dir, "mnt")
config.Settings.LogLocation = path.Join(dir, "log")
fmt.Printf("SystemMountPoint: %s, LogLocation: %s\n", config.Settings.SystemMountPoint, config.Settings.LogLocation)
}
func TestPrepareRun(t *testing.T) {
setupNewTestEnv("preparerun")
mockCmdRun = func(c *exec.Cmd) error {
return nil
}
var testBkp = Backup{
Name: "testbkp",
TargetDevice: "mytarget",
TargetPath: "mypath",
}
err := testBkp.PrepareRun()
if err != nil {
t.Log("When this fails, something's fishy...")
t.Fail()
}
}
func TestRun(t *testing.T) {
setupNewTestEnv("run")
config.Devices = map[string]*Device{
"mytarget": new(Device),
}
config.Devices["mytarget"].Name = "mytarget"
config.Devices["mytarget"].UUID = "123-456-789-abc-def"
mockCmdRun = func(c *exec.Cmd) error {
return nil
}
var testBkp = Backup{
Name: "testbkp",
TargetDevice: "mytargets",
TargetPath: "mypath",
ScriptPath: "/dev/null",
SourcePath: "/dev/random",
}
err := testBkp.Run()
if err == nil || err.Error() != "device mytargets not found" {
if err != nil {
t.Logf("The error should be 'device mytargets not found', but is '%s'", err.Error())
t.Fail()
}
}
testBkp.TargetDevice = "mytarget"
config.Devices["mytarget"].Mount()
err = testBkp.PrepareRun()
err = testBkp.Run()
if err != nil {
t.Logf("Error which should not occur: %s", err)
t.Fail()
}
mockCmdRun = func(c *exec.Cmd) error {
return nil
}
}