forked from grafana/tempo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_test.go
More file actions
161 lines (126 loc) · 4.58 KB
/
local_test.go
File metadata and controls
161 lines (126 loc) · 4.58 KB
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
package local
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"math/rand"
"os"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/grafana/tempo/tempodb/backend"
"github.com/grafana/tempo/tempodb/encoding/common"
)
const objectName = "test"
const objectReaderName = "test-reader"
func TestReadWrite(t *testing.T) {
tempDir, err := ioutil.TempDir("/tmp", "")
defer os.RemoveAll(tempDir)
assert.NoError(t, err, "unexpected error creating temp dir")
fakeTracesFile, err := ioutil.TempFile("/tmp", "")
defer os.Remove(fakeTracesFile.Name())
assert.NoError(t, err, "unexpected error creating temp file")
r, w, _, err := New(&Config{
Path: tempDir,
})
assert.NoError(t, err, "unexpected error creating local backend")
blockID := uuid.New()
tenantIDs := []string{"fake"}
for i := 0; i < 10; i++ {
tenantIDs = append(tenantIDs, fmt.Sprintf("%d", rand.Int()))
}
fakeMeta := &backend.BlockMeta{
BlockID: blockID,
}
fakeObject := make([]byte, 20)
_, err = rand.Read(fakeObject)
assert.NoError(t, err, "unexpected error creating fakeObject")
ctx := context.Background()
for _, id := range tenantIDs {
fakeMeta.TenantID = id
err = w.WriteBlockMeta(ctx, fakeMeta)
assert.NoError(t, err, "unexpected error writing meta")
err = w.Write(ctx, objectName, fakeMeta.BlockID, id, fakeObject)
assert.NoError(t, err, "unexpected error writing")
err = w.WriteReader(ctx, objectReaderName, fakeMeta.BlockID, id, bytes.NewBuffer(fakeObject), int64(len(fakeObject)))
assert.NoError(t, err, "unexpected error writing reader")
}
actualMeta, err := r.BlockMeta(ctx, blockID, fakeMeta.TenantID)
assert.NoError(t, err, "unexpected error reading meta")
assert.Equal(t, fakeMeta, actualMeta)
actualObject, err := r.Read(ctx, objectName, blockID, tenantIDs[0])
assert.NoError(t, err, "unexpected error reading")
assert.Equal(t, fakeObject, actualObject)
actualReadRange := make([]byte, 5)
err = r.ReadRange(ctx, objectReaderName, blockID, tenantIDs[0], 5, actualReadRange)
assert.NoError(t, err, "unexpected error range")
assert.Equal(t, fakeObject[5:10], actualReadRange)
list, err := r.Blocks(ctx, tenantIDs[0])
assert.NoError(t, err, "unexpected error reading blocklist")
assert.Len(t, list, 1)
assert.Equal(t, blockID, list[0])
tenants, err := r.Tenants(ctx)
assert.NoError(t, err, "unexpected error reading tenants")
assert.Len(t, tenants, len(tenantIDs))
}
func TestCompaction(t *testing.T) {
tempDir, err := ioutil.TempDir("/tmp", "")
defer os.RemoveAll(tempDir)
assert.NoError(t, err, "unexpected error creating temp dir")
fakeTracesFile, err := ioutil.TempFile("/tmp", "")
defer os.Remove(fakeTracesFile.Name())
assert.NoError(t, err, "unexpected error creating temp file")
r, w, c, err := New(&Config{
Path: tempDir,
})
assert.NoError(t, err, "unexpected error creating local backend")
blockID := uuid.New()
tenantIDs := []string{"fake"}
for i := 0; i < 10; i++ {
tenantIDs = append(tenantIDs, fmt.Sprintf("%d", rand.Int()))
}
fakeMeta := &backend.BlockMeta{
BlockID: blockID,
}
shardNum := common.ValidateShardCount(int(fakeMeta.BloomShardCount))
fakeBloom := make([][]byte, shardNum)
fakeIndex := make([]byte, 20)
fakeTraces := make([]byte, 200)
for i := range fakeBloom {
fakeBloom[i] = make([]byte, 20)
_, err := rand.Read(fakeBloom[i])
assert.NoError(t, err, "unexpected error creating fakeBloom")
}
_, err = rand.Read(fakeIndex)
assert.NoError(t, err, "unexpected error creating fakeIndex")
_, err = rand.Read(fakeTraces)
assert.NoError(t, err, "unexpected error creating fakeTraces")
_, err = fakeTracesFile.Write(fakeTraces)
assert.NoError(t, err, "unexpected error writing fakeTraces")
ctx := context.Background()
for _, id := range tenantIDs {
fakeMeta.TenantID = id
err = w.WriteBlockMeta(ctx, fakeMeta)
assert.NoError(t, err, "unexpected error writing")
compactedMeta, err := c.CompactedBlockMeta(blockID, id)
assert.Equal(t, backend.ErrMetaDoesNotExist, err)
assert.Nil(t, compactedMeta)
err = c.MarkBlockCompacted(blockID, id)
assert.NoError(t, err)
compactedMeta, err = c.CompactedBlockMeta(blockID, id)
assert.NoError(t, err)
assert.NotNil(t, compactedMeta)
meta, err := r.BlockMeta(ctx, blockID, id)
assert.Equal(t, backend.ErrMetaDoesNotExist, err)
assert.Nil(t, meta)
err = c.ClearBlock(blockID, id)
assert.NoError(t, err)
compactedMeta, err = c.CompactedBlockMeta(blockID, id)
assert.Equal(t, backend.ErrMetaDoesNotExist, err)
assert.Nil(t, compactedMeta)
meta, err = r.BlockMeta(ctx, blockID, id)
assert.Equal(t, backend.ErrMetaDoesNotExist, err)
assert.Nil(t, meta)
}
}