Skip to content

Commit fb552da

Browse files
committed
Merge pull request socketplane#121 from dave-tucker/tests
Dockerize Testing, Enable Tests Requiring Sudo, Update Docs
2 parents 04e03a1 + 88a041f commit fb552da

File tree

8 files changed

+135
-62
lines changed

8 files changed

+135
-62
lines changed

HACKING.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
HACKING
2+
=======
3+
4+
How to contribute to SocketPlane
5+
6+
## Development Environment
7+
8+
- [Golang](http://golang.org/doc/code.html)
9+
- [Docker](https://docs.docker.com/installation/#installation)
10+
- [Fig](http://www.fig.sh/install.html)
11+
12+
The system running Docker must have the `openvswitch` kernel module must be loaded for the test suite to be run. You can load it using `modprobe openvswitch`
13+
14+
Support for boot2docker is provided using @dave-tucker's [fork](https://github.com/dave-tucker/boot2docker/tree/openvswitch)
15+
16+
```bash
17+
git clone https://github.com/dave-tucker/boot2docker.git
18+
git checkout openvswitch
19+
docker build -t boot2docker . && docker run --rm boot2docker > boot2docker.iso
20+
# if b2d is running, destroy it
21+
boot2docker destroy
22+
boot2docker init --iso="$(pwd)/boot2docker.iso"
23+
boot2docker up
24+
boot2docker ssh modprobe openvswitch
25+
```
26+
27+
## Workflow
28+
29+
We use the standard GitHub workflow which is made a lot easier by using [`hub`](https://hub.github.com/)
30+
31+
1. Create a fork
32+
33+
hub fork
34+
35+
2. Create a branch for your work
36+
37+
# For bugs
38+
git checkout -b bug/42
39+
# For long-lived feature branches
40+
git checkout -b feature/something-cool
41+
42+
3. Make your changes and commit
43+
44+
git add --all
45+
git commit -s
46+
47+
4. Push your changes to your GitHub fork
48+
49+
git push <github-user> <branch-name>
50+
51+
5. Raise a Pull Request
52+
53+
git pull-request
54+
55+
6. To make changes following a code review, checkout your working branch
56+
57+
git checkout <branch-name>
58+
59+
7. Make changes and then commit
60+
61+
git add --all
62+
git commit --amend
63+
git push --force
64+
65+
## Running the Tests
66+
67+
To run the tests inside a Docker container
68+
69+
```bash
70+
make test
71+
# or
72+
make test-all
73+
```
74+
75+
To run the tests locally you must run Open vSwitch, either via `fig up -d` or by installing it through your distribution's package manager.
76+
77+
```bash
78+
make test-local
79+
# or
80+
make test-all-local
81+
```

Makefile

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1+
.PHONY: build test test-all test-local test-all-local
2+
13
build:
24
docker build -t socketplane/socketplane .
35

6+
coverage:
7+
gover
8+
goveralls -coverprofile=gover.coverprofile -service=$(CI_SERVICE) -repotoken=$(COVERALLS_TOKEN)
9+
410
test:
5-
go test -covermode=count -test.short -coverprofile=daemon.cover.out -coverpkg=./... ./daemon
6-
go test -covermode=count -test.short -coverprofile=datastore.cover.out -coverpkg=./... ./ipam
7-
go test -covermode=count -test.short -coverprofile=socketplane.cover.out
11+
fig up -d
12+
docker run --privileged=true --net=host --rm -v $(shell pwd):/go/src/github.com/socketplane/socketplane -w /go/src/github.com/socketplane/socketplane davetucker/golang-ci:1.3 make test-local
13+
fig stop
814

915
test-all:
10-
go test -covermode=count -coverprofile=daemon.cover.out -coverpkg=./... ./daemon
11-
go test -covermode=count -coverprofile=datastore.cover.out -coverpkg=./... ./ipam
12-
go test -covermode=count -coverprofile=socketplane.cover.out
16+
fig up -d
17+
docker run --privileged=true --net=host --rm -v $(shell pwd):/go/src/github.com/socketplane/socketplane -w /go/src/github.com/socketplane/socketplane davetucker/golang-ci:1.3 make test-all-local
18+
fig stop
19+
20+
test-local:
21+
go test -covermode=count -test.short -coverprofile=daemon.coverprofile -coverpkg=./... ./daemon
22+
go test -covermode=count -test.short -coverprofile=datastore.coverprofile -coverpkg=./... ./ipam
23+
go test -covermode=count -test.short -coverprofile=socketplane.coverprofile
24+
25+
test-all-local:
26+
go test -covermode=count -coverprofile=daemon.coverprofile -coverpkg=./... ./daemon
27+
go test -covermode=count -coverprofile=datastore.coverprofile -coverpkg=./... ./ipam
28+
go test -covermode=count -coverprofile=socketplane.coverprofile

README.md

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -152,47 +152,7 @@ The Socketplane agent runs in its own container and you might find the following
152152

153153
## Hacking
154154

155-
Hacking uses the standard GitHub workflow which is made a lot easier by using [`hub`](https://hub.github.com/)
156-
157-
1. Clone this repository
158-
159-
git clone [email protected]:socketplane/socketplane
160-
161-
2. Create a fork
162-
163-
hub fork
164-
165-
3. Create a branch for your work
166-
167-
# For bugs
168-
git checkout -b bug/42
169-
# For long-lived feature branches
170-
git checkout -b feature/something-cool
171-
172-
4. Make your changes and commit
173-
174-
git add --all
175-
git commit -s
176-
177-
5. Push your changes to your GitHub fork
178-
179-
git push <github-user> <branch-name>
180-
181-
6. Raise a Pull Request
182-
183-
git pull-request
184-
185-
If you need to make changes to your pull request in response to commets etc...
186-
187-
7. Checkout your working branch
188-
189-
git checkout <branch-name>
190-
191-
8. Make changes and then commit
192-
193-
git add --all
194-
git commit --amend
195-
git push --force
155+
See [HACKING.md](HACKING.md)
196156

197157
## Contact us
198158

circle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ dependencies:
2424

2525
test:
2626
override:
27-
- sudo -E make test:
27+
- sudo -E make test-local:
2828
pwd: ../.go_workspace/src/${REPO_PATH}
2929
post:
3030
- sudo fig stop:

daemon/network.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ func CreateNetwork(id string, subnet *net.IPNet) (*Network, error) {
7575
// Interface does not exist, use the generated subnet
7676
gateway = ipam.Request(*subnet)
7777
network = &Network{id, subnet.String(), gateway.String(), vlan}
78-
AddInternalPort(ovs, defaultBridgeName, network.ID, vlan)
78+
if err = AddInternalPort(ovs, defaultBridgeName, network.ID, vlan); err != nil {
79+
return network, err
80+
}
7981
// TODO : Lame. Remove the sleep. This is required now to keep netlink happy
8082
// in the next step to find the created interface.
8183
time.Sleep(time.Second * 1)

daemon/network_test.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package daemon
22

33
import (
44
"fmt"
5+
"log"
56
"net"
7+
"os"
68
"testing"
79

810
"github.com/socketplane/socketplane/datastore"
@@ -11,8 +13,10 @@ import (
1113
var subnetArray []*net.IPNet
1214

1315
func TestInit(t *testing.T) {
14-
if testing.Short() {
15-
t.Skip("skipping test in short mode.")
16+
if os.Getuid() != 0 {
17+
msg := "Skipped test because it requires root privileges."
18+
log.Printf(msg)
19+
t.Skip(msg)
1620
}
1721
err := datastore.Init("eth0", true)
1822
if err != nil {
@@ -28,8 +32,10 @@ func TestInit(t *testing.T) {
2832
}
2933

3034
func TestNetworkCreate(t *testing.T) {
31-
if testing.Short() {
32-
t.Skip("skipping test in short mode.")
35+
if os.Getuid() != 0 {
36+
msg := "Skipped test because it requires root privileges."
37+
log.Printf(msg)
38+
t.Skip(msg)
3339
}
3440
for i := 0; i < len(subnetArray); i++ {
3541
network, err := CreateNetwork(fmt.Sprintf("Network-%d", i+1), subnetArray[i])
@@ -41,8 +47,10 @@ func TestNetworkCreate(t *testing.T) {
4147
}
4248

4349
func TestGetNetwork(t *testing.T) {
44-
if testing.Short() {
45-
t.Skip("skipping test in short mode.")
50+
if os.Getuid() != 0 {
51+
msg := "Skipped test because it requires root privileges."
52+
log.Printf(msg)
53+
t.Skip(msg)
4654
}
4755
for i := 0; i < 5; i++ {
4856
network, _ := GetNetwork(fmt.Sprintf("Network-%d", i+1))
@@ -56,8 +64,10 @@ func TestGetNetwork(t *testing.T) {
5664
}
5765

5866
func TestCleanup(t *testing.T) {
59-
if testing.Short() {
60-
t.Skip("skipping test in short mode.")
67+
if os.Getuid() != 0 {
68+
msg := "Skipped test because it requires root privileges."
69+
log.Printf(msg)
70+
t.Skip(msg)
6171
}
6272
for i := 0; i < 5; i++ {
6373
err := DeleteNetwork(fmt.Sprintf("Network-%d", i+1))

daemon/ovs_driver.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func UpdatePortContext(ovs *libovsdb.OvsdbClient, portName string, key string, c
289289
return nil
290290
}
291291

292-
func AddInternalPort(ovs *libovsdb.OvsdbClient, bridgeName string, portName string, tag uint) {
292+
func AddInternalPort(ovs *libovsdb.OvsdbClient, bridgeName string, portName string, tag uint) error {
293293
namedPortUuid := "port"
294294
namedIntfUuid := "intf"
295295

@@ -339,14 +339,18 @@ func AddInternalPort(ovs *libovsdb.OvsdbClient, bridgeName string, portName stri
339339
reply, _ := ovs.Transact("Open_vSwitch", operations...)
340340
if len(reply) < len(operations) {
341341
log.Error("Number of Replies should be atleast equal to number of Operations")
342+
return errors.New("Number of Replies should be atleast equal to number of Operations")
342343
}
343344
for i, o := range reply {
344345
if o.Error != "" && i < len(operations) {
345-
log.Errorf("Transaction Failed due to an error : %v details: %v in %v", o.Error, o.Details, operations[i])
346+
msg := fmt.Sprintf("Transaction Failed due to an error : %v details: %v in %v", o.Error, o.Details, operations[i])
347+
return errors.New(msg)
346348
} else if o.Error != "" {
347-
log.Errorf("Transaction Failed due to an error : %v", o.Error)
349+
msg := fmt.Sprintf("Transaction Failed due to an error : %v", o.Error)
350+
return errors.New(msg)
348351
}
349352
}
353+
return nil
350354
}
351355

352356
func populateCache(updates libovsdb.TableUpdates) {

tools/run-tests.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/bin/sh
22

33
if [ -z "$coveralls_token" ]; then
4-
make test
4+
make test-local
55
else
6-
make test-all
6+
make test-all-local
77
fi
88

0 commit comments

Comments
 (0)