...

Source file src/github.com/prometheus/alertmanager/test/cli/acceptance/cli_test.go

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

     1  // Copyright 2019 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  	"os"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	. "github.com/prometheus/alertmanager/test/cli"
    25  )
    26  
    27  func TestMain(m *testing.M) {
    28  	if ok, err := AmtoolOk(); !ok {
    29  		panic("unable to access amtool binary: " + err.Error())
    30  	}
    31  	os.Exit(m.Run())
    32  }
    33  
    34  // TestAmtoolVersion checks that amtool is executable and
    35  // is reporting valid version info.
    36  func TestAmtoolVersion(t *testing.T) {
    37  	t.Parallel()
    38  	version, err := Version()
    39  	if err != nil {
    40  		t.Fatal("Unable to get amtool version", err)
    41  	}
    42  	t.Logf("testing amtool version: %v", version)
    43  }
    44  
    45  func TestAddAlert(t *testing.T) {
    46  	t.Parallel()
    47  
    48  	conf := `
    49  route:
    50    receiver: "default"
    51    group_by: [alertname]
    52    group_wait:      1s
    53    group_interval:  1s
    54    repeat_interval: 1ms
    55  
    56  receivers:
    57  - name: "default"
    58    webhook_configs:
    59    - url: 'http://%s'
    60      send_resolved: true
    61  `
    62  
    63  	at := NewAcceptanceTest(t, &AcceptanceOpts{
    64  		Tolerance: 150 * time.Millisecond,
    65  	})
    66  	co := at.Collector("webhook")
    67  	wh := NewWebhook(co)
    68  
    69  	amc := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
    70  
    71  	am := amc.Members()[0]
    72  
    73  	alert1 := Alert("alertname", "test1").Active(1, 2)
    74  	am.AddAlertsAt(0, alert1)
    75  	co.Want(Between(1, 2), Alert("alertname", "test1").Active(1))
    76  
    77  	at.Run()
    78  
    79  	t.Log(co.Check())
    80  }
    81  
    82  func TestQueryAlert(t *testing.T) {
    83  	t.Parallel()
    84  
    85  	conf := `
    86  route:
    87    receiver: "default"
    88    group_by: [alertname]
    89    group_wait:      1s
    90    group_interval:  1s
    91    repeat_interval: 1ms
    92  
    93  receivers:
    94  - name: "default"
    95    webhook_configs:
    96    - url: 'http://%s'
    97      send_resolved: true
    98  `
    99  
   100  	at := NewAcceptanceTest(t, &AcceptanceOpts{
   101  		Tolerance: 1 * time.Second,
   102  	})
   103  	co := at.Collector("webhook")
   104  	wh := NewWebhook(co)
   105  
   106  	amc := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
   107  	require.NoError(t, amc.Start())
   108  	defer amc.Terminate()
   109  
   110  	am := amc.Members()[0]
   111  
   112  	alert1 := Alert("alertname", "test1", "severity", "warning").Active(1)
   113  	alert2 := Alert("alertname", "test2", "severity", "info").Active(1)
   114  	am.AddAlerts(alert1, alert2)
   115  
   116  	alerts, err := am.QueryAlerts()
   117  	if err != nil {
   118  		t.Fatal("Failed to query alerts", err)
   119  	}
   120  	expectedAlerts := 2
   121  	if len(alerts) != expectedAlerts {
   122  		t.Fatalf("Incorrect number of alerts, expected %v, got %v", expectedAlerts, len(alerts))
   123  	}
   124  }
   125  
   126  func TestQuerySilence(t *testing.T) {
   127  	t.Parallel()
   128  
   129  	conf := `
   130  route:
   131    receiver: "default"
   132    group_by: [alertname]
   133    group_wait:      1s
   134    group_interval:  1s
   135    repeat_interval: 1ms
   136  
   137  receivers:
   138  - name: "default"
   139    webhook_configs:
   140    - url: 'http://%s'
   141      send_resolved: true
   142  `
   143  
   144  	at := NewAcceptanceTest(t, &AcceptanceOpts{
   145  		Tolerance: 1 * time.Second,
   146  	})
   147  	co := at.Collector("webhook")
   148  	wh := NewWebhook(co)
   149  
   150  	amc := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
   151  	require.NoError(t, amc.Start())
   152  	defer amc.Terminate()
   153  
   154  	am := amc.Members()[0]
   155  
   156  	silence1 := Silence(0, 4).Match("alertname=test1", "severity=warn").Comment("test1")
   157  	silence2 := Silence(0, 4).Match("foo").Comment("test foo")
   158  
   159  	am.SetSilence(0, silence1)
   160  	am.SetSilence(0, silence2)
   161  
   162  	sils, err := am.QuerySilence()
   163  	if err != nil {
   164  		t.Error("Failed to query silences: ", err)
   165  	}
   166  	expectedSils := 2
   167  	if len(sils) != expectedSils {
   168  		t.Errorf("Incorrect number of silences queried, expected: %v, actual: %v", expectedSils, len(sils))
   169  	}
   170  }
   171  

View as plain text