Skip to content

Commit

Permalink
test: fix process test error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
natesales committed Sep 2, 2024
1 parent 31fa079 commit b1bb55e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
8 changes: 4 additions & 4 deletions pkg/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,10 @@ func Load(configBlob []byte) (*config.Config, error) {
}

if err := parseStatics(c.Kernel.Statics, c.Kernel.Statics4, c.Kernel.Statics6); err != nil {
log.Fatalf("parsing statics: %s", err)
return nil, fmt.Errorf("parsing statics: %s", err)
}
if err := parseStatics(c.Kernel.KStatics, c.Kernel.KStatics4, c.Kernel.KStatics6); err != nil {
log.Fatalf("parsing kstatics: %s", err)
return nil, fmt.Errorf("parsing kstatics: %s", err)
}

// Parse BFD configs
Expand Down Expand Up @@ -485,12 +485,12 @@ func Load(configBlob []byte) (*config.Config, error) {
if c.RTRServer != "" {
rtrServerParts := strings.Split(c.RTRServer, ":")
if len(rtrServerParts) != 2 {
log.Fatalf("Invalid rtr-server '%s' format should be host:port", rtrServerParts)
return nil, fmt.Errorf("invalid rtr-server '%s' format should be host:port", rtrServerParts)
}
c.RTRServerHost = rtrServerParts[0]
rtrServerPort, err := strconv.Atoi(rtrServerParts[1])
if err != nil {
log.Fatalf("Invalid RTR server port %s", rtrServerParts[1])
return nil, fmt.Errorf("invalid RTR server port %s", rtrServerParts[1])
}
c.RTRServerPort = rtrServerPort
}
Expand Down
43 changes: 18 additions & 25 deletions pkg/process/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ func TestCategorizeCommunity(t *testing.T) {
}
for _, tc := range testCases {
cType := categorizeCommunity(tc.input)
if cType != "" && tc.shouldError {
t.Errorf("categorizeCommunity should have errored on '%s' but didn't. expected error, got '%s'", tc.input, cType)
} else if cType == "" && !tc.shouldError {
t.Errorf("categorizeCommunity shouldn't have errored on '%s' but did. expected '%s'", tc.input, tc.expectedOutput)
} else if cType != tc.expectedOutput {
t.Errorf("categorizeCommunity %s failed. expected '%v' got '%v'", tc.input, tc.expectedOutput, cType)
if tc.shouldError {
assert.Equal(t, "", cType)
} else {
assert.Equal(t, tc.expectedOutput, cType)
}
}
}
Expand Down Expand Up @@ -111,22 +109,18 @@ peers:
assert.NoError(t, err)

assert.Len(t, globalConfig.Peers, 2)
for peerName, peerData := range globalConfig.Peers {
switch peerName {
case "Peer 10":
assert.Equal(t, 65510, util.Deref(peerData.ASN))
assert.Equal(t, 110, util.Deref(peerData.LocalPref))
assert.True(t, util.Deref(peerData.SetLocalPref))
assert.Nil(t, peerData.DefaultLocalPref)
case "Peer 20":
assert.Equal(t, 65520, util.Deref(peerData.ASN))
assert.Equal(t, 100, util.Deref(peerData.LocalPref))
assert.False(t, util.Deref(peerData.SetLocalPref))
assert.Equal(t, 120, util.Deref(peerData.DefaultLocalPref))
default:
t.Errorf("peer %s unexpected", peerName)
}
}

peer10 := globalConfig.Peers["Peer 10"]
assert.Equal(t, 65510, util.Deref(peer10.ASN))
assert.Equal(t, 110, util.Deref(peer10.LocalPref))
assert.True(t, util.Deref(peer10.SetLocalPref))
assert.Nil(t, peer10.DefaultLocalPref)

peer20 := globalConfig.Peers["Peer 20"]
assert.Equal(t, 65520, util.Deref(peer20.ASN))
assert.Equal(t, 100, util.Deref(peer20.LocalPref))
assert.False(t, util.Deref(peer20.SetLocalPref))
assert.Equal(t, 120, util.Deref(peer20.DefaultLocalPref))
}

func TestLoadConfigInvalidYAML(t *testing.T) {
Expand Down Expand Up @@ -186,9 +180,8 @@ kernel:
"2001:db8:2::/64" : "2001:db8::1"
`
_, err := Load([]byte(configFile))
if err == nil || !strings.Contains(err.Error(), "Invalid static prefix") {
t.Errorf("expected invalid static prefix error, got %+v", err)
}
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Invalid static prefix")
}

func TestLoadConfigInvalidVIP(t *testing.T) {
Expand Down

0 comments on commit b1bb55e

Please sign in to comment.