Skip to content

Commit

Permalink
add tests and vendor dependency on testify require
Browse files Browse the repository at this point in the history
Signed-off-by: harsimran pabla <[email protected]>
  • Loading branch information
harsimran-pabla committed Apr 11, 2023
1 parent 57b3c8c commit fe6d156
Show file tree
Hide file tree
Showing 10 changed files with 3,687 additions and 3 deletions.
13 changes: 10 additions & 3 deletions bgp/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"sort"
"strings"
Expand All @@ -22,6 +23,12 @@ import (
"github.com/cilium/cilium-cli/status"
)

const (
padding = 3
minWidth = 5
paddingChar = ' '
)

// GetPeeringState gets peering state from all/specific cilium agent pods.
func (s *Status) GetPeeringState(ctx context.Context) error {
ctx, cancelFn := context.WithTimeout(ctx, s.params.WaitDuration)
Expand Down Expand Up @@ -115,13 +122,13 @@ func (s *Status) writeStatus(res map[string][]*models.BgpPeer) error {
}
fmt.Println(string(jsonStatus))
} else {
printSummary(res)
printSummary(os.Stdout, res)
}

return nil
}

func printSummary(peersPerNode map[string][]*models.BgpPeer) {
func printSummary(out io.Writer, peersPerNode map[string][]*models.BgpPeer) {
// sort by node names
var nodes []string
for node := range peersPerNode {
Expand All @@ -138,7 +145,7 @@ func printSummary(peersPerNode map[string][]*models.BgpPeer) {
}

// tab writer with min width 5 and padding 3
w := tabwriter.NewWriter(os.Stdout, 5, 0, 3, ' ', 0)
w := tabwriter.NewWriter(out, minWidth, 0, padding, paddingChar, 0)
fmt.Fprintln(w, "Node\tLocal AS\tPeer AS\tPeer Address\tSession State\tUptime\tFamily\tReceived\tAdvertised")

for _, node := range nodes {
Expand Down
142 changes: 142 additions & 0 deletions bgp/peers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium

package bgp

import (
"bufio"
"bytes"
"strings"
"testing"
"time"

"github.com/cilium/cilium/api/v1/models"

"github.com/stretchr/testify/require"
)

var (
expectedColFormat = []string{
"Node",
"Local AS",
"Peer AS",
"Peer Address",
"Session State",
"Uptime",
"Family",
"Received",
"Advertised",
}

ipv4Unicast_1 = &models.BgpPeerFamilies{
Accepted: 1,
Advertised: 1,
Afi: "ipv4",
Received: 1,
Safi: "unicast",
}

ipv6Unicast_1 = &models.BgpPeerFamilies{
Accepted: 0,
Advertised: 0,
Afi: "ipv6",
Received: 0,
Safi: "unicast",
}

peer1 = &models.BgpPeer{
LocalAsn: 65001,
PeerAddress: "192.168.0.2",
PeerAsn: 65002,
SessionState: "established",
UptimeNanoseconds: int64(time.Second),
Families: []*models.BgpPeerFamilies{ipv4Unicast_1, ipv6Unicast_1},
}

peer2 = &models.BgpPeer{
LocalAsn: 65101,
PeerAddress: "192.168.0.2",
PeerAsn: 65102,
SessionState: "established",
UptimeNanoseconds: int64(time.Second),
Families: []*models.BgpPeerFamilies{ipv4Unicast_1, ipv6Unicast_1},
}
)

func Test_printSummary(t *testing.T) {
testCases := []struct {
Name string
Config map[string][]*models.BgpPeer
expectedRows int
}{
{
Name: "Single node output, with single peer",
Config: map[string][]*models.BgpPeer{
"node_1": {peer1},
},
expectedRows: 3,
},
{
Name: "Single node output, with multiple peers",
Config: map[string][]*models.BgpPeer{
"node_1": {peer1, peer2},
},
expectedRows: 5,
},
{
Name: "Two node output, with single peer",
Config: map[string][]*models.BgpPeer{
"node_1": {peer1},
"node_2": {peer1},
},
expectedRows: 5,
},
{
Name: "Two node output, with multiple peers",
Config: map[string][]*models.BgpPeer{
"node_1": {peer1, peer2},
"node_2": {peer1, peer2},
},
expectedRows: 9,
},
}

for _, tt := range testCases {
var out bytes.Buffer

// function to test
printSummary(&out, tt.Config)

// validate rows and cols
rows := 0
scanner := bufio.NewScanner(&out)
for scanner.Scan() {
// First row should match col format
if rows == 0 {
validateColFormat(t, scanner.Text())
}

rows += 1
}
require.Equal(t, tt.expectedRows, rows)
}
}

func validateColFormat(t *testing.T, output string) {
outputSlice := strings.Split(output, strings.Repeat(string(paddingChar), padding))

// clean up white spaces, and empty [""] which can come in output
i := 0
for _, outputCol := range outputSlice {
outputCol = strings.TrimSpace(outputCol)

// we can get empty col, so delete that while maintaining order
if outputCol != "" {
outputSlice[i] = outputCol
i++
}
}
outputSlice = outputSlice[:i]

require.Equal(t, expectedColFormat, outputSlice)
}
28 changes: 28 additions & 0 deletions vendor/github.com/stretchr/testify/require/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fe6d156

Please sign in to comment.