config_test.go 4.52 KB
Newer Older
ale's avatar
ale committed
1
2
3
package tabacco

import (
ale's avatar
ale committed
4
5
6
	"context"
	"errors"
	"fmt"
ale's avatar
ale committed
7
	"os"
ale's avatar
ale committed
8
	"strings"
ale's avatar
ale committed
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
	"testing"
)

func TestReadConfig(t *testing.T) {
	conf, err := ReadConfig("testdata/agent.yml")
	if err != nil {
		t.Fatal("ReadConfig()", err)
	}
	if len(conf.SourceSpecs) != 1 {
		t.Fatalf("no SourceSpecs in config: %+v", conf)
	}
	if len(conf.HandlerSpecs) != 1 {
		t.Fatalf("no HandlerSpecs in config: %+v", conf)
	}
}

func TestConfigManager(t *testing.T) {
	conf, err := ReadConfig("testdata/agent.yml")
	if err != nil {
		t.Fatal("ReadConfig()", err)
	}
	mgr, err := NewConfigManager(conf)
	if err != nil {
		t.Fatal("NewConfigManager()", err)
	}
	defer mgr.Close()

	// Test one of the accessor methods.
	if s := mgr.getSourceSpecs(); len(s) != 1 {
		t.Fatalf("getSourceSpecs() bad result: %+v", s)
	}

	// Test the Notify() mechanism by checking that it triggers
	// right away when setting up a new listener.
	<-mgr.Notify()
}

func TestRandomSeed(t *testing.T) {
	seedFile := ".test_seed"
	defer os.Remove(seedFile) // nolint

	seed := mustGetSeed(seedFile)
	if seed == 0 {
		t.Fatal("seed is zero")
	}
	seed2 := mustGetSeed(seedFile)
	if seed2 != seed {
		t.Fatal("failed to persist random seed")
	}
}
ale's avatar
ale committed
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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
207
208

func TestConfig_Parse(t *testing.T) {
	type testdata struct {
		config     *Config
		expectedOK bool
		checkFn    func(*runtimeAssets, []Dataset) error
	}
	tdd := []testdata{
		// The following tests cover a few ways to generate
		// the same two "user account" atoms as outlined in
		// the README.
		{
			&Config{
				SourceSpecs: []SourceSpec{
					{
						Name:    "users/account1",
						Handler: "file",
						Atoms: []Atom{
							{RelativePath: "/data/account1"},
						},
					},
					{
						Name:    "users/account2",
						Handler: "file",
						Atoms: []Atom{
							{RelativePath: "/data/account2"},
						},
					},
				},
				HandlerSpecs: []HandlerSpec{
					{
						Name:   "file",
						Type:   "file",
						Params: map[string]interface{}{"path": "/"},
					},
				},
			},
			true,
			checkTwoUserAccountsAtoms,
		},
		{
			&Config{
				SourceSpecs: []SourceSpec{
					{
						Name:    "users",
						Handler: "file",
						Atoms: []Atom{
							{Name: "account1"},
							{Name: "account2"},
						},
					},
				},
				HandlerSpecs: []HandlerSpec{
					{
						Name:   "file",
						Type:   "file",
						Params: map[string]interface{}{"path": "/data"},
					},
				},
			},
			true,
			checkTwoUserAccountsAtoms,
		},
		{
			&Config{
				SourceSpecs: []SourceSpec{
					{
						Name:         "users",
						Handler:      "file",
						AtomsCommand: "echo account1; echo account2",
					},
				},
				HandlerSpecs: []HandlerSpec{
					{
						Name:   "file",
						Type:   "file",
						Params: map[string]interface{}{"path": "/data"},
					},
				},
			},
			true,
			checkTwoUserAccountsAtoms,
		},
	}

	for _, td := range tdd {
		// Set a default repository config.
		td.config.Repository.Name = "default"
		td.config.Repository.Type = "restic"
		td.config.Repository.Params = map[string]interface{}{
			"uri":      "/tmp",
			"password": "hello",
		}

		ra, err := td.config.parse()
		if err != nil && td.expectedOK {
			t.Errorf("unexpected error for config %+v: %v", td.config, err)
		} else if err == nil && !td.expectedOK {
			t.Errorf("missing error for config %+v", td.config)
		} else {
			datasets, err := parseAllSources(ra, td.config.SourceSpecs)
			if err != nil {
				t.Errorf("failed to parse sources %+v: %v", td.config.SourceSpecs, err)
			}
			if td.checkFn != nil {
				if err := td.checkFn(ra, datasets); err != nil {
					t.Errorf("check failed for config %+v: %v", td.config, err)
				}
			}
		}
		if ra != nil {
			ra.Close()
		}
	}
}

func parseAllSources(ra *runtimeAssets, specs []SourceSpec) ([]Dataset, error) {
	var out []Dataset
	for _, spec := range specs {
		ds, err := spec.Parse(context.Background())
		if err != nil {
			return nil, err
		}
		dsb := ra.handlerMap[ds.Handler].DatasetsForBackup(ds)
		out = append(out, dsb...)
	}
	return out, nil
}

func checkTwoUserAccountsAtoms(ra *runtimeAssets, datasets []Dataset) error {
	var numAtoms int
	for _, ds := range datasets {
		if ds.Name == "" {
			return errors.New("empty dataset name")
		}
		if ds.Handler != "file" {
			return fmt.Errorf("expected handler 'file', got '%s'", ds.Handler)
		}
		for _, atom := range ds.Atoms {
			if !strings.HasPrefix(atom.SourcePath, "/data/") {
				return fmt.Errorf("bad atom source path: %s", atom.SourcePath)
			}
			numAtoms++
		}
	}
	if numAtoms != 2 {
		return fmt.Errorf("expected 2 atoms across all datasets, got %d atoms across %d datasets", numAtoms, len(datasets))
	}
	return nil
}