@@ -1102,6 +1102,9 @@ func TestMultitenantAlertmanager_InitialSyncWithSharding(t *testing.T) {
11021102}
11031103
11041104func TestMultitenantAlertmanager_PerTenantSharding (t * testing.T ) {
1105+ externalURL := flagext.URLValue {}
1106+ err := externalURL .Set ("http://localhost:8080/alertmanager" )
1107+ require .NoError (t , err )
11051108 tc := []struct {
11061109 name string
11071110 tenantShardSize int
@@ -1112,6 +1115,7 @@ func TestMultitenantAlertmanager_PerTenantSharding(t *testing.T) {
11121115 withSharding bool
11131116 enabledTenants []string
11141117 disabledTenants []string
1118+ statusCode func (user string ) int
11151119 }{
11161120 {
11171121 name : "sharding disabled, 1 instance" ,
@@ -1125,13 +1129,25 @@ func TestMultitenantAlertmanager_PerTenantSharding(t *testing.T) {
11251129 configs : 10 ,
11261130 expectedTenants : 1 ,
11271131 enabledTenants : []string {"u-1" },
1132+ statusCode : func (user string ) int {
1133+ if user == "u-1" {
1134+ return http .StatusOK
1135+ }
1136+ return http .StatusUnauthorized
1137+ },
11281138 },
11291139 {
11301140 name : "sharding disabled, 1 instance, single user disabled" ,
11311141 instances : 1 ,
11321142 configs : 10 ,
11331143 expectedTenants : 9 ,
11341144 disabledTenants : []string {"u-2" },
1145+ statusCode : func (user string ) int {
1146+ if user == "u-2" {
1147+ return http .StatusUnauthorized
1148+ }
1149+ return http .StatusOK
1150+ },
11351151 },
11361152 {
11371153 name : "sharding disabled, 2 instances" ,
@@ -1155,6 +1171,12 @@ func TestMultitenantAlertmanager_PerTenantSharding(t *testing.T) {
11551171 configs : 10 ,
11561172 expectedTenants : 1 ,
11571173 enabledTenants : []string {"u-3" },
1174+ statusCode : func (user string ) int {
1175+ if user == "u-3" {
1176+ return http .StatusOK
1177+ }
1178+ return http .StatusUnauthorized
1179+ },
11581180 },
11591181 {
11601182 name : "sharding enabled, 1 instance, enabled tenants, single user disabled" ,
@@ -1164,6 +1186,12 @@ func TestMultitenantAlertmanager_PerTenantSharding(t *testing.T) {
11641186 configs : 10 ,
11651187 expectedTenants : 9 ,
11661188 disabledTenants : []string {"u-4" },
1189+ statusCode : func (user string ) int {
1190+ if user == "u-4" {
1191+ return http .StatusUnauthorized
1192+ }
1193+ return http .StatusOK
1194+ },
11671195 },
11681196 {
11691197 name : "sharding enabled, 2 instances, RF = 1" ,
@@ -1197,6 +1225,15 @@ func TestMultitenantAlertmanager_PerTenantSharding(t *testing.T) {
11971225 configs : 10 ,
11981226 expectedTenants : 24 , // (configs - disabled-tenants) * replication factor
11991227 disabledTenants : []string {"u-1" , "u-2" },
1228+ statusCode : func (user string ) int {
1229+ switch user {
1230+ case "u-1" :
1231+ return http .StatusUnauthorized
1232+ case "u-2" :
1233+ return http .StatusUnauthorized
1234+ }
1235+ return http .StatusOK
1236+ },
12001237 },
12011238 }
12021239
@@ -1221,13 +1258,15 @@ func TestMultitenantAlertmanager_PerTenantSharding(t *testing.T) {
12211258 Templates : []* alertspb.TemplateDesc {},
12221259 }))
12231260 }
1261+ clientPool := newPassthroughAlertmanagerClientPool ()
12241262
12251263 // Then, create the alertmanager instances, start them and add their registries to the slice.
12261264 for i := 1 ; i <= tt .instances ; i ++ {
12271265 instanceIDs = append (instanceIDs , fmt .Sprintf ("alertmanager-%d" , i ))
12281266 instanceID := fmt .Sprintf ("alertmanager-%d" , i )
12291267
12301268 amConfig := mockAlertmanagerConfig (t )
1269+ amConfig .ExternalURL = externalURL
12311270 amConfig .ShardingRing .ReplicationFactor = tt .replicationFactor
12321271 amConfig .ShardingRing .InstanceID = instanceID
12331272 amConfig .ShardingRing .InstanceAddr = fmt .Sprintf ("127.0.0.%d" , i )
@@ -1252,6 +1291,11 @@ func TestMultitenantAlertmanager_PerTenantSharding(t *testing.T) {
12521291 instances = append (instances , am )
12531292 instanceIDs = append (instanceIDs , instanceID )
12541293 registries .AddUserRegistry (instanceID , reg )
1294+
1295+ if tt .withSharding {
1296+ clientPool .setServer (amConfig .ShardingRing .InstanceAddr + ":0" , am )
1297+ am .distributor .alertmanagerClientsPool = clientPool
1298+ }
12551299 }
12561300
12571301 // If we're testing sharding, we need make sure the ring is settled.
@@ -1281,6 +1325,23 @@ func TestMultitenantAlertmanager_PerTenantSharding(t *testing.T) {
12811325 assert .Equal (t , tt .expectedTenants , numInstances )
12821326 assert .Equal (t , float64 (tt .expectedTenants ), metrics .GetSumOfGauges ("cortex_alertmanager_tenants_owned" ))
12831327 assert .Equal (t , float64 (tt .configs * tt .instances ), metrics .GetSumOfGauges ("cortex_alertmanager_tenants_discovered" ))
1328+
1329+ statusCode := tt .statusCode
1330+ if statusCode == nil {
1331+ statusCode = func (user string ) int {
1332+ return http .StatusOK
1333+ }
1334+ }
1335+ for ami , am := range instances {
1336+ for i := 1 ; i <= tt .configs ; i ++ {
1337+ u := fmt .Sprintf ("u-%d" , i )
1338+ req := httptest .NewRequest ("GET" , "http://localhost:8080/alertmanager/" , nil )
1339+ w := httptest .NewRecorder ()
1340+ am .ServeHTTP (w , req .WithContext (user .InjectOrgID (req .Context (), u )))
1341+ resp := w .Result ()
1342+ require .Equal (t , statusCode (u ), resp .StatusCode , ami )
1343+ }
1344+ }
12841345 })
12851346 }
12861347}
@@ -2109,8 +2170,8 @@ func (am *passthroughAlertmanagerClient) ReadState(ctx context.Context, in *aler
21092170 return am .server .ReadState (ctx , in )
21102171}
21112172
2112- func (am * passthroughAlertmanagerClient ) HandleRequest (context.Context , * httpgrpc.HTTPRequest , ... grpc.CallOption ) (* httpgrpc.HTTPResponse , error ) {
2113- return nil , fmt . Errorf ( "unexpected call to HandleRequest" )
2173+ func (am * passthroughAlertmanagerClient ) HandleRequest (ctx context.Context , in * httpgrpc.HTTPRequest , opts ... grpc.CallOption ) (* httpgrpc.HTTPResponse , error ) {
2174+ return am . server . HandleRequest ( ctx , in )
21142175}
21152176
21162177func (am * passthroughAlertmanagerClient ) RemoteAddress () string {
0 commit comments