From 2454d5e09db560ad7dd4b7fff243d74d88be11ae Mon Sep 17 00:00:00 2001 From: IWASE Yusuke Date: Thu, 20 Jul 2017 16:57:59 +0900 Subject: config/bgp_configs_test: Add test for config validation Signed-off-by: IWASE Yusuke --- config/bgp_configs_test.go | 63 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) (limited to 'config') diff --git a/config/bgp_configs_test.go b/config/bgp_configs_test.go index 656652ee..152ace2c 100644 --- a/config/bgp_configs_test.go +++ b/config/bgp_configs_test.go @@ -16,8 +16,15 @@ package config import ( - "github.com/stretchr/testify/assert" + "bufio" + "os" + "path" + "runtime" + "strings" "testing" + + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" ) func TestEqual(t *testing.T) { @@ -53,3 +60,57 @@ func TestEqual(t *testing.T) { ps2.PrefixSetName = "ps2" assert.False(ps1.Equal(&ps2)) } + +func extractTomlFromMarkdown(fileMd string, fileToml string) error { + fMd, err := os.Open(fileMd) + if err != nil { + return err + } + defer fMd.Close() + + fToml, err := os.Create(fileToml) + if err != nil { + return err + } + defer fToml.Close() + + isBody := false + scanner := bufio.NewScanner(fMd) + fTomlWriter := bufio.NewWriter(fToml) + for scanner.Scan() { + if curText := scanner.Text(); strings.HasPrefix(curText, "```toml") { + isBody = true + } else if strings.HasPrefix(curText, "```") { + isBody = false + } else if isBody { + if _, err := fTomlWriter.WriteString(curText + "\n"); err != nil { + return err + } + } + } + + fTomlWriter.Flush() + if err := scanner.Err(); err != nil { + return err + } + return nil +} + +func TestConfigExample(t *testing.T) { + assert := assert.New(t) + + _, f, _, _ := runtime.Caller(0) + fileMd := path.Join(path.Dir(f), "../docs/sources/configuration.md") + fileToml := "/tmp/gobgpd.example.toml" + assert.NoError(extractTomlFromMarkdown(fileMd, fileToml)) + defer os.Remove(fileToml) + + format := detectConfigFileType(fileToml, "") + c := &BgpConfigSet{} + v := viper.New() + v.SetConfigFile(fileToml) + v.SetConfigType(format) + assert.NoError(v.ReadInConfig()) + assert.NoError(v.UnmarshalExact(c)) + assert.NoError(setDefaultConfigValuesWithViper(v, c)) +} -- cgit v1.2.3