Skip to content

Commit

Permalink
Merge pull request #16 from fgschwan/master
Browse files Browse the repository at this point in the history
Added tests + automatic test/coverage in travis + docs/readme
  • Loading branch information
jozef-slezak committed Oct 10, 2017
2 parents f0ce5e6 + 1701a77 commit 1424035
Show file tree
Hide file tree
Showing 213 changed files with 12,886 additions and 10,687 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ before_install:
- sudo add-apt-repository ppa:masterminds/glide -y
- sudo apt-get update -q
- sudo apt-get install glide -y
- go get github.com/mattn/goveralls

script:
- make all

after_success:
- goveralls -coverprofile=/tmp/coverage.out -service=travis-ci
38 changes: 34 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
include Makeroutines.mk

COVER_DIR=/tmp/

# fix sirupsen/Sirupsen problem
define fix_sirupsen_case_sensitivity_problem
@echo "# fixing sirupsen case sensitivity problem, please wait ..."
@-rm -rf vendor/github.com/Sirupsen
@-find ./ -type f -name "*.go" -exec sed -i -e 's/github.com\/Sirupsen\/logrus/github.com\/sirupsen\/logrus/g' {} \;
endef


# run all tests with coverage
define test_cover_only
@echo "# running unit tests with coverage analysis"
@go test -covermode=count -coverprofile=${COVER_DIR}coverage_unit1.out ./bgp
@go test -covermode=count -coverprofile=${COVER_DIR}coverage_unit2.out ./bgp/gobgp
@echo "# merging coverage results"
@gocovmerge ${COVER_DIR}coverage_unit1.out ${COVER_DIR}coverage_unit2.out > ${COVER_DIR}coverage.out
@echo "# coverage data generated into ${COVER_DIR}coverage.out"
@echo "# done"
endef

# build all binaries
build:
@echo "# building"
Expand All @@ -17,6 +31,7 @@ build:
get-tools:
@go get -u -f "github.com/alecthomas/gometalinter"
@gometalinter --install
@go get -u -f "github.com/wadey/gocovmerge"

# install dependencies
install-dep:
Expand All @@ -33,18 +48,32 @@ update-dep:
# run checkstyle
checkstyle:
@echo "# running code analysis"
@gometalinter --vendor --exclude=vendor --deadline 1m --enable-gc --disable=aligncheck --disable=gotype --exclude=mock ./...
@gometalinter --vendor --exclude=vendor --deadline 1m --enable-gc --disable=aligncheck --disable=gotype --disable=gotypex --exclude=mock ./...
@echo "# done"

# run all tests
test:
@echo "# running unit tests"
@go test $$(go list ./... | grep -v /vendor/)

# run tests with coverage report
test-cover:
$(call test_cover_only)

# get coverage percentage in console(without report)
test-cover-without-report:
@echo "# getting test coverage"
@go test -cover $$(go list ./... | grep -v /vendor/)

# build examples
build-examples:
@echo "# building plugin examples"
@cd examples/gobgp_watch_plugin && go build

# run checkstyle
# run examples
run-examples:
@echo "# running examples"
@make build-examples
@echo "# running examples"
@./scripts/run_gobgp_watcher_examples.sh
@echo "# done"

Expand All @@ -68,8 +97,9 @@ all:
@make update-dep
@make checkstyle
@make build
@make test-cover
@make build-examples
@make run-examples
@make clean

.PHONY: build install-dep update-dep checkstyle coverage clean all run-examples
.PHONY: build install-dep update-dep checkstyle coverage clean run-examples test test-cover-without-report
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Ligato BGP Agent
[![Build Status](https://travis-ci.org/ligato/bgp-agent.svg?branch=master)](https://travis-ci.org/ligato/bgp-agent)
[![Coverage Status](https://coveralls.io/repos/github/ligato/bgp-agent/badge.svg?branch=master)](https://coveralls.io/github/ligato/bgp-agent?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/ligato/bgp-agent)](https://goreportcard.com/report/github.com/ligato/bgp-agent)
[![GoDoc](https://godoc.org/github.com/ligato/bgp-agent?status.svg)](https://godoc.org/github.com/ligato/bgp-agent)
[![GitHub license](https://img.shields.io/badge/license-Apache%20license%202.0-blue.svg)](https://github.com/ligato/bgp-agent/blob/master/LICENSE)
Expand All @@ -15,10 +16,22 @@ The architecture of the `Ligato BGP Agent` is shown in the following figure.

Every plugin has its source of BGP information (GoBGP,ExaBGP, Quagga). Communication with the source is vendor specific and therefore also the retrieved BGP information is usually vendor specific. The BGP information is translated into unified format and forwarded to clients. If the source of the BGP information supports listening for updates, plugin forwards to its clients also informations from updates.

Clients can register directly to plugins, choosing what information they want to consume. Each plugin can expose different set of BGP informations depending at capabilities of their source(GoBGP,ExaBGP,...) or plugin's client target(different plugins for different AFI/SAFI). But each type of BGP information, no matter from which plugin it came, has the same unified format. Also plugins can be clients to other plugins.
Clients can register directly to plugins, choosing what information they want to consume. Each plugin can expose different set of BGP informations depending at capabilities of their source(GoBGP,ExaBGP,...) or plugin's client target(different plugins for different AFI/SAFI). But each type of BGP information, no matter from which plugin it came, has the same unified format.

Plugins can be clients of other plugins too. This means that the architecture is quite flexible for the future usage. Different plugins can provide different types of BGP information/from different sources and can be kept separate as building stones for (hierarchy of) aggregator plugins. The aggregator plugin uses other plugins to retrieve needed information for its own registered clients. The aggregator plugins can for example provide information for one AFI/SAFI across multiple sources. This can be usefull if one source can provide all needed information or such source exists but can't be used for whatever reason. There are many possibilities how to combine plugins together to satisfy specific use cases.

Currently, only [GoBGP plugin](bgp/gobgp/README.md) that exposes IPv4 reachable routes is available. ExaBGP,Quagga plugins are not implemented.

## Quickstart
For a quick start with the BGP Agent, you can use makefile and start examples
```
make run-examples
```
The command pulls needed docker images from [Dockerhub](https://hub.docker.com/r/ligato/gobgp-for-rr/),
setups networking, builds the examples, runs them, validates their output and cleans after them.

You can check in the command output the most basic test, the [gobgp_watch_plugin](https://github.com/fgschwan/bgp-agent/tree/master/examples/gobgp_watch_plugin).

## Documentation
GoDoc can be browsed [online](https://godoc.org/github.com/ligato/bgp-agent).

Expand Down
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())
}
32 changes: 32 additions & 0 deletions bgp/bgp_api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 (
"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(x *testing.T) {
t := TestHelper{golangTesting: x}
t.DefaultSetup()

t.Given.WrappingFuncAsToChanResult()
t.When.SentRouteToWrappingFunc()
t.Then.ChannelReceiveIt()
}
Loading

0 comments on commit 1424035

Please sign in to comment.