config_test.go 4.98 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
	"log"
ale's avatar
ale committed
8
	"os"
ale's avatar
ale committed
9
	"strings"
ale's avatar
ale committed
10
	"testing"
ale's avatar
ale committed
11
	"time"
ale's avatar
ale committed
12 13 14 15 16 17 18
)

func TestReadConfig(t *testing.T) {
	conf, err := ReadConfig("testdata/agent.yml")
	if err != nil {
		t.Fatal("ReadConfig()", err)
	}
ale's avatar
ale committed
19 20
	if l := len(conf.SourceSpecs); l != 1 {
		t.Fatalf("couldn't read all SourceSpecs: got %d, expected %d", l, 1)
ale's avatar
ale committed
21
	}
ale's avatar
ale committed
22 23
	if l := len(conf.HandlerSpecs); l != 3 {
		t.Fatalf("couldn't read all HandlerSpecs: got %d, expected %d", l, 3)
ale's avatar
ale committed
24 25 26 27 28 29 30 31
	}
}

func TestConfigManager(t *testing.T) {
	conf, err := ReadConfig("testdata/agent.yml")
	if err != nil {
		t.Fatal("ReadConfig()", err)
	}
ale's avatar
ale committed
32
	log.Printf("loaded %d sources", len(conf.SourceSpecs))
ale's avatar
ale committed
33 34 35 36 37 38 39 40 41 42 43 44 45
	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.
ale's avatar
ale committed
46 47 48 49 50 51
	tmr := time.NewTimer(1 * time.Second)
	select {
	case <-mgr.Notify():
	case <-tmr.C:
		t.Fatal("Notify() channel did not trigger")
	}
ale's avatar
ale committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
}

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
67 68 69 70 71

func TestConfig_Parse(t *testing.T) {
	type testdata struct {
		config     *Config
		expectedOK bool
ale's avatar
ale committed
72
		checkFn    func(*runtimeAssets, []*Dataset) error
ale's avatar
ale committed
73 74 75 76 77 78 79
	}
	tdd := []testdata{
		// The following tests cover a few ways to generate
		// the same two "user account" atoms as outlined in
		// the README.
		{
			&Config{
ale's avatar
ale committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
				SourceSpecs: []*SourceSpec{
					&SourceSpec{
						Name:     "users",
						Handler:  "file",
						Schedule: "@random_every 24h",
						Datasets: []*DatasetSpec{
							&DatasetSpec{
								Name: "account1",
								Atoms: []Atom{
									{Name: "account1"},
								},
							},
							{
								Name: "account2",
								Atoms: []Atom{
									{Name: "account2"},
								},
							},
ale's avatar
ale committed
98 99 100
						},
					},
				},
ale's avatar
ale committed
101 102
				HandlerSpecs: []*HandlerSpec{
					&HandlerSpec{
ale's avatar
ale committed
103 104 105 106 107 108 109 110 111 112 113
						Name:   "file",
						Type:   "file",
						Params: map[string]interface{}{"path": "/"},
					},
				},
			},
			true,
			checkTwoUserAccountsAtoms,
		},
		{
			&Config{
ale's avatar
ale committed
114 115 116 117 118 119 120 121 122 123 124 125 126
				SourceSpecs: []*SourceSpec{
					&SourceSpec{
						Name:     "users",
						Handler:  "file",
						Schedule: "@random_every 24h",
						Datasets: []*DatasetSpec{
							&DatasetSpec{
								Name: "users",
								Atoms: []Atom{
									{Name: "account1"},
									{Name: "account2"},
								},
							},
ale's avatar
ale committed
127 128 129
						},
					},
				},
ale's avatar
ale committed
130 131
				HandlerSpecs: []*HandlerSpec{
					&HandlerSpec{
ale's avatar
ale committed
132 133 134 135 136 137 138 139 140 141 142
						Name:   "file",
						Type:   "file",
						Params: map[string]interface{}{"path": "/data"},
					},
				},
			},
			true,
			checkTwoUserAccountsAtoms,
		},
		{
			&Config{
ale's avatar
ale committed
143 144 145 146 147 148
				SourceSpecs: []*SourceSpec{
					&SourceSpec{
						Name:            "users",
						Handler:         "file",
						Schedule:        "@random_every 24h",
						DatasetsCommand: "echo '[{name: account1, atoms: [{name: account1}, {name: account2}]}]'",
ale's avatar
ale committed
149 150
					},
				},
ale's avatar
ale committed
151 152
				HandlerSpecs: []*HandlerSpec{
					&HandlerSpec{
ale's avatar
ale committed
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
						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()
		}
	}
}

ale's avatar
ale committed
195 196
func parseAllSources(ra *runtimeAssets, specs []*SourceSpec) ([]*Dataset, error) {
	var out []*Dataset
ale's avatar
ale committed
197 198 199 200 201
	for _, spec := range specs {
		ds, err := spec.Parse(context.Background())
		if err != nil {
			return nil, err
		}
ale's avatar
ale committed
202
		out = append(out, ds...)
ale's avatar
ale committed
203 204 205 206
	}
	return out, nil
}

ale's avatar
ale committed
207
func checkTwoUserAccountsAtoms(ra *runtimeAssets, datasets []*Dataset) error {
ale's avatar
ale committed
208 209 210 211 212 213
	var numAtoms int
	for _, ds := range datasets {
		if ds.Name == "" {
			return errors.New("empty dataset name")
		}
		for _, atom := range ds.Atoms {
ale's avatar
ale committed
214 215
			if !strings.HasPrefix(atom.Name, "users/") {
				return fmt.Errorf("bad atom name: %s", atom.Name)
ale's avatar
ale committed
216 217 218 219 220 221 222 223 224
			}
			numAtoms++
		}
	}
	if numAtoms != 2 {
		return fmt.Errorf("expected 2 atoms across all datasets, got %d atoms across %d datasets", numAtoms, len(datasets))
	}
	return nil
}