|
| 1 | +package merger |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | +) |
| 8 | + |
| 9 | +func TestV1SilenceID_ReturnsNewestSilence(t *testing.T) { |
| 10 | + in := [][]byte{ |
| 11 | + []byte(`{"status":"success","data":{` + |
| 12 | + `"id":"77b580dd-1d9c-4b7e-9bba-13ac173cb4e5",` + |
| 13 | + `"matchers":[` + |
| 14 | + `{` + |
| 15 | + `"name":"instance",` + |
| 16 | + `"value":"prometheus-one",` + |
| 17 | + `"isRegex":false,` + |
| 18 | + `"isEqual":true` + |
| 19 | + `}` + |
| 20 | + `],` + |
| 21 | + `"startsAt":"2021-04-28T17:31:01.725956017Z",` + |
| 22 | + `"endsAt":"2021-04-28T20:31:01.722829007+02:00",` + |
| 23 | + `"updatedAt":"2021-04-28T17:32:01.725956017Z",` + |
| 24 | + `"createdBy":"",` + |
| 25 | + `"comment":"The newer silence",` + |
| 26 | + `"status":{"state":"active"}` + |
| 27 | + `}}`), |
| 28 | + []byte(`{"status":"success","data":{` + |
| 29 | + `"id":"77b580dd-1d9c-4b7e-9bba-13ac173cb4e5",` + |
| 30 | + `"matchers":[` + |
| 31 | + `{` + |
| 32 | + `"name":"instance",` + |
| 33 | + `"value":"prometheus-one",` + |
| 34 | + `"isRegex":false,` + |
| 35 | + `"isEqual":true` + |
| 36 | + `}` + |
| 37 | + `],` + |
| 38 | + `"startsAt":"2021-04-28T17:31:01.725956017Z",` + |
| 39 | + `"endsAt":"2021-04-28T20:31:01.722829007+02:00",` + |
| 40 | + `"updatedAt":"2021-04-28T17:31:01.725956017Z",` + |
| 41 | + `"createdBy":"",` + |
| 42 | + `"comment":"Silence Comment #1",` + |
| 43 | + `"status":{"state":"active"}` + |
| 44 | + `}}`), |
| 45 | + } |
| 46 | + |
| 47 | + expected := []byte(`{"status":"success","data":{` + |
| 48 | + `"id":"77b580dd-1d9c-4b7e-9bba-13ac173cb4e5",` + |
| 49 | + `"status":{"state":"active"},` + |
| 50 | + `"updatedAt":"2021-04-28T17:32:01.725Z",` + |
| 51 | + `"comment":"The newer silence",` + |
| 52 | + `"createdBy":"",` + |
| 53 | + `"endsAt":"2021-04-28T20:31:01.722+02:00",` + |
| 54 | + `"matchers":[` + |
| 55 | + `{` + |
| 56 | + `"isEqual":true,` + |
| 57 | + `"isRegex":false,` + |
| 58 | + `"name":"instance",` + |
| 59 | + `"value":"prometheus-one"` + |
| 60 | + `}` + |
| 61 | + `],` + |
| 62 | + `"startsAt":"2021-04-28T17:31:01.725Z"` + |
| 63 | + `}}`) |
| 64 | + |
| 65 | + out, err := V1SilenceID{}.MergeResponses(in) |
| 66 | + require.NoError(t, err) |
| 67 | + require.Equal(t, string(expected), string(out)) |
| 68 | +} |
| 69 | + |
| 70 | +func TestV1SilenceID_InvalidDifferentIDs(t *testing.T) { |
| 71 | + in := [][]byte{ |
| 72 | + []byte(`{"status":"success","data":{` + |
| 73 | + `"id":"77b580dd-1d9c-4b7e-9bba-13ac173cb4e5",` + |
| 74 | + `"matchers":[` + |
| 75 | + `{` + |
| 76 | + `"name":"instance",` + |
| 77 | + `"value":"prometheus-one",` + |
| 78 | + `"isRegex":false,` + |
| 79 | + `"isEqual":true` + |
| 80 | + `}` + |
| 81 | + `],` + |
| 82 | + `"startsAt":"2021-04-28T17:31:01.725956017Z",` + |
| 83 | + `"endsAt":"2021-04-28T20:31:01.722829007+02:00",` + |
| 84 | + `"updatedAt":"2021-04-28T17:32:01.725956017Z",` + |
| 85 | + `"createdBy":"",` + |
| 86 | + `"comment":"Silence Comment #1",` + |
| 87 | + `"status":{"state":"active"}` + |
| 88 | + `}}`), |
| 89 | + []byte(`{"status":"success","data":{` + |
| 90 | + `"id":"261248d1-4ff7-4cf1-9957-850c65f4e48b",` + |
| 91 | + `"matchers":[` + |
| 92 | + `{` + |
| 93 | + `"name":"instance",` + |
| 94 | + `"value":"prometheus-one",` + |
| 95 | + `"isRegex":false,` + |
| 96 | + `"isEqual":true` + |
| 97 | + `}` + |
| 98 | + `],` + |
| 99 | + `"startsAt":"2021-04-28T17:31:01.725956017Z",` + |
| 100 | + `"endsAt":"2021-04-28T20:31:01.722829007+02:00",` + |
| 101 | + `"updatedAt":"2021-04-28T17:31:01.725956017Z",` + |
| 102 | + `"createdBy":"",` + |
| 103 | + `"comment":"Silence Comment #2",` + |
| 104 | + `"status":{"state":"active"}` + |
| 105 | + `}}`), |
| 106 | + } |
| 107 | + |
| 108 | + _, err := V1SilenceID{}.MergeResponses(in) |
| 109 | + require.Error(t, err) |
| 110 | +} |
0 commit comments