K8s: Match status codes from single tenant (#90153)

Co-authored-by: Jean-Philippe Quémémer <jeanphilippe.quemener@grafana.com>
This commit is contained in:
Stephanie Hingtgen
2024-07-10 12:48:02 -06:00
committed by GitHub
parent 936b3a7a5d
commit bb187ce4b1
4 changed files with 48 additions and 17 deletions

View File

@@ -27,14 +27,14 @@ type QueryDataResponse struct {
backend.QueryDataResponse `json:",inline"`
}
// If errors exist, return multi-status
// GetResponseCode return the right status code for the response by checking the responses.
func GetResponseCode(rsp *backend.QueryDataResponse) int {
if rsp == nil {
return http.StatusInternalServerError
return http.StatusBadRequest
}
for _, v := range rsp.Responses {
if v.Error != nil {
return http.StatusMultiStatus
for _, res := range rsp.Responses {
if res.Error != nil {
return http.StatusBadRequest
}
}
return http.StatusOK

View File

@@ -2,9 +2,12 @@ package v0alpha1_test
import (
"encoding/json"
"fmt"
"testing"
"github.com/grafana/grafana-plugin-sdk-go/backend"
data "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
query "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
@@ -76,3 +79,39 @@ func TestParseQueriesIntoQueryDataRequest(t *testing.T) {
}
}`, string(out))
}
func TestGetResponseCode(t *testing.T) {
t.Run("return 200 if no errors in responses", func(t *testing.T) {
assert.Equal(t, 200, query.GetResponseCode(&backend.QueryDataResponse{
Responses: map[string]backend.DataResponse{
"A": {
Error: nil,
},
"B": {
Error: nil,
},
},
}))
})
t.Run("return 400 if there is an error in the responses", func(t *testing.T) {
assert.Equal(t, 400, query.GetResponseCode(&backend.QueryDataResponse{
Responses: map[string]backend.DataResponse{
"A": {
Error: fmt.Errorf("some wild error"),
},
},
}))
})
t.Run("return 400 if there is a partial error", func(t *testing.T) {
assert.Equal(t, 400, query.GetResponseCode(&backend.QueryDataResponse{
Responses: map[string]backend.DataResponse{
"A": {
Error: nil,
},
"B": {
Error: fmt.Errorf("some partial error"),
},
},
}))
})
}