...

Source file src/github.com/prometheus/alertmanager/config/coordinator_test.go

Documentation: github.com/prometheus/alertmanager/config

     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 config
    15  
    16  import (
    17  	"errors"
    18  	"testing"
    19  
    20  	"github.com/go-kit/log"
    21  	"github.com/prometheus/client_golang/prometheus"
    22  )
    23  
    24  type fakeRegisterer struct {
    25  	registeredCollectors []prometheus.Collector
    26  }
    27  
    28  func (r *fakeRegisterer) Register(prometheus.Collector) error {
    29  	return nil
    30  }
    31  
    32  func (r *fakeRegisterer) MustRegister(c ...prometheus.Collector) {
    33  	r.registeredCollectors = append(r.registeredCollectors, c...)
    34  }
    35  
    36  func (r *fakeRegisterer) Unregister(prometheus.Collector) bool {
    37  	return false
    38  }
    39  
    40  func TestCoordinatorRegistersMetrics(t *testing.T) {
    41  	fr := fakeRegisterer{}
    42  	NewCoordinator("testdata/conf.good.yml", &fr, log.NewNopLogger())
    43  
    44  	if len(fr.registeredCollectors) == 0 {
    45  		t.Error("expected NewCoordinator to register metrics on the given registerer")
    46  	}
    47  }
    48  
    49  func TestCoordinatorNotifiesSubscribers(t *testing.T) {
    50  	callBackCalled := false
    51  	c := NewCoordinator("testdata/conf.good.yml", prometheus.NewRegistry(), log.NewNopLogger())
    52  	c.Subscribe(func(*Config) error {
    53  		callBackCalled = true
    54  		return nil
    55  	})
    56  
    57  	err := c.Reload()
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	if !callBackCalled {
    63  		t.Fatal("expected coordinator.Reload() to call subscribers")
    64  	}
    65  }
    66  
    67  func TestCoordinatorFailReloadWhenSubscriberFails(t *testing.T) {
    68  	errMessage := "something happened"
    69  	c := NewCoordinator("testdata/conf.good.yml", prometheus.NewRegistry(), log.NewNopLogger())
    70  
    71  	c.Subscribe(func(*Config) error {
    72  		return errors.New(errMessage)
    73  	})
    74  
    75  	err := c.Reload()
    76  	if err == nil {
    77  		t.Fatal("expected reload to throw an error")
    78  	}
    79  
    80  	if err.Error() != errMessage {
    81  		t.Fatalf("expected error message %q but got %q", errMessage, err)
    82  	}
    83  }
    84  

View as plain text