...

Source file src/github.com/prometheus/alertmanager/test/with_api_v2/acceptance/inhibit_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 TestInhibiting(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	// This integration test checks that alerts can be inhibited and that an
    28  	// inhibited alert will be notified again as soon as the inhibiting alert
    29  	// gets resolved.
    30  
    31  	conf := `
    32  route:
    33    receiver: "default"
    34    group_by: []
    35    group_wait:      1s
    36    group_interval:  1s
    37    repeat_interval: 1s
    38  
    39  receivers:
    40  - name: "default"
    41    webhook_configs:
    42    - url: 'http://%s'
    43  
    44  inhibit_rules:
    45  - source_match:
    46      alertname: JobDown
    47    target_match:
    48      alertname: InstanceDown
    49    equal:
    50      - job
    51      - zone
    52  `
    53  
    54  	at := NewAcceptanceTest(t, &AcceptanceOpts{
    55  		Tolerance: 150 * time.Millisecond,
    56  	})
    57  
    58  	co := at.Collector("webhook")
    59  	wh := NewWebhook(t, co)
    60  
    61  	amc := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
    62  
    63  	amc.Push(At(1), Alert("alertname", "test1", "job", "testjob", "zone", "aa"))
    64  	amc.Push(At(1), Alert("alertname", "InstanceDown", "job", "testjob", "zone", "aa"))
    65  	amc.Push(At(1), Alert("alertname", "InstanceDown", "job", "testjob", "zone", "ab"))
    66  
    67  	// This JobDown in zone aa should inhibit InstanceDown in zone aa in the
    68  	// second batch of notifications.
    69  	amc.Push(At(2.2), Alert("alertname", "JobDown", "job", "testjob", "zone", "aa"))
    70  
    71  	// InstanceDown in zone aa should fire again in the third batch of
    72  	// notifications once JobDown in zone aa gets resolved.
    73  	amc.Push(At(3.6), Alert("alertname", "JobDown", "job", "testjob", "zone", "aa").Active(2.2, 3.6))
    74  
    75  	co.Want(Between(2, 2.5),
    76  		Alert("alertname", "test1", "job", "testjob", "zone", "aa").Active(1),
    77  		Alert("alertname", "InstanceDown", "job", "testjob", "zone", "aa").Active(1),
    78  		Alert("alertname", "InstanceDown", "job", "testjob", "zone", "ab").Active(1),
    79  	)
    80  
    81  	co.Want(Between(3, 3.5),
    82  		Alert("alertname", "test1", "job", "testjob", "zone", "aa").Active(1),
    83  		Alert("alertname", "InstanceDown", "job", "testjob", "zone", "ab").Active(1),
    84  		Alert("alertname", "JobDown", "job", "testjob", "zone", "aa").Active(2.2),
    85  	)
    86  
    87  	co.Want(Between(4, 4.5),
    88  		Alert("alertname", "test1", "job", "testjob", "zone", "aa").Active(1),
    89  		Alert("alertname", "InstanceDown", "job", "testjob", "zone", "aa").Active(1),
    90  		Alert("alertname", "InstanceDown", "job", "testjob", "zone", "ab").Active(1),
    91  		Alert("alertname", "JobDown", "job", "testjob", "zone", "aa").Active(2.2, 3.6),
    92  	)
    93  
    94  	at.Run()
    95  
    96  	t.Log(co.Check())
    97  }
    98  
    99  func TestAlwaysInhibiting(t *testing.T) {
   100  	t.Parallel()
   101  
   102  	// This integration test checks that when inhibited and inhibiting alerts
   103  	// gets resolved at the same time, the final notification contains both
   104  	// alerts.
   105  
   106  	conf := `
   107  route:
   108    receiver: "default"
   109    group_by: []
   110    group_wait:      1s
   111    group_interval:  1s
   112    repeat_interval: 1s
   113  
   114  receivers:
   115  - name: "default"
   116    webhook_configs:
   117    - url: 'http://%s'
   118  
   119  inhibit_rules:
   120  - source_match:
   121      alertname: JobDown
   122    target_match:
   123      alertname: InstanceDown
   124    equal:
   125      - job
   126      - zone
   127  `
   128  
   129  	at := NewAcceptanceTest(t, &AcceptanceOpts{
   130  		Tolerance: 150 * time.Millisecond,
   131  	})
   132  
   133  	co := at.Collector("webhook")
   134  	wh := NewWebhook(t, co)
   135  
   136  	amc := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
   137  
   138  	amc.Push(At(1), Alert("alertname", "InstanceDown", "job", "testjob", "zone", "aa"))
   139  	amc.Push(At(1), Alert("alertname", "JobDown", "job", "testjob", "zone", "aa"))
   140  
   141  	amc.Push(At(2.6), Alert("alertname", "JobDown", "job", "testjob", "zone", "aa").Active(1, 2.6))
   142  	amc.Push(At(2.6), Alert("alertname", "InstanceDown", "job", "testjob", "zone", "aa").Active(1, 2.6))
   143  
   144  	co.Want(Between(2, 2.5),
   145  		Alert("alertname", "JobDown", "job", "testjob", "zone", "aa").Active(1),
   146  	)
   147  
   148  	co.Want(Between(3, 3.5),
   149  		Alert("alertname", "InstanceDown", "job", "testjob", "zone", "aa").Active(1, 2.6),
   150  		Alert("alertname", "JobDown", "job", "testjob", "zone", "aa").Active(1, 2.6),
   151  	)
   152  
   153  	at.Run()
   154  
   155  	t.Log(co.Check())
   156  }
   157  

View as plain text