| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package collecttest |
| 4 | |
| 5 | import ( |
| 6 | "encoding/json" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/stretchr/testify/assert" |
| 10 | "github.com/stretchr/testify/require" |
| 11 | "gopkg.in/yaml.v2" |
| 12 | ) |
| 13 | |
| 14 | type configurationProvider interface { |
| 15 | Configuration() any |
| 16 | } |
| 17 | |
| 18 | func TestConfigurationSerialize(t *testing.T, mod configurationProvider, cfgJSON, cfgYAML []byte) { |
| 19 | t.Helper() |
| 20 | tests := map[string]struct { |
| 21 | config []byte |
| 22 | unmarshal func(in []byte, out any) error |
| 23 | marshal func(in any) ([]byte, error) |
| 24 | }{ |
| 25 | "json": {config: cfgJSON, marshal: json.Marshal, unmarshal: json.Unmarshal}, |
| 26 | "yaml": {config: cfgYAML, marshal: yaml.Marshal, unmarshal: yaml.Unmarshal}, |
| 27 | } |
| 28 | |
| 29 | for name, test := range tests { |
| 30 | t.Run(name, func(t *testing.T) { |
| 31 | require.NoError(t, test.unmarshal(test.config, mod), "unmarshal test->mod") |
| 32 | bs, err := test.marshal(mod.Configuration()) |
| 33 | require.NoError(t, err, "marshal mod config") |
| 34 | |
| 35 | var want map[string]any |
| 36 | var got map[string]any |
| 37 | |
| 38 | require.NoError(t, test.unmarshal(test.config, &want), "unmarshal test->map") |
| 39 | require.NoError(t, test.unmarshal(bs, &got), "unmarshal mod->map") |
| 40 | require.NotNil(t, want, "want map") |
| 41 | require.NotNil(t, got, "got map") |
| 42 | assert.Equal(t, want, got) |
| 43 | }) |
| 44 | } |
| 45 | } |