manager_test.go 4.12 KB
Newer Older
ale's avatar
ale committed
1 2 3 4 5 6 7
package tabacco

import (
	"context"
	"fmt"
	"testing"
	"time"
8 9

	"git.autistici.org/ale/tabacco/jobs"
ale's avatar
ale committed
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
)

type dummyMetadataEntry struct {
	backupID string
	backupTS time.Time
	name     string
	dsName   string
	host     string
	handler  string
	atom     Atom
}

func (e dummyMetadataEntry) match(req FindRequest) bool {
	if req.Pattern != "" && !req.matchPattern(e.name) {
		return false
	}
	if req.Host != "" && req.Host != e.host {
		return false
	}
	return true
}

func (e dummyMetadataEntry) toDataset() Dataset {
	return Dataset{
		Name:    e.dsName,
		Handler: e.handler,
	}
}

func (e dummyMetadataEntry) toBackup() Backup {
	return Backup{
		ID:        e.backupID,
		Timestamp: e.backupTS,
		Host:      e.host,
	}
}

type dummyMetadataStore struct {
	log []dummyMetadataEntry
}

func (d *dummyMetadataStore) FindAtoms(_ context.Context, req FindRequest) ([][]Version, error) {
	tmp := make(map[string]map[string][]dummyMetadataEntry)
	for _, l := range d.log {
		if !l.match(req) {
			continue
		}

		m, ok := tmp[l.name]
		if !ok {
			m = make(map[string][]dummyMetadataEntry)
			tmp[l.name] = m
		}
		m[l.backupID] = append(m[l.name], l)
	}

	count := req.NumVersions
	if count < 1 {
		count = 1
	}

	var out [][]Version
	for _, dsmap := range tmp {
		var dsVersions []Version
		for _, dslog := range dsmap {
			v := Version{
				Dataset: dslog[0].toDataset(),
				Backup:  dslog[0].toBackup(),
			}
			if len(dslog) > count {
				dslog = dslog[len(dslog)-count:]
			}
			for _, l := range dslog {
				v.Dataset.Atoms = append(v.Dataset.Atoms, l.atom)
			}
			dsVersions = append(dsVersions, v)
		}
		out = append(out, dsVersions)
	}
	return out, nil
}

func (d *dummyMetadataStore) AddDataset(_ context.Context, backup Backup, ds Dataset) error {
	for _, atom := range ds.Atoms {
		name := fmt.Sprintf("%s/%s", ds.Name, atom.Name)
		d.log = append(d.log, dummyMetadataEntry{
			backupID: backup.ID,
			backupTS: backup.Timestamp,
			host:     backup.Host,
			name:     name,
			dsName:   ds.Name,
			handler:  ds.Handler,
			atom:     atom,
		})
	}
	return nil
}

ale's avatar
ale committed
108
func TestManager_Backup(t *testing.T) {
ale's avatar
ale committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	store := &dummyMetadataStore{}
	repoSpec := RepositorySpec{
		Name: "main",
		Type: "restic",
		Params: map[string]interface{}{
			"uri":      "/tmp/restic/repo",
			"password": "testpass",
		},
	}
	handlerSpecs := []HandlerSpec{
		{
			Name: "file1",
			Type: "file",
			Params: map[string]interface{}{
				"path": "/source/of/file1",
			},
ale's avatar
ale committed
125
			PreBackupCommand: "echo hello",
ale's avatar
ale committed
126 127 128 129 130
		},
		{
			Name: "dbpipe",
			Type: "pipe",
			Params: map[string]interface{}{
ale's avatar
ale committed
131 132
				"backup_command":  "echo ${backup.id} ${ds.name} ${atom.name}",
				"restore_command": "cat",
ale's avatar
ale committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
			},
		},
	}
	sourceSpecs := []SourceSpec{
		{
			Name:    "source1",
			Handler: "file1",
			Atoms: []Atom{
				{
					Name:         "user1",
					RelativePath: "user1",
				},
				{
					Name:         "user2",
					RelativePath: "user2",
				},
			},
		},
		{
152 153 154
			Name:         "source2",
			Handler:      "dbpipe",
			AtomsCommand: "echo user1 user1 ; echo user2 user2",
ale's avatar
ale committed
155 156
		},
	}
157
	queueSpec := jobs.QueueSpec{
ale's avatar
ale committed
158 159
		Workers: map[string]int{"backup": 2},
	}
ale's avatar
ale committed
160 161

	// Run the backup.
ale's avatar
ale committed
162
	configMgr, err := NewConfigManager(&Config{
ale's avatar
ale committed
163 164 165 166 167 168 169 170 171 172 173 174
		Queue:        queueSpec,
		Repository:   repoSpec,
		HandlerSpecs: handlerSpecs,
		SourceSpecs:  sourceSpecs,
		DryRun:       true,
	})
	if err != nil {
		t.Fatal(err)
	}
	defer configMgr.Close()

	m, err := NewManager(context.TODO(), configMgr, store)
ale's avatar
ale committed
175 176 177 178 179
	if err != nil {
		t.Fatal(err)
	}
	defer m.Close()

ale's avatar
ale committed
180
	backup, err := m.Backup(context.TODO(), configMgr.getSourceSpecs())
ale's avatar
ale committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	if err != nil {
		t.Fatal(err)
	}
	if backup.ID == "" || backup.Host == "" {
		t.Fatalf("empty fields in backup: %+v", backup)
	}

	// Try to find atoms in the metadata store.
	// Let's try with a pattern first.
	resp, err := store.FindAtoms(context.TODO(), FindRequest{Pattern: "source1/*", NumVersions: 1})
	if err != nil {
		t.Fatal("FindAtoms", err)
	}
	if len(resp) != 2 {
		t.Fatalf("bad response: %+v", resp)
	}

	// A pattern matching a single atom.
	resp, err = store.FindAtoms(context.TODO(), FindRequest{Pattern: "source1/user2"})
	if err != nil {
		t.Fatal("FindAtoms", err)
	}
	if len(resp) != 1 {
		t.Fatalf("bad response: %+v", resp)
	}
}