-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing forked gateway tests (#5118)
* Add missing tests for `storegateway`. * Fork cortex tsdb testutil package. * Fix import lint.
- Loading branch information
1 parent
34a4b24
commit 7d50172
Showing
13 changed files
with
3,796 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package testutil | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/oklog/ulid" | ||
"github.com/prometheus/prometheus/tsdb" | ||
"github.com/stretchr/testify/require" | ||
"github.com/thanos-io/thanos/pkg/block/metadata" | ||
"github.com/thanos-io/thanos/pkg/objstore" | ||
) | ||
|
||
func MockStorageBlock(t testing.TB, bucket objstore.Bucket, userID string, minT, maxT int64) tsdb.BlockMeta { | ||
// Generate a block ID whose timestamp matches the maxT (for simplicity we assume it | ||
// has been compacted and shipped in zero time, even if not realistic). | ||
id := ulid.MustNew(uint64(maxT), rand.Reader) | ||
|
||
meta := tsdb.BlockMeta{ | ||
Version: 1, | ||
ULID: id, | ||
MinTime: minT, | ||
MaxTime: maxT, | ||
Compaction: tsdb.BlockMetaCompaction{ | ||
Level: 1, | ||
Sources: []ulid.ULID{id}, | ||
}, | ||
} | ||
|
||
metaContent, err := json.Marshal(meta) | ||
if err != nil { | ||
panic("failed to marshal mocked block meta") | ||
} | ||
|
||
metaContentReader := strings.NewReader(string(metaContent)) | ||
metaPath := fmt.Sprintf("%s/%s/meta.json", userID, id.String()) | ||
require.NoError(t, bucket.Upload(context.Background(), metaPath, metaContentReader)) | ||
|
||
// Upload an empty index, just to make sure the meta.json is not the only object in the block location. | ||
indexPath := fmt.Sprintf("%s/%s/index", userID, id.String()) | ||
require.NoError(t, bucket.Upload(context.Background(), indexPath, strings.NewReader(""))) | ||
|
||
return meta | ||
} | ||
|
||
func MockStorageDeletionMark(t testing.TB, bucket objstore.Bucket, userID string, meta tsdb.BlockMeta) *metadata.DeletionMark { | ||
mark := metadata.DeletionMark{ | ||
ID: meta.ULID, | ||
DeletionTime: time.Now().Add(-time.Minute).Unix(), | ||
Version: metadata.DeletionMarkVersion1, | ||
} | ||
|
||
markContent, err := json.Marshal(mark) | ||
if err != nil { | ||
panic("failed to marshal mocked block meta") | ||
} | ||
|
||
markContentReader := strings.NewReader(string(markContent)) | ||
markPath := fmt.Sprintf("%s/%s/%s", userID, meta.ULID.String(), metadata.DeletionMarkFilename) | ||
require.NoError(t, bucket.Upload(context.Background(), markPath, markContentReader)) | ||
|
||
return &mark | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package testutil | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/thanos-io/thanos/pkg/objstore" | ||
|
||
"github.com/cortexproject/cortex/pkg/storage/bucket/filesystem" | ||
) | ||
|
||
func PrepareFilesystemBucket(t testing.TB) (objstore.Bucket, string) { | ||
storageDir, err := ioutil.TempDir(os.TempDir(), "bucket") | ||
require.NoError(t, err) | ||
|
||
t.Cleanup(func() { | ||
require.NoError(t, os.RemoveAll(storageDir)) | ||
}) | ||
|
||
bkt, err := filesystem.NewBucketClient(filesystem.Config{Directory: storageDir}) | ||
require.NoError(t, err) | ||
|
||
return objstore.BucketWithMetrics("test", bkt, nil), storageDir | ||
} |
Oops, something went wrong.