mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2024-12-22 08:16:25 +00:00
chore(tests): add integration tests for backup cmd
This commit is contained in:
parent
6424c64304
commit
eb4013b51d
73
cmd/backup_test.go
Normal file
73
cmd/backup_test.go
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func runCmd(t *testing.T, args ...string) error {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
viper.Reset()
|
||||||
|
rootCmd.SetArgs(args)
|
||||||
|
|
||||||
|
err := rootCmd.Execute()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBackupCmd(t *testing.T) {
|
||||||
|
workDir := t.TempDir()
|
||||||
|
|
||||||
|
// Prepare content to be backed up
|
||||||
|
locationDir := path.Join(workDir, "my-location")
|
||||||
|
err := os.Mkdir(locationDir, 0750)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
err = os.WriteFile(path.Join(locationDir, "back-me-up.txt"), []byte("hello world"), 0640)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
// Write config file
|
||||||
|
config, err := yaml.Marshal(map[string]interface{}{
|
||||||
|
"version": 2,
|
||||||
|
"locations": map[string]map[string]interface{}{
|
||||||
|
"my-location": {
|
||||||
|
"type": "local",
|
||||||
|
"from": []string{locationDir},
|
||||||
|
"to": []string{"test"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"backends": map[string]map[string]interface{}{
|
||||||
|
"test": {
|
||||||
|
"type": "local",
|
||||||
|
"path": path.Join(workDir, "test-backend"),
|
||||||
|
"key": "supersecret",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
assert.Nil(t, err)
|
||||||
|
configPath := path.Join(workDir, ".autorestic.yml")
|
||||||
|
err = os.WriteFile(configPath, config, 0640)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
// Init repo (not initialized by default)
|
||||||
|
err = runCmd(t, "exec", "--ci", "-a", "-c", configPath, "init")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
// Do the backup
|
||||||
|
err = runCmd(t, "backup", "--ci", "-a", "-c", configPath)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
// Restore in a separate dir
|
||||||
|
restoreDir := path.Join(workDir, "restore")
|
||||||
|
err = runCmd(t, "restore", "--ci", "-c", configPath, "-l", "my-location", "--to", restoreDir)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
// Check restored file
|
||||||
|
restoredContent, err := os.ReadFile(path.Join(restoreDir, locationDir, "back-me-up.txt"))
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, "hello world", string(restoredContent))
|
||||||
|
}
|
2
go.mod
2
go.mod
@ -12,6 +12,7 @@ require (
|
|||||||
github.com/spf13/cobra v1.4.0
|
github.com/spf13/cobra v1.4.0
|
||||||
github.com/spf13/viper v1.11.0
|
github.com/spf13/viper v1.11.0
|
||||||
github.com/stretchr/testify v1.9.0
|
github.com/stretchr/testify v1.9.0
|
||||||
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@ -35,5 +36,4 @@ require (
|
|||||||
golang.org/x/text v0.3.8 // indirect
|
golang.org/x/text v0.3.8 // indirect
|
||||||
gopkg.in/ini.v1 v1.66.4 // indirect
|
gopkg.in/ini.v1 v1.66.4 // indirect
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user