Skip to content

Commit 4ba2312

Browse files
committed
config: adjust test to generate json files
Signed-off-by: Jakob Hahn <[email protected]>
1 parent 24aee77 commit 4ba2312

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

config/config_test.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package config
1616
import (
1717
"encoding/json"
1818
"fmt"
19+
"os"
1920
"strings"
2021
"testing"
2122

@@ -24,9 +25,45 @@ import (
2425
yaml "gopkg.in/yaml.v3"
2526
)
2627

28+
func yamlToJson(t *testing.T, path string) error {
29+
t.Helper()
30+
data := make(map[string]any)
31+
fileReader, err := os.Open(fmt.Sprintf("%s.yml", path))
32+
if err != nil {
33+
return fmt.Errorf("error reading config file: %s", err)
34+
}
35+
defer fileReader.Close()
36+
37+
decoder := yaml.NewDecoder(fileReader)
38+
if err := decoder.Decode(&data); err != nil {
39+
return err
40+
}
41+
42+
jsonData, err := json.Marshal(&data)
43+
if err != nil {
44+
return err
45+
}
46+
47+
file, err := os.Create(fmt.Sprintf("%s.json", path))
48+
if err != nil {
49+
return err
50+
}
51+
defer file.Close()
52+
t.Cleanup(func() { os.Remove(fmt.Sprintf("%s.json", path)) })
53+
54+
if _, err = file.Write(jsonData); err != nil {
55+
return err
56+
}
57+
return nil
58+
}
59+
2760
func TestLoadConfig(t *testing.T) {
2861
diff := make([]*SafeConfig, 2)
29-
for idx, file := range []string{"testdata/blackbox-good.yml", "testdata/blackbox-good.json"} {
62+
path := "testdata/blackbox-good"
63+
require.NoError(t, yamlToJson(t, path))
64+
65+
for idx, format := range []string{"yml", "json"} {
66+
file := fmt.Sprintf("%s.%s", path, format)
3067
t.Run(file, func(t *testing.T) {
3168
sc := NewSafeConfig(prometheus.NewRegistry())
3269

@@ -72,7 +109,11 @@ func TestRegexpMarshal(t *testing.T) {
72109

73110
// Testing the capability of Marsheling the config without errors
74111
func TestConfigMarshal(t *testing.T) {
75-
for _, file := range []string{"testdata/blackbox-good.yml", "testdata/blackbox-good.json"} {
112+
path := "testdata/blackbox-good"
113+
require.NoError(t, yamlToJson(t, path))
114+
115+
for _, format := range []string{"yml", "json"} {
116+
file := fmt.Sprintf("%s.%s", path, format)
76117
t.Run(file, func(t *testing.T) {
77118
sc := NewSafeConfig(prometheus.NewRegistry())
78119
err := sc.ReloadConfig(file, nil)
@@ -185,6 +226,7 @@ func TestLoadBadConfigs(t *testing.T) {
185226
},
186227
}
187228
for _, test := range tests {
229+
require.NoError(t, yamlToJson(t, test.input))
188230
for _, format := range test.format {
189231
path := fmt.Sprintf("%s.%s", test.input, format)
190232
t.Run(path, func(t *testing.T) {

0 commit comments

Comments
 (0)