cmd/gitbayd/restoredrill_test.go

main
gitbay/cmd/gitbayd/restoredrill_test.go history · blame · raw

63 lines · 1863 bytes

1 symbol in this file
 1package main
 2
 3import (
 4	"os"
 5	"path/filepath"
 6	"strings"
 7	"testing"
 8)
 9
10// A drill restores a full archive into an empty directory as a usable
11// root, verifies it, and refuses a directory that already holds files.
12func TestRestoreDrill(t *testing.T) {
13	cfg := testConfig(t)
14	root := cfg.Server.Root
15	st, err := openStore(cfg)
16	if err != nil {
17		t.Fatal(err)
18	}
19	uid, err := st.CreateUser("krz", false)
20	if err != nil {
21		t.Fatal(err)
22	}
23	rid, err := st.CreateRepo("user", uid, "thing", "public")
24	if err != nil {
25		t.Fatal(err)
26	}
27	if _, err := st.CreateIssue(rid, uid, "bug", "", "md"); err != nil {
28		t.Fatal(err)
29	}
30	if err := st.RecordEvent(rid, uid, "push", "{}"); err != nil {
31		t.Fatal(err)
32	}
33	st.Close()
34	work := t.TempDir()
35	gitIn(t, work, "init", "-q", "-b", "main")
36	writeFile(t, filepath.Join(work, "a.txt"), []byte("a\n"))
37	gitIn(t, work, "add", "a.txt")
38	gitIn(t, work, "commit", "-q", "-m", "one")
39	gitIn(t, work, "clone", "-q", "--bare", work, filepath.Join(root, "repos", "krz", "thing.git"))
40	writeFile(t, filepath.Join(root, "ssh", "host_ed25519"), []byte("key\n"))
41
42	archive := filepath.Join(t.TempDir(), "b.tar.gz")
43	if err := runBackup(cfg, archive, false); err != nil {
44		t.Fatal(err)
45	}
46	into := filepath.Join(t.TempDir(), "restored")
47	if err := restoreDrill(archive, "", into); err != nil {
48		t.Fatal(err)
49	}
50	for _, p := range []string{"gitbay.db", "ssh/host_ed25519", "repos/krz/thing.git/HEAD"} {
51		if _, err := os.Stat(filepath.Join(into, p)); err != nil {
52			t.Errorf("restored root lacks %s: %v", p, err)
53		}
54	}
55	if got := gitIn(t, filepath.Join(into, "repos", "krz", "thing.git"), "log", "--format=%s", "main"); got != "one" {
56		t.Errorf("restored log %q", got)
57	}
58
59	err = restoreDrill(archive, "", into)
60	if err == nil || !strings.Contains(err.Error(), "not empty") {
61		t.Fatalf("drill into a non-empty directory: %v", err)
62	}
63}