Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/util/compare_yaml_test.go
4093 views
1
package util
2
3
import (
4
"testing"
5
6
config_util "github.com/prometheus/common/config"
7
"github.com/stretchr/testify/require"
8
)
9
10
type NoSecretConfig struct {
11
ValOne int `yaml:"val_one,omitempty"`
12
ValTwo string `yaml:"val_two,omitempty"`
13
}
14
15
type SecretConfig struct {
16
Password config_util.Secret `yaml:"password,omitempty"`
17
ValThree int `yaml:"val_three,omitempty"`
18
}
19
20
func TestCompareEqualNoSecrets(t *testing.T) {
21
s1 := NoSecretConfig{
22
ValOne: 123,
23
ValTwo: "456",
24
}
25
s2 := NoSecretConfig{
26
ValOne: 123,
27
ValTwo: "456",
28
}
29
require.True(t, CompareYAML(s1, s2))
30
}
31
32
func TestCompareNotEqualNoSecrets(t *testing.T) {
33
s1 := NoSecretConfig{
34
ValOne: 123,
35
ValTwo: "321",
36
}
37
s2 := NoSecretConfig{
38
ValOne: 123,
39
ValTwo: "456",
40
}
41
require.False(t, CompareYAML(s1, s2))
42
}
43
44
func TestCompareEqualWithSecrets(t *testing.T) {
45
s1 := SecretConfig{
46
Password: config_util.Secret("pass"),
47
ValThree: 3,
48
}
49
50
s2 := SecretConfig{
51
Password: config_util.Secret("pass"),
52
ValThree: 3,
53
}
54
require.True(t, CompareYAMLWithHook(s1, s2, noScrubbedSecretsHook))
55
}
56
57
func TestCompareNotEqualWithSecrets(t *testing.T) {
58
s1 := SecretConfig{
59
Password: config_util.Secret("pass"),
60
ValThree: 3,
61
}
62
63
s2 := SecretConfig{
64
Password: config_util.Secret("not_pass"),
65
ValThree: 3,
66
}
67
require.False(t, CompareYAMLWithHook(s1, s2, noScrubbedSecretsHook))
68
69
s3 := SecretConfig{
70
Password: config_util.Secret("pass"),
71
ValThree: 4,
72
}
73
require.False(t, CompareYAMLWithHook(s1, s3, noScrubbedSecretsHook))
74
}
75
76
func noScrubbedSecretsHook(in interface{}) (ok bool, out interface{}, err error) {
77
switch v := in.(type) {
78
case config_util.Secret:
79
return true, string(v), nil
80
case *config_util.URL:
81
return true, v.String(), nil
82
default:
83
return false, nil, nil
84
}
85
}
86
87