-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtier_test.go
174 lines (141 loc) · 3.6 KB
/
tier_test.go
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package feature_test
import (
"io/ioutil"
"os"
"reflect"
"testing"
"github.com/segmentio/feature"
)
func TestTier(t *testing.T) {
tests := []struct {
scenario string
function func(*testing.T, *feature.Tier)
}{
{
scenario: "newly created tiers have no collections",
function: testTierEmpty,
},
{
scenario: "opening a collection which does not exist returns an error",
function: testTierOpenCollectionNotExist,
},
{
scenario: "collections created are exposed when listing collections",
function: testTierCreateCollectionAndList,
},
{
scenario: "collections deleted are not exposed when listing collections",
function: testTierDeleteCollectionAndList,
},
{
scenario: "ids added to a collection are exposed when listing ids",
function: testTierCollectionAddAndList,
},
}
for _, test := range tests {
t.Run(test.scenario, func(t *testing.T) {
tmp, err := ioutil.TempDir("", "feature")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)
path := feature.MountPoint(tmp)
tier, err := path.CreateTier("standard", "1")
if err != nil {
t.Fatal(err)
}
defer tier.Close()
test.function(t, tier)
})
}
}
func testTierEmpty(t *testing.T, tier *feature.Tier) {
expectCollections(t, tier, []string{})
}
func testTierOpenCollectionNotExist(t *testing.T, tier *feature.Tier) {
_, err := tier.OpenCollection("whatever")
if err == nil || !os.IsNotExist(err) {
t.Error("unexpected error:", err)
}
}
func testTierCreateCollectionAndList(t *testing.T, tier *feature.Tier) {
c1 := createCollection(t, tier, "collection-1")
c2 := createCollection(t, tier, "collection-2")
c3 := createCollection(t, tier, "collection-3")
defer c1.Close()
defer c2.Close()
defer c3.Close()
expectCollections(t, tier, []string{
"collection-1",
"collection-2",
"collection-3",
})
}
func testTierDeleteCollectionAndList(t *testing.T, tier *feature.Tier) {
c1 := createCollection(t, tier, "collection-1")
c2 := createCollection(t, tier, "collection-2")
c3 := createCollection(t, tier, "collection-3")
defer c1.Close()
defer c2.Close()
defer c3.Close()
deleteCollection(t, tier, "collection-2")
expectCollections(t, tier, []string{
"collection-1",
"collection-3",
})
}
func testTierCollectionAddAndList(t *testing.T, tier *feature.Tier) {
col := createCollection(t, tier, "collection")
defer col.Close()
populateCollection(t, col, []string{
"id-1",
"id-2",
"id-3",
})
expectIDs(t, tier, "collection", []string{
"id-1",
"id-2",
"id-3",
})
}
func createCollection(t testing.TB, tier *feature.Tier, collection string) *feature.Collection {
t.Helper()
c, err := tier.CreateCollection(collection)
if err != nil {
t.Fatal(err)
}
return c
}
func deleteCollection(t testing.TB, tier *feature.Tier, collection string) {
t.Helper()
if err := tier.DeleteCollection(collection); err != nil {
t.Error(err)
}
}
func expectCollections(t testing.TB, tier *feature.Tier, collections []string) {
t.Helper()
found := readAll(t, tier.Collections())
if !reflect.DeepEqual(found, collections) {
t.Error("collections mismatch")
t.Logf("want: %q", collections)
t.Logf("got: %q", found)
}
}
func expectIDs(t testing.TB, tier *feature.Tier, collection string, ids []string) {
t.Helper()
found := readAll(t, tier.IDs(collection))
if !reflect.DeepEqual(found, ids) {
t.Error("ids mismatch")
t.Logf("want: %q", ids)
t.Logf("got: %q", found)
}
}
func populateCollection(t testing.TB, col *feature.Collection, ids []string) {
t.Helper()
for _, id := range ids {
col.Add(id)
}
if err := col.Sync(); err != nil {
t.Error(err)
}
}