|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package docs |
| 6 | + |
| 7 | +import ( |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestRenderAlertRuleTemplates(t *testing.T) { |
| 18 | + cases := []struct { |
| 19 | + name string |
| 20 | + setupFunc func(t *testing.T) string |
| 21 | + expectError bool |
| 22 | + expectEmpty bool |
| 23 | + validateFunc func(t *testing.T, result string) |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "no templates directory", |
| 27 | + setupFunc: func(t *testing.T) string { |
| 28 | + tmpDir := t.TempDir() |
| 29 | + return tmpDir |
| 30 | + }, |
| 31 | + expectError: false, |
| 32 | + expectEmpty: true, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "empty templates directory", |
| 36 | + setupFunc: func(t *testing.T) string { |
| 37 | + tmpDir := t.TempDir() |
| 38 | + templatesDir := filepath.Join(tmpDir, "kibana", "alerting_rule_template") |
| 39 | + require.NoError(t, os.MkdirAll(templatesDir, 0o755)) |
| 40 | + return tmpDir |
| 41 | + }, |
| 42 | + expectError: false, |
| 43 | + expectEmpty: true, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "single valid template", |
| 47 | + setupFunc: func(t *testing.T) string { |
| 48 | + tmpDir := t.TempDir() |
| 49 | + templatesDir := filepath.Join(tmpDir, "kibana", "alerting_rule_template") |
| 50 | + require.NoError(t, os.MkdirAll(templatesDir, 0o755)) |
| 51 | + |
| 52 | + template := `{ |
| 53 | + "attributes": { |
| 54 | + "name": "Test Alert Rule", |
| 55 | + "description": "This is a test alert rule description" |
| 56 | + } |
| 57 | + }` |
| 58 | + templateFile := filepath.Join(templatesDir, "test_rule.json") |
| 59 | + require.NoError(t, os.WriteFile(templateFile, []byte(template), 0o644)) |
| 60 | + return tmpDir |
| 61 | + }, |
| 62 | + expectError: false, |
| 63 | + expectEmpty: false, |
| 64 | + validateFunc: func(t *testing.T, result string) { |
| 65 | + assert.Contains(t, result, "Alert rule templates provide pre-defined configurations") |
| 66 | + assert.Contains(t, result, "**Test Alert Rule**") |
| 67 | + assert.Contains(t, result, "This is a test alert rule description") |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "multiple valid templates", |
| 72 | + setupFunc: func(t *testing.T) string { |
| 73 | + tmpDir := t.TempDir() |
| 74 | + templatesDir := filepath.Join(tmpDir, "kibana", "alerting_rule_template") |
| 75 | + require.NoError(t, os.MkdirAll(templatesDir, 0o755)) |
| 76 | + |
| 77 | + template1 := `{ |
| 78 | + "attributes": { |
| 79 | + "name": "First Rule", |
| 80 | + "description": "First description" |
| 81 | + } |
| 82 | + }` |
| 83 | + template2 := `{ |
| 84 | + "attributes": { |
| 85 | + "name": "Second Rule", |
| 86 | + "description": "Second description" |
| 87 | + } |
| 88 | + }` |
| 89 | + require.NoError(t, os.WriteFile(filepath.Join(templatesDir, "rule1.json"), []byte(template1), 0o644)) |
| 90 | + require.NoError(t, os.WriteFile(filepath.Join(templatesDir, "rule2.json"), []byte(template2), 0o644)) |
| 91 | + return tmpDir |
| 92 | + }, |
| 93 | + expectError: false, |
| 94 | + expectEmpty: false, |
| 95 | + validateFunc: func(t *testing.T, result string) { |
| 96 | + assert.Contains(t, result, "**First Rule**") |
| 97 | + assert.Contains(t, result, "First description") |
| 98 | + assert.Contains(t, result, "**Second Rule**") |
| 99 | + assert.Contains(t, result, "Second description") |
| 100 | + }, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "skip non-json files", |
| 104 | + setupFunc: func(t *testing.T) string { |
| 105 | + tmpDir := t.TempDir() |
| 106 | + templatesDir := filepath.Join(tmpDir, "kibana", "alerting_rule_template") |
| 107 | + require.NoError(t, os.MkdirAll(templatesDir, 0o755)) |
| 108 | + |
| 109 | + template := `{ |
| 110 | + "attributes": { |
| 111 | + "name": "Valid Rule", |
| 112 | + "description": "Valid description" |
| 113 | + } |
| 114 | + }` |
| 115 | + require.NoError(t, os.WriteFile(filepath.Join(templatesDir, "valid.json"), []byte(template), 0o644)) |
| 116 | + require.NoError(t, os.WriteFile(filepath.Join(templatesDir, "ignore.txt"), []byte("ignored"), 0o644)) |
| 117 | + require.NoError(t, os.WriteFile(filepath.Join(templatesDir, "README.md"), []byte("# readme"), 0o644)) |
| 118 | + return tmpDir |
| 119 | + }, |
| 120 | + expectError: false, |
| 121 | + expectEmpty: false, |
| 122 | + validateFunc: func(t *testing.T, result string) { |
| 123 | + assert.Contains(t, result, "**Valid Rule**") |
| 124 | + assert.NotContains(t, result, "ignored") |
| 125 | + assert.NotContains(t, result, "readme") |
| 126 | + }, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "skip subdirectories", |
| 130 | + setupFunc: func(t *testing.T) string { |
| 131 | + tmpDir := t.TempDir() |
| 132 | + templatesDir := filepath.Join(tmpDir, "kibana", "alerting_rule_template") |
| 133 | + require.NoError(t, os.MkdirAll(templatesDir, 0o755)) |
| 134 | + |
| 135 | + template := `{ |
| 136 | + "attributes": { |
| 137 | + "name": "Root Rule", |
| 138 | + "description": "Root description" |
| 139 | + } |
| 140 | + }` |
| 141 | + require.NoError(t, os.WriteFile(filepath.Join(templatesDir, "root.json"), []byte(template), 0o644)) |
| 142 | + |
| 143 | + // Create subdirectory with a file that should be skipped |
| 144 | + subDir := filepath.Join(templatesDir, "subdir") |
| 145 | + require.NoError(t, os.MkdirAll(subDir, 0o755)) |
| 146 | + require.NoError(t, os.WriteFile(filepath.Join(subDir, "nested.json"), []byte(template), 0o644)) |
| 147 | + return tmpDir |
| 148 | + }, |
| 149 | + expectError: false, |
| 150 | + expectEmpty: false, |
| 151 | + validateFunc: func(t *testing.T, result string) { |
| 152 | + // Should only have one rule mentioned |
| 153 | + assert.Equal(t, 1, strings.Count(result, "**Root Rule**")) |
| 154 | + }, |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "unreadable file", |
| 158 | + setupFunc: func(t *testing.T) string { |
| 159 | + tmpDir := t.TempDir() |
| 160 | + templatesDir := filepath.Join(tmpDir, "kibana", "alerting_rule_template") |
| 161 | + require.NoError(t, os.MkdirAll(templatesDir, 0o755)) |
| 162 | + |
| 163 | + unreadableFile := filepath.Join(templatesDir, "unreadable.json") |
| 164 | + require.NoError(t, os.WriteFile(unreadableFile, []byte("content"), 0o000)) |
| 165 | + return tmpDir |
| 166 | + }, |
| 167 | + expectError: true, |
| 168 | + expectEmpty: false, |
| 169 | + }, |
| 170 | + { |
| 171 | + name: "invalid json file", |
| 172 | + setupFunc: func(t *testing.T) string { |
| 173 | + tmpDir := t.TempDir() |
| 174 | + templatesDir := filepath.Join(tmpDir, "kibana", "alerting_rule_template") |
| 175 | + require.NoError(t, os.MkdirAll(templatesDir, 0o755)) |
| 176 | + |
| 177 | + invalidJSON := `{ "attributes": { "name": "Invalid" }` |
| 178 | + require.NoError(t, os.WriteFile(filepath.Join(templatesDir, "invalid.json"), []byte(invalidJSON), 0o644)) |
| 179 | + return tmpDir |
| 180 | + }, |
| 181 | + expectError: true, |
| 182 | + expectEmpty: false, |
| 183 | + }, |
| 184 | + } |
| 185 | + |
| 186 | + for _, tc := range cases { |
| 187 | + t.Run(tc.name, func(t *testing.T) { |
| 188 | + packageRoot := tc.setupFunc(t) |
| 189 | + |
| 190 | + result, err := renderAlertRuleTemplates(packageRoot, newEmptyLinkMap()) |
| 191 | + |
| 192 | + if tc.expectError { |
| 193 | + assert.Error(t, err) |
| 194 | + return |
| 195 | + } |
| 196 | + |
| 197 | + require.NoError(t, err) |
| 198 | + |
| 199 | + if tc.expectEmpty { |
| 200 | + assert.Empty(t, result) |
| 201 | + } else { |
| 202 | + assert.NotEmpty(t, result) |
| 203 | + if tc.validateFunc != nil { |
| 204 | + tc.validateFunc(t, result) |
| 205 | + } |
| 206 | + } |
| 207 | + }) |
| 208 | + } |
| 209 | +} |
0 commit comments