forked from creativeprojects/resticprofile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_test.go
206 lines (188 loc) · 8.13 KB
/
integration_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
package main
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/creativeprojects/resticprofile/config"
"github.com/creativeprojects/resticprofile/term"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
echoBinary string
)
func init() {
// build restic mock
cmd := exec.Command("go", "build", "./shell/echo")
cmd.Run()
if runtime.GOOS == "windows" {
echoBinary = "echo.exe"
} else {
echoBinary = "./echo"
}
}
// TestFromConfigFileToCommandLine loads all examples/integration_test.* configuration files
// and run some commands to display all the arguments that were sent
func TestFromConfigFileToCommandLine(t *testing.T) {
files, err := filepath.Glob("./examples/integration_test.*")
require.NoError(t, err)
require.Greater(t, len(files), 0)
// we can use the same files to test a glob pattern
globFiles := "\"" + strings.Join(files, "\" \"") + "\""
globFilesOnWindows := strings.Replace(globFiles, `\`, `\\`, -1)
integrationData := []struct {
profileName string
commandName string
cmdlineArgs []string
legacy string // legacy mode is not wired up and probably shouldn't
expected string
expectedOnWindows string
}{
{
"default",
"snapshots",
[]string{},
`["snapshots" "--password-file=examples/key" "--repo=rest:http://user:password@localhost:8000/path"]`,
`["snapshots" "--password-file=examples/key" "--repo=rest:http://user:password@localhost:8000/path"]`,
`["snapshots" "--password-file=examples\\key" "--repo=rest:http://user:password@localhost:8000/path"]`,
},
{
"simple",
"backup",
[]string{"--option"},
`["backup" "--exclude=/**/.git" "--password-file=examples/key" "--repo=rest:http://user:password@localhost:8000/path" "--option" "/source"]`,
`["backup" "--exclude=/**/.git" "--password-file=examples/key" "--repo=rest:http://user:password@localhost:8000/path" "--option" "/source"]`,
`["backup" "--exclude=/**/.git" "--password-file=examples\\key" "--repo=rest:http://user:password@localhost:8000/path" "--option" "\\source"]`,
},
{
"glob1",
"backup",
[]string{},
`["backup" "--exclude=[aA]*" "--password-file=examples/key" "--repo=rest:http://user:password@localhost:8000/path" "/source"]`,
`["backup" "--exclude=[aA]*" "--password-file=examples/key" "--repo=rest:http://user:password@localhost:8000/path" "/source"]`,
`["backup" "--exclude=[aA]*" "--password-file=examples\\key" "--repo=rest:http://user:password@localhost:8000/path" "\\source"]`,
},
{
"glob2",
"backup",
[]string{"examples/integration*"},
`["backup" "--exclude=examples/integration*" "--password-file=examples/key" "--repo=rest:http://user:password@localhost:8000/path" ` + globFiles + " " + globFiles + "]",
`["backup" "--exclude=examples/integration*" "--password-file=examples/key" "--repo=rest:http://user:password@localhost:8000/path" ` + globFiles + " " + globFiles + "]",
`["backup" "--exclude=examples/integration*" "--password-file=examples\\key" "--repo=rest:http://user:password@localhost:8000/path" "examples/integration*" ` + globFilesOnWindows + "]",
},
{
"spaces",
"backup",
[]string{"some path"},
`["backup" "--exclude=My\\ Documents" "--password-file=examples/different\\ key" "--repo=rest:http://user:password@localhost:8000/path" "some" "path" "/source dir"]`,
`["backup" "--exclude=My Documents" "--password-file=examples/different key" "--repo=rest:http://user:password@localhost:8000/path" "some path" "/source dir"]`,
`["backup" "--exclude=My Documents" "--password-file=examples\\different key" "--repo=rest:http://user:password@localhost:8000/path" "some path" "\\source dir"]`,
},
{
"quotes",
"backup",
[]string{"quo'te", "quo\"te"},
`["backup" "--exclude=MyDocuments --exclude=My\"Documents --password-file=examples/key --repo=rest:http://user:password@localhost:8000/path quote" "quote /source'dir /sourcedir"]`,
`["backup" "--exclude=My'Documents" "--exclude=My\"Documents" "--password-file=examples/key" "--repo=rest:http://user:password@localhost:8000/path" "quo'te" "quo\"te" "/source'dir" "/source\"dir"]`,
`["backup" "--exclude=My'Documents" "--exclude=My\"Documents" "--password-file=examples\\key" "--repo=rest:http://user:password@localhost:8000/path" "quo'te" "quo\"te" "\\source'dir" "\\source\"dir"]`,
},
{
"mixed",
"backup",
[]string{"/path/with space; echo foo"},
`["backup" "--password-file=examples/key" "--repo=rest:http://user:password@localhost:8000/path" "/path/with" "space"]` + "\n" + `foo /Côte dIvoire /path/with\ space;\ echo\ foo`,
`["backup" "--password-file=examples/key" "--repo=rest:http://user:password@localhost:8000/path" "/path/with space; echo foo" "/Côte d'Ivoire" "/path/with space; echo foo'"]`,
`["backup" "--password-file=examples\\key" "--repo=rest:http://user:password@localhost:8000/path" "/path/with space; echo foo" "\\Côte d'Ivoire" "\\path\\with space; echo foo'"]`,
},
{
"mixed",
"backup",
[]string{},
`["backup" "--password-file=examples/key" "--repo=rest:http://user:password@localhost:8000/path" "/Côte dIvoire /path/with\\ space;\\ echo\\ foo"]`,
`["backup" "--password-file=examples/key" "--repo=rest:http://user:password@localhost:8000/path" "/Côte d'Ivoire" "/path/with space; echo foo'"]`,
`["backup" "--password-file=examples\\key" "--repo=rest:http://user:password@localhost:8000/path" "\\Côte d'Ivoire" "\\path\\with space; echo foo'"]`,
},
{
"fix",
"backup",
[]string{},
`["backup" "--exclude=My\\ Documents" "--password-file=examples/different\\ key" "--repo=rest:http://user:password@localhost:8000/path" "/source dir"]`,
`["backup" "--exclude=My Documents" "--password-file=examples/different key" "--repo=rest:http://user:password@localhost:8000/path" "/source dir"]`,
``,
},
}
// try all the config files one by one
for _, configFile := range files {
t.Run(configFile, func(t *testing.T) {
cfg, err := config.LoadFile(configFile, "")
require.NoError(t, err)
require.NotNil(t, cfg)
// try all the fixtures one by one (on each file)
for _, fixture := range integrationData {
t.Run(fixture.profileName+"/"+fixture.commandName, func(t *testing.T) {
profile, err := cfg.GetProfile(fixture.profileName)
require.NoError(t, err)
require.NotNil(t, profile)
ctx := &Context{
request: Request{
profile: fixture.profileName,
arguments: fixture.cmdlineArgs,
},
binary: echoBinary,
profile: profile,
command: fixture.commandName,
}
wrapper := newResticWrapper(ctx)
buffer := &bytes.Buffer{}
// setting the output via the package global setter could lead to some issues
// when some tests are running in parallel. I should fix that at some point :-/
term.SetOutput(buffer)
err = wrapper.runCommand(fixture.commandName)
term.SetOutput(os.Stdout)
require.NoError(t, err)
expected := fixture.expected
if runtime.GOOS == "windows" {
if fixture.expectedOnWindows != "" {
expected = fixture.expectedOnWindows
} else {
t.SkipNow()
}
}
assert.Equal(t, expected, strings.TrimSpace(buffer.String()))
})
if runtime.GOOS == "windows" {
continue
}
// legacy test
t.Run(fixture.profileName+"/"+fixture.commandName+"/legacy", func(t *testing.T) {
profile, err := cfg.GetProfile(fixture.profileName)
require.NoError(t, err)
require.NotNil(t, profile)
profile.SetLegacyArg(true)
ctx := &Context{
request: Request{
profile: fixture.profileName,
arguments: fixture.cmdlineArgs,
},
binary: echoBinary,
profile: profile,
command: fixture.commandName,
}
wrapper := newResticWrapper(ctx)
buffer := &bytes.Buffer{}
// setting the output via the package global setter could lead to some issues
// when some tests are running in parallel. I should fix that at some point :-/
term.SetOutput(buffer)
err = wrapper.runCommand(fixture.commandName)
term.SetOutput(os.Stdout)
require.NoError(t, err)
assert.Equal(t, fixture.legacy, strings.TrimSpace(buffer.String()))
})
}
})
}
}