Skip to content
quarry Artifact [e12b386f88]
Login

Artifact [e12b386f88]

Artifact e12b386f88ae122bbe0a7ca96cb2963fb25a48958c75e99d0795a47f8a07e34f:


package api

import (
	"testing"
	"time"

	"craftdesign.group/quarry/internal/oltp"
	"craftdesign.group/quarry/internal/ops"
)

// buckets is the binding store as these ops use it.
type fakeBuckets struct{ bound map[string]oltp.BucketBinding }

func (f *fakeBuckets) Bucket(repo string) (oltp.BucketBinding, bool, error) {
	b, ok := f.bound[repo]
	return b, ok, nil
}
func (f *fakeBuckets) BindBucket(repo string, b oltp.BucketBinding) error {
	f.bound[repo] = b
	return nil
}
func (f *fakeBuckets) UnbindBucket(repo string) error { delete(f.bound, repo); return nil }
func (f *fakeBuckets) SetBucketProbe(repo string, at time.Time, probeErr string, corsOK bool) error {
	return nil
}

// A repository with no storage of its own uses the forge's, so a forge
// can host attachments for everybody on it. Without this, every
// repository had to be handed an endpoint and a key before a single
// file could go on a ticket.
func TestARepositoryWithoutABucketUsesTheForges(t *testing.T) {
	forge := oltp.BucketBinding{Endpoint: "https://forge.example", Bucket: "shared", Region: "auto"}
	own := oltp.BucketBinding{Endpoint: "https://mine.example", Bucket: "mine", Region: "auto"}

	for _, tc := range []struct {
		name, repo string
		bound      map[string]oltp.BucketBinding
		wantBucket string
		wantForge  bool
		wantBound  bool
	}{
		{"no binding anywhere", "alpha", map[string]oltp.BucketBinding{}, "", false, false},
		{"the forge supplies one", "alpha",
			map[string]oltp.BucketBinding{oltp.ForgeBucket: forge}, "shared", true, true},
		{"its own wins over the forge's", "alpha",
			map[string]oltp.BucketBinding{oltp.ForgeBucket: forge, "alpha": own}, "mine", false, true},
	} {
		d := Deps{Buckets: &fakeBuckets{bound: tc.bound}}
		info, _, err := d.bucketOf(tc.repo)
		if err != nil {
			t.Fatalf("FAIL: %s: %v", tc.name, err)
		}
		if info.Bound != tc.wantBound || info.Bucket != tc.wantBucket || info.Forge != tc.wantForge {
			t.Errorf("FAIL: %s: bound=%v bucket=%q forge=%v, want %v/%q/%v",
				tc.name, info.Bound, info.Bucket, info.Forge, tc.wantBound, tc.wantBucket, tc.wantForge)
			continue
		}
		t.Logf("ok: %s", tc.name)
	}
}

// The forge's storage is the operator's own bill, so it takes the
// operator's letter rather than a repository's.
func TestTheForgeBucketIsTheOperatorsToBind(t *testing.T) {
	r := ops.NewRegistry()
	if err := (Deps{Buckets: &fakeBuckets{bound: map[string]oltp.BucketBinding{}}}).registerForgeBucket(r); err != nil {
		t.Fatal(err)
	}
	for _, name := range []string{"fleet.bucket.get", "fleet.bucket.set", "fleet.bucket.unset"} {
		op, ok := r.Lookup(name)
		if !ok {
			t.Errorf("FAIL: %s is not registered", name)
			continue
		}
		if op.Caps != "s" || op.Auth != ops.Admin {
			t.Errorf("FAIL: %s = caps %q auth %v, want s/Admin", name, op.Caps, op.Auth)
			continue
		}
		t.Logf("ok: %s is the operator's", name)
	}
}