mirror of https://github.com/grafana/grafana.git
Alerting/Chore: Move tests from tests package (#34059)
Instead put in package folder but with package name suffixed with _test This enables code coverage within the pkg while still allow the tests to operate from external to package perspective (only exported things).
This commit is contained in:
parent
5e0e91cd9a
commit
babb17afd6
|
|
@ -1,4 +1,4 @@
|
||||||
package tests
|
package schedule_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
@ -12,6 +12,8 @@ import (
|
||||||
"github.com/google/go-cmp/cmp/cmpopts"
|
"github.com/google/go-cmp/cmp/cmpopts"
|
||||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
"github.com/grafana/grafana/pkg/services/ngalert/eval"
|
"github.com/grafana/grafana/pkg/services/ngalert/eval"
|
||||||
|
"github.com/grafana/grafana/pkg/services/ngalert/metrics"
|
||||||
|
"github.com/grafana/grafana/pkg/services/ngalert/tests"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/services/ngalert/state"
|
"github.com/grafana/grafana/pkg/services/ngalert/state"
|
||||||
|
|
||||||
|
|
@ -27,6 +29,8 @@ import (
|
||||||
"github.com/benbjohnson/clock"
|
"github.com/benbjohnson/clock"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var nilMetrics = metrics.NewMetrics(nil)
|
||||||
|
|
||||||
type evalAppliedInfo struct {
|
type evalAppliedInfo struct {
|
||||||
alertDefKey models.AlertRuleKey
|
alertDefKey models.AlertRuleKey
|
||||||
now time.Time
|
now time.Time
|
||||||
|
|
@ -34,9 +38,9 @@ type evalAppliedInfo struct {
|
||||||
|
|
||||||
func TestWarmStateCache(t *testing.T) {
|
func TestWarmStateCache(t *testing.T) {
|
||||||
evaluationTime, _ := time.Parse("2006-01-02", "2021-03-25")
|
evaluationTime, _ := time.Parse("2006-01-02", "2021-03-25")
|
||||||
dbstore := setupTestEnv(t, 1)
|
dbstore := tests.SetupTestEnv(t, 1)
|
||||||
|
|
||||||
rule := createTestAlertRule(t, dbstore, 600)
|
rule := tests.CreateTestAlertRule(t, dbstore, 600)
|
||||||
|
|
||||||
expectedEntries := []*state.State{
|
expectedEntries := []*state.State{
|
||||||
{
|
{
|
||||||
|
|
@ -119,15 +123,15 @@ func TestWarmStateCache(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAlertingTicker(t *testing.T) {
|
func TestAlertingTicker(t *testing.T) {
|
||||||
dbstore := setupTestEnv(t, 1)
|
dbstore := tests.SetupTestEnv(t, 1)
|
||||||
t.Cleanup(registry.ClearOverrides)
|
t.Cleanup(registry.ClearOverrides)
|
||||||
|
|
||||||
alerts := make([]*models.AlertRule, 0)
|
alerts := make([]*models.AlertRule, 0)
|
||||||
// create alert rule with zero interval (should never run)
|
// create alert rule with zero interval (should never run)
|
||||||
alerts = append(alerts, createTestAlertRule(t, dbstore, 0))
|
alerts = append(alerts, tests.CreateTestAlertRule(t, dbstore, 0))
|
||||||
|
|
||||||
// create alert rule with one second interval
|
// create alert rule with one second interval
|
||||||
alerts = append(alerts, createTestAlertRule(t, dbstore, 1))
|
alerts = append(alerts, tests.CreateTestAlertRule(t, dbstore, 1))
|
||||||
|
|
||||||
evalAppliedCh := make(chan evalAppliedInfo, len(alerts))
|
evalAppliedCh := make(chan evalAppliedInfo, len(alerts))
|
||||||
stopAppliedCh := make(chan models.AlertRuleKey, len(alerts))
|
stopAppliedCh := make(chan models.AlertRuleKey, len(alerts))
|
||||||
|
|
@ -167,7 +171,7 @@ func TestAlertingTicker(t *testing.T) {
|
||||||
|
|
||||||
// change alert rule interval to three seconds
|
// change alert rule interval to three seconds
|
||||||
var threeSecInterval int64 = 3
|
var threeSecInterval int64 = 3
|
||||||
alerts[0] = updateTestAlertRuleIntervalSeconds(t, dbstore, alerts[0], threeSecInterval)
|
alerts[0] = tests.UpdateTestAlertRuleIntervalSeconds(t, dbstore, alerts[0], threeSecInterval)
|
||||||
t.Logf("alert rule: %v interval reset to: %d", alerts[0].GetKey(), threeSecInterval)
|
t.Logf("alert rule: %v interval reset to: %d", alerts[0].GetKey(), threeSecInterval)
|
||||||
|
|
||||||
expectedAlertRulesEvaluated = []models.AlertRuleKey{alerts[1].GetKey()}
|
expectedAlertRulesEvaluated = []models.AlertRuleKey{alerts[1].GetKey()}
|
||||||
|
|
@ -209,7 +213,7 @@ func TestAlertingTicker(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
// create alert rule with one second interval
|
// create alert rule with one second interval
|
||||||
alerts = append(alerts, createTestAlertRule(t, dbstore, 1))
|
alerts = append(alerts, tests.CreateTestAlertRule(t, dbstore, 1))
|
||||||
|
|
||||||
expectedAlertRulesEvaluated = []models.AlertRuleKey{alerts[2].GetKey()}
|
expectedAlertRulesEvaluated = []models.AlertRuleKey{alerts[2].GetKey()}
|
||||||
t.Run(fmt.Sprintf("on 7th tick alert rules: %s should be evaluated", concatenate(expectedAlertRulesEvaluated)), func(t *testing.T) {
|
t.Run(fmt.Sprintf("on 7th tick alert rules: %s should be evaluated", concatenate(expectedAlertRulesEvaluated)), func(t *testing.T) {
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package tests
|
package state_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
// +build integration
|
// +build integration
|
||||||
|
|
||||||
package tests
|
package store_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/grafana/grafana/pkg/registry"
|
"github.com/grafana/grafana/pkg/registry"
|
||||||
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||||
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
||||||
|
"github.com/grafana/grafana/pkg/services/ngalert/tests"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
@ -25,19 +26,19 @@ func mockTimeNow() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAlertInstanceOperations(t *testing.T) {
|
func TestAlertInstanceOperations(t *testing.T) {
|
||||||
dbstore := setupTestEnv(t, baseIntervalSeconds)
|
dbstore := tests.SetupTestEnv(t, baseIntervalSeconds)
|
||||||
t.Cleanup(registry.ClearOverrides)
|
t.Cleanup(registry.ClearOverrides)
|
||||||
|
|
||||||
alertRule1 := createTestAlertRule(t, dbstore, 60)
|
alertRule1 := tests.CreateTestAlertRule(t, dbstore, 60)
|
||||||
orgID := alertRule1.OrgID
|
orgID := alertRule1.OrgID
|
||||||
|
|
||||||
alertRule2 := createTestAlertRule(t, dbstore, 60)
|
alertRule2 := tests.CreateTestAlertRule(t, dbstore, 60)
|
||||||
require.Equal(t, orgID, alertRule2.OrgID)
|
require.Equal(t, orgID, alertRule2.OrgID)
|
||||||
|
|
||||||
alertRule3 := createTestAlertRule(t, dbstore, 60)
|
alertRule3 := tests.CreateTestAlertRule(t, dbstore, 60)
|
||||||
require.Equal(t, orgID, alertRule3.OrgID)
|
require.Equal(t, orgID, alertRule3.OrgID)
|
||||||
|
|
||||||
alertRule4 := createTestAlertRule(t, dbstore, 60)
|
alertRule4 := tests.CreateTestAlertRule(t, dbstore, 60)
|
||||||
require.Equal(t, orgID, alertRule4.OrgID)
|
require.Equal(t, orgID, alertRule4.OrgID)
|
||||||
|
|
||||||
t.Run("can save and read new alert instance", func(t *testing.T) {
|
t.Run("can save and read new alert instance", func(t *testing.T) {
|
||||||
|
|
@ -24,8 +24,8 @@ import (
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
// setupTestEnv initializes a store to used by the tests.
|
// SetupTestEnv initializes a store to used by the tests.
|
||||||
func setupTestEnv(t *testing.T, baseIntervalSeconds int64) *store.DBstore {
|
func SetupTestEnv(t *testing.T, baseIntervalSeconds int64) *store.DBstore {
|
||||||
cfg := setting.NewCfg()
|
cfg := setting.NewCfg()
|
||||||
// AlertNG is disabled by default and only if it's enabled
|
// AlertNG is disabled by default and only if it's enabled
|
||||||
// its database migrations run and the relative database tables are created
|
// its database migrations run and the relative database tables are created
|
||||||
|
|
@ -65,7 +65,7 @@ func overrideAlertNGInRegistry(t *testing.T, cfg *setting.Cfg) ngalert.AlertNG {
|
||||||
}
|
}
|
||||||
|
|
||||||
// createTestAlertRule creates a dummy alert definition to be used by the tests.
|
// createTestAlertRule creates a dummy alert definition to be used by the tests.
|
||||||
func createTestAlertRule(t *testing.T, dbstore *store.DBstore, intervalSeconds int64) *models.AlertRule {
|
func CreateTestAlertRule(t *testing.T, dbstore *store.DBstore, intervalSeconds int64) *models.AlertRule {
|
||||||
d := rand.Intn(1000)
|
d := rand.Intn(1000)
|
||||||
ruleGroup := fmt.Sprintf("ruleGroup-%d", d)
|
ruleGroup := fmt.Sprintf("ruleGroup-%d", d)
|
||||||
err := dbstore.UpdateRuleGroup(store.UpdateRuleGroupCmd{
|
err := dbstore.UpdateRuleGroup(store.UpdateRuleGroupCmd{
|
||||||
|
|
@ -118,7 +118,7 @@ func createTestAlertRule(t *testing.T, dbstore *store.DBstore, intervalSeconds i
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateTestAlertRule update a dummy alert definition to be used by the tests.
|
// updateTestAlertRule update a dummy alert definition to be used by the tests.
|
||||||
func updateTestAlertRuleIntervalSeconds(t *testing.T, dbstore *store.DBstore, existingRule *models.AlertRule, intervalSeconds int64) *models.AlertRule {
|
func UpdateTestAlertRuleIntervalSeconds(t *testing.T, dbstore *store.DBstore, existingRule *models.AlertRule, intervalSeconds int64) *models.AlertRule {
|
||||||
cmd := store.UpdateRuleGroupCmd{
|
cmd := store.UpdateRuleGroupCmd{
|
||||||
OrgID: 1,
|
OrgID: 1,
|
||||||
NamespaceUID: "namespace",
|
NamespaceUID: "namespace",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue