Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
fix: Generate specific error message if no SLIs are requested when us…
Browse files Browse the repository at this point in the history
…ing file-based SLIs (#901)

Signed-off-by: Arthur Pitman <[email protected]>

Signed-off-by: Arthur Pitman <[email protected]>
  • Loading branch information
arthurpitman authored Sep 6, 2022
1 parent f96ed89 commit 8722c36
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/sli/get_sli_triggered_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ func (eh *GetSLIEventHandler) getSLIResultsFromDynatraceDashboard(ctx context.Co
}

func (eh *GetSLIEventHandler) getSLIResultsFromCustomQueries(ctx context.Context, timeframe common.Timeframe) ([]result.SLIResult, error) {
if len(eh.event.GetIndicators()) == 0 {
return nil, errors.New("no SLIs were requested")
}

slis, err := eh.resourceClient.GetSLIs(ctx, eh.event.GetProject(), eh.event.GetStage(), eh.event.GetService())
if err != nil {
log.WithError(err).Error("could not retrieve custom SLI definitions")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,36 @@ func TestRetrieveMetricsFromFile_SLO(t *testing.T) {

assertThatCustomSLITestIsCorrect(t, handler, testIndicatorSLOValue, rClient, getSLIFinishedEventSuccessAssertionsFunc, createSuccessfulSLIResultAssertionsFunc(testIndicatorSLOValue, 95, expectedSLORequest))
}

// TestErrorMessageWhenNoSLIsAreRequested tests that the correct error message is generated when no SLIs are requested.
func TestErrorMessageWhenNoSLIsAreRequested(t *testing.T) {
tests := []struct {
name string
slis map[string]string
}{
{
name: "No SLIs requested and no SLIs defined",
},
{
name: "No SLIs requested and a single SLI is defined",
slis: map[string]string{
"response_time_p95": "metricSelector=builtin:service.response.time:merge(\"dt.entity.service\"):percentile(95)&entitySelector=type(SERVICE),tag(keptn_project:sockshop),tag(keptn_stage:staging)",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

eventSenderClient := &eventSenderClientMock{}

// no need to have something here, because we should not send an API request
handler := test.NewFileBasedURLHandler(t)

rClient := newResourceClientMockWithSLIs(t, tt.slis)

runTestAndAssertNoError(t, createTestGetSLIEventDataWithNoIndicators(), handler, eventSenderClient, rClient, "")
assertCorrectGetSLIEvents(t, eventSenderClient.eventSink, getSLIFinishedEventFailureAssertionsFunc, createFailedSLIResultAssertionsFunc("no metric", "no SLIs were requested"))
})
}
}
11 changes: 11 additions & 0 deletions internal/sli/test_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ func createTestGetSLIEventDataWithIndicator(indicator string) *getSLIEventData {
}
}

func createTestGetSLIEventDataWithNoIndicators() *getSLIEventData {
return &getSLIEventData{
project: "sockshop",
stage: "staging",
service: "carts",
indicators: []string{},
sliStart: testSLIStart,
sliEnd: testSLIEnd,
}
}

// convertTimeStringToUnixMillisecondsString converts a ISO8601 (or fallback format RFC3339) time string to a string with the Unix timestamp, or "0" if this cannot be done.
func convertTimeStringToUnixMillisecondsString(timeString string) string {
time, err := timeutils.ParseTimestamp(timeString)
Expand Down

0 comments on commit 8722c36

Please sign in to comment.