github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/storage/tsdb/config_test.go (about) 1 package tsdb 2 3 import ( 4 "flag" 5 "testing" 6 "time" 7 8 "github.com/grafana/dskit/flagext" 9 "github.com/stretchr/testify/assert" 10 11 "github.com/cortexproject/cortex/pkg/storage/bucket" 12 ) 13 14 func TestConfig_Validate(t *testing.T) { 15 t.Parallel() 16 17 tests := map[string]struct { 18 setup func(*BlocksStorageConfig) 19 expectedErr error 20 }{ 21 "should pass on S3 backend": { 22 setup: func(cfg *BlocksStorageConfig) { 23 cfg.Bucket.Backend = "s3" 24 }, 25 expectedErr: nil, 26 }, 27 "should pass on GCS backend": { 28 setup: func(cfg *BlocksStorageConfig) { 29 cfg.Bucket.Backend = "gcs" 30 }, 31 expectedErr: nil, 32 }, 33 "should fail on unknown storage backend": { 34 setup: func(cfg *BlocksStorageConfig) { 35 cfg.Bucket.Backend = "unknown" 36 }, 37 expectedErr: bucket.ErrUnsupportedStorageBackend, 38 }, 39 "should fail on invalid ship concurrency": { 40 setup: func(cfg *BlocksStorageConfig) { 41 cfg.TSDB.ShipConcurrency = 0 42 }, 43 expectedErr: errInvalidShipConcurrency, 44 }, 45 "should pass on invalid ship concurrency but shipping is disabled": { 46 setup: func(cfg *BlocksStorageConfig) { 47 cfg.TSDB.ShipConcurrency = 0 48 cfg.TSDB.ShipInterval = 0 49 }, 50 expectedErr: nil, 51 }, 52 "should fail on invalid opening concurrency": { 53 setup: func(cfg *BlocksStorageConfig) { 54 cfg.TSDB.MaxTSDBOpeningConcurrencyOnStartup = 0 55 }, 56 expectedErr: errInvalidOpeningConcurrency, 57 }, 58 "should fail on invalid compaction interval": { 59 setup: func(cfg *BlocksStorageConfig) { 60 cfg.TSDB.HeadCompactionInterval = 0 61 }, 62 expectedErr: errInvalidCompactionInterval, 63 }, 64 "should fail on too high compaction interval": { 65 setup: func(cfg *BlocksStorageConfig) { 66 cfg.TSDB.HeadCompactionInterval = 10 * time.Minute 67 }, 68 expectedErr: errInvalidCompactionInterval, 69 }, 70 "should fail on invalid compaction concurrency": { 71 setup: func(cfg *BlocksStorageConfig) { 72 cfg.TSDB.HeadCompactionConcurrency = 0 73 }, 74 expectedErr: errInvalidCompactionConcurrency, 75 }, 76 "should pass on valid compaction concurrency": { 77 setup: func(cfg *BlocksStorageConfig) { 78 cfg.TSDB.HeadCompactionConcurrency = 10 79 }, 80 expectedErr: nil, 81 }, 82 "should fail on negative stripe size": { 83 setup: func(cfg *BlocksStorageConfig) { 84 cfg.TSDB.StripeSize = -2 85 }, 86 expectedErr: errInvalidStripeSize, 87 }, 88 "should fail on stripe size 0": { 89 setup: func(cfg *BlocksStorageConfig) { 90 cfg.TSDB.StripeSize = 0 91 }, 92 expectedErr: errInvalidStripeSize, 93 }, 94 "should fail on stripe size 1": { 95 setup: func(cfg *BlocksStorageConfig) { 96 cfg.TSDB.StripeSize = 1 97 }, 98 expectedErr: errInvalidStripeSize, 99 }, 100 "should pass on valid stripe size": { 101 setup: func(cfg *BlocksStorageConfig) { 102 cfg.TSDB.StripeSize = 1 << 14 103 }, 104 expectedErr: nil, 105 }, 106 "should fail on empty block ranges": { 107 setup: func(cfg *BlocksStorageConfig) { 108 cfg.TSDB.BlockRanges = nil 109 }, 110 expectedErr: errEmptyBlockranges, 111 }, 112 "should fail on invalid TSDB WAL segment size": { 113 setup: func(cfg *BlocksStorageConfig) { 114 cfg.TSDB.WALSegmentSizeBytes = 0 115 }, 116 expectedErr: errInvalidWALSegmentSizeBytes, 117 }, 118 } 119 120 for testName, testData := range tests { 121 testData := testData 122 123 t.Run(testName, func(t *testing.T) { 124 cfg := &BlocksStorageConfig{} 125 flagext.DefaultValues(cfg) 126 testData.setup(cfg) 127 128 actualErr := cfg.Validate() 129 assert.Equal(t, testData.expectedErr, actualErr) 130 }) 131 } 132 } 133 134 func TestConfig_DurationList(t *testing.T) { 135 t.Parallel() 136 137 tests := map[string]struct { 138 cfg BlocksStorageConfig 139 expectedRanges []int64 140 f func(*BlocksStorageConfig) 141 }{ 142 "default to 2h": { 143 cfg: BlocksStorageConfig{}, 144 expectedRanges: []int64{7200000}, 145 f: func(c *BlocksStorageConfig) { 146 c.RegisterFlags(&flag.FlagSet{}) 147 }, 148 }, 149 "parse ranges correctly": { 150 cfg: BlocksStorageConfig{ 151 TSDB: TSDBConfig{ 152 BlockRanges: []time.Duration{ 153 2 * time.Hour, 154 10 * time.Hour, 155 50 * time.Hour, 156 }, 157 }, 158 }, 159 expectedRanges: []int64{7200000, 36000000, 180000000}, 160 f: func(*BlocksStorageConfig) {}, 161 }, 162 "handle multiple flag parse": { 163 cfg: BlocksStorageConfig{}, 164 expectedRanges: []int64{7200000}, 165 f: func(c *BlocksStorageConfig) { 166 c.RegisterFlags(&flag.FlagSet{}) 167 c.RegisterFlags(&flag.FlagSet{}) 168 }, 169 }, 170 } 171 172 for name, data := range tests { 173 testdata := data 174 175 t.Run(name, func(t *testing.T) { 176 testdata.f(&testdata.cfg) 177 assert.Equal(t, testdata.expectedRanges, testdata.cfg.TSDB.BlockRanges.ToMilliseconds()) 178 }) 179 } 180 }