2018-08-26 15:17:15 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2019-09-28 14:21:05 +02:00
|
|
|
"bufio"
|
2018-12-11 22:01:50 +01:00
|
|
|
"bytes"
|
2019-09-28 14:21:05 +02:00
|
|
|
"fmt"
|
2018-10-11 13:09:04 +02:00
|
|
|
"path"
|
2018-08-26 15:17:15 +02:00
|
|
|
"path/filepath"
|
2019-09-28 14:21:05 +02:00
|
|
|
"strings"
|
2018-08-26 16:44:34 +02:00
|
|
|
"testing"
|
2018-12-11 22:01:50 +01:00
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/kr/pretty"
|
2019-09-28 14:21:05 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-12-11 22:01:50 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-04-12 01:26:41 +02:00
|
|
|
"github.com/zrepl/yaml-config"
|
2018-08-26 15:17:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSampleConfigsAreParsedWithoutErrors(t *testing.T) {
|
|
|
|
paths, err := filepath.Glob("./samples/*")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("glob failed: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range paths {
|
|
|
|
|
2018-10-11 13:09:04 +02:00
|
|
|
if path.Ext(p) != ".yml" {
|
|
|
|
t.Logf("skipping file %s", p)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-08-26 23:11:50 +02:00
|
|
|
t.Run(p, func(t *testing.T) {
|
|
|
|
c, err := ParseConfig(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error parsing %s:\n%+v", p, err)
|
|
|
|
}
|
2018-08-26 15:17:15 +02:00
|
|
|
|
2018-08-26 23:11:50 +02:00
|
|
|
t.Logf("file: %s", p)
|
|
|
|
t.Log(pretty.Sprint(c))
|
|
|
|
})
|
2018-08-26 15:17:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-08-31 21:50:59 +02:00
|
|
|
|
2020-02-23 23:24:12 +01:00
|
|
|
// template must be a template/text template with a single '{{ . }}' as placeholder for val
|
2022-10-24 22:09:02 +02:00
|
|
|
//
|
2022-10-23 18:16:37 +02:00
|
|
|
//nolint:deadcode,unused
|
2018-12-11 22:01:50 +01:00
|
|
|
func testValidConfigTemplate(t *testing.T, tmpl string, val string) *Config {
|
|
|
|
tmp, err := template.New("master").Parse(tmpl)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err = tmp.Execute(&buf, val)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return testValidConfig(t, buf.String())
|
|
|
|
}
|
2018-08-31 21:50:59 +02:00
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func testValidConfig(t *testing.T, input string) *Config {
|
2018-08-31 21:50:59 +02:00
|
|
|
t.Helper()
|
|
|
|
conf, err := testConfig(t, input)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, conf)
|
|
|
|
return conf
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConfig(t *testing.T, input string) (*Config, error) {
|
|
|
|
t.Helper()
|
|
|
|
return ParseConfigBytes([]byte(input))
|
2018-12-11 22:01:50 +01:00
|
|
|
}
|
2019-09-28 14:21:05 +02:00
|
|
|
|
|
|
|
func trimSpaceEachLineAndPad(s, pad string) string {
|
|
|
|
var out strings.Builder
|
|
|
|
scan := bufio.NewScanner(strings.NewReader(s))
|
|
|
|
for scan.Scan() {
|
|
|
|
fmt.Fprintf(&out, "%s%s\n", pad, bytes.TrimSpace(scan.Bytes()))
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrimSpaceEachLineAndPad(t *testing.T) {
|
|
|
|
foo := `
|
|
|
|
foo
|
|
|
|
bar baz
|
|
|
|
`
|
|
|
|
assert.Equal(t, " \n foo\n bar baz\n \n", trimSpaceEachLineAndPad(foo, " "))
|
|
|
|
}
|
2022-04-12 01:26:41 +02:00
|
|
|
|
|
|
|
func TestCronSpec(t *testing.T) {
|
|
|
|
|
|
|
|
expectAccept := []string{
|
|
|
|
`"* * * * *"`,
|
|
|
|
`"0-10 * * * *"`,
|
|
|
|
`"* 0-5,8,12 * * *"`,
|
|
|
|
}
|
|
|
|
|
|
|
|
expectFail := []string{
|
|
|
|
`* * * *`,
|
|
|
|
``,
|
|
|
|
`23`,
|
|
|
|
`"@reboot"`,
|
|
|
|
`"@every 1h30m"`,
|
|
|
|
`"@daily"`,
|
|
|
|
`* * * * * *`,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, input := range expectAccept {
|
|
|
|
t.Run(input, func(t *testing.T) {
|
|
|
|
s := fmt.Sprintf("spec: %s\n", input)
|
|
|
|
var v struct {
|
|
|
|
Spec CronSpec
|
|
|
|
}
|
|
|
|
v.Spec.Schedule = nil
|
|
|
|
t.Logf("input:\n%s", s)
|
|
|
|
err := yaml.UnmarshalStrict([]byte(s), &v)
|
|
|
|
t.Logf("error: %T %s", err, err)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, v.Spec.Schedule)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, input := range expectFail {
|
|
|
|
t.Run(input, func(t *testing.T) {
|
|
|
|
s := fmt.Sprintf("spec: %s\n", input)
|
|
|
|
var v struct {
|
|
|
|
Spec CronSpec
|
|
|
|
}
|
|
|
|
v.Spec.Schedule = nil
|
|
|
|
t.Logf("input: %q", s)
|
|
|
|
err := yaml.UnmarshalStrict([]byte(s), &v)
|
|
|
|
t.Logf("error: %T %s", err, err)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, v.Spec.Schedule)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|