github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/cortex/runtime_config_test.go (about) 1 package cortex 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 10 "github.com/cortexproject/cortex/pkg/util/validation" 11 ) 12 13 // Given limits are usually loaded via a config file, and that 14 // a configmap is limited to 1MB, we need to minimise the limits file. 15 // One way to do it is via YAML anchors. 16 func TestLoadRuntimeConfig_ShouldLoadAnchoredYAML(t *testing.T) { 17 yamlFile := strings.NewReader(` 18 overrides: 19 '1234': &id001 20 ingestion_burst_size: 15000 21 ingestion_rate: 1500 22 max_global_series_per_metric: 7000 23 max_global_series_per_user: 15000 24 max_samples_per_query: 100000 25 max_series_per_metric: 0 26 max_series_per_query: 30000 27 max_series_per_user: 0 28 ruler_max_rule_groups_per_tenant: 20 29 ruler_max_rules_per_rule_group: 20 30 '1235': *id001 31 '1236': *id001 32 `) 33 runtimeCfg, err := loadRuntimeConfig(yamlFile) 34 require.NoError(t, err) 35 36 limits := validation.Limits{ 37 IngestionRate: 1500, 38 IngestionBurstSize: 15000, 39 MaxGlobalSeriesPerUser: 15000, 40 MaxGlobalSeriesPerMetric: 7000, 41 MaxSeriesPerQuery: 30000, 42 MaxSamplesPerQuery: 100000, 43 RulerMaxRulesPerRuleGroup: 20, 44 RulerMaxRuleGroupsPerTenant: 20, 45 } 46 47 loadedLimits := runtimeCfg.(*runtimeConfigValues).TenantLimits 48 require.Equal(t, 3, len(loadedLimits)) 49 require.Equal(t, limits, *loadedLimits["1234"]) 50 require.Equal(t, limits, *loadedLimits["1235"]) 51 require.Equal(t, limits, *loadedLimits["1236"]) 52 } 53 54 func TestLoadRuntimeConfig_ShouldLoadEmptyFile(t *testing.T) { 55 yamlFile := strings.NewReader(` 56 # This is an empty YAML. 57 `) 58 actual, err := loadRuntimeConfig(yamlFile) 59 require.NoError(t, err) 60 assert.Equal(t, &runtimeConfigValues{}, actual) 61 } 62 63 func TestLoadRuntimeConfig_MissingPointerFieldsAreNil(t *testing.T) { 64 yamlFile := strings.NewReader(` 65 # This is an empty YAML. 66 `) 67 actual, err := loadRuntimeConfig(yamlFile) 68 require.NoError(t, err) 69 70 actualCfg, ok := actual.(*runtimeConfigValues) 71 require.Truef(t, ok, "expected to be able to cast %+v to runtimeConfigValues", actual) 72 73 // Ensure that when settings are omitted, the pointers are nil. See #4228 74 assert.Nil(t, actualCfg.IngesterLimits) 75 } 76 77 func TestLoadRuntimeConfig_ShouldReturnErrorOnMultipleDocumentsInTheConfig(t *testing.T) { 78 cases := []string{ 79 ` 80 --- 81 --- 82 `, ` 83 --- 84 overrides: 85 '1234': 86 ingestion_burst_size: 123 87 --- 88 overrides: 89 '1234': 90 ingestion_burst_size: 123 91 `, ` 92 --- 93 # This is an empty YAML. 94 --- 95 overrides: 96 '1234': 97 ingestion_burst_size: 123 98 `, ` 99 --- 100 overrides: 101 '1234': 102 ingestion_burst_size: 123 103 --- 104 # This is an empty YAML. 105 `, 106 } 107 108 for _, tc := range cases { 109 actual, err := loadRuntimeConfig(strings.NewReader(tc)) 110 assert.Equal(t, errMultipleDocuments, err) 111 assert.Nil(t, actual) 112 } 113 }