Skip to content

Commit

Permalink
Small fix in clustersDetail endpoint + additional UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
epapbak committed Oct 21, 2021
1 parent eb4ba52 commit 2204183
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 22 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require (
github.com/BurntSushi/toml v0.3.1
github.com/DATA-DOG/go-sqlmock v1.4.1
github.com/RedHatInsights/insights-content-service v0.0.0-20201009081018-083923779f00
github.com/RedHatInsights/insights-operator-utils v1.21.1-0.20211013104539-199e7104dbb0
github.com/RedHatInsights/insights-results-aggregator-data v1.3.0
github.com/RedHatInsights/insights-operator-utils v1.21.3
github.com/RedHatInsights/insights-results-aggregator-data v1.3.2
github.com/Shopify/sarama v1.27.1
github.com/deckarep/golang-set v1.7.1
github.com/dgrijalva/jwt-go v3.2.0+incompatible
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ github.com/RedHatInsights/insights-operator-utils v1.6.7/go.mod h1:ott1/rkxcyQtK
github.com/RedHatInsights/insights-operator-utils v1.8.3/go.mod h1:L6alrkNSM+uBzlQdIihhGnwTpdw+bD8i8Fdh/OE9rdo=
github.com/RedHatInsights/insights-operator-utils v1.12.0/go.mod h1:mN5jURLpSG+j7y3VPAUTPyHsTWSxrqSHSerSacDBgFU=
github.com/RedHatInsights/insights-operator-utils v1.21.0/go.mod h1:B2hizFGwXCc8MT34QqVJ1A8ANTyGQZQWXQw/gSCEsaU=
github.com/RedHatInsights/insights-operator-utils v1.21.1-0.20211013104539-199e7104dbb0 h1:vxHt5EuBV3+3MtpprRwZXmUiIfeBtOXN2jedkkicBvA=
github.com/RedHatInsights/insights-operator-utils v1.21.1-0.20211013104539-199e7104dbb0/go.mod h1:B2hizFGwXCc8MT34QqVJ1A8ANTyGQZQWXQw/gSCEsaU=
github.com/RedHatInsights/insights-operator-utils v1.21.1 h1:k06NlZMCg/j6E6euuwRfsfVHllGA6XKhTLoouKDk5Ok=
github.com/RedHatInsights/insights-operator-utils v1.21.1/go.mod h1:B2hizFGwXCc8MT34QqVJ1A8ANTyGQZQWXQw/gSCEsaU=
github.com/RedHatInsights/insights-operator-utils v1.21.2/go.mod h1:3Pfsgsi7GCu2wgIqQlt1llpyQyyxsDWEGdgnPvadM40=
github.com/RedHatInsights/insights-operator-utils v1.21.3 h1:ZQ1nrLp4LrBUnJkn9TLbHWru8+QCiJObfgg1MQBgMgQ=
github.com/RedHatInsights/insights-operator-utils v1.21.3/go.mod h1:qa1a8NdarIzcZkr5mu9fBw4OarOfg1qZFZC1vNGbyas=
github.com/RedHatInsights/insights-results-aggregator v0.0.0-20200604090056-3534f6dd9c1c/go.mod h1:7Pc15NYXErx7BMJ4rF1Hacm+29G6atzjhwBpXNFMt+0=
github.com/RedHatInsights/insights-results-aggregator-data v0.0.0-20200825113234-e84e924194bc/go.mod h1:DcDgoCCmBuUSKQOGrTi0BfFLdSjAp/KxIwyqKUd46sM=
github.com/RedHatInsights/insights-results-aggregator-data v0.0.0-20201014142608-de97c4b07d5c/go.mod h1:x8IvreR2g24veCKVMXDPOR6a0D86QK9UCBfi5Xm5Gnc=
github.com/RedHatInsights/insights-results-aggregator-data v0.0.0-20201109115720-126bd0348556/go.mod h1:+j10GLCbx42McRJE7uU+aVayf5Elwx4nKULFiPkki6U=
github.com/RedHatInsights/insights-results-aggregator-data v1.0.1-0.20210614072933-b25730b1e023/go.mod h1:SDeBuNY8AIwkD4JB5/I54ArWG7qngP5/Ydn7xbu2iZo=
github.com/RedHatInsights/insights-results-aggregator-data v1.1.2/go.mod h1:rbiccYJ8whbpLLvNGMiaFzyMPs1A/2/Jh0P2U9DXF+4=
github.com/RedHatInsights/insights-results-aggregator-data v1.3.0 h1:H8GB/DHckuttLU/5I1MHUVRebRJfEYS0nyvxZZnhfKo=
github.com/RedHatInsights/insights-results-aggregator-data v1.3.0/go.mod h1:Ylo2cWFmraBzkwKLew54kZSsUTgeVvFJdIi/oRkdxtc=
github.com/RedHatInsights/insights-results-aggregator-data v1.3.1/go.mod h1:Ylo2cWFmraBzkwKLew54kZSsUTgeVvFJdIi/oRkdxtc=
github.com/RedHatInsights/insights-results-aggregator-data v1.3.2 h1:3QdTNy2f7ly4Iioae4r86QYCyxdW0itmfs4QYhsy3eY=
github.com/RedHatInsights/insights-results-aggregator-data v1.3.2/go.mod h1:E1UaB+IjJ/muxvMstVoqJrB82zVKNykjTtCiM3tMHoM=
github.com/RedHatInsights/kafka-zerolog v0.0.0-20210304172207-928f026dc7ec h1:/msFfckx6EIj0rZncrMUfNixFvsLbOiRIe4J0AurhDo=
github.com/RedHatInsights/kafka-zerolog v0.0.0-20210304172207-928f026dc7ec/go.mod h1:HJul5oCsCRNiRlh/ayJDGdW3PzGlid/5aaQwJBn7was=
github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0=
Expand Down
3 changes: 3 additions & 0 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,9 @@
}
}
},
"400": {
"description": "Request body could not be parsed, so the query is not processed"
},
"404": {
"description": "Resource not found, usually caused when some rule selector, organization or user doesn't exist"
},
Expand Down
16 changes: 5 additions & 11 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,30 +457,24 @@ func (server *HTTPServer) RuleClusterDetailEndpoint(writer http.ResponseWriter,
if request.ContentLength > 0 {
if activeClusters, successful := readClusterListFromBody(writer, request); successful {
clusters, err = server.Storage.ListOfClustersForOrgSpecificRule(orgID, selector, activeClusters)
} else {
return
}
} else {
clusters, err = server.Storage.ListOfClustersForOrgSpecificRule(orgID, selector, nil)
}

if err != nil {
log.Error().Err(err).Msgf("Unable to get list of clusters for specific rule %s", selector)
//err received from this call can be either TableNotFoundError (500) or ItemNotFoundError (404)
//err received at this point can be either TableNotFoundError (500) or ItemNotFoundError (404)
handleServerError(writer, err)
return
}

//err = responses.SendOK(writer, responses.BuildOkResponseWithData("clusters", clusters))
resp := responses.BuildOkResponse()

ruleID, errorKey, err := getRuleAndErrorKeyFromRuleID(string(selector))
if err != nil {
log.Error().Err(err).Msg("Error splitting the rule selector")

}
resp["meta"] = utypes.HittingClustersMetadata{
Count: len(clusters),
Component: utypes.Component(ruleID),
ErrorKey: errorKey,
Count: len(clusters),
Selector: selector,
}
resp["data"] = clusters
err = responses.SendOK(writer, resp)
Expand Down
99 changes: 96 additions & 3 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,28 @@ func TestRuleClusterDetailEndpoint_NoRowsFoundForGivenSelectorDBError(t *testing
})
}

