/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ package config_test import ( "fmt" "testing" "text/template" "github.com/stretchr/testify/assert" "git.st8l.com/luxolus/kdnotify/config" "git.st8l.com/luxolus/kdnotify/schema/notify/state" "git.st8l.com/luxolus/kdnotify/schema/notify/ty" ) func Test_WatchConfig(t *testing.T) { t.Parallel() cases := []struct { Name string Yaml string Expected config.WatchConfig ShouldError bool CompareFunc func(t *testing.T, expected, actual config.WatchConfig) bool }{ { Name: "Empty", Yaml: "{}", }, { Name: "EmptyWithKey", Yaml: "{ watch: {} }", }, { Name: "Minimal", Yaml: ` watch: rules: - exec: /bin/echo hello test `, Expected: config.WatchConfig{ Rules: []config.WatchRule{ { Exec: s2t("/bin/echo hello test"), }, }, }, CompareFunc: CompareWatchConfig, }, { Name: "StringArgs", Yaml: ` watch: rules: - exec: /bin/echo hello test type: INSTANCE state: STOP instance: foo_instance `, Expected: config.WatchConfig{ Rules: []config.WatchRule{ { Exec: s2t("/bin/echo hello test"), Type: ty.INSTANCE, State: state.STOP, Instance: []string{"foo_instance"}, }, }, }, CompareFunc: CompareWatchConfig, }, { Name: "ArrayArgs", Yaml: ` watch: rules: - exec: /bin/echo hello test type: [INSTANCE, GROUP] state: [MASTER] instance: [foo_instance, bar_instance] `, Expected: config.WatchConfig{ Rules: []config.WatchRule{ { Exec: s2t("/bin/echo hello test"), Type: ty.INSTANCE | ty.GROUP, State: state.MASTER, Instance: []string{"foo_instance", "bar_instance"}, }, }, }, CompareFunc: CompareWatchConfig, }, } for i, test := range cases { test := test // Avoid closure capture by ref tname := fmt.Sprintf("%s@%d:%d", test.Name, i+1, len(cases)) t.Run(tname, func(t *testing.T) { t.Parallel() cfg, err := config.WatchConfigFromYAML([]byte(test.Yaml)) if !test.ShouldError && assert.NoError(t, err, "failed to unmarshal yaml") { if test.CompareFunc != nil { test.CompareFunc(t, test.Expected, cfg) } } else { assert.Error(t, err) } }) } } func CompareWatchConfig(t *testing.T, expected, actual config.WatchConfig) bool { return assert.Equal(t, expected, actual) } func s2t(str string) *template.Template { return template.Must(template.New("WatchRule").Parse(str)) }