github.com/grafana/pyroscope@v1.18.0/pkg/phlaredb/bucketindex/index_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/index_test.go
     3  // Provenance-includes-license: Apache-2.0
     4  // Provenance-includes-copyright: The Cortex Authors.
     5  
     6  package bucketindex
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/oklog/ulid/v2"
    12  	"github.com/prometheus/common/model"
    13  	"github.com/stretchr/testify/assert"
    14  
    15  	"github.com/grafana/pyroscope/pkg/phlaredb/block"
    16  	"github.com/grafana/pyroscope/pkg/phlaredb/sharding"
    17  )
    18  
    19  func TestIndex_RemoveBlock(t *testing.T) {
    20  	block1 := ulid.MustNew(1, nil)
    21  	block2 := ulid.MustNew(2, nil)
    22  	block3 := ulid.MustNew(3, nil)
    23  	idx := &Index{
    24  		Blocks:             Blocks{{ID: block1}, {ID: block2}, {ID: block3}},
    25  		BlockDeletionMarks: BlockDeletionMarks{{ID: block2}, {ID: block3}},
    26  	}
    27  
    28  	idx.RemoveBlock(block2)
    29  	assert.ElementsMatch(t, []ulid.ULID{block1, block3}, idx.Blocks.GetULIDs())
    30  	assert.ElementsMatch(t, []ulid.ULID{block3}, idx.BlockDeletionMarks.GetULIDs())
    31  }
    32  
    33  func TestBlockFromMeta(t *testing.T) {
    34  	blockID := ulid.MustNew(1, nil)
    35  
    36  	tests := map[string]struct {
    37  		meta     block.Meta
    38  		expected Block
    39  	}{
    40  		"meta.json": {
    41  			meta: block.Meta{
    42  				ULID:    blockID,
    43  				MinTime: model.Time(10),
    44  				MaxTime: model.Time(20),
    45  				Labels: map[string]string{
    46  					sharding.CompactorShardIDLabel: "1_of_8",
    47  				},
    48  			},
    49  			expected: Block{
    50  				ID:               blockID,
    51  				MinTime:          model.Time(10),
    52  				MaxTime:          model.Time(20),
    53  				CompactorShardID: "1_of_8",
    54  				CompactionLevel:  0,
    55  			},
    56  		},
    57  	}
    58  
    59  	for testName, testData := range tests {
    60  		t.Run(testName, func(t *testing.T) {
    61  			assert.Equal(t, testData.expected, *BlockFromMeta(testData.meta))
    62  		})
    63  	}
    64  }
    65  
    66  func TestBlock_Within(t *testing.T) {
    67  	tests := []struct {
    68  		block    *Block
    69  		minT     int64
    70  		maxT     int64
    71  		expected bool
    72  	}{
    73  		{
    74  			block:    &Block{MinTime: 10, MaxTime: 20},
    75  			minT:     5,
    76  			maxT:     9,
    77  			expected: false,
    78  		}, {
    79  			block:    &Block{MinTime: 10, MaxTime: 20},
    80  			minT:     5,
    81  			maxT:     10,
    82  			expected: true,
    83  		}, {
    84  			block:    &Block{MinTime: 10, MaxTime: 20},
    85  			minT:     5,
    86  			maxT:     10,
    87  			expected: true,
    88  		}, {
    89  			block:    &Block{MinTime: 10, MaxTime: 20},
    90  			minT:     11,
    91  			maxT:     13,
    92  			expected: true,
    93  		}, {
    94  			block:    &Block{MinTime: 10, MaxTime: 20},
    95  			minT:     19,
    96  			maxT:     21,
    97  			expected: true,
    98  		}, {
    99  			block:    &Block{MinTime: 10, MaxTime: 20},
   100  			minT:     20,
   101  			maxT:     21,
   102  			expected: true,
   103  		},
   104  	}
   105  
   106  	for _, tc := range tests {
   107  		assert.Equal(t, tc.expected, tc.block.Within(model.Time(tc.minT), model.Time(tc.maxT)))
   108  	}
   109  }
   110  
   111  func TestBlockDeletionMark_DeletionMark(t *testing.T) {
   112  	block1 := ulid.MustNew(1, nil)
   113  	mark := &BlockDeletionMark{ID: block1, DeletionTime: 1}
   114  
   115  	assert.Equal(t, &block.DeletionMark{
   116  		ID:           block1,
   117  		Version:      block.DeletionMarkVersion1,
   118  		DeletionTime: 1,
   119  	}, mark.BlockDeletionMark())
   120  }
   121  
   122  func TestBlockDeletionMarks_Clone(t *testing.T) {
   123  	block1 := ulid.MustNew(1, nil)
   124  	block2 := ulid.MustNew(2, nil)
   125  	orig := BlockDeletionMarks{{ID: block1, DeletionTime: 1}, {ID: block2, DeletionTime: 2}}
   126  
   127  	// The clone must be identical.
   128  	clone := orig.Clone()
   129  	assert.Equal(t, orig, clone)
   130  
   131  	// Changes to the original shouldn't be reflected to the clone.
   132  	orig[0].DeletionTime = -1
   133  	assert.Equal(t, int64(1), clone[0].DeletionTime)
   134  }