|
| 1 | +package alerting |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/grafana/grafana/pkg/infra/log" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + testFileBrokenYAML = "./testdata/common/broken-yaml" |
| 13 | + testFileEmptyFile = "./testdata/common/empty-file" |
| 14 | + testFileEmptyFolder = "./testdata/common/empty-folder" |
| 15 | + testFileSupportedFiletypes = "./testdata/common/supported-filetypes" |
| 16 | + testFileCorrectProperties = "./testdata/alert_rules/correct-properties" |
| 17 | + testFileCorrectPropertiesWithOrg = "./testdata/alert_rules/correct-properties-with-org" |
| 18 | + testFileMultipleRules = "./testdata/alert_rules/multiple-rules" |
| 19 | + testFileMultipleFiles = "./testdata/alert_rules/multiple-files" |
| 20 | + testFileCorrectProperties_cp = "./testdata/contact_points/correct-properties" |
| 21 | + testFileCorrectPropertiesWithOrg_cp = "./testdata/contact_points/correct-properties-with-org" |
| 22 | + testFileEmptyUID = "./testdata/contact_points/empty-uid" |
| 23 | + testFileMissingUID = "./testdata/contact_points/missing-uid" |
| 24 | + testFileWhitespaceUID = "./testdata/contact_points/whitespace-uid" |
| 25 | + testFileMultipleCps = "./testdata/contact_points/multiple-contact-points" |
| 26 | +) |
| 27 | + |
| 28 | +func TestConfigReader(t *testing.T) { |
| 29 | + configReader := newRulesConfigReader(log.NewNopLogger()) |
| 30 | + ctx := context.Background() |
| 31 | + t.Run("a broken YAML file should error", func(t *testing.T) { |
| 32 | + _, err := configReader.readConfig(ctx, testFileBrokenYAML) |
| 33 | + require.Error(t, err) |
| 34 | + }) |
| 35 | + t.Run("a rule file with correct properties should not error", func(t *testing.T) { |
| 36 | + ruleFiles, err := configReader.readConfig(ctx, testFileCorrectProperties) |
| 37 | + require.NoError(t, err) |
| 38 | + t.Run("when no organization is present it should be set to 1", func(t *testing.T) { |
| 39 | + require.Equal(t, int64(1), ruleFiles[0].Groups[0].Rules[0].OrgID) |
| 40 | + }) |
| 41 | + }) |
| 42 | + t.Run("a rule file with correct properties and specific org should not error", func(t *testing.T) { |
| 43 | + ruleFiles, err := configReader.readConfig(ctx, testFileCorrectPropertiesWithOrg) |
| 44 | + require.NoError(t, err) |
| 45 | + t.Run("when an organization is set it should not overwrite if with the default of 1", func(t *testing.T) { |
| 46 | + require.Equal(t, int64(1337), ruleFiles[0].Groups[0].Rules[0].OrgID) |
| 47 | + }) |
| 48 | + }) |
| 49 | + t.Run("an empty rule file should not make the config reader error", func(t *testing.T) { |
| 50 | + _, err := configReader.readConfig(ctx, testFileEmptyFile) |
| 51 | + require.NoError(t, err) |
| 52 | + }) |
| 53 | + t.Run("an empty folder should not make the config reader error", func(t *testing.T) { |
| 54 | + _, err := configReader.readConfig(ctx, testFileEmptyFolder) |
| 55 | + require.NoError(t, err) |
| 56 | + }) |
| 57 | + t.Run("the config reader should be able to read multiple files in the folder", func(t *testing.T) { |
| 58 | + ruleFiles, err := configReader.readConfig(ctx, testFileMultipleFiles) |
| 59 | + require.NoError(t, err) |
| 60 | + require.Len(t, ruleFiles, 2) |
| 61 | + }) |
| 62 | + t.Run("the config reader should be able to read multiple rule groups", func(t *testing.T) { |
| 63 | + ruleFiles, err := configReader.readConfig(ctx, testFileMultipleRules) |
| 64 | + require.NoError(t, err) |
| 65 | + require.Len(t, ruleFiles[0].Groups, 2) |
| 66 | + }) |
| 67 | + t.Run("the config reader should support .yaml,.yml and .json files", func(t *testing.T) { |
| 68 | + ruleFiles, err := configReader.readConfig(ctx, testFileSupportedFiletypes) |
| 69 | + require.NoError(t, err) |
| 70 | + require.Len(t, ruleFiles, 3) |
| 71 | + }) |
| 72 | + t.Run("a contact point file with correct properties should not error", func(t *testing.T) { |
| 73 | + file, err := configReader.readConfig(ctx, testFileCorrectProperties_cp) |
| 74 | + require.NoError(t, err) |
| 75 | + t.Run("when no organization is present it should be set to 1", func(t *testing.T) { |
| 76 | + require.Equal(t, int64(1), file[0].ContactPoints[0].OrgID) |
| 77 | + }) |
| 78 | + }) |
| 79 | + t.Run("a contact point file with correct properties and specific org should not error", func(t *testing.T) { |
| 80 | + file, err := configReader.readConfig(ctx, testFileCorrectPropertiesWithOrg_cp) |
| 81 | + require.NoError(t, err) |
| 82 | + t.Run("when an organization is set it should not overwrite if with the default of 1", func(t *testing.T) { |
| 83 | + require.Equal(t, int64(1337), file[0].ContactPoints[0].OrgID) |
| 84 | + }) |
| 85 | + }) |
| 86 | + t.Run("a contact point file with empty UID should fail", func(t *testing.T) { |
| 87 | + _, err := configReader.readConfig(ctx, testFileEmptyUID) |
| 88 | + require.Error(t, err) |
| 89 | + }) |
| 90 | + t.Run("a contact point file with missing UID should fail", func(t *testing.T) { |
| 91 | + _, err := configReader.readConfig(ctx, testFileMissingUID) |
| 92 | + require.Error(t, err) |
| 93 | + }) |
| 94 | + t.Run("a contact point file with whitespace UID should fail", func(t *testing.T) { |
| 95 | + _, err := configReader.readConfig(ctx, testFileWhitespaceUID) |
| 96 | + require.Error(t, err) |
| 97 | + }) |
| 98 | + t.Run("the config reader should be able to read a file with multiple contact points", func(t *testing.T) { |
| 99 | + file, err := configReader.readConfig(ctx, testFileMultipleCps) |
| 100 | + require.NoError(t, err) |
| 101 | + require.Len(t, file[0].ContactPoints, 2) |
| 102 | + }) |
| 103 | +} |
0 commit comments