1414package config
1515
1616import (
17+ "fmt"
1718 "os"
1819 "path/filepath"
1920 "testing"
@@ -26,8 +27,10 @@ func TestGenerateChecksum(t *testing.T) {
2627
2728 // Define paths for the temporary files.
2829 yamlFilePath := filepath .Join (tmpDir , "test.yml" )
29- ruleFilePath := filepath .Join (tmpDir , "rule_file.yml" )
30- scrapeConfigFilePath := filepath .Join (tmpDir , "scrape_config.yml" )
30+ ruleFile := "rule_file.yml"
31+ ruleFilePath := filepath .Join (tmpDir , ruleFile )
32+ scrapeConfigFile := "scrape_config.yml"
33+ scrapeConfigFilePath := filepath .Join (tmpDir , scrapeConfigFile )
3134
3235 // Define initial and modified content for the files.
3336 originalRuleContent := "groups:\n - name: example\n rules:\n - alert: ExampleAlert"
@@ -36,181 +39,202 @@ func TestGenerateChecksum(t *testing.T) {
3639 originalScrapeConfigContent := "scrape_configs:\n - job_name: example"
3740 modifiedScrapeConfigContent := "scrape_configs:\n - job_name: modified_example"
3841
39- // Define YAML content referencing the rule and scrape config files.
40- yamlContent := `
42+ testCases := []struct {
43+ name string
44+ ruleFilePath string
45+ scrapeConfigFilePath string
46+ }{
47+ {
48+ name : "Auto reload using relative path." ,
49+ ruleFilePath : ruleFile ,
50+ scrapeConfigFilePath : scrapeConfigFile ,
51+ },
52+ {
53+ name : "Auto reload using absolute path." ,
54+ ruleFilePath : ruleFilePath ,
55+ scrapeConfigFilePath : scrapeConfigFilePath ,
56+ },
57+ }
58+
59+ for _ , tc := range testCases {
60+ t .Run (tc .name , func (t * testing.T ) {
61+ // Define YAML content referencing the rule and scrape config files.
62+ yamlContent := fmt .Sprintf (`
4163rule_files:
42- - rule_file.yml
64+ - %s
4365scrape_config_files:
44- - scrape_config.yml
45- `
66+ - %s
67+ ` , tc . ruleFilePath , tc . scrapeConfigFilePath )
4668
47- // Write initial content to files.
48- require .NoError (t , os .WriteFile (ruleFilePath , []byte (originalRuleContent ), 0o644 ))
49- require .NoError (t , os .WriteFile (scrapeConfigFilePath , []byte (originalScrapeConfigContent ), 0o644 ))
50- require .NoError (t , os .WriteFile (yamlFilePath , []byte (yamlContent ), 0o644 ))
69+ // Write initial content to files.
70+ require .NoError (t , os .WriteFile (ruleFilePath , []byte (originalRuleContent ), 0o644 ))
71+ require .NoError (t , os .WriteFile (scrapeConfigFilePath , []byte (originalScrapeConfigContent ), 0o644 ))
72+ require .NoError (t , os .WriteFile (yamlFilePath , []byte (yamlContent ), 0o644 ))
5173
52- // Generate the original checksum.
53- originalChecksum := calculateChecksum (t , yamlFilePath )
74+ // Generate the original checksum.
75+ originalChecksum := calculateChecksum (t , yamlFilePath )
5476
55- t .Run ("Rule File Change" , func (t * testing.T ) {
56- // Modify the rule file.
57- require .NoError (t , os .WriteFile (ruleFilePath , []byte (modifiedRuleContent ), 0o644 ))
77+ t .Run ("Rule File Change" , func (t * testing.T ) {
78+ // Modify the rule file.
79+ require .NoError (t , os .WriteFile (ruleFilePath , []byte (modifiedRuleContent ), 0o644 ))
5880
59- // Checksum should change.
60- modifiedChecksum := calculateChecksum (t , yamlFilePath )
61- require .NotEqual (t , originalChecksum , modifiedChecksum )
81+ // Checksum should change.
82+ modifiedChecksum := calculateChecksum (t , yamlFilePath )
83+ require .NotEqual (t , originalChecksum , modifiedChecksum )
6284
63- // Revert the rule file.
64- require .NoError (t , os .WriteFile (ruleFilePath , []byte (originalRuleContent ), 0o644 ))
85+ // Revert the rule file.
86+ require .NoError (t , os .WriteFile (ruleFilePath , []byte (originalRuleContent ), 0o644 ))
6587
66- // Checksum should return to the original.
67- revertedChecksum := calculateChecksum (t , yamlFilePath )
68- require .Equal (t , originalChecksum , revertedChecksum )
69- })
88+ // Checksum should return to the original.
89+ revertedChecksum := calculateChecksum (t , yamlFilePath )
90+ require .Equal (t , originalChecksum , revertedChecksum )
91+ })
7092
71- t .Run ("Scrape Config Change" , func (t * testing.T ) {
72- // Modify the scrape config file.
73- require .NoError (t , os .WriteFile (scrapeConfigFilePath , []byte (modifiedScrapeConfigContent ), 0o644 ))
93+ t .Run ("Scrape Config Change" , func (t * testing.T ) {
94+ // Modify the scrape config file.
95+ require .NoError (t , os .WriteFile (scrapeConfigFilePath , []byte (modifiedScrapeConfigContent ), 0o644 ))
7496
75- // Checksum should change.
76- modifiedChecksum := calculateChecksum (t , yamlFilePath )
77- require .NotEqual (t , originalChecksum , modifiedChecksum )
97+ // Checksum should change.
98+ modifiedChecksum := calculateChecksum (t , yamlFilePath )
99+ require .NotEqual (t , originalChecksum , modifiedChecksum )
78100
79- // Revert the scrape config file.
80- require .NoError (t , os .WriteFile (scrapeConfigFilePath , []byte (originalScrapeConfigContent ), 0o644 ))
101+ // Revert the scrape config file.
102+ require .NoError (t , os .WriteFile (scrapeConfigFilePath , []byte (originalScrapeConfigContent ), 0o644 ))
81103
82- // Checksum should return to the original.
83- revertedChecksum := calculateChecksum (t , yamlFilePath )
84- require .Equal (t , originalChecksum , revertedChecksum )
85- })
104+ // Checksum should return to the original.
105+ revertedChecksum := calculateChecksum (t , yamlFilePath )
106+ require .Equal (t , originalChecksum , revertedChecksum )
107+ })
86108
87- t .Run ("Rule File Deletion" , func (t * testing.T ) {
88- // Delete the rule file.
89- require .NoError (t , os .Remove (ruleFilePath ))
109+ t .Run ("Rule File Deletion" , func (t * testing.T ) {
110+ // Delete the rule file.
111+ require .NoError (t , os .Remove (ruleFilePath ))
90112
91- // Checksum should change.
92- deletedChecksum := calculateChecksum (t , yamlFilePath )
93- require .NotEqual (t , originalChecksum , deletedChecksum )
113+ // Checksum should change.
114+ deletedChecksum := calculateChecksum (t , yamlFilePath )
115+ require .NotEqual (t , originalChecksum , deletedChecksum )
94116
95- // Restore the rule file.
96- require .NoError (t , os .WriteFile (ruleFilePath , []byte (originalRuleContent ), 0o644 ))
117+ // Restore the rule file.
118+ require .NoError (t , os .WriteFile (ruleFilePath , []byte (originalRuleContent ), 0o644 ))
97119
98- // Checksum should return to the original.
99- revertedChecksum := calculateChecksum (t , yamlFilePath )
100- require .Equal (t , originalChecksum , revertedChecksum )
101- })
120+ // Checksum should return to the original.
121+ revertedChecksum := calculateChecksum (t , yamlFilePath )
122+ require .Equal (t , originalChecksum , revertedChecksum )
123+ })
102124
103- t .Run ("Scrape Config Deletion" , func (t * testing.T ) {
104- // Delete the scrape config file.
105- require .NoError (t , os .Remove (scrapeConfigFilePath ))
125+ t .Run ("Scrape Config Deletion" , func (t * testing.T ) {
126+ // Delete the scrape config file.
127+ require .NoError (t , os .Remove (scrapeConfigFilePath ))
106128
107- // Checksum should change.
108- deletedChecksum := calculateChecksum (t , yamlFilePath )
109- require .NotEqual (t , originalChecksum , deletedChecksum )
129+ // Checksum should change.
130+ deletedChecksum := calculateChecksum (t , yamlFilePath )
131+ require .NotEqual (t , originalChecksum , deletedChecksum )
110132
111- // Restore the scrape config file.
112- require .NoError (t , os .WriteFile (scrapeConfigFilePath , []byte (originalScrapeConfigContent ), 0o644 ))
133+ // Restore the scrape config file.
134+ require .NoError (t , os .WriteFile (scrapeConfigFilePath , []byte (originalScrapeConfigContent ), 0o644 ))
113135
114- // Checksum should return to the original.
115- revertedChecksum := calculateChecksum (t , yamlFilePath )
116- require .Equal (t , originalChecksum , revertedChecksum )
117- })
136+ // Checksum should return to the original.
137+ revertedChecksum := calculateChecksum (t , yamlFilePath )
138+ require .Equal (t , originalChecksum , revertedChecksum )
139+ })
118140
119- t .Run ("Main File Change" , func (t * testing.T ) {
120- // Modify the main YAML file.
121- modifiedYamlContent := `
141+ t .Run ("Main File Change" , func (t * testing.T ) {
142+ // Modify the main YAML file.
143+ modifiedYamlContent := fmt . Sprintf ( `
122144global:
123145 scrape_interval: 3s
124146rule_files:
125- - rule_file.yml
147+ - %s
126148scrape_config_files:
127- - scrape_config.yml
128- `
129- require .NoError (t , os .WriteFile (yamlFilePath , []byte (modifiedYamlContent ), 0o644 ))
149+ - %s
150+ ` , tc . ruleFilePath , tc . scrapeConfigFilePath )
151+ require .NoError (t , os .WriteFile (yamlFilePath , []byte (modifiedYamlContent ), 0o644 ))
130152
131- // Checksum should change.
132- modifiedChecksum := calculateChecksum (t , yamlFilePath )
133- require .NotEqual (t , originalChecksum , modifiedChecksum )
153+ // Checksum should change.
154+ modifiedChecksum := calculateChecksum (t , yamlFilePath )
155+ require .NotEqual (t , originalChecksum , modifiedChecksum )
134156
135- // Revert the main YAML file.
136- require .NoError (t , os .WriteFile (yamlFilePath , []byte (yamlContent ), 0o644 ))
157+ // Revert the main YAML file.
158+ require .NoError (t , os .WriteFile (yamlFilePath , []byte (yamlContent ), 0o644 ))
137159
138- // Checksum should return to the original.
139- revertedChecksum := calculateChecksum (t , yamlFilePath )
140- require .Equal (t , originalChecksum , revertedChecksum )
141- })
160+ // Checksum should return to the original.
161+ revertedChecksum := calculateChecksum (t , yamlFilePath )
162+ require .Equal (t , originalChecksum , revertedChecksum )
163+ })
142164
143- t .Run ("Rule File Removed from YAML Config" , func (t * testing.T ) {
144- // Modify the YAML content to remove the rule file.
145- modifiedYamlContent := `
165+ t .Run ("Rule File Removed from YAML Config" , func (t * testing.T ) {
166+ // Modify the YAML content to remove the rule file.
167+ modifiedYamlContent := fmt . Sprintf ( `
146168scrape_config_files:
147- - scrape_config.yml
148- `
149- require .NoError (t , os .WriteFile (yamlFilePath , []byte (modifiedYamlContent ), 0o644 ))
169+ - %s
170+ ` , tc . scrapeConfigFilePath )
171+ require .NoError (t , os .WriteFile (yamlFilePath , []byte (modifiedYamlContent ), 0o644 ))
150172
151- // Checksum should change.
152- modifiedChecksum := calculateChecksum (t , yamlFilePath )
153- require .NotEqual (t , originalChecksum , modifiedChecksum )
173+ // Checksum should change.
174+ modifiedChecksum := calculateChecksum (t , yamlFilePath )
175+ require .NotEqual (t , originalChecksum , modifiedChecksum )
154176
155- // Revert the YAML content.
156- require .NoError (t , os .WriteFile (yamlFilePath , []byte (yamlContent ), 0o644 ))
177+ // Revert the YAML content.
178+ require .NoError (t , os .WriteFile (yamlFilePath , []byte (yamlContent ), 0o644 ))
157179
158- // Checksum should return to the original.
159- revertedChecksum := calculateChecksum (t , yamlFilePath )
160- require .Equal (t , originalChecksum , revertedChecksum )
161- })
180+ // Checksum should return to the original.
181+ revertedChecksum := calculateChecksum (t , yamlFilePath )
182+ require .Equal (t , originalChecksum , revertedChecksum )
183+ })
162184
163- t .Run ("Scrape Config Removed from YAML Config" , func (t * testing.T ) {
164- // Modify the YAML content to remove the scrape config file.
165- modifiedYamlContent := `
185+ t .Run ("Scrape Config Removed from YAML Config" , func (t * testing.T ) {
186+ // Modify the YAML content to remove the scrape config file.
187+ modifiedYamlContent := fmt . Sprintf ( `
166188rule_files:
167- - rule_file.yml
168- `
169- require .NoError (t , os .WriteFile (yamlFilePath , []byte (modifiedYamlContent ), 0o644 ))
170-
171- // Checksum should change.
172- modifiedChecksum := calculateChecksum (t , yamlFilePath )
173- require .NotEqual (t , originalChecksum , modifiedChecksum )
174-
175- // Revert the YAML content.
176- require .NoError (t , os .WriteFile (yamlFilePath , []byte (yamlContent ), 0o644 ))
177-
178- // Checksum should return to the original.
179- revertedChecksum := calculateChecksum (t , yamlFilePath )
180- require .Equal (t , originalChecksum , revertedChecksum )
181- })
182-
183- t .Run ("Empty Rule File" , func (t * testing.T ) {
184- // Write an empty rule file.
185- require .NoError (t , os .WriteFile (ruleFilePath , []byte ("" ), 0o644 ))
186-
187- // Checksum should change.
188- emptyChecksum := calculateChecksum (t , yamlFilePath )
189- require .NotEqual (t , originalChecksum , emptyChecksum )
190-
191- // Restore the rule file.
192- require .NoError (t , os .WriteFile (ruleFilePath , []byte (originalRuleContent ), 0o644 ))
193-
194- // Checksum should return to the original.
195- revertedChecksum := calculateChecksum (t , yamlFilePath )
196- require .Equal (t , originalChecksum , revertedChecksum )
197- })
198-
199- t .Run ("Empty Scrape Config File" , func (t * testing.T ) {
200- // Write an empty scrape config file.
201- require .NoError (t , os .WriteFile (scrapeConfigFilePath , []byte ("" ), 0o644 ))
202-
203- // Checksum should change.
204- emptyChecksum := calculateChecksum (t , yamlFilePath )
205- require .NotEqual (t , originalChecksum , emptyChecksum )
206-
207- // Restore the scrape config file.
208- require .NoError (t , os .WriteFile (scrapeConfigFilePath , []byte (originalScrapeConfigContent ), 0o644 ))
209-
210- // Checksum should return to the original.
211- revertedChecksum := calculateChecksum (t , yamlFilePath )
212- require .Equal (t , originalChecksum , revertedChecksum )
213- })
189+ - %s
190+ ` , tc .ruleFilePath )
191+ require .NoError (t , os .WriteFile (yamlFilePath , []byte (modifiedYamlContent ), 0o644 ))
192+
193+ // Checksum should change.
194+ modifiedChecksum := calculateChecksum (t , yamlFilePath )
195+ require .NotEqual (t , originalChecksum , modifiedChecksum )
196+
197+ // Revert the YAML content.
198+ require .NoError (t , os .WriteFile (yamlFilePath , []byte (yamlContent ), 0o644 ))
199+
200+ // Checksum should return to the original.
201+ revertedChecksum := calculateChecksum (t , yamlFilePath )
202+ require .Equal (t , originalChecksum , revertedChecksum )
203+ })
204+
205+ t .Run ("Empty Rule File" , func (t * testing.T ) {
206+ // Write an empty rule file.
207+ require .NoError (t , os .WriteFile (ruleFilePath , []byte ("" ), 0o644 ))
208+
209+ // Checksum should change.
210+ emptyChecksum := calculateChecksum (t , yamlFilePath )
211+ require .NotEqual (t , originalChecksum , emptyChecksum )
212+
213+ // Restore the rule file.
214+ require .NoError (t , os .WriteFile (ruleFilePath , []byte (originalRuleContent ), 0o644 ))
215+
216+ // Checksum should return to the original.
217+ revertedChecksum := calculateChecksum (t , yamlFilePath )
218+ require .Equal (t , originalChecksum , revertedChecksum )
219+ })
220+
221+ t .Run ("Empty Scrape Config File" , func (t * testing.T ) {
222+ // Write an empty scrape config file.
223+ require .NoError (t , os .WriteFile (scrapeConfigFilePath , []byte ("" ), 0o644 ))
224+
225+ // Checksum should change.
226+ emptyChecksum := calculateChecksum (t , yamlFilePath )
227+ require .NotEqual (t , originalChecksum , emptyChecksum )
228+
229+ // Restore the scrape config file.
230+ require .NoError (t , os .WriteFile (scrapeConfigFilePath , []byte (originalScrapeConfigContent ), 0o644 ))
231+
232+ // Checksum should return to the original.
233+ revertedChecksum := calculateChecksum (t , yamlFilePath )
234+ require .Equal (t , originalChecksum , revertedChecksum )
235+ })
236+ })
237+ }
214238}
215239
216240// calculateChecksum generates a checksum for the given YAML file path.
0 commit comments