Skip to content
quarry Artifact [fd43ac376f]
Login

Artifact [fd43ac376f]

Artifact fd43ac376fa53f346d12767909f747b0d7932fa2a62a68affc3de36f43895cea:


package oltp

import (
	"errors"
	"path/filepath"
	"testing"
)

// A binding answers both ways, and the table refuses the two shapes
// RFC 0015 rules out: a second forum for one repository, and a store
// bound to two parents.
func TestCompanionBindingAnswersBothWays(t *testing.T) {
	d, err := Open(filepath.Join(t.TempDir(), "oltp.db"))
	if err != nil {
		t.Fatal(err)
	}
	defer d.Close()
	if _, ok, _ := d.Companion("fossil", "forum"); ok {
		t.Fatal("FAIL: an unbound repository reported a companion")
	}
	if err := d.BindCompanion("fossil", "forum", "fossil-forum"); err != nil {
		t.Fatal(err)
	}
	store, ok, err := d.Companion("fossil", "forum")
	if err != nil || !ok || store != "fossil-forum" {
		t.Fatalf("FAIL: companion = %q %v %v", store, ok, err)
	}
	repo, feature, ok, err := d.CompanionParent("fossil-forum")
	if err != nil || !ok || repo != "fossil" || feature != "forum" {
		t.Fatalf("FAIL: parent = %q %q %v %v", repo, feature, ok, err)
	}
	if _, _, ok, _ := d.CompanionParent("fossil"); ok {
		t.Error("FAIL: the parent was reported as somebody's companion")
	}
	if err := d.BindCompanion("fossil", "forum", "other"); err == nil {
		t.Error("FAIL: a second forum for one repository was accepted")
	}
	if err := d.BindCompanion("sqlite", "forum", "fossil-forum"); err == nil {
		t.Error("FAIL: one store was bound to two parents")
	}
	if list, _ := d.Companions(); len(list) != 1 || list[0].Store != "fossil-forum" {
		t.Errorf("FAIL: listing = %+v", list)
	}
	if err := d.UnbindCompanion("fossil", "forum"); err != nil {
		t.Fatal(err)
	}
	if _, ok, _ := d.Companion("fossil", "forum"); ok {
		t.Error("FAIL: the binding survived unbind")
	}
	t.Log("ok: one companion per feature, one parent per store")
}

// A companion store holds its parent's content, so a takedown on the
// parent keeps the store from being dropped or purged, and a lift
// frees it.
func TestAParentsTakedownKeepsItsCompanionStore(t *testing.T) {
	d, err := Open(filepath.Join(t.TempDir(), "oltp.db"))
	if err != nil {
		t.Fatal(err)
	}
	defer d.Close()
	for _, n := range []string{"parent", "parentforum"} {
		if err := d.AddRepo(n, t.TempDir(), "", Public); err != nil {
			t.Fatal(err)
		}
	}
	if err := d.BindCompanion("parent", "forum", "parentforum"); err != nil {
		t.Fatal(err)
	}
	if err := d.SetTakedown(Takedown{Repo: "parent", Strength: LegalHold, Category: "preservation", Actor: "admin"}); err != nil {
		t.Fatal(err)
	}
	if err := d.DeleteRepo("parentforum"); !errors.Is(err, ErrHeld) {
		t.Errorf("FAIL: the held parent's forum store was dropped: %v", err)
	}
	if err := d.PurgeRepo("parentforum"); !errors.Is(err, ErrHeld) {
		t.Errorf("FAIL: the held parent's forum store was purged: %v", err)
	}
	if err := d.EndTakedown("parent", LegalHold, "admin", "", "lifted"); err != nil {
		t.Fatal(err)
	}
	if err := d.DeleteRepo("parentforum"); err != nil {
		t.Errorf("FAIL: the store stayed held after the lift: %v", err)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}