aboutsummaryrefslogtreecommitdiff
path: root/object/store/loose/quarantine_test.go
blob: 4fd1b8f9fc57371b5ba568a8883868f5f4d5e371 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package loose_test

import (
	"bytes"
	"testing"

	"codeberg.org/lindenii/furgit/internal/testgit"
	objectid "codeberg.org/lindenii/furgit/object/id"
	objectstore "codeberg.org/lindenii/furgit/object/store"
	objecttype "codeberg.org/lindenii/furgit/object/type"
)

func TestLooseQuarantinePromotePublishesWrittenObjects(t *testing.T) {
	t.Parallel()

	testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
		testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
		store := openLooseStore(t, testRepo, algo)

		quarantiner, ok := any(store).(objectstore.ObjectQuarantiner)
		if !ok {
			t.Fatal("loose store does not implement ObjectQuarantiner")
		}

		quarantine, err := quarantiner.BeginObjectQuarantine(objectstore.ObjectQuarantineOptions{})
		if err != nil {
			t.Fatalf("BeginObjectQuarantine: %v", err)
		}

		content := []byte("quarantined loose object\n")

		id, err := quarantine.WriteBytesContent(objecttype.TypeBlob, content)
		if err != nil {
			t.Fatalf("quarantine.WriteBytesContent: %v", err)
		}

		ty, got, err := quarantine.ReadBytesContent(id)
		if err != nil {
			t.Fatalf("quarantine.ReadBytesContent: %v", err)
		}

		if ty != objecttype.TypeBlob {
			t.Fatalf("quarantine.ReadBytesContent type = %v, want %v", ty, objecttype.TypeBlob)
		}

		if !bytes.Equal(got, content) {
			t.Fatal("quarantine.ReadBytesContent mismatch")
		}

		_, _, err = store.ReadBytesContent(id)
		if err == nil {
			t.Fatal("store.ReadBytesContent unexpectedly saw quarantined object before promote")
		}

		err = quarantine.Promote()
		if err != nil {
			t.Fatalf("quarantine.Promote: %v", err)
		}

		err = store.Refresh()
		if err != nil {
			t.Fatalf("store.Refresh: %v", err)
		}

		ty, got, err = store.ReadBytesContent(id)
		if err != nil {
			t.Fatalf("store.ReadBytesContent after promote: %v", err)
		}

		if ty != objecttype.TypeBlob {
			t.Fatalf("store.ReadBytesContent type = %v, want %v", ty, objecttype.TypeBlob)
		}

		if !bytes.Equal(got, content) {
			t.Fatal("store.ReadBytesContent mismatch")
		}
	})
}

func TestLooseQuarantineDiscardDropsWrittenObjects(t *testing.T) {
	t.Parallel()

	testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
		testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
		store := openLooseStore(t, testRepo, algo)

		quarantiner, ok := any(store).(objectstore.ObjectQuarantiner)
		if !ok {
			t.Fatal("expected objectstore.ObjectQuarantiner")
		}

		quarantine, err := quarantiner.BeginObjectQuarantine(objectstore.ObjectQuarantineOptions{})
		if err != nil {
			t.Fatalf("BeginObjectQuarantine: %v", err)
		}

		content := []byte("discarded loose object\n")

		id, err := quarantine.WriteBytesContent(objecttype.TypeBlob, content)
		if err != nil {
			t.Fatalf("quarantine.WriteBytesContent: %v", err)
		}

		err = quarantine.Discard()
		if err != nil {
			t.Fatalf("quarantine.Discard: %v", err)
		}

		err = store.Refresh()
		if err != nil {
			t.Fatalf("store.Refresh: %v", err)
		}

		_, _, err = store.ReadBytesContent(id)
		if err == nil {
			t.Fatal("store.ReadBytesContent unexpectedly saw discarded object")
		}
	})
}