alertmanager/config/coordinator_test.go
TJ Hoplock f6b942cf9b
chore!: adopt log/slog, drop go-kit/log (#4089)
* chore!: adopt log/slog, drop go-kit/log

The bulk of this change set was automated by the following script which
is being used to aid in converting the various exporters/projects to use
slog:

https://gist.github.com/tjhop/49f96fb7ebbe55b12deee0b0312d8434

This commit includes several changes:
- bump exporter-tookit to v0.13.1 for log/slog support
- updates golangci-lint deprecated configs
- enables sloglint linter
- removes old go-kit/log linter configs
- introduce some `if logger == nil { $newLogger }` additions to prevent
  nil references
- converts cluster membership config to use a stdlib compatible slog
  adapter, rather than creating a custom io.Writer for use as the
membership `logOutput` config

Signed-off-by: TJ Hoplock <t.hoplock@gmail.com>

* chore: address PR feedback

Signed-off-by: TJ Hoplock <t.hoplock@gmail.com>

---------

Signed-off-by: TJ Hoplock <t.hoplock@gmail.com>
2024-11-06 09:09:57 +00:00

84 lines
2.2 KiB
Go

// Copyright 2019 Prometheus Team
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"errors"
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/promslog"
)
type fakeRegisterer struct {
registeredCollectors []prometheus.Collector
}
func (r *fakeRegisterer) Register(prometheus.Collector) error {
return nil
}
func (r *fakeRegisterer) MustRegister(c ...prometheus.Collector) {
r.registeredCollectors = append(r.registeredCollectors, c...)
}
func (r *fakeRegisterer) Unregister(prometheus.Collector) bool {
return false
}
func TestCoordinatorRegistersMetrics(t *testing.T) {
fr := fakeRegisterer{}
NewCoordinator("testdata/conf.good.yml", &fr, promslog.NewNopLogger())
if len(fr.registeredCollectors) == 0 {
t.Error("expected NewCoordinator to register metrics on the given registerer")
}
}
func TestCoordinatorNotifiesSubscribers(t *testing.T) {
callBackCalled := false
c := NewCoordinator("testdata/conf.good.yml", prometheus.NewRegistry(), promslog.NewNopLogger())
c.Subscribe(func(*Config) error {
callBackCalled = true
return nil
})
err := c.Reload()
if err != nil {
t.Fatal(err)
}
if !callBackCalled {
t.Fatal("expected coordinator.Reload() to call subscribers")
}
}
func TestCoordinatorFailReloadWhenSubscriberFails(t *testing.T) {
errMessage := "something happened"
c := NewCoordinator("testdata/conf.good.yml", prometheus.NewRegistry(), promslog.NewNopLogger())
c.Subscribe(func(*Config) error {
return errors.New(errMessage)
})
err := c.Reload()
if err == nil {
t.Fatal("expected reload to throw an error")
}
if err.Error() != errMessage {
t.Fatalf("expected error message %q but got %q", errMessage, err)
}
}