repository_restic_test.go 2.96 KB
Newer Older
ale's avatar
ale committed
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
package tabacco

import (
	"bytes"
	"context"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"testing"
)

// Create a temporary directory with two subdirs: 'data', for
// the test backup data, and 'repo' to store the (remote)
// repository. Populate 'data' with two tiny files.
func createTempDirWithData(t *testing.T) string {
	tmpdir, err := ioutil.TempDir("", "")
	if err != nil {
		t.Fatal(err)
	}
	for _, d := range []string{"data", "repo", "restore"} {
		os.Mkdir(filepath.Join(tmpdir, d), 0700)
	}
	ioutil.WriteFile(
		filepath.Join(tmpdir, "data", "file1"),
		[]byte("this is file number one."),
		0600,
	)
	ioutil.WriteFile(
		filepath.Join(tmpdir, "data", "file2"),
		[]byte("this is file number two."),
		0600,
	)
	return tmpdir
}

// nolint: gocyclo
func TestRestic(t *testing.T) {
	store := &dummyMetadataStore{}

	tmpdir := createTempDirWithData(t)
	defer os.RemoveAll(tmpdir)

	repoSpec := RepositorySpec{
		Name: "main",
		Type: "restic",
		Params: map[string]interface{}{
			"uri":      tmpdir + "/repo",
			"password": "testpass",
		},
	}
	handlerSpecs := []HandlerSpec{
		{
			Name: "data",
			Type: "file",
			Params: map[string]interface{}{
				"path": filepath.Join(tmpdir, "data"),
			},
		},
	}
	sourceSpecs := []SourceSpec{
		{
			Name:    "source1",
			Handler: "data",
			Atoms: []Atom{
				{
					Name:         "f1",
					RelativePath: "file1",
				},
				{
					Name:         "f2",
					RelativePath: "file2",
				},
			},
		},
	}
ale's avatar
ale committed
77
	queueSpec := MultiQueueSpec{
ale's avatar
ale committed
78
		Workers: map[string]int{"backup": 2, "restore": 1},
ale's avatar
ale committed
79
	}
ale's avatar
ale committed
80 81

	// Run the backup.
ale's avatar
ale committed
82
	configMgr, err := NewConfigManager(&Config{
ale's avatar
ale committed
83 84 85 86 87 88 89 90 91 92 93
		Queue:        queueSpec,
		Repository:   repoSpec,
		HandlerSpecs: handlerSpecs,
		SourceSpecs:  sourceSpecs,
	})
	if err != nil {
		t.Fatal(err)
	}
	defer configMgr.Close()

	m, err := NewManager(context.TODO(), configMgr, store)
ale's avatar
ale committed
94 95 96 97 98
	if err != nil {
		t.Fatal(err)
	}
	defer m.Close()

ale's avatar
ale committed
99
	backup, err := m.Backup(context.TODO(), configMgr.getSourceSpecs())
ale's avatar
ale committed
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
	if err != nil {
		t.Fatal(err)
	}
	if backup.ID == "" || backup.Host == "" {
		t.Fatalf("empty fields in backup: %+v", backup)
	}

	// Check the 'restic snapshots' output.
	output, _ := exec.Command("env", "RESTIC_PASSWORD=testpass", "restic", "-r", tmpdir+"/repo", "snapshots", "--json").Output()
	snaps, err := parseResticSnapshots(output)
	if err != nil {
		t.Fatalf("parsing restic snaphots output: %v", err)
	}
	if len(snaps) != 1 {
		t.Fatalf("wrong number of snapshots: %+v", snaps)
	}
	snap := snaps[0]
	if len(snap.Tags) != 2 {
		t.Fatalf("woops, bad number of tags: %+v", snap)
	}

	// Now try to restore.
	err = m.Restore(
		context.TODO(),
		FindRequest{Pattern: "source1/*"},
		tmpdir+"/restore",
	)
	if err != nil {
		t.Fatal("Restore", err)
	}
	data, err := ioutil.ReadFile(filepath.Join(tmpdir, "restore", tmpdir, "data", "file1"))
	if err != nil {
		t.Fatalf("data/file1 has not been restored: %v", err)
	}
	if !bytes.Equal(data, []byte("this is file number one.")) {
		t.Fatalf("data/file1 has bad restored contents: %s", string(data))
	}
}