repository_restic_test.go 3.29 KB
Newer Older
ale's avatar
ale committed
1 2 3 4 5 6 7 8 9 10
package tabacco

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

	"git.autistici.org/ale/tabacco/jobs"
ale's avatar
ale committed
13 14 15 16 17
)

// 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.
18 19
//
// nolint
ale's avatar
ale committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
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) {
ale's avatar
ale committed
43 44 45 46 47
	// Check that we can actually run restic.
	if err := checkResticVersion("restic"); err != nil {
		t.Skip("can't run restic: ", err)
	}

ale's avatar
ale committed
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
	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",
				},
			},
		},
	}
86
	queueSpec := jobs.QueueSpec{
ale's avatar
ale committed
87
		Workers: map[string]int{"backup": 2, "restore": 1},
ale's avatar
ale committed
88
	}
ale's avatar
ale committed
89 90

	// Run the backup.
ale's avatar
ale committed
91
	configMgr, err := NewConfigManager(&Config{
ale's avatar
ale committed
92 93 94 95 96 97 98 99 100 101 102
		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
103 104 105 106 107
	if err != nil {
		t.Fatal(err)
	}
	defer m.Close()

ale's avatar
ale committed
108
	backup, err := m.Backup(context.TODO(), configMgr.getSourceSpecs())
ale's avatar
ale committed
109 110 111 112 113 114 115 116
	if err != nil {
		t.Fatal(err)
	}
	if backup.ID == "" || backup.Host == "" {
		t.Fatalf("empty fields in backup: %+v", backup)
	}

	// Check the 'restic snapshots' output.
ale's avatar
ale committed
117
	output, err := exec.Command("env", "RESTIC_REPOSITORY=", "RESTIC_PASSWORD_FILE=", "RESTIC_PASSWORD=testpass", "restic", "-r", tmpdir+"/repo", "snapshots", "--json").Output()
118 119 120
	if err != nil {
		t.Fatalf("'restic snapshots' failed: %v", err)
	}
ale's avatar
ale committed
121 122
	snaps, err := parseResticSnapshots(output)
	if err != nil {
123
		t.Fatalf("parsing restic snaphots output: %v, output:\n%s", err, string(output))
ale's avatar
ale committed
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
	}
	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))
	}
}