|
|
|
@ -14,6 +14,7 @@ |
|
|
|
|
package config |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"fmt" |
|
|
|
|
"os" |
|
|
|
|
"path/filepath" |
|
|
|
|
"testing" |
|
|
|
@ -26,8 +27,10 @@ func TestGenerateChecksum(t *testing.T) { |
|
|
|
|
|
|
|
|
|
// Define paths for the temporary files.
|
|
|
|
|
yamlFilePath := filepath.Join(tmpDir, "test.yml") |
|
|
|
|
ruleFilePath := filepath.Join(tmpDir, "rule_file.yml") |
|
|
|
|
scrapeConfigFilePath := filepath.Join(tmpDir, "scrape_config.yml") |
|
|
|
|
ruleFile := "rule_file.yml" |
|
|
|
|
ruleFilePath := filepath.Join(tmpDir, ruleFile) |
|
|
|
|
scrapeConfigFile := "scrape_config.yml" |
|
|
|
|
scrapeConfigFilePath := filepath.Join(tmpDir, scrapeConfigFile) |
|
|
|
|
|
|
|
|
|
// Define initial and modified content for the files.
|
|
|
|
|
originalRuleContent := "groups:\n- name: example\n rules:\n - alert: ExampleAlert" |
|
|
|
@ -36,13 +39,32 @@ func TestGenerateChecksum(t *testing.T) { |
|
|
|
|
originalScrapeConfigContent := "scrape_configs:\n- job_name: example" |
|
|
|
|
modifiedScrapeConfigContent := "scrape_configs:\n- job_name: modified_example" |
|
|
|
|
|
|
|
|
|
testCases := []struct { |
|
|
|
|
name string |
|
|
|
|
ruleFilePath string |
|
|
|
|
scrapeConfigFilePath string |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
name: "Auto reload using relative path.", |
|
|
|
|
ruleFilePath: ruleFile, |
|
|
|
|
scrapeConfigFilePath: scrapeConfigFile, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
name: "Auto reload using absolute path.", |
|
|
|
|
ruleFilePath: ruleFilePath, |
|
|
|
|
scrapeConfigFilePath: scrapeConfigFilePath, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, tc := range testCases { |
|
|
|
|
t.Run(tc.name, func(t *testing.T) { |
|
|
|
|
// Define YAML content referencing the rule and scrape config files.
|
|
|
|
|
yamlContent := ` |
|
|
|
|
yamlContent := fmt.Sprintf(` |
|
|
|
|
rule_files: |
|
|
|
|
- rule_file.yml |
|
|
|
|
- %s |
|
|
|
|
scrape_config_files: |
|
|
|
|
- scrape_config.yml |
|
|
|
|
` |
|
|
|
|
- %s |
|
|
|
|
`, tc.ruleFilePath, tc.scrapeConfigFilePath) |
|
|
|
|
|
|
|
|
|
// Write initial content to files.
|
|
|
|
|
require.NoError(t, os.WriteFile(ruleFilePath, []byte(originalRuleContent), 0o644)) |
|
|
|
@ -118,14 +140,14 @@ scrape_config_files: |
|
|
|
|
|
|
|
|
|
t.Run("Main File Change", func(t *testing.T) { |
|
|
|
|
// Modify the main YAML file.
|
|
|
|
|
modifiedYamlContent := ` |
|
|
|
|
modifiedYamlContent := fmt.Sprintf(` |
|
|
|
|
global: |
|
|
|
|
scrape_interval: 3s |
|
|
|
|
rule_files: |
|
|
|
|
- rule_file.yml |
|
|
|
|
- %s |
|
|
|
|
scrape_config_files: |
|
|
|
|
- scrape_config.yml |
|
|
|
|
` |
|
|
|
|
- %s |
|
|
|
|
`, tc.ruleFilePath, tc.scrapeConfigFilePath) |
|
|
|
|
require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644)) |
|
|
|
|
|
|
|
|
|
// Checksum should change.
|
|
|
|
@ -142,10 +164,10 @@ scrape_config_files: |
|
|
|
|
|
|
|
|
|
t.Run("Rule File Removed from YAML Config", func(t *testing.T) { |
|
|
|
|
// Modify the YAML content to remove the rule file.
|
|
|
|
|
modifiedYamlContent := ` |
|
|
|
|
modifiedYamlContent := fmt.Sprintf(` |
|
|
|
|
scrape_config_files: |
|
|
|
|
- scrape_config.yml |
|
|
|
|
` |
|
|
|
|
- %s |
|
|
|
|
`, tc.scrapeConfigFilePath) |
|
|
|
|
require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644)) |
|
|
|
|
|
|
|
|
|
// Checksum should change.
|
|
|
|
@ -162,10 +184,10 @@ scrape_config_files: |
|
|
|
|
|
|
|
|
|
t.Run("Scrape Config Removed from YAML Config", func(t *testing.T) { |
|
|
|
|
// Modify the YAML content to remove the scrape config file.
|
|
|
|
|
modifiedYamlContent := ` |
|
|
|
|
modifiedYamlContent := fmt.Sprintf(` |
|
|
|
|
rule_files: |
|
|
|
|
- rule_file.yml |
|
|
|
|
` |
|
|
|
|
- %s |
|
|
|
|
`, tc.ruleFilePath) |
|
|
|
|
require.NoError(t, os.WriteFile(yamlFilePath, []byte(modifiedYamlContent), 0o644)) |
|
|
|
|
|
|
|
|
|
// Checksum should change.
|
|
|
|
@ -211,6 +233,8 @@ rule_files: |
|
|
|
|
revertedChecksum := calculateChecksum(t, yamlFilePath) |
|
|
|
|
require.Equal(t, originalChecksum, revertedChecksum) |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// calculateChecksum generates a checksum for the given YAML file path.
|
|
|
|
|