2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-24 11:55:28 +00:00

Merge pull request #853 from middelink/cmd_backup-tag-integration-test

restic backup --tag integration test
This commit is contained in:
Alexander Neumann 2017-03-05 16:08:02 +01:00
commit a809c9ac5f
2 changed files with 62 additions and 11 deletions

View File

@ -2,7 +2,7 @@ package main
import (
"fmt"
"os"
"io"
"restic/errors"
"sort"
@ -64,7 +64,7 @@ func runSnapshots(opts SnapshotOptions, gopts GlobalOptions, args []string) erro
for id := range repo.List(restic.SnapshotFile, done) {
sn, err := restic.LoadSnapshot(repo, id)
if err != nil {
fmt.Fprintf(os.Stderr, "error loading snapshot %s: %v\n", id, err)
Warnf("error loading snapshot %s: %v\n", id, err)
continue
}
@ -85,19 +85,19 @@ func runSnapshots(opts SnapshotOptions, gopts GlobalOptions, args []string) erro
}
if gopts.JSON {
err := printSnapshotsJSON(list)
err := printSnapshotsJSON(gopts.stdout, list)
if err != nil {
fmt.Fprintf(os.Stderr, "error printing snapshot: %v\n", err)
Warnf("error printing snapshot: %v\n", err)
}
return nil
}
printSnapshotsReadable(list)
printSnapshotsReadable(gopts.stdout, list)
return nil
}
// printSnapshotsReadable prints a text table of the snapshots in list to stdout.
func printSnapshotsReadable(list []*restic.Snapshot) {
func printSnapshotsReadable(stdout io.Writer, list []*restic.Snapshot) {
tab := NewTable()
tab.Header = fmt.Sprintf("%-8s %-19s %-10s %-10s %-3s %s", "ID", "Date", "Host", "Tags", "", "Directory")
@ -146,9 +146,7 @@ func printSnapshotsReadable(list []*restic.Snapshot) {
}
}
tab.Write(os.Stdout)
return
tab.Write(stdout)
}
// Snapshot helps to print Snaphots as JSON
@ -159,7 +157,7 @@ type Snapshot struct {
}
// printSnapshotsJSON writes the JSON representation of list to stdout.
func printSnapshotsJSON(list []*restic.Snapshot) error {
func printSnapshotsJSON(stdout io.Writer, list []*restic.Snapshot) error {
var snapshots []Snapshot
@ -172,6 +170,6 @@ func printSnapshotsJSON(list []*restic.Snapshot) error {
snapshots = append(snapshots, k)
}
return json.NewEncoder(os.Stdout).Encode(snapshots)
return json.NewEncoder(stdout).Encode(snapshots)
}

View File

@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
"io"
"io/ioutil"
@ -160,6 +161,33 @@ func testRunFind(t testing.TB, gopts GlobalOptions, pattern string) []string {
return strings.Split(string(buf.Bytes()), "\n")
}
func testRunSnapshots(t testing.TB, gopts GlobalOptions) (*Snapshot, map[string]Snapshot) {
buf := bytes.NewBuffer(nil)
globalOptions.stdout = buf
globalOptions.JSON = true
defer func() {
globalOptions.stdout = os.Stdout
globalOptions.JSON = gopts.JSON
}()
opts := SnapshotOptions{}
OK(t, runSnapshots(opts, globalOptions, []string{}))
snapshots := []Snapshot{}
OK(t, json.Unmarshal(buf.Bytes(), &snapshots))
var newest *Snapshot
snapmap := make(map[string]Snapshot, len(snapshots))
for _, sn := range snapshots {
snapmap[sn.ID] = sn
if newest == nil || sn.Time.After(newest.Time) {
newest = &sn
}
}
return newest, snapmap
}
func testRunForget(t testing.TB, gopts GlobalOptions, args ...string) {
opts := ForgetOptions{}
OK(t, runForget(opts, gopts, args))
@ -602,6 +630,31 @@ func TestIncrementalBackup(t *testing.T) {
})
}
func TestBackupTags(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
testRunInit(t, gopts)
SetupTarTestFixture(t, env.testdata, datafile)
opts := BackupOptions{}
testRunBackup(t, []string{env.testdata}, opts, gopts)
testRunCheck(t, gopts)
newest, _ := testRunSnapshots(t, gopts)
Assert(t, newest != nil, "expected a new backup, got nil")
Assert(t, len(newest.Tags) == 0,
"expected no tags, got %v", newest.Tags)
opts.Tags = []string{"NL"}
testRunBackup(t, []string{env.testdata}, opts, gopts)
testRunCheck(t, gopts)
newest, _ = testRunSnapshots(t, gopts)
Assert(t, newest != nil, "expected a new backup, got nil")
Assert(t, len(newest.Tags) == 1 && newest.Tags[0] == "NL",
"expected one NL tag, got %v", newest.Tags)
})
}
func testRunKeyListOtherIDs(t testing.TB, gopts GlobalOptions) []string {
buf := bytes.NewBuffer(nil)