|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + backendapp "github.com/slok/sloth/internal/http/backend/app" |
| 10 | + "github.com/slok/sloth/internal/http/backend/model" |
| 11 | + "github.com/slok/sloth/internal/http/mcp/tools/toolsmock" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/mock" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestNewGetSLOTool(t *testing.T) { |
| 18 | + tests := map[string]struct { |
| 19 | + input getSLOToolInput |
| 20 | + mock func(m *toolsmock.SLOGetter) |
| 21 | + expErr bool |
| 22 | + expResp getSLOToolOutput |
| 23 | + }{ |
| 24 | + "It should map the backend request and response.": { |
| 25 | + input: getSLOToolInput{SLOID: "slo-id"}, |
| 26 | + mock: func(m *toolsmock.SLOGetter) { |
| 27 | + expReq := backendapp.GetSLORequest{SLOID: "slo-id"} |
| 28 | + m.On("GetSLO", mock.Anything, expReq).Once().Return(&backendapp.GetSLOResponse{ |
| 29 | + SLO: backendapp.RealTimeSLODetails{ |
| 30 | + SLO: model.SLO{ |
| 31 | + ID: "slo-id", |
| 32 | + SlothID: "sloth-slo-id", |
| 33 | + Name: "availability", |
| 34 | + ServiceID: "checkout", |
| 35 | + Objective: 99.9, |
| 36 | + PeriodDuration: 30 * 24 * time.Hour, |
| 37 | + IsGrouped: true, |
| 38 | + GroupLabels: map[string]string{"region": "eu-west-1"}, |
| 39 | + }, |
| 40 | + Budget: model.SLOBudgetDetails{ |
| 41 | + BurningBudgetPercent: 123.4, |
| 42 | + BurnedBudgetWindowPercent: 77.7, |
| 43 | + }, |
| 44 | + Alerts: model.SLOAlerts{ |
| 45 | + FiringPage: &model.Alert{Name: "PageAlert"}, |
| 46 | + FiringWarning: &model.Alert{Name: "WarnAlert"}, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, nil) |
| 50 | + }, |
| 51 | + expResp: getSLOToolOutput{SLO: listSLOsToolOutputItem{ |
| 52 | + ID: "slo-id", |
| 53 | + SlothID: "sloth-slo-id", |
| 54 | + Name: "availability", |
| 55 | + ServiceID: "checkout", |
| 56 | + Objective: 99.9, |
| 57 | + Period: "720h0m0s", |
| 58 | + IsGrouped: true, |
| 59 | + GroupLabels: map[string]string{"region": "eu-west-1"}, |
| 60 | + BurningBudgetPercent: 123.4, |
| 61 | + BurnedBudgetWindowPercent: 77.7, |
| 62 | + HasPageAlert: true, |
| 63 | + PageAlertName: "PageAlert", |
| 64 | + HasWarningAlert: true, |
| 65 | + WarningAlertName: "WarnAlert", |
| 66 | + }}, |
| 67 | + }, |
| 68 | + "Having a backend error should fail.": { |
| 69 | + input: getSLOToolInput{SLOID: "slo-id"}, |
| 70 | + mock: func(m *toolsmock.SLOGetter) { |
| 71 | + m.On("GetSLO", mock.Anything, backendapp.GetSLORequest{SLOID: "slo-id"}).Once().Return(nil, fmt.Errorf("something wrong")) |
| 72 | + }, |
| 73 | + expErr: true, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + for name, test := range tests { |
| 78 | + t.Run(name, func(t *testing.T) { |
| 79 | + m := toolsmock.NewSLOGetter(t) |
| 80 | + if test.mock != nil { |
| 81 | + test.mock(m) |
| 82 | + } |
| 83 | + |
| 84 | + tool, handler := NewGetSLOTool(m) |
| 85 | + require.NotNil(t, tool) |
| 86 | + assert.Equal(t, "get_slo", tool.Name) |
| 87 | + require.NotNil(t, tool.Annotations) |
| 88 | + assert.True(t, tool.Annotations.ReadOnlyHint) |
| 89 | + |
| 90 | + result, gotResp, err := handler(context.Background(), nil, test.input) |
| 91 | + assert.Nil(t, result) |
| 92 | + |
| 93 | + if test.expErr { |
| 94 | + assert.Error(t, err) |
| 95 | + } else { |
| 96 | + require.NoError(t, err) |
| 97 | + } |
| 98 | + |
| 99 | + assert.Equal(t, test.expResp, gotResp) |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
0 commit comments