service_test.go 1.82 KB
Newer Older
1 2 3 4 5 6 7 8 9
package server

import (
	"context"
	"fmt"
	"os"
	"testing"
	"time"

ale's avatar
ale committed
10
	"git.autistici.org/ai3/tools/tabacco"
11 12 13 14 15 16 17 18 19 20 21 22 23
)

const DBFILE = ".service_test.db"

func addTestEntry(t *testing.T, svc *Service, backupID, host, dsName string) {
	err := svc.AddDataset(
		context.Background(),
		tabacco.Backup{
			ID:        backupID,
			Host:      host,
			Timestamp: time.Now(),
		},
		tabacco.Dataset{
ale's avatar
ale committed
24
			Source: "file",
25 26
			Atoms: []tabacco.Atom{
				{
ale's avatar
ale committed
27 28
					Name: dsName + "/sub1",
					Path: "/path/dataset1/sub1",
29 30
				},
				{
ale's avatar
ale committed
31 32
					Name: dsName + "/sub2",
					Path: "/path/dataset1/sub2",
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
				},
			},
		},
	)
	if err != nil {
		t.Fatal("AddDataset", err)
	}
}

func TestService_AddDataset(t *testing.T) {
	defer os.Remove(DBFILE)
	svc, err := New("sqlite3", DBFILE)
	if err != nil {
		t.Fatal(err)
	}
	defer svc.Close()

ale's avatar
ale committed
50
	addTestEntry(t, svc, "1234", "host1", "file/dataset1")
51 52 53 54 55 56 57 58 59 60 61 62
}

func TestService_FindAtoms(t *testing.T) {
	defer os.Remove(DBFILE)
	svc, err := New("sqlite3", DBFILE)
	if err != nil {
		t.Fatal(err)
	}
	defer svc.Close()

	// Create 10 fake backups, which differ only in host.
	for i := 0; i < 10; i++ {
ale's avatar
ale committed
63
		addTestEntry(t, svc, fmt.Sprintf("backup%06d", i), fmt.Sprintf("host%d", i), "file/dataset1")
64 65 66 67 68 69
	}

	// Searching for a specific atom (common to all backups)
	// should return exactly 10 results.
	vv, err := svc.FindAtoms(
		context.Background(),
ale's avatar
ale committed
70
		&tabacco.FindRequest{
71 72
			Pattern:     "*/sub1",
			NumVersions: 10,
73 74 75 76 77 78 79 80 81 82 83 84 85
		},
	)
	if err != nil {
		t.Fatal("FindAtoms", err)
	}
	if len(vv) != 10 {
		t.Fatalf("bad result: %+v", vv)
	}

	// Augmenting the previous search with a host filter should
	// return a single result.
	vv, err = svc.FindAtoms(
		context.Background(),
ale's avatar
ale committed
86
		&tabacco.FindRequest{
87 88 89 90 91 92 93 94 95 96 97
			Pattern: "*/sub1",
			Host:    "host7",
		},
	)
	if err != nil {
		t.Fatal("FindAtoms", err)
	}
	if len(vv) != 1 {
		t.Fatalf("bad result: %+v", vv)
	}
}