Skip to content

Commit bdd82da

Browse files
authored
Add Github workflows for unit tests, go tidiness and code linters (contiv#3)
The static code checkers revealed a lot of issues, some of them quite "interesting". I fixed them, even though there is still room for improvement in terms of code quality. Signed-off-by: Antonin Bas <[email protected]>
1 parent d9bf657 commit bdd82da

File tree

20 files changed

+431
-208
lines changed

20 files changed

+431
-208
lines changed

.github/workflows/go.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Go
2+
on:
3+
pull_request:
4+
branches:
5+
- main
6+
push:
7+
branches:
8+
- main
9+
10+
jobs:
11+
test:
12+
runs-on: [ubuntu-latest]
13+
steps:
14+
- name: Set up Go 1.15
15+
uses: actions/setup-go@v1
16+
with:
17+
go-version: 1.15
18+
- name: Check-out code
19+
uses: actions/checkout@v2
20+
- name: Run unit tests
21+
run: |
22+
make test
23+
24+
tidy:
25+
runs-on: [ubuntu-latest]
26+
steps:
27+
- name: Set up Go 1.15
28+
uses: actions/setup-go@v1
29+
with:
30+
go-version: 1.15
31+
- name: Check-out code
32+
uses: actions/checkout@v2
33+
- name: Check tidiness
34+
run: |
35+
./ci/check-tidy.sh
36+
37+
golangci:
38+
runs-on: [ubuntu-latest]
39+
steps:
40+
- name: Check-out code
41+
uses: actions/checkout@v2
42+
- name: Run code linters
43+
run: |
44+
make golangci

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.cache
2+
.golangci-bin
3+
4+
.DS_Store
5+
6+
# VIM
7+
.*.swp
8+
9+
# Emacs
10+
*~
11+
12+
.idea/
13+
.vscode/

.golangci.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
run:
2+
tests: true
3+
timeout: 5m
4+
skip-files:
5+
- ".*\\.pb\\.go"
6+
skip-dirs-use-default: true
7+
8+
linters-settings:
9+
goimports:
10+
local-prefixes: antrea-io/libOpenflow
11+
12+
linters:
13+
disable-all: true
14+
enable: # see https://golangci-lint.run/usage/linters/
15+
- deadcode
16+
- staticcheck
17+
- govet
18+
- gofmt
19+
- goimports
20+
- gosec
21+
- misspell
22+
23+
run:
24+
deadline: 5m

Makefile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
GO ?= go
2+
3+
all: test
4+
5+
.PHONY: test
6+
test:
7+
$(GO) test -v ./...
8+
9+
# code linting
10+
.golangci-bin:
11+
@echo "===> Installing Golangci-lint <==="
12+
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $@ v1.41.1
13+
14+
.PHONY: golangci
15+
golangci: .golangci-bin
16+
@echo "===> Running golangci <==="
17+
@GOOS=linux .golangci-bin/golangci-lint run -c .golangci.yml
18+
19+
.PHONY: golangci-fix
20+
golangci-fix: .golangci-bin
21+
@echo "===> Running golangci-fix <==="
22+
@GOOS=linux .golangci-bin/golangci-lint run -c .golangci.yml --fix

ci/check-tidy.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
: "${GO:=go}"
6+
7+
goversion=$($GO version)
8+
9+
$GO mod tidy
10+
11+
rc=0
12+
diff=$(git diff --exit-code -- go.mod go.sum) || rc=$?
13+
14+
if [ $rc -ne 0 ]; then
15+
echo "Found some differences when running 'go mod tidy'"
16+
echo "**********"
17+
echo "$diff"
18+
echo "**********"
19+
echo "Please ensure you are using the correct version of go ($goversion), run 'go mod tidy', and commit the changes"
20+
fi
21+
22+
exit $rc

common/header.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (h *Header) UnmarshalBinary(data []byte) error {
6464
}
6565

6666
const (
67-
reserved = iota
67+
reserved = iota //nolint:deadcode
6868
HelloElemType_VersionBitmap
6969
)
7070

@@ -136,10 +136,12 @@ func (h *HelloElemVersionBitmap) Len() (n uint16) {
136136

137137
func (h *HelloElemVersionBitmap) MarshalBinary() (data []byte, err error) {
138138
data = make([]byte, int(h.Len()))
139-
bytes := make([]byte, 0)
139+
var bytes []byte
140140
next := 0
141141

142-
bytes, err = h.HelloElemHeader.MarshalBinary()
142+
if bytes, err = h.HelloElemHeader.MarshalBinary(); err != nil {
143+
return
144+
}
143145
copy(data[next:], bytes)
144146
next += len(bytes)
145147

@@ -202,11 +204,13 @@ func (h *Hello) Len() (n uint16) {
202204

203205
func (h *Hello) MarshalBinary() (data []byte, err error) {
204206
data = make([]byte, int(h.Len()))
205-
bytes := make([]byte, 0)
207+
var bytes []byte
206208
next := 0
207209

208210
h.Header.Length = h.Len()
209-
bytes, err = h.Header.MarshalBinary()
211+
if bytes, err = h.Header.MarshalBinary(); err != nil {
212+
return
213+
}
210214
copy(data[next:], bytes)
211215
next += len(bytes)
212216

openflow13/action.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,12 @@ func (a *ActionOutput) Len() (n uint16) {
154154

155155
func (a *ActionOutput) MarshalBinary() (data []byte, err error) {
156156
data = make([]byte, int(a.Len()))
157-
b := make([]byte, 0)
157+
var b []byte
158158
n := 0
159159

160-
b, err = a.ActionHeader.MarshalBinary()
160+
if b, err = a.ActionHeader.MarshalBinary(); err != nil {
161+
return
162+
}
161163
copy(data[n:], b)
162164
n += len(b)
163165
binary.BigEndian.PutUint32(data[n:], a.Port)
@@ -243,10 +245,12 @@ func (a *ActionGroup) Len() (n uint16) {
243245

244246
func (a *ActionGroup) MarshalBinary() (data []byte, err error) {
245247
data = make([]byte, int(a.Len()))
246-
b := make([]byte, 0)
248+
var b []byte
247249
n := 0
248250

249-
b, err = a.ActionHeader.MarshalBinary()
251+
if b, err = a.ActionHeader.MarshalBinary(); err != nil {
252+
return
253+
}
250254
copy(data[n:], b)
251255
n += len(b)
252256
binary.BigEndian.PutUint32(data[n:], a.GroupId)
@@ -447,22 +451,31 @@ func (a *ActionSetField) Len() (n uint16) {
447451

448452
func (a *ActionSetField) MarshalBinary() (data []byte, err error) {
449453
data = make([]byte, int(a.Len()))
454+
var b []byte
450455
n := 0
451-
b, err := a.ActionHeader.MarshalBinary()
456+
if b, err = a.ActionHeader.MarshalBinary(); err != nil {
457+
return
458+
}
452459
copy(data, b)
453460
n += int(a.ActionHeader.Len())
454461

455-
b, err = a.Field.MarshalBinary()
462+
if b, err = a.Field.MarshalBinary(); err != nil {
463+
return
464+
}
456465
copy(data[n:], b)
457466

458467
return
459468
}
460469
func (a *ActionSetField) UnmarshalBinary(data []byte) error {
461470
n := 0
462-
err := a.ActionHeader.UnmarshalBinary(data[n:])
471+
if err := a.ActionHeader.UnmarshalBinary(data[n:]); err != nil {
472+
return err
473+
}
463474
n += int(a.ActionHeader.Len())
464-
err = a.Field.UnmarshalBinary(data[n:])
475+
if err := a.Field.UnmarshalBinary(data[n:]); err != nil {
476+
return err
477+
}
465478
n += int(a.Field.Len())
466479

467-
return err
480+
return nil
468481
}

openflow13/bundles.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,12 @@ func (e *VendorError) Len() uint16 {
263263

264264
func (e *VendorError) MarshalBinary() (data []byte, err error) {
265265
data = make([]byte, int(e.Len()))
266+
var headerBytes []byte
266267
n := 0
267268

268-
headerBytes, err := e.Header.MarshalBinary()
269+
if headerBytes, err = e.Header.MarshalBinary(); err != nil {
270+
return
271+
}
269272
copy(data[n:], headerBytes)
270273
n += len(headerBytes)
271274
binary.BigEndian.PutUint16(data[n:], e.Type)
@@ -274,7 +277,9 @@ func (e *VendorError) MarshalBinary() (data []byte, err error) {
274277
n += 2
275278
binary.BigEndian.PutUint32(data[n:], e.ExperimenterID)
276279
n += 4
277-
headerBytes, err = e.Data.MarshalBinary()
280+
if headerBytes, err = e.Data.MarshalBinary(); err != nil {
281+
return
282+
}
278283
copy(data[n:], headerBytes)
279284
n += len(headerBytes)
280285
return

openflow13/flowmod.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package openflow13
33
import (
44
"encoding/binary"
55

6-
"antrea-io/libOpenflow/common"
76
log "github.com/sirupsen/logrus"
7+
8+
"antrea-io/libOpenflow/common"
89
)
910

1011
// ofp_flow_mod 1.3
@@ -76,7 +77,9 @@ func (f *FlowMod) Len() (n uint16) {
7677

7778
func (f *FlowMod) MarshalBinary() (data []byte, err error) {
7879
f.Header.Length = f.Len()
79-
data, err = f.Header.MarshalBinary()
80+
if data, err = f.Header.MarshalBinary(); err != nil {
81+
return
82+
}
8083

8184
bytes := make([]byte, 40)
8285
n := 0
@@ -105,11 +108,15 @@ func (f *FlowMod) MarshalBinary() (data []byte, err error) {
105108
n += 2 // for pad
106109
data = append(data, bytes...)
107110

108-
bytes, err = f.Match.MarshalBinary()
111+
if bytes, err = f.Match.MarshalBinary(); err != nil {
112+
return
113+
}
109114
data = append(data, bytes...)
110115

111116
for _, instr := range f.Instructions {
112-
bytes, err = instr.MarshalBinary()
117+
if bytes, err = instr.MarshalBinary(); err != nil {
118+
return
119+
}
113120
data = append(data, bytes...)
114121
log.Debugf("flowmod instr: %v", bytes)
115122
}
@@ -212,9 +219,12 @@ func (f *FlowRemoved) Len() (n uint16) {
212219

213220
func (f *FlowRemoved) MarshalBinary() (data []byte, err error) {
214221
data = make([]byte, int(f.Len()))
222+
var bytes []byte
215223
next := 0
216224

217-
bytes, err := f.Header.MarshalBinary()
225+
if bytes, err = f.Header.MarshalBinary(); err != nil {
226+
return
227+
}
218228
copy(data[next:], bytes)
219229
next += int(f.Header.Len())
220230

@@ -241,16 +251,19 @@ func (f *FlowRemoved) MarshalBinary() (data []byte, err error) {
241251
binary.BigEndian.PutUint64(data[next:], f.ByteCount)
242252
next += 8
243253

244-
bytes, err = f.Match.MarshalBinary()
254+
if bytes, err = f.Match.MarshalBinary(); err != nil {
255+
return
256+
}
245257
copy(data[next:], bytes)
246258
next += int(f.Match.Len())
247259
return
248260
}
249261

250262
func (f *FlowRemoved) UnmarshalBinary(data []byte) error {
251263
next := 0
252-
var err error
253-
err = f.Header.UnmarshalBinary(data[next:])
264+
if err := f.Header.UnmarshalBinary(data[next:]); err != nil {
265+
return err
266+
}
254267
next += int(f.Header.Len())
255268

256269
f.Cookie = binary.BigEndian.Uint64(data[next:])
@@ -274,10 +287,12 @@ func (f *FlowRemoved) UnmarshalBinary(data []byte) error {
274287
f.ByteCount = binary.BigEndian.Uint64(data[next:])
275288
next += 8
276289

277-
err = f.Match.UnmarshalBinary(data[next:])
290+
if err := f.Match.UnmarshalBinary(data[next:]); err != nil {
291+
return err
292+
}
278293
next += int(f.Match.Len())
279294

280-
return err
295+
return nil
281296
}
282297

283298
// ofp_flow_removed_reason 1.3

openflow13/group.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ package openflow13
55
import (
66
"encoding/binary"
77

8-
"antrea-io/libOpenflow/common"
98
log "github.com/sirupsen/logrus"
9+
10+
"antrea-io/libOpenflow/common"
1011
)
1112

1213
const (

0 commit comments

Comments
 (0)