github.com/thanos-io/thanos@v0.32.5/pkg/alert/config_test.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package alert 5 6 import ( 7 "testing" 8 "time" 9 10 "gopkg.in/yaml.v2" 11 12 "github.com/efficientgo/core/testutil" 13 "github.com/thanos-io/thanos/pkg/httpconfig" 14 ) 15 16 func TestUnmarshalAPIVersion(t *testing.T) { 17 for _, tc := range []struct { 18 v string 19 20 err bool 21 expected APIVersion 22 }{ 23 { 24 v: "v1", 25 expected: APIv1, 26 }, 27 { 28 v: "v3", 29 err: true, 30 }, 31 { 32 v: "{}", 33 err: true, 34 }, 35 } { 36 var got APIVersion 37 err := yaml.Unmarshal([]byte(tc.v), &got) 38 if tc.err { 39 testutil.NotOk(t, err) 40 continue 41 } 42 testutil.Ok(t, err) 43 testutil.Equals(t, tc.expected, got) 44 } 45 } 46 47 func TestBuildAlertmanagerConfiguration(t *testing.T) { 48 for _, tc := range []struct { 49 address string 50 51 err bool 52 expected AlertmanagerConfig 53 }{ 54 { 55 address: "http://localhost:9093", 56 expected: AlertmanagerConfig{ 57 EndpointsConfig: httpconfig.EndpointsConfig{ 58 StaticAddresses: []string{"localhost:9093"}, 59 Scheme: "http", 60 }, 61 APIVersion: APIv1, 62 }, 63 }, 64 { 65 address: "https://am.example.com", 66 expected: AlertmanagerConfig{ 67 EndpointsConfig: httpconfig.EndpointsConfig{ 68 StaticAddresses: []string{"am.example.com"}, 69 Scheme: "https", 70 }, 71 APIVersion: APIv1, 72 }, 73 }, 74 { 75 address: "dns+http://localhost:9093", 76 expected: AlertmanagerConfig{ 77 EndpointsConfig: httpconfig.EndpointsConfig{ 78 StaticAddresses: []string{"dns+localhost:9093"}, 79 Scheme: "http", 80 }, 81 APIVersion: APIv1, 82 }, 83 }, 84 { 85 address: "dnssrv+http://localhost", 86 expected: AlertmanagerConfig{ 87 EndpointsConfig: httpconfig.EndpointsConfig{ 88 StaticAddresses: []string{"dnssrv+localhost"}, 89 Scheme: "http", 90 }, 91 APIVersion: APIv1, 92 }, 93 }, 94 { 95 address: "ssh+http://localhost", 96 expected: AlertmanagerConfig{ 97 EndpointsConfig: httpconfig.EndpointsConfig{ 98 StaticAddresses: []string{"localhost"}, 99 Scheme: "ssh+http", 100 }, 101 APIVersion: APIv1, 102 }, 103 }, 104 { 105 address: "dns+https://localhost/path/prefix/", 106 expected: AlertmanagerConfig{ 107 EndpointsConfig: httpconfig.EndpointsConfig{ 108 StaticAddresses: []string{"dns+localhost:9093"}, 109 Scheme: "https", 110 PathPrefix: "/path/prefix/", 111 }, 112 APIVersion: APIv1, 113 }, 114 }, 115 { 116 address: "http://user:pass@localhost:9093", 117 expected: AlertmanagerConfig{ 118 HTTPClientConfig: httpconfig.ClientConfig{ 119 BasicAuth: httpconfig.BasicAuth{ 120 Username: "user", 121 Password: "pass", 122 }, 123 }, 124 EndpointsConfig: httpconfig.EndpointsConfig{ 125 StaticAddresses: []string{"localhost:9093"}, 126 Scheme: "http", 127 }, 128 APIVersion: APIv1, 129 }, 130 }, 131 { 132 address: "://user:pass@localhost:9093", 133 err: true, 134 }, 135 { 136 address: "http://user:pass@", 137 err: true, 138 }, 139 { 140 address: "dnssrv+_http._tcp.example.com", 141 err: true, 142 }, 143 } { 144 t.Run(tc.address, func(t *testing.T) { 145 cfg, err := BuildAlertmanagerConfig(tc.address, time.Duration(0)) 146 if tc.err { 147 testutil.NotOk(t, err) 148 return 149 } 150 151 testutil.Equals(t, tc.expected, cfg) 152 }) 153 } 154 }