github.com/grafana/pyroscope@v1.18.0/pkg/phlaredb/block/global_markers_test.go (about)

     1  // SPDX-License-Identifier: AGPL-3.0-only
     2  // Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/storage/tsdb/bucketindex/markers_test.go
     3  // Provenance-includes-license: Apache-2.0
     4  // Provenance-includes-copyright: The Cortex Authors.
     5  
     6  package block
     7  
     8  import (
     9  	"context"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/oklog/ulid/v2"
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  
    17  	"github.com/grafana/pyroscope/pkg/objstore/testutil"
    18  )
    19  
    20  func TestDeletionMarkFilepath(t *testing.T) {
    21  	id := ulid.MustNew(1, nil)
    22  
    23  	assert.Equal(t, "markers/"+id.String()+"-deletion-mark.json", DeletionMarkFilepath(id))
    24  }
    25  
    26  func TestIsDeletionMarkFilename(t *testing.T) {
    27  	expected := ulid.MustNew(1, nil)
    28  
    29  	_, ok := IsDeletionMarkFilename("xxx")
    30  	assert.False(t, ok)
    31  
    32  	_, ok = IsDeletionMarkFilename("xxx-deletion-mark.json")
    33  	assert.False(t, ok)
    34  
    35  	_, ok = IsDeletionMarkFilename("tenant-deletion-mark.json")
    36  	assert.False(t, ok)
    37  
    38  	actual, ok := IsDeletionMarkFilename(expected.String() + "-deletion-mark.json")
    39  	assert.True(t, ok)
    40  	assert.Equal(t, expected, actual)
    41  }
    42  
    43  func TestNoCompactMarkFilepath(t *testing.T) {
    44  	id := ulid.MustNew(1, nil)
    45  
    46  	assert.Equal(t, "markers/"+id.String()+"-no-compact-mark.json", NoCompactMarkFilepath(id))
    47  }
    48  
    49  func TestIsNoCompactMarkFilename(t *testing.T) {
    50  	expected := ulid.MustNew(1, nil)
    51  
    52  	_, ok := IsNoCompactMarkFilename("xxx")
    53  	assert.False(t, ok)
    54  
    55  	_, ok = IsNoCompactMarkFilename("xxx-no-compact-mark.json")
    56  	assert.False(t, ok)
    57  
    58  	_, ok = IsNoCompactMarkFilename("tenant-no-compact-mark.json")
    59  	assert.False(t, ok)
    60  
    61  	actual, ok := IsNoCompactMarkFilename(expected.String() + "-no-compact-mark.json")
    62  	assert.True(t, ok)
    63  	assert.Equal(t, expected, actual)
    64  }
    65  
    66  func TestListBlockDeletionMarks(t *testing.T) {
    67  	var (
    68  		ctx    = context.Background()
    69  		block1 = ulid.MustNew(1, nil)
    70  		block2 = ulid.MustNew(2, nil)
    71  		block3 = ulid.MustNew(3, nil)
    72  	)
    73  
    74  	t.Run("should return an empty map on empty bucket", func(t *testing.T) {
    75  		bkt, _ := testutil.NewFilesystemBucket(t, ctx, t.TempDir())
    76  
    77  		actualMarks, actualErr := ListBlockDeletionMarks(ctx, bkt)
    78  		require.NoError(t, actualErr)
    79  		assert.Empty(t, actualMarks)
    80  	})
    81  
    82  	t.Run("should return a map with the locations of the block deletion marks found", func(t *testing.T) {
    83  		bkt, _ := testutil.NewFilesystemBucket(t, ctx, t.TempDir())
    84  
    85  		require.NoError(t, bkt.Upload(ctx, DeletionMarkFilepath(block1), strings.NewReader("{}")))
    86  		require.NoError(t, bkt.Upload(ctx, NoCompactMarkFilepath(block2), strings.NewReader("{}")))
    87  		require.NoError(t, bkt.Upload(ctx, DeletionMarkFilepath(block3), strings.NewReader("{}")))
    88  
    89  		actualMarks, actualErr := ListBlockDeletionMarks(ctx, bkt)
    90  		require.NoError(t, actualErr)
    91  		assert.Equal(t, map[ulid.ULID]struct{}{
    92  			block1: {},
    93  			block3: {},
    94  		}, actualMarks)
    95  	})
    96  }