...

Source file src/github.com/prometheus/alertmanager/test/with_api_v2/acceptance/silence_test.go

Documentation: github.com/prometheus/alertmanager/test/with_api_v2/acceptance

     1  // Copyright 2018 Prometheus Team
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package test
    15  
    16  import (
    17  	"fmt"
    18  	"testing"
    19  	"time"
    20  
    21  	. "github.com/prometheus/alertmanager/test/with_api_v2"
    22  )
    23  
    24  func TestSilencing(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	conf := `
    28  route:
    29    receiver: "default"
    30    group_by: []
    31    group_wait:      1s
    32    group_interval:  1s
    33    repeat_interval: 1ms
    34  
    35  receivers:
    36  - name: "default"
    37    webhook_configs:
    38    - url: 'http://%s'
    39  `
    40  
    41  	at := NewAcceptanceTest(t, &AcceptanceOpts{
    42  		Tolerance: 150 * time.Millisecond,
    43  	})
    44  
    45  	co := at.Collector("webhook")
    46  	wh := NewWebhook(t, co)
    47  
    48  	amc := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
    49  
    50  	// No repeat interval is configured. Thus, we receive an alert
    51  	// notification every second.
    52  	amc.Push(At(1), Alert("alertname", "test1").Active(1))
    53  	amc.Push(At(1), Alert("alertname", "test2").Active(1))
    54  
    55  	co.Want(Between(2, 2.5),
    56  		Alert("alertname", "test1").Active(1),
    57  		Alert("alertname", "test2").Active(1),
    58  	)
    59  
    60  	// Add a silence that affects the first alert.
    61  	amc.SetSilence(At(2.3), Silence(2.5, 4.5).Match("alertname", "test1"))
    62  
    63  	co.Want(Between(3, 3.5), Alert("alertname", "test2").Active(1))
    64  	co.Want(Between(4, 4.5), Alert("alertname", "test2").Active(1))
    65  
    66  	// Silence should be over now and we receive both alerts again.
    67  
    68  	co.Want(Between(5, 5.5),
    69  		Alert("alertname", "test1").Active(1),
    70  		Alert("alertname", "test2").Active(1),
    71  	)
    72  
    73  	at.Run()
    74  
    75  	t.Log(co.Check())
    76  }
    77  
    78  func TestSilenceDelete(t *testing.T) {
    79  	t.Parallel()
    80  
    81  	conf := `
    82  route:
    83    receiver: "default"
    84    group_by: []
    85    group_wait:      1s
    86    group_interval:  1s
    87    repeat_interval: 1ms
    88  
    89  receivers:
    90  - name: "default"
    91    webhook_configs:
    92    - url: 'http://%s'
    93  `
    94  
    95  	at := NewAcceptanceTest(t, &AcceptanceOpts{
    96  		Tolerance: 150 * time.Millisecond,
    97  	})
    98  
    99  	co := at.Collector("webhook")
   100  	wh := NewWebhook(t, co)
   101  
   102  	amc := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
   103  
   104  	// No repeat interval is configured. Thus, we receive an alert
   105  	// notification every second.
   106  	amc.Push(At(1), Alert("alertname", "test1").Active(1))
   107  	amc.Push(At(1), Alert("alertname", "test2").Active(1))
   108  
   109  	// Silence everything for a long time and delete the silence after
   110  	// two iterations.
   111  	sil := Silence(1.5, 100).MatchRE("alertname", ".+")
   112  
   113  	amc.SetSilence(At(1.3), sil)
   114  	amc.DelSilence(At(3.5), sil)
   115  
   116  	co.Want(Between(3.5, 4.5),
   117  		Alert("alertname", "test1").Active(1),
   118  		Alert("alertname", "test2").Active(1),
   119  	)
   120  
   121  	at.Run()
   122  
   123  	t.Log(co.Check())
   124  }
   125  

View as plain text