diff options
author | IWASE Yusuke <iwase.yusuke0@gmail.com> | 2017-07-20 16:57:59 +0900 |
---|---|---|
committer | IWASE Yusuke <iwase.yusuke0@gmail.com> | 2017-07-24 11:12:05 +0900 |
commit | 2454d5e09db560ad7dd4b7fff243d74d88be11ae (patch) | |
tree | 1322e2d7fd1997b680dc8561c5b535092193f622 /config/bgp_configs_test.go | |
parent | a57c14cdb2edbde824c5fce4ec57ca7df3e34963 (diff) |
config/bgp_configs_test: Add test for config validation
Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com>
Diffstat (limited to 'config/bgp_configs_test.go')
-rw-r--r-- | config/bgp_configs_test.go | 63 |
1 files changed, 62 insertions, 1 deletions
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)) +} |