1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
package datadog
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"sort"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func sptr(i string) *string {
return &i
}
func TestServiceLevelObjectiveSerialization(t *testing.T) {
slo := ServiceLevelObjective{
ID: sptr("12345678901234567890123456789012"),
Name: sptr("Test SLO"),
Description: sptr("Test Description"),
Tags: []string{"product:foo"},
Thresholds: []*ServiceLevelObjectiveThreshold{
{
TimeFrame: String("7d"),
Target: Float64(99),
Warning: Float64(99.5),
},
},
Type: &ServiceLevelObjectiveTypeMonitor,
MonitorIDs: []int{1},
}
raw, err := json.Marshal(&slo)
assert.NoError(t, err)
assert.NotEmpty(t, raw)
var deserializedSLO ServiceLevelObjective
err = json.Unmarshal(raw, &deserializedSLO)
assert.NoError(t, err)
assert.Equal(t, slo.ID, deserializedSLO.ID)
assert.Equal(t, slo.Name, deserializedSLO.Name)
assert.Equal(t, slo.Description, deserializedSLO.Description)
assert.EqualValues(t, slo.Tags, deserializedSLO.Tags)
assert.EqualValues(t, slo.Thresholds, deserializedSLO.Thresholds)
assert.Equal(t, slo.Type, deserializedSLO.Type)
assert.EqualValues(t, slo.MonitorIDs, deserializedSLO.MonitorIDs)
assert.Nil(t, deserializedSLO.Groups)
}
const sloTestFixturePrefix = "./tests/fixtures/service_level_objectives/"
func testSLOGetMock(t *testing.T, expectedInputFixturePath, fixturePath string) (*httptest.Server, *Client) {
if expectedInputFixturePath != "" {
expectedInputFixturePath = sloTestFixturePrefix + expectedInputFixturePath
}
fixturePath = sloTestFixturePrefix + fixturePath
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if expectedInputFixturePath != "" && r != nil {
body := r.Body
if body == nil {
t.Fatal("nil body received")
}
defer body.Close()
payload, err := ioutil.ReadAll(body)
if err != nil {
t.Fatal(err)
}
var payloadContent interface{}
err = json.Unmarshal(payload, &payloadContent)
if err != nil {
t.Fatal(err)
}
expectedPayload, err := ioutil.ReadFile(expectedInputFixturePath)
if err != nil {
t.Fatal(err)
}
var expectedPayloadContent interface{}
err = json.Unmarshal(expectedPayload, &expectedPayloadContent)
if err != nil {
t.Fatal(err)
}
if !assert.Equal(t, expectedPayloadContent, payloadContent) {
t.Fatalf("expected input did not match actual input")
}
}
response, err := ioutil.ReadFile(fixturePath)
if err != nil {
t.Fatal(err)
}
w.Write(response)
}))
datadogClient := Client{
baseUrl: ts.URL,
HttpClient: http.DefaultClient,
}
return ts, &datadogClient
}
func getMockSLO(id string) *ServiceLevelObjective {
var sloID *string = nil
if id != "" {
sloID = &id
}
return &ServiceLevelObjective{
ID: sloID,
Name: sptr("Test SLO"),
Description: sptr("test slo description"),
Tags: []string{"product:foo"},
Thresholds: []*ServiceLevelObjectiveThreshold{
{
TimeFrame: String("7d"),
Target: Float64(99),
Warning: Float64(99.5),
},
{
TimeFrame: String("30d"),
Target: Float64(98),
Warning: Float64(99),
},
{
TimeFrame: String("90d"),
Target: Float64(98),
Warning: Float64(99),
},
},
}
}
func TestServiceLevelObjectiveIntegration(t *testing.T) {
t.Run("CreateMonitor", func(t2 *testing.T) {
ts, c := testSLOGetMock(t2, "create_request_monitor.json", "create_response_monitor.json")
defer ts.Close()
slo := getMockSLO("")
slo.SetType(ServiceLevelObjectiveTypeMonitor)
slo.MonitorIDs = []int{1}
created, err := c.CreateServiceLevelObjective(slo)
assert.NoError(t2, err)
assert.Equal(t2, "12345678901234567890123456789012", created.GetID())
})
t.Run("CreateMetric", func(t2 *testing.T) {
ts, c := testSLOGetMock(t2, "create_request_metric.json", "create_response_metric.json")
defer ts.Close()
slo := getMockSLO("")
slo.SetType(ServiceLevelObjectiveTypeMetric)
slo.SetQuery(ServiceLevelObjectiveMetricQuery{
Numerator: String("sum:my.metric{type:good}.as_count()"),
Denominator: String("sum:my.metric{*}.as_count()"),
})
created, err := c.CreateServiceLevelObjective(slo)
assert.NoError(t2, err)
assert.Equal(t2, "abcdefabcdefabcdefabcdefabcdefab", created.GetID())
})
t.Run("Update", func(t2 *testing.T) {
ts, c := testSLOGetMock(t2, "update_request.json", "update_response.json")
defer ts.Close()
slo := getMockSLO("12345678901234567890123456789012")
slo.SetType(ServiceLevelObjectiveTypeMonitor)
slo.MonitorIDs = []int{1}
slo, err := c.UpdateServiceLevelObjective(slo)
assert.NoError(t2, err)
assert.Equal(t2, "12345678901234567890123456789012", slo.GetID())
assert.Equal(t2, 1563283900, slo.GetModifiedAt())
})
t.Run("Delete", func(t2 *testing.T) {
ts, c := testSLOGetMock(t2, "", "delete_response.json")
defer ts.Close()
slo := getMockSLO("12345678901234567890123456789012")
err := c.DeleteServiceLevelObjective(slo.GetID())
assert.NoError(t2, err)
})
t.Run("DeleteMany", func(t2 *testing.T) {
ts, c := testSLOGetMock(t2, "delete_many_request.json", "delete_many_response.json")
defer ts.Close()
err := c.DeleteServiceLevelObjectives(
[]string{"12345678901234567890123456789012", "abcdefabcdefabcdefabcdefabcdefab"},
)
assert.NoError(t2, err)
})
t.Run("DeleteByTimeframe", func(t2 *testing.T) {
ts, c := testSLOGetMock(t2, "delete_by_timeframe_request.json", "delete_by_timeframe_response.json")
defer ts.Close()
/* Some Context for this test case: This is useful for doing individual time-frame deletes across different SLOs (used by the web list view bulk delete)
`12345678901234567890123456789012` was defined with 2 time frames: "7d" and "30d"
`abcdefabcdefabcdefabcdefabcdefab` was defined with 2 time frames: "30d" and "90d"
When we delete `7d` from `12345678901234567890123456789012` we still have `30d` timeframe remaining, hence this is "updated"
When we delete `30d` and `90d` from `abcdefabcdefabcdefabcdefabcdefab` we are left with 0 time frames, hence this is "deleted"
and the entire SLO config is deleted
*/
resp, err := c.DeleteServiceLevelObjectiveTimeFrames(map[string][]string{
"12345678901234567890123456789012": {"7d"},
"abcdefabcdefabcdefabcdefabcdefab": {"30d", "90d"},
})
assert.NoError(t2, err)
assert.EqualValues(t2, resp.UpdatedIDs, []string{"12345678901234567890123456789012"})
assert.EqualValues(t2, resp.DeletedIDs, []string{"abcdefabcdefabcdefabcdefabcdefab"})
})
t.Run("GetByID", func(t2 *testing.T) {
ts, c := testSLOGetMock(t2, "", "get_by_id_response.json")
defer ts.Close()
slo, err := c.GetServiceLevelObjective("12345678901234567890123456789012")
assert.NoError(t2, err)
assert.Equal(t2, "12345678901234567890123456789012", slo.GetID())
})
t.Run("SearchWithIDs", func(t2 *testing.T) {
ts, c := testSLOGetMock(t2, "", "get_many_response.json")
defer ts.Close()
slos, err := c.SearchServiceLevelObjectives(1000, 0, "", []string{"12345678901234567890123456789012", "abcdefabcdefabcdefabcdefabcdefab"})
assert.NoError(t2, err)
assert.Len(t2, slos, 2)
contains := func(slos []*ServiceLevelObjective, id string) bool {
for _, slo := range slos {
if slo.GetID() == id {
return true
}
}
return false
}
assert.True(t2, contains(slos, "12345678901234567890123456789012"))
assert.True(t2, contains(slos, "abcdefabcdefabcdefabcdefabcdefab"))
})
t.Run("SearchWithQuery", func(t2 *testing.T) {
ts, c := testSLOGetMock(t2, "", "search_response.json")
defer ts.Close()
slos, err := c.SearchServiceLevelObjectives(1000, 0, "service:foo AND team:a", nil)
assert.NoError(t2, err)
assert.Len(t2, slos, 1)
assert.Equal(t2, "12345678901234567890123456789012", slos[0].GetID())
})
t.Run("thresholds are sortable by duration", func(t2 *testing.T) {
thresholds := ServiceLevelObjectiveThresholds{
{
TimeFrame: String("30d"),
Target: Float64(99.9),
},
{
TimeFrame: String("7d"),
Target: Float64(98.9),
},
{
TimeFrame: String("90d"),
Target: Float64(97.9),
},
}
sort.Sort(thresholds)
assert.Equal(t2, "7d", thresholds[0].GetTimeFrame())
assert.Equal(t2, "30d", thresholds[1].GetTimeFrame())
assert.Equal(t2, "90d", thresholds[2].GetTimeFrame())
})
t.Run("thresholds are comparable", func(t2 *testing.T) {
threshold1 := &ServiceLevelObjectiveThreshold{
TimeFrame: String("30d"),
Target: Float64(99.9),
}
threshold2 := &ServiceLevelObjectiveThreshold{
TimeFrame: String("30d"),
Target: Float64(99.9),
}
assert.True(t2, threshold1.Equal(threshold2))
threshold3 := &ServiceLevelObjectiveThreshold{
TimeFrame: String("30d"),
Target: Float64(0.9),
}
assert.False(t2, threshold3.Equal(threshold2))
threshold4 := &ServiceLevelObjectiveThreshold{
TimeFrame: String("7d"),
Target: Float64(99.9),
}
assert.False(t2, threshold2.Equal(threshold4))
})
t.Run("CheckCanDelete", func(t2 *testing.T) {
ts, c := testSLOGetMock(t2, "", "check_can_delete_response.json")
defer ts.Close()
resp, err := c.CheckCanDeleteServiceLevelObjectives(
[]string{"12345678901234567890123456789012", "abcdefabcdefabcdefabcdefabcdefab"},
)
assert.NoError(t2, err)
assert.EqualValues(t2, []string{"12345678901234567890123456789012"}, resp.Data.OK)
assert.EqualValues(t2,
map[string]string{
"abcdefabcdefabcdefabcdefabcdefab": "SLO abcdefabcdefabcdefabcdefabcdefab is used in dashboard 123-456-789",
},
resp.Errors,
)
})
t.Run("GetServiceLevelObjectiveHistory-Metric", func(t2 *testing.T) {
ts, c := testSLOGetMock(t2, "", "get_history_metric_response.json")
defer ts.Close()
resp, err := c.GetServiceLevelObjectiveHistory(
"12345678901234567890123456789012",
time.Unix(1571162100, 0),
time.Unix(1571766900, 0),
)
assert.NoError(t2, err)
assert.Nil(t2, resp.Error)
assert.Equal(t2, float32(100), resp.Data.Overall.SliValue)
assert.Equal(t2, json.Number("3698988"), resp.Data.Metrics.Numerator.Sum)
assert.Equal(t2, json.Number("3698988"), resp.Data.Metrics.Denominator.Sum)
})
t.Run("GetServiceLevelObjectiveHistory-Monitor", func(t2 *testing.T) {
ts, c := testSLOGetMock(t2, "", "get_history_monitor_response.json")
defer ts.Close()
resp, err := c.GetServiceLevelObjectiveHistory(
"12345678901234567890123456789012",
time.Unix(1571162100, 0),
time.Unix(1571766900, 0),
)
assert.NoError(t2, err)
assert.Nil(t2, resp.Error)
assert.Equal(t2, float32(6.765872955322266), resp.Data.Overall.SliValue)
assert.Len(t2, resp.Data.Groups, 1)
assert.Equal(t2, float32(6.765872955322266), resp.Data.Groups[0].SliValue)
assert.Equal(t2, "some:tag", resp.Data.Groups[0].Name)
})
}
|