github.com/osrg/gobgp/v3@v3.30.0/pkg/config/oc/bgp_configs_test.go (about) 1 // Copyright (C) 2016 Nippon Telegraph and Telephone Corporation. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 // implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package oc 17 18 import ( 19 "bufio" 20 "os" 21 "path" 22 "runtime" 23 "strings" 24 "testing" 25 26 "github.com/spf13/viper" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestEqual(t *testing.T) { 31 assert := assert.New(t) 32 p1 := Prefix{ 33 IpPrefix: "192.168.0.0", 34 MasklengthRange: "24..32", 35 } 36 p2 := Prefix{ 37 IpPrefix: "192.168.0.0", 38 MasklengthRange: "24..32", 39 } 40 assert.True(p1.Equal(&p2)) 41 assert.False(p1.Equal(nil)) 42 var p3 *Prefix 43 assert.False(p3.Equal(&p1)) 44 p3 = &Prefix{ 45 IpPrefix: "192.168.0.0", 46 MasklengthRange: "24..32", 47 } 48 assert.True(p3.Equal(&p1)) 49 p3.IpPrefix = "10.10.0.0" 50 assert.False(p3.Equal(&p1)) 51 ps1 := PrefixSet{ 52 PrefixSetName: "ps", 53 PrefixList: []Prefix{p1, p2}, 54 } 55 ps2 := PrefixSet{ 56 PrefixSetName: "ps", 57 PrefixList: []Prefix{p2, p1}, 58 } 59 assert.True(ps1.Equal(&ps2)) 60 ps2.PrefixSetName = "ps2" 61 assert.False(ps1.Equal(&ps2)) 62 } 63 64 func extractTomlFromMarkdown(fileMd string, fileToml string) error { 65 fMd, err := os.Open(fileMd) 66 if err != nil { 67 return err 68 } 69 defer fMd.Close() 70 71 fToml, err := os.Create(fileToml) 72 if err != nil { 73 return err 74 } 75 defer fToml.Close() 76 77 isBody := false 78 scanner := bufio.NewScanner(fMd) 79 fTomlWriter := bufio.NewWriter(fToml) 80 for scanner.Scan() { 81 if curText := scanner.Text(); strings.HasPrefix(curText, "```toml") { 82 isBody = true 83 } else if strings.HasPrefix(curText, "```") { 84 isBody = false 85 } else if isBody { 86 if _, err := fTomlWriter.WriteString(curText + "\n"); err != nil { 87 return err 88 } 89 } 90 } 91 92 fTomlWriter.Flush() 93 return scanner.Err() 94 } 95 96 func TestConfigExample(t *testing.T) { 97 assert := assert.New(t) 98 99 _, f, _, _ := runtime.Caller(0) 100 fileMd := path.Join(path.Dir(f), "../../../docs/sources/configuration.md") 101 fileToml := "/tmp/gobgpd.example.toml" 102 assert.NoError(extractTomlFromMarkdown(fileMd, fileToml)) 103 defer os.Remove(fileToml) 104 105 format := detectConfigFileType(fileToml, "") 106 c := &BgpConfigSet{} 107 v := viper.New() 108 v.SetConfigFile(fileToml) 109 v.SetConfigType(format) 110 assert.NoError(v.ReadInConfig()) 111 assert.NoError(v.UnmarshalExact(c)) 112 assert.NoError(setDefaultConfigValuesWithViper(v, c)) 113 114 // Test if we can set the parameters for a peer-group 115 for _, peerGroup := range c.PeerGroups { 116 if peerGroup.Config.PeerGroupName != "my-peer-group" { 117 continue 118 } 119 120 assert.True(peerGroup.Config.SendSoftwareVersion) 121 } 122 123 // Test if the peer-group inheritance works for neighbors 124 for _, neighbor := range c.Neighbors { 125 if neighbor.Config.PeerGroup != "my-peer-group" { 126 continue 127 } 128 129 assert.True(neighbor.Config.SendSoftwareVersion) 130 } 131 }