func TestRuleClusterDetailEndpoint_BadBodyInRequest(t *testing.T) {
errStr := "Internal Server Error"

mockStorage, closer := helpers.MustGetMockStorage(t, true)
defer closer()

_ = mockStorage.WriteRecommendationsForCluster(testdata.OrgID, testdata.ClusterName, testdata.Report2Rules)

// A request body omitting the closing '"'
getRequestBody := fmt.Sprintf(`{"clusters":["%v]}`, testdata.ClusterName)

helpers.AssertAPIRequest(t, mockStorage, nil, &helpers.APIRequest{
Method: http.MethodGet,
Endpoint: server.RuleClusterDetailEndpoint,
EndpointArgs: []interface{}{testdata.Rule3CompositeID, testdata.OrgID, testdata.UserID},
Body: getRequestBody,
}, &helpers.APIResponse{
StatusCode: http.StatusInternalServerError,
Body: `{"status": "` + errStr + `"}`,
})
}

func TestRuleClusterDetailEndpoint_OtherDBErrors(t *testing.T) {
const errStr = "Internal Server Error"

Expand Down Expand Up @@ -1292,13 +1314,13 @@ func TestRuleClusterDetailEndpoint_ValidParameters(t *testing.T) {
mockStorage, closer := helpers.MustGetMockStorage(t, true)
defer closer()

respBody := `{"data":[{"cluster":"%v"}],"meta":{"component":"%v","count":%v, "error_key":"%v"},"status":"ok"}`
respBody := `{"data":[{"cluster":"%v"}],"meta":{"count":%v, "rule_selector":"%v"},"status":"ok"}`

_ = mockStorage.WriteRecommendationsForCluster(testdata.OrgID, testdata.ClusterName, testdata.Report2Rules)
_ = mockStorage.WriteRecommendationsForCluster(testdata.Org2ID, testdata.ClusterName, testdata.Report2Rules)

expected := fmt.Sprintf(respBody,
testdata.ClusterName, testdata.Rule1ID, 1, testdata.ErrorKey1,
testdata.ClusterName, 1, testdata.Rule1CompositeID,
)
helpers.AssertAPIRequest(t, mockStorage, nil, &helpers.APIRequest{
Method: http.MethodGet,
Expand All @@ -1318,7 +1340,7 @@ func TestRuleClusterDetailEndpoint_ValidParameters(t *testing.T) {
})

expected = fmt.Sprintf(respBody,
testdata.ClusterName, testdata.Rule2ID, 1, testdata.ErrorKey2,
testdata.ClusterName, 1, testdata.Rule2CompositeID,
)
helpers.AssertAPIRequest(t, mockStorage, nil, &helpers.APIRequest{
Method: http.MethodGet,
Expand All @@ -1337,3 +1359,74 @@ func TestRuleClusterDetailEndpoint_ValidParameters(t *testing.T) {
Body: expected,
})
}

func TestRuleClusterDetailEndpoint_ValidParametersActiveClusters(t *testing.T) {
mockStorage, closer := helpers.MustGetMockStorage(t, true)
defer closer()

respBody := `{"data":[{"cluster":"%v"}],"meta":{"count":%v, "rule_selector":"%v"},"status":"ok"}`

_ = mockStorage.WriteRecommendationsForCluster(testdata.OrgID, testdata.ClusterName, testdata.Report2Rules)
_ = mockStorage.WriteRecommendationsForCluster(testdata.OrgID, testdata.GetRandomClusterID(), testdata.Report2Rules)

getRequestBody := fmt.Sprintf(`{"clusters":["%v"]}`, testdata.ClusterName)
expected := fmt.Sprintf(respBody,
testdata.ClusterName, 1, testdata.Rule1CompositeID,
)
helpers.AssertAPIRequest(t, mockStorage, nil, &helpers.APIRequest{
Method: http.MethodGet,
Endpoint: server.RuleClusterDetailEndpoint,
EndpointArgs: []interface{}{testdata.Rule1CompositeID, testdata.OrgID, testdata.UserID},
Body: getRequestBody,
}, &helpers.APIResponse{
StatusCode: http.StatusOK,
Body: expected,
})

expected = fmt.Sprintf(respBody,
testdata.ClusterName, 1, testdata.Rule2CompositeID,
)
helpers.AssertAPIRequest(t, mockStorage, nil, &helpers.APIRequest{
Method: http.MethodGet,
Endpoint: server.RuleClusterDetailEndpoint,
EndpointArgs: []interface{}{testdata.Rule2CompositeID, testdata.OrgID, testdata.UserID},
Body: getRequestBody,
}, &helpers.APIResponse{
StatusCode: http.StatusOK,
Body: expected,
})
}

func TestRuleClusterDetailEndpoint_InvalidParametersActiveClusters(t *testing.T) {
mockStorage, closer := helpers.MustGetMockStorage(t, true)
defer closer()

errStr := `Error during parsing param 'org_id' with value 'x'. Error: 'unsigned integer expected'`

_ = mockStorage.WriteRecommendationsForCluster(testdata.OrgID, testdata.ClusterName, testdata.Report2Rules)
_ = mockStorage.WriteRecommendationsForCluster(testdata.OrgID, testdata.GetRandomClusterID(), testdata.Report2Rules)

getRequestBody := fmt.Sprintf(`{"clusters":["%v"]}`, testdata.ClusterName)

helpers.AssertAPIRequest(t, mockStorage, nil, &helpers.APIRequest{
Method: http.MethodGet,
Endpoint: server.RuleClusterDetailEndpoint,
EndpointArgs: []interface{}{testdata.Rule1CompositeID, "x", testdata.UserID},
Body: getRequestBody,
}, &helpers.APIResponse{
StatusCode: http.StatusBadRequest,
Body: `{"status": "` + errStr + `"}`,
})

errStr = fmt.Sprintf(`Error during parsing param 'rule_selector' with value '%v'. Error: 'Param rule_selector is not a valid rule selector (plugin_name|error_key)'`, testdata.Rule1.Module)

helpers.AssertAPIRequest(t, mockStorage, nil, &helpers.APIRequest{
Method: http.MethodGet,
Endpoint: server.RuleClusterDetailEndpoint,
EndpointArgs: []interface{}{testdata.Rule1.Module, testdata.OrgID, testdata.UserID},
Body: getRequestBody,
}, &helpers.APIResponse{
StatusCode: http.StatusBadRequest,
Body: `{"status": "` + errStr + `"}`,
})
}
1 change: 1 addition & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ func (storage DBStorage) ListOfClustersForOrgSpecificRule(
if err != nil {
return results, err
}

defer closeRows(rows)

var (
Expand Down

0 comments on commit 2204183

Please sign in to comment.