Skip to content

Commit eb4013b

Browse files
committed
chore(tests): add integration tests for backup cmd
1 parent 6424c64 commit eb4013b

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

cmd/backup_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
"path"
6+
"testing"
7+
8+
"github.com/spf13/viper"
9+
"github.com/stretchr/testify/assert"
10+
"gopkg.in/yaml.v3"
11+
)
12+
13+
func runCmd(t *testing.T, args ...string) error {
14+
t.Helper()
15+
16+
viper.Reset()
17+
rootCmd.SetArgs(args)
18+
19+
err := rootCmd.Execute()
20+
return err
21+
}
22+
23+
func TestBackupCmd(t *testing.T) {
24+
workDir := t.TempDir()
25+
26+
// Prepare content to be backed up
27+
locationDir := path.Join(workDir, "my-location")
28+
err := os.Mkdir(locationDir, 0750)
29+
assert.Nil(t, err)
30+
err = os.WriteFile(path.Join(locationDir, "back-me-up.txt"), []byte("hello world"), 0640)
31+
assert.Nil(t, err)
32+
33+
// Write config file
34+
config, err := yaml.Marshal(map[string]interface{}{
35+
"version": 2,
36+
"locations": map[string]map[string]interface{}{
37+
"my-location": {
38+
"type": "local",
39+
"from": []string{locationDir},
40+
"to": []string{"test"},
41+
},
42+
},
43+
"backends": map[string]map[string]interface{}{
44+
"test": {
45+
"type": "local",
46+
"path": path.Join(workDir, "test-backend"),
47+
"key": "supersecret",
48+
},
49+
},
50+
})
51+
assert.Nil(t, err)
52+
configPath := path.Join(workDir, ".autorestic.yml")
53+
err = os.WriteFile(configPath, config, 0640)
54+
assert.Nil(t, err)
55+
56+
// Init repo (not initialized by default)
57+
err = runCmd(t, "exec", "--ci", "-a", "-c", configPath, "init")
58+
assert.Nil(t, err)
59+
60+
// Do the backup
61+
err = runCmd(t, "backup", "--ci", "-a", "-c", configPath)
62+
assert.Nil(t, err)
63+
64+
// Restore in a separate dir
65+
restoreDir := path.Join(workDir, "restore")
66+
err = runCmd(t, "restore", "--ci", "-c", configPath, "-l", "my-location", "--to", restoreDir)
67+
assert.Nil(t, err)
68+
69+
// Check restored file
70+
restoredContent, err := os.ReadFile(path.Join(restoreDir, locationDir, "back-me-up.txt"))
71+
assert.Nil(t, err)
72+
assert.Equal(t, "hello world", string(restoredContent))
73+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/spf13/cobra v1.4.0
1313
github.com/spf13/viper v1.11.0
1414
github.com/stretchr/testify v1.9.0
15+
gopkg.in/yaml.v3 v3.0.1
1516
)
1617

1718
require (
@@ -35,5 +36,4 @@ require (
3536
golang.org/x/text v0.3.8 // indirect
3637
gopkg.in/ini.v1 v1.66.4 // indirect
3738
gopkg.in/yaml.v2 v2.4.0 // indirect
38-
gopkg.in/yaml.v3 v3.0.1 // indirect
3939
)

0 commit comments

Comments
 (0)