Skip to content

Commit

Permalink
changed also bgp_api_test to BDD's given/when/test idiom
Browse files Browse the repository at this point in the history
  • Loading branch information
fgschwan committed Oct 9, 2017
1 parent 3a4015b commit 1701a77
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 18 deletions.
87 changes: 87 additions & 0 deletions bgp/bgp_api_helper_for_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 2017 Pantheon technologies s.r.o.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//Package bgp_test contains tests for API helper functions
package bgp_test

import (
"github.com/ligato/bgp-agent/bgp"
"github.com/ligato/cn-infra/logging/logrus"
. "github.com/onsi/gomega"
"net"
"testing"
)

// TestHelper allows tests to be written in given/when/then idiom of BDD
type TestHelper struct {
vars *Variables
Given Given
When When
Then Then
golangTesting *testing.T
}

// Variables is container of variables that should be accessible from every BDD component
type Variables struct {
golangT *testing.T
channel chan bgp.ReachableIPRoute
wrappingFunc func(info *bgp.ReachableIPRoute)
sentRoute bgp.ReachableIPRoute
}

// Given is composition of multiple test step methods (see BDD Given keyword)
type Given struct {
vars *Variables
}

// When is composition of multiple test step methods (see BDD When keyword)
type When struct {
vars *Variables
}

// Then is composition of multiple test step methods (see BDD Then keyword)
type Then struct {
vars *Variables
}

// DefaultSetup setups needed variables and ensures that these variables are accessible from all test BDD components (given, when, then)
func (t *TestHelper) DefaultSetup() {
// creating and linking variables to test parts
t.vars = &Variables{}
t.Given.vars = t.vars
t.When.vars = t.vars
t.Then.vars = t.vars

// registering gomega
RegisterTestingT(t.vars.golangT)

// initialize used channel
t.vars.channel = make(chan bgp.ReachableIPRoute, 1)
}

func (g *Given) WrappingFuncAsToChanResult() {
g.vars.wrappingFunc = bgp.ToChan(g.vars.channel, logrus.DefaultLogger())
}

func (w *When) SentRouteToWrappingFunc() {
w.vars.sentRoute = bgp.ReachableIPRoute{As: 1, Prefix: "1.2.3.4/32", Nexthop: net.IPv4(192, 168, 1, 1)}
w.vars.wrappingFunc(&w.vars.sentRoute)
}

func (t *Then) ChannelReceiveIt() {
Expect(t.vars.channel).ToNot(BeEmpty())
received := <-t.vars.channel
Expect(received).To(Equal(t.vars.sentRoute))
Expect(t.vars.channel).To(BeEmpty())
}
26 changes: 8 additions & 18 deletions bgp/bgp_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package bgp
//Package bgp_test contains tests for API helper functions
package bgp_test

import (
"github.com/ligato/cn-infra/logging/logrus"
. "github.com/onsi/gomega"
"net"
"reflect"
"testing"
)

// TestToChan tests ability of ToChan(...) function to create wrapping function that wraps given channel and forwards
// all ReachableIPRoute data (given to the wrapping function by parameter) to the channel inside.
// Test doesn't check logging capabilities of wrapping function.
func TestToChan(t *testing.T) {
// prepare of tested instances/helper instances
RegisterTestingT(t)
channel := make(chan ReachableIPRoute, 1)
wrappingFunc := ToChan(channel, logrus.DefaultLogger())
func TestToChan(x *testing.T) {
t := TestHelper{golangTesting: x}
t.DefaultSetup()

// testing ToChan's returned function
Expect(channel).To(BeEmpty())
sent := ReachableIPRoute{As: 1, Prefix: "1.2.3.4/32", Nexthop: net.IPv4(192, 168, 1, 1)}
wrappingFunc(&sent)
Expect(channel).ToNot(BeEmpty())
received := <-channel
reflect.DeepEqual(sent, received)
Expect(channel).To(BeEmpty())
t.Given.WrappingFuncAsToChanResult()
t.When.SentRouteToWrappingFunc()
t.Then.ChannelReceiveIt()
}

0 comments on commit 1701a77

Please sign in to comment.