github.com/thanos-io/thanos@v0.32.5/pkg/store/cache/cachekey/cachekey_test.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package cachekey 5 6 import ( 7 "testing" 8 9 "github.com/efficientgo/core/testutil" 10 ) 11 12 func TestParseBucketCacheKey(t *testing.T) { 13 testcases := []struct { 14 key string 15 expected BucketCacheKey 16 expectedErr error 17 }{ 18 { 19 key: "exists:name", 20 expected: BucketCacheKey{ 21 Verb: ExistsVerb, 22 Name: "name", 23 Start: 0, 24 End: 0, 25 }, 26 expectedErr: nil, 27 }, 28 { 29 key: "content:name", 30 expected: BucketCacheKey{ 31 Verb: ContentVerb, 32 Name: "name", 33 Start: 0, 34 End: 0, 35 }, 36 expectedErr: nil, 37 }, 38 { 39 key: "iter:name", 40 expected: BucketCacheKey{ 41 Verb: IterVerb, 42 Name: "name", 43 Start: 0, 44 End: 0, 45 }, 46 expectedErr: nil, 47 }, 48 { 49 key: "attrs:name", 50 expected: BucketCacheKey{ 51 Verb: AttributesVerb, 52 Name: "name", 53 Start: 0, 54 End: 0, 55 }, 56 expectedErr: nil, 57 }, 58 { 59 key: "subrange:name:10:20", 60 expected: BucketCacheKey{ 61 Verb: SubrangeVerb, 62 Name: "name", 63 Start: 10, 64 End: 20, 65 }, 66 expectedErr: nil, 67 }, 68 // Any VerbType other than SubrangeVerb should not have a "start" and "end". 69 { 70 key: "iter:name:10:20", 71 expected: BucketCacheKey{}, 72 expectedErr: ErrInvalidBucketCacheKeyFormat, 73 }, 74 // Key must always have a name. 75 { 76 key: "iter", 77 expected: BucketCacheKey{}, 78 expectedErr: ErrInvalidBucketCacheKeyFormat, 79 }, 80 // Invalid VerbType should return an error. 81 { 82 key: "random:name", 83 expected: BucketCacheKey{}, 84 expectedErr: ErrInvalidBucketCacheKeyVerb, 85 }, 86 // Start must be an integer. 87 { 88 key: "subrange:name:random:10", 89 expected: BucketCacheKey{}, 90 expectedErr: ErrParseKeyInt, 91 }, 92 // End must be an integer. 93 { 94 key: "subrange:name:10:random", 95 expected: BucketCacheKey{}, 96 expectedErr: ErrParseKeyInt, 97 }, 98 // SubrangeVerb must have start and end. 99 { 100 key: "subrange:name", 101 expected: BucketCacheKey{}, 102 expectedErr: ErrInvalidBucketCacheKeyFormat, 103 }, 104 // SubrangeVerb must have start and end both. 105 { 106 key: "subrange:name:10", 107 expected: BucketCacheKey{}, 108 expectedErr: ErrInvalidBucketCacheKeyFormat, 109 }, 110 // Key must not be an empty string. 111 { 112 key: "", 113 expected: BucketCacheKey{}, 114 expectedErr: ErrInvalidBucketCacheKeyFormat, 115 }, 116 } 117 118 for _, tc := range testcases { 119 res, err := ParseBucketCacheKey(tc.key) 120 testutil.Equals(t, tc.expectedErr, err) 121 testutil.Equals(t, tc.expected, res) 122 } 123 }