github.com/thanos-io/thanos@v0.32.5/internal/cortex/util/validation/notifications_limit_flag_test.go (about) 1 // Copyright (c) The Cortex Authors. 2 // Licensed under the Apache License 2.0. 3 4 package validation 5 6 import ( 7 "bytes" 8 "flag" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 "gopkg.in/yaml.v2" 14 ) 15 16 func TestNotificationLimitsMap(t *testing.T) { 17 for name, tc := range map[string]struct { 18 args []string 19 expected NotificationRateLimitMap 20 error string 21 }{ 22 "basic test": { 23 args: []string{"-map-flag", "{\"email\": 100 }"}, 24 expected: NotificationRateLimitMap{ 25 "email": 100, 26 }, 27 }, 28 29 "unknown integration": { 30 args: []string{"-map-flag", "{\"unknown\": 200 }"}, 31 error: "invalid value \"{\\\"unknown\\\": 200 }\" for flag -map-flag: unknown integration name: unknown", 32 }, 33 34 "parsing error": { 35 args: []string{"-map-flag", "{\"hello\": ..."}, 36 error: "invalid value \"{\\\"hello\\\": ...\" for flag -map-flag: invalid character '.' looking for beginning of value", 37 }, 38 } { 39 t.Run(name, func(t *testing.T) { 40 v := NotificationRateLimitMap{} 41 42 fs := flag.NewFlagSet("test", flag.ContinueOnError) 43 fs.SetOutput(&bytes.Buffer{}) // otherwise errors would go to stderr. 44 fs.Var(v, "map-flag", "Map flag, you can pass JSON into this") 45 err := fs.Parse(tc.args) 46 47 if tc.error != "" { 48 require.NotNil(t, err) 49 assert.Equal(t, tc.error, err.Error()) 50 } else { 51 assert.NoError(t, err) 52 assert.Equal(t, tc.expected, v) 53 } 54 }) 55 } 56 } 57 58 type TestStruct struct { 59 Flag NotificationRateLimitMap `yaml:"flag"` 60 } 61 62 func TestNotificationsLimitMapYaml(t *testing.T) { 63 64 var testStruct TestStruct 65 testStruct.Flag = map[string]float64{} 66 67 require.NoError(t, testStruct.Flag.Set("{\"email\": 500 }")) 68 expected := []byte(`flag: 69 email: 500 70 `) 71 72 actual, err := yaml.Marshal(testStruct) 73 require.NoError(t, err) 74 assert.Equal(t, expected, actual) 75 76 var actualStruct TestStruct 77 actualStruct.Flag = NotificationRateLimitMap{} // must be set, otherwise unmarshalling panics. 78 79 err = yaml.Unmarshal(expected, &actualStruct) 80 require.NoError(t, err) 81 assert.Equal(t, testStruct, actualStruct) 82 } 83 84 func TestUnknownIntegrationWhenLoadingYaml(t *testing.T) { 85 var s TestStruct 86 s.Flag = NotificationRateLimitMap{} // must be set, otherwise unmarshalling panics. 87 88 yamlInput := `flag: 89 unknown_integration: 500 90 ` 91 92 err := yaml.Unmarshal([]byte(yamlInput), &s) 93 require.NotNil(t, err) 94 require.Equal(t, "unknown integration name: unknown_integration", err.Error()) 95 } 96 97 func TestWrongYamlStructureWhenLoadingYaml(t *testing.T) { 98 var s TestStruct 99 s.Flag = NotificationRateLimitMap{} // must be set, otherwise unmarshalling panics. 100 101 yamlInput := `flag: 102 email: 103 rate_limit: 7777 104 burst_size: 7777 105 ` 106 107 err := yaml.Unmarshal([]byte(yamlInput), &s) 108 require.NotNil(t, err) 109 require.Contains(t, err.Error(), "cannot unmarshal !!map into float64") 110 }