(t *testing.T)
| 108 | } |
| 109 | |
| 110 | func TestClusterUnaryServerInterceptor(t *testing.T) { |
| 111 | testCases := map[string]struct { |
| 112 | incomingContext context.Context |
| 113 | serverCluster string |
| 114 | verifyErr func(err error, softValidation bool) |
| 115 | expectedLogs string |
| 116 | expectedMetrics string |
| 117 | shouldPanic bool |
| 118 | }{ |
| 119 | "empty server cluster makes ClusterUnaryServerInterceptor panic": { |
| 120 | incomingContext: newIncomingContext(false, ""), |
| 121 | serverCluster: "", |
| 122 | shouldPanic: true, |
| 123 | }, |
| 124 | "equal request and server clusters give no error": { |
| 125 | incomingContext: newIncomingContext(true, "cluster"), |
| 126 | serverCluster: "cluster", |
| 127 | }, |
| 128 | "different request and server clusters give rise to an error with grpcutil.WRONG_CLUSTER_VALIDATION_LABEL cause if soft validation disabled": { |
| 129 | incomingContext: newIncomingContext(true, "wrong-cluster"), |
| 130 | serverCluster: "cluster", |
| 131 | expectedLogs: `level=warn method=/Test/Me cluster_validation_labels=[cluster] soft_validation=%v msg="request with wrong cluster validation label" request_cluster_validation_label=wrong-cluster`, |
| 132 | expectedMetrics: ` |
| 133 | # HELP test_server_invalid_cluster_validation_label_requests_total Number of requests received by server with invalid cluster validation label. |
| 134 | # TYPE test_server_invalid_cluster_validation_label_requests_total counter |
| 135 | test_server_invalid_cluster_validation_label_requests_total{cluster_validation_label="cluster", method="/Test/Me",protocol="grpc",request_cluster_validation_label="wrong-cluster"} 1 |
| 136 | `, |
| 137 | verifyErr: func(err error, softValidation bool) { |
| 138 | if !softValidation { |
| 139 | require.Equal(t, grpcutil.Status(codes.FailedPrecondition, `rejected request with wrong cluster validation label "wrong-cluster" - it should be one of [cluster]`, &grpcutil.ErrorDetails{Cause: grpcutil.WRONG_CLUSTER_VALIDATION_LABEL}).Err(), err) |
| 140 | } |
| 141 | }, |
| 142 | }, |
| 143 | "empty request cluster and non-empty server cluster give an error with grpcutil.WRONG_CLUSTER_VALIDATION_LABEL cause if soft validation disabled": { |
| 144 | incomingContext: newIncomingContext(true, ""), |
| 145 | serverCluster: "cluster", |
| 146 | expectedLogs: `level=warn method=/Test/Me cluster_validation_labels=[cluster] soft_validation=%v msg="request with no cluster validation label"`, |
| 147 | expectedMetrics: ` |
| 148 | # HELP test_server_invalid_cluster_validation_label_requests_total Number of requests received by server with invalid cluster validation label. |
| 149 | # TYPE test_server_invalid_cluster_validation_label_requests_total counter |
| 150 | test_server_invalid_cluster_validation_label_requests_total{cluster_validation_label="cluster",method="/Test/Me",protocol="grpc",request_cluster_validation_label=""} 1 |
| 151 | `, |
| 152 | verifyErr: func(err error, softValidation bool) { |
| 153 | if !softValidation { |
| 154 | require.Equal(t, grpcutil.Status(codes.FailedPrecondition, `rejected request with empty cluster validation label - it should be one of [cluster]`, &grpcutil.ErrorDetails{Cause: grpcutil.WRONG_CLUSTER_VALIDATION_LABEL}).Err(), err) |
| 155 | } |
| 156 | }, |
| 157 | }, |
| 158 | "no request cluster and non-empty server cluster give an error with grpcutil.WRONG_CLUSTER_VALIDATION_LABEL cause if soft validation disabled": { |
| 159 | incomingContext: newIncomingContext(false, ""), |
| 160 | serverCluster: "cluster", |
| 161 | expectedLogs: `level=warn method=/Test/Me cluster_validation_labels=[cluster] soft_validation=%v msg="request with no cluster validation label"`, |
| 162 | expectedMetrics: ` |
| 163 | # HELP test_server_invalid_cluster_validation_label_requests_total Number of requests received by server with invalid cluster validation label. |
| 164 | # TYPE test_server_invalid_cluster_validation_label_requests_total counter |
| 165 | test_server_invalid_cluster_validation_label_requests_total{cluster_validation_label="cluster",method="/Test/Me",protocol="grpc",request_cluster_validation_label=""} 1 |
| 166 | `, |
| 167 | verifyErr: func(err error, softValidation bool) { |
nothing calls this directly
no test coverage detected