CloudMonitoring: Move data manipulation to backend (#41379)

This commit is contained in:
Isabella Siu
2021-11-10 08:58:04 -05:00
committed by GitHub
parent a45e4ff73f
commit fc3d3ff003
12 changed files with 531 additions and 442 deletions
@@ -1,6 +1,7 @@
package cloudmonitoring
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
@@ -42,6 +43,10 @@ func Test_parseResourcePath(t *testing.T) {
}
}
func fakeResponseFn(input []byte) ([]byte, error) {
return input, nil
}
func Test_doRequest(t *testing.T) {
// test that it forwards the header and body
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -55,8 +60,10 @@ func Test_doRequest(t *testing.T) {
if err != nil {
t.Error(err)
}
s := Service{}
rw := httptest.NewRecorder()
res := doRequest(rw, req, srv.Client())
res := s.doRequest(rw, req, srv.Client(), fakeResponseFn)
if res.Header().Get("foo") != "bar" {
t.Errorf("Unexpected headers: %v", res.Header())
}
@@ -112,3 +119,149 @@ func Test_setRequestVariables(t *testing.T) {
t.Errorf("Unexpected result URL. Got %s, expecting %s", req.URL.String(), expectedURL)
}
}
func Test_processData_functions(t *testing.T) {
// metricDescriptors
metricDescriptorResp := metricDescriptorResponse{
Descriptors: []metricDescriptor{
{
ValueType: "INT64",
MetricKind: "DELTA",
Type: "actions.googleapis.com/smarthome_action/local_event_count",
Unit: "1",
Service: "foo",
ServiceShortName: "bar",
DisplayName: "Local event count",
Description: "baz",
},
},
}
marshaledMDResponse, _ := json.Marshal(metricDescriptorResp)
metricDescriptorResult := []metricDescriptor{
{
ValueType: "INT64",
MetricKind: "DELTA",
Type: "actions.googleapis.com/smarthome_action/local_event_count",
Unit: "1",
Service: "actions.googleapis.com",
ServiceShortName: "actions",
DisplayName: "Local event count",
Description: "baz",
},
}
marshaledMDResult, _ := json.Marshal(metricDescriptorResult)
// services
serviceResp := serviceResponse{
Services: []serviceDescription{
{
Name: "blah/foo",
DisplayName: "bar",
},
{
Name: "abc",
DisplayName: "",
},
},
}
marshaledServiceResponse, _ := json.Marshal(serviceResp)
serviceResult := []selectableValue{
{
Value: "foo",
Label: "bar",
},
{
Value: "abc",
Label: "abc",
},
}
marshaledServiceResult, _ := json.Marshal(serviceResult)
// slos
sloResp := sloResponse{
SLOs: []sloDescription{
{
Name: "blah/foo",
DisplayName: "bar",
Goal: 0.1,
},
{
Name: "abc",
DisplayName: "xyz",
Goal: 0.2,
},
},
}
marshaledSLOResponse, _ := json.Marshal(sloResp)
sloResult := []selectableValue{
{
Value: "foo",
Label: "bar",
Goal: 0.1,
},
{
Value: "abc",
Label: "xyz",
Goal: 0.2,
},
}
marshaledSLOResult, _ := json.Marshal(sloResult)
// cloudresourcemanager
cloudResourceResp := projectResponse{
Projects: []projectDescription{
{
ProjectID: "foo",
Name: "bar",
},
{
ProjectID: "abc",
Name: "abc",
},
},
}
marshaledCRResponse, _ := json.Marshal(cloudResourceResp)
tests := []struct {
name string
responseFn processResponse
input []byte
result []byte
}{
{
"metricDescriptor",
processMetricDescriptors,
marshaledMDResponse,
marshaledMDResult,
},
{
"services",
processServices,
marshaledServiceResponse,
marshaledServiceResult,
},
{
"slos",
processSLOs,
marshaledSLOResponse,
marshaledSLOResult,
},
{
"cloudresourcemanager",
processProjects,
marshaledCRResponse,
marshaledServiceResult,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
res, err := test.responseFn(test.input)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if string(test.result) != string(res) {
t.Errorf("Unexpected result. Got %s, expecting %s", res, test.result)
}
})
}
}