package oltp
import (
"testing"
"time"
)
// A ticket answers what begin was told until it is spent or stale; a
// hold lists in the queue until released; the policy switches ride
// the binding and survive a re-bind.
func TestUploadTicketsHoldsAndPolicy(t *testing.T) {
d := testDB(t)
id, err := d.PutUploadTicket(UploadTicket{Repo: "w", Target: "T", Name: "a.png", Size: 12, Type: "image/png", SHA256: "abc", Who: "dan"})
if err != nil || id == "" {
t.Fatalf("FAIL: put: %q %v", id, err)
}
got, ok, err := d.UploadTicket(id)
if err != nil || !ok || got.Size != 12 || got.Who != "dan" || got.Type != "image/png" {
t.Fatalf("FAIL: read back %+v %v %v", got, ok, err)
}
if err := d.DeleteUploadTicket(id); err != nil {
t.Fatal(err)
}
if _, ok, _ := d.UploadTicket(id); ok {
t.Errorf("FAIL: a spent ticket still answers")
}
stale, _ := d.PutUploadTicket(UploadTicket{Repo: "w", Target: "T", Name: "b", SHA256: "x"})
old := time.Now().UTC().Add(-UploadTicketTTL - time.Minute).Format(time.RFC3339)
if _, err := d.db.Exec(`UPDATE upload_ticket SET created = ? WHERE id = ?`, old, stale); err != nil {
t.Fatal(err)
}
if _, ok, _ := d.UploadTicket(stale); ok {
t.Errorf("FAIL: a stale ticket still answers")
}
if err := d.HoldAttachment(Hold{Repo: "w", Artifact: "art1", Target: "T", Name: "a.png", Who: "dan"}); err != nil {
t.Fatal(err)
}
holds, err := d.Holds("w")
if err != nil || len(holds) != 1 || holds[0].Artifact != "art1" {
t.Fatalf("FAIL: holds = %+v %v", holds, err)
}
if set, _ := d.HeldArtifacts("w"); !set["art1"] || set["art2"] {
t.Errorf("FAIL: held set = %v", set)
}
if err := d.ReleaseHold("w", "art1"); err != nil {
t.Fatal(err)
}
if holds, _ := d.Holds("w"); len(holds) != 0 {
t.Errorf("FAIL: released hold still listed: %+v", holds)
}
if err := d.SetBucketPolicy("w", true, true); err == nil {
t.Errorf("FAIL: policy on an unbound repository was accepted")
}
if err := d.BindBucket("w", BucketBinding{Endpoint: "https://e", Bucket: "b", Region: "auto", AccessKey: "k", Secret: "s"}); err != nil {
t.Fatal(err)
}
if err := d.SetBucketPolicy("w", true, false); err != nil {
t.Fatal(err)
}
b, _, _ := d.Bucket("w")
if !b.Archives || b.DeleteObjects {
t.Errorf("FAIL: policy = %+v", b)
}
if err := d.BindBucket("w", BucketBinding{Endpoint: "https://e2", Bucket: "b", Region: "auto", AccessKey: "k", Secret: "s"}); err != nil {
t.Fatal(err)
}
if b, _, _ = d.Bucket("w"); !b.Archives || b.Endpoint != "https://e2" {
t.Errorf("FAIL: a re-bind lost the policy: %+v", b)
}
t.Log("ok: tickets expire and spend, holds list and release, policy rides the binding")
}