Alerting: Implement GetStatus in the remote Alertmanager struct (#84887)
* Alerting: Implement GetStatus in the remote Alertmanager struct * update tests * fix tests, extract AlertmanagerConfig from PostableConfig * get the remote AM config instead of the Grafana one from the remote AM * pass grafana AM config in test * return error in GetStatus instead of logging it (internal AM)
This commit is contained in:
@@ -422,22 +422,32 @@ func (_c *AlertmanagerMock_GetSilence_Call) RunAndReturn(run func(context.Contex
|
||||
return _c
|
||||
}
|
||||
|
||||
// GetStatus provides a mock function with given fields:
|
||||
func (_m *AlertmanagerMock) GetStatus() definitions.GettableStatus {
|
||||
ret := _m.Called()
|
||||
// GetStatus provides a mock function with given fields: _a0
|
||||
func (_m *AlertmanagerMock) GetStatus(_a0 context.Context) (definitions.GettableStatus, error) {
|
||||
ret := _m.Called(_a0)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for GetStatus")
|
||||
}
|
||||
|
||||
var r0 definitions.GettableStatus
|
||||
if rf, ok := ret.Get(0).(func() definitions.GettableStatus); ok {
|
||||
r0 = rf()
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(0).(func(context.Context) (definitions.GettableStatus, error)); ok {
|
||||
return rf(_a0)
|
||||
}
|
||||
if rf, ok := ret.Get(0).(func(context.Context) definitions.GettableStatus); ok {
|
||||
r0 = rf(_a0)
|
||||
} else {
|
||||
r0 = ret.Get(0).(definitions.GettableStatus)
|
||||
}
|
||||
|
||||
return r0
|
||||
if rf, ok := ret.Get(1).(func(context.Context) error); ok {
|
||||
r1 = rf(_a0)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// AlertmanagerMock_GetStatus_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetStatus'
|
||||
@@ -446,23 +456,24 @@ type AlertmanagerMock_GetStatus_Call struct {
|
||||
}
|
||||
|
||||
// GetStatus is a helper method to define mock.On call
|
||||
func (_e *AlertmanagerMock_Expecter) GetStatus() *AlertmanagerMock_GetStatus_Call {
|
||||
return &AlertmanagerMock_GetStatus_Call{Call: _e.mock.On("GetStatus")}
|
||||
// - _a0 context.Context
|
||||
func (_e *AlertmanagerMock_Expecter) GetStatus(_a0 interface{}) *AlertmanagerMock_GetStatus_Call {
|
||||
return &AlertmanagerMock_GetStatus_Call{Call: _e.mock.On("GetStatus", _a0)}
|
||||
}
|
||||
|
||||
func (_c *AlertmanagerMock_GetStatus_Call) Run(run func()) *AlertmanagerMock_GetStatus_Call {
|
||||
func (_c *AlertmanagerMock_GetStatus_Call) Run(run func(_a0 context.Context)) *AlertmanagerMock_GetStatus_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
run()
|
||||
run(args[0].(context.Context))
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *AlertmanagerMock_GetStatus_Call) Return(_a0 definitions.GettableStatus) *AlertmanagerMock_GetStatus_Call {
|
||||
_c.Call.Return(_a0)
|
||||
func (_c *AlertmanagerMock_GetStatus_Call) Return(_a0 definitions.GettableStatus, _a1 error) *AlertmanagerMock_GetStatus_Call {
|
||||
_c.Call.Return(_a0, _a1)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *AlertmanagerMock_GetStatus_Call) RunAndReturn(run func() definitions.GettableStatus) *AlertmanagerMock_GetStatus_Call {
|
||||
func (_c *AlertmanagerMock_GetStatus_Call) RunAndReturn(run func(context.Context) (definitions.GettableStatus, error)) *AlertmanagerMock_GetStatus_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ type Alertmanager interface {
|
||||
ApplyConfig(context.Context, *models.AlertConfiguration) error
|
||||
SaveAndApplyConfig(ctx context.Context, config *apimodels.PostableUserConfig) error
|
||||
SaveAndApplyDefaultConfig(ctx context.Context) error
|
||||
GetStatus() apimodels.GettableStatus
|
||||
GetStatus(context.Context) (apimodels.GettableStatus, error)
|
||||
|
||||
// Silences
|
||||
CreateSilence(context.Context, *apimodels.PostableSilence) (string, error)
|
||||
|
||||
@@ -217,7 +217,9 @@ func TestMultiOrgAlertmanager_AlertmanagerFor(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
internalAm, ok := am.(*alertmanager)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, "N/A", *am.GetStatus().VersionInfo.Version)
|
||||
status, err := am.GetStatus(ctx)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "N/A", *status.VersionInfo.Version)
|
||||
require.Equal(t, int64(2), internalAm.orgID)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,22 +1,24 @@
|
||||
package notifier
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
||||
)
|
||||
|
||||
// TODO: We no longer do apimodels at this layer, move it to the API.
|
||||
func (am *alertmanager) GetStatus() apimodels.GettableStatus {
|
||||
func (am *alertmanager) GetStatus(_ context.Context) (apimodels.GettableStatus, error) {
|
||||
config := &apimodels.PostableUserConfig{}
|
||||
status := am.Base.GetStatus() // TODO: This should return a GettableStatus, for now it returns PostableUserConfig.
|
||||
if status == nil {
|
||||
return *apimodels.NewGettableStatus(&config.AlertmanagerConfig)
|
||||
return *apimodels.NewGettableStatus(&config.AlertmanagerConfig), nil
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(status, config); err != nil {
|
||||
am.logger.Error("Unable to unmarshall alertmanager config", "Err", err)
|
||||
return apimodels.GettableStatus{}, fmt.Errorf("unable to unmarshal alertmanager config: %w", err)
|
||||
}
|
||||
|
||||
return *apimodels.NewGettableStatus(&config.AlertmanagerConfig)
|
||||
return *apimodels.NewGettableStatus(&config.AlertmanagerConfig), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user