github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/alertmanager/merger/v1_silences_test.go (about)

     1  package merger
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestV1Silences(t *testing.T) {
    10  
    11  	// This test is to check the parsing round-trip is working as expected, the merging logic is
    12  	// tested in TestMergeV2Silences. The test data is based on captures from an actual Alertmanager.
    13  
    14  	in := [][]byte{
    15  		[]byte(`{"status":"success","data":[` +
    16  			`{` +
    17  			`"id":"77b580dd-1d9c-4b7e-9bba-13ac173cb4e5",` +
    18  			`"matchers":[` +
    19  			`{` +
    20  			`"name":"instance",` +
    21  			`"value":"prometheus-one",` +
    22  			`"isRegex":false,` +
    23  			`"isEqual":true` +
    24  			`}` +
    25  			`],` +
    26  			`"startsAt":"2021-04-28T17:31:01.725956017Z",` +
    27  			`"endsAt":"2021-04-28T20:31:01.722829007+02:00",` +
    28  			`"updatedAt":"2021-04-28T17:31:01.725956017Z",` +
    29  			`"createdBy":"",` +
    30  			`"comment":"Silence Comment #1",` +
    31  			`"status":{"state":"active"}` +
    32  			`},` +
    33  			`{` +
    34  			`"id":"17526003-c745-4464-a355-4f06de26a236",` +
    35  			`"matchers":[` +
    36  			`{` +
    37  			`"name":"instance",` +
    38  			`"value":"prometheus-one",` +
    39  			`"isRegex":false,` +
    40  			`"isEqual":true` +
    41  			`}` +
    42  			`],` +
    43  			`"startsAt":"2021-04-28T17:31:01.731140275Z",` +
    44  			`"endsAt":"2021-04-28T18:31:01.727579131Z",` +
    45  			`"updatedAt":"2021-04-28T17:31:01.731140275Z",` +
    46  			`"createdBy":"",` +
    47  			`"comment":"Silence Comment #2",` +
    48  			`"status":{"state":"active"}},` +
    49  			`{` +
    50  			`"id":"261248d1-4ff7-4cf1-9957-850c65f4e48b",` +
    51  			`"matchers":[` +
    52  			`{` +
    53  			`"name":"instance",` +
    54  			`"value":"prometheus-one",` +
    55  			`"isRegex":false,` +
    56  			`"isEqual":true` +
    57  			`}` +
    58  			`],` +
    59  			`"startsAt":"2021-04-28T17:31:01.73572697Z",` +
    60  			`"endsAt":"2021-04-28T18:31:01.732873879Z",` +
    61  			`"updatedAt":"2021-04-28T17:31:01.73572697Z",` +
    62  			`"createdBy":"",` +
    63  			`"comment":"Silence Comment #3",` +
    64  			`"status":{"state":"active"}}` +
    65  			`]}`),
    66  		[]byte(`{"status":"success","data":[]}`),
    67  	}
    68  
    69  	// Note that our implementation for v1 uses v2 code internally. This means the JSON fields
    70  	// come out in a slightly different order, and the timestamps lave less digits.
    71  	expected := []byte(`{"status":"success","data":[` +
    72  		`{` +
    73  		`"id":"77b580dd-1d9c-4b7e-9bba-13ac173cb4e5",` +
    74  		`"status":{"state":"active"},` +
    75  		`"updatedAt":"2021-04-28T17:31:01.725Z",` +
    76  		`"comment":"Silence Comment #1",` +
    77  		`"createdBy":"",` +
    78  		`"endsAt":"2021-04-28T20:31:01.722+02:00",` +
    79  		`"matchers":[` +
    80  		`{` +
    81  		`"isEqual":true,` +
    82  		`"isRegex":false,` +
    83  		`"name":"instance",` +
    84  		`"value":"prometheus-one"` +
    85  		`}` +
    86  		`],` +
    87  		`"startsAt":"2021-04-28T17:31:01.725Z"` +
    88  		`},` +
    89  		`{` +
    90  		`"id":"17526003-c745-4464-a355-4f06de26a236",` +
    91  		`"status":{"state":"active"},` +
    92  		`"updatedAt":"2021-04-28T17:31:01.731Z",` +
    93  		`"comment":"Silence Comment #2",` +
    94  		`"createdBy":"",` +
    95  		`"endsAt":"2021-04-28T18:31:01.727Z",` +
    96  		`"matchers":[` +
    97  		`{` +
    98  		`"isEqual":true,` +
    99  		`"isRegex":false,` +
   100  		`"name":"instance",` +
   101  		`"value":"prometheus-one"` +
   102  		`}` +
   103  		`],` +
   104  		`"startsAt":"2021-04-28T17:31:01.731Z"` +
   105  		`},` +
   106  		`{` +
   107  		`"id":"261248d1-4ff7-4cf1-9957-850c65f4e48b",` +
   108  		`"status":{"state":"active"},` +
   109  		`"updatedAt":"2021-04-28T17:31:01.735Z",` +
   110  		`"comment":"Silence Comment #3",` +
   111  		`"createdBy":"",` +
   112  		`"endsAt":"2021-04-28T18:31:01.732Z",` +
   113  		`"matchers":[` +
   114  		`{` +
   115  		`"isEqual":true,` +
   116  		`"isRegex":false,` +
   117  		`"name":"instance",` +
   118  		`"value":"prometheus-one"` +
   119  		`}` +
   120  		`],` +
   121  		`"startsAt":"2021-04-28T17:31:01.735Z"` +
   122  		`}` +
   123  		`]}`)
   124  
   125  	out, err := V1Silences{}.MergeResponses(in)
   126  	require.NoError(t, err)
   127  	require.Equal(t, string(expected), string(out))
   128  }