Skip to content

Commit

Permalink
Add Github workflows for unit tests, go tidiness and code linters (#3)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
antoninbas authored Jul 6, 2021
1 parent d9bf657 commit bdd82da
Show file tree
Hide file tree
Showing 20 changed files with 431 additions and 208 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Go
on:
pull_request:
branches:
- main
push:
branches:
- main

jobs:
test:
runs-on: [ubuntu-latest]
steps:
- name: Set up Go 1.15
uses: actions/setup-go@v1
with:
go-version: 1.15
- name: Check-out code
uses: actions/checkout@v2
- name: Run unit tests
run: |
make test
tidy:
runs-on: [ubuntu-latest]
steps:
- name: Set up Go 1.15
uses: actions/setup-go@v1
with:
go-version: 1.15
- name: Check-out code
uses: actions/checkout@v2
- name: Check tidiness
run: |
./ci/check-tidy.sh
golangci:
runs-on: [ubuntu-latest]
steps:
- name: Check-out code
uses: actions/checkout@v2
- name: Run code linters
run: |
make golangci
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.cache
.golangci-bin

.DS_Store

# VIM
.*.swp

# Emacs
*~

.idea/
.vscode/
24 changes: 24 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
run:
tests: true
timeout: 5m
skip-files:
- ".*\\.pb\\.go"
skip-dirs-use-default: true

linters-settings:
goimports:
local-prefixes: antrea-io/libOpenflow

linters:
disable-all: true
enable: # see https://golangci-lint.run/usage/linters/
- deadcode
- staticcheck
- govet
- gofmt
- goimports
- gosec
- misspell

run:
deadline: 5m
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
GO ?= go

all: test

.PHONY: test
test:
$(GO) test -v ./...

# code linting
.golangci-bin:
@echo "===> Installing Golangci-lint <==="
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $@ v1.41.1

.PHONY: golangci
golangci: .golangci-bin
@echo "===> Running golangci <==="
@GOOS=linux .golangci-bin/golangci-lint run -c .golangci.yml

.PHONY: golangci-fix
golangci-fix: .golangci-bin
@echo "===> Running golangci-fix <==="
@GOOS=linux .golangci-bin/golangci-lint run -c .golangci.yml --fix
22 changes: 22 additions & 0 deletions ci/check-tidy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

set -e

: "${GO:=go}"

goversion=$($GO version)

$GO mod tidy

rc=0
diff=$(git diff --exit-code -- go.mod go.sum) || rc=$?

if [ $rc -ne 0 ]; then
echo "Found some differences when running 'go mod tidy'"
echo "**********"
echo "$diff"
echo "**********"
echo "Please ensure you are using the correct version of go ($goversion), run 'go mod tidy', and commit the changes"
fi

exit $rc
14 changes: 9 additions & 5 deletions common/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (h *Header) UnmarshalBinary(data []byte) error {
}

const (
reserved = iota
reserved = iota //nolint:deadcode
HelloElemType_VersionBitmap
)

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

func (h *HelloElemVersionBitmap) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(h.Len()))
bytes := make([]byte, 0)
var bytes []byte
next := 0

bytes, err = h.HelloElemHeader.MarshalBinary()
if bytes, err = h.HelloElemHeader.MarshalBinary(); err != nil {
return
}
copy(data[next:], bytes)
next += len(bytes)

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

func (h *Hello) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(h.Len()))
bytes := make([]byte, 0)
var bytes []byte
next := 0

h.Header.Length = h.Len()
bytes, err = h.Header.MarshalBinary()
if bytes, err = h.Header.MarshalBinary(); err != nil {
return
}
copy(data[next:], bytes)
next += len(bytes)

Expand Down
31 changes: 22 additions & 9 deletions openflow13/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,12 @@ func (a *ActionOutput) Len() (n uint16) {

func (a *ActionOutput) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(a.Len()))
b := make([]byte, 0)
var b []byte
n := 0

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

func (a *ActionGroup) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(a.Len()))
b := make([]byte, 0)
var b []byte
n := 0

b, err = a.ActionHeader.MarshalBinary()
if b, err = a.ActionHeader.MarshalBinary(); err != nil {
return
}
copy(data[n:], b)
n += len(b)
binary.BigEndian.PutUint32(data[n:], a.GroupId)
Expand Down Expand Up @@ -447,22 +451,31 @@ func (a *ActionSetField) Len() (n uint16) {

func (a *ActionSetField) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(a.Len()))
var b []byte
n := 0
b, err := a.ActionHeader.MarshalBinary()
if b, err = a.ActionHeader.MarshalBinary(); err != nil {
return
}
copy(data, b)
n += int(a.ActionHeader.Len())

b, err = a.Field.MarshalBinary()
if b, err = a.Field.MarshalBinary(); err != nil {
return
}
copy(data[n:], b)

return
}
func (a *ActionSetField) UnmarshalBinary(data []byte) error {
n := 0
err := a.ActionHeader.UnmarshalBinary(data[n:])
if err := a.ActionHeader.UnmarshalBinary(data[n:]); err != nil {
return err
}
n += int(a.ActionHeader.Len())
err = a.Field.UnmarshalBinary(data[n:])
if err := a.Field.UnmarshalBinary(data[n:]); err != nil {
return err
}
n += int(a.Field.Len())

return err
return nil
}
9 changes: 7 additions & 2 deletions openflow13/bundles.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,12 @@ func (e *VendorError) Len() uint16 {

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

headerBytes, err := e.Header.MarshalBinary()
if headerBytes, err = e.Header.MarshalBinary(); err != nil {
return
}
copy(data[n:], headerBytes)
n += len(headerBytes)
binary.BigEndian.PutUint16(data[n:], e.Type)
Expand All @@ -274,7 +277,9 @@ func (e *VendorError) MarshalBinary() (data []byte, err error) {
n += 2
binary.BigEndian.PutUint32(data[n:], e.ExperimenterID)
n += 4
headerBytes, err = e.Data.MarshalBinary()
if headerBytes, err = e.Data.MarshalBinary(); err != nil {
return
}
copy(data[n:], headerBytes)
n += len(headerBytes)
return
Expand Down
35 changes: 25 additions & 10 deletions openflow13/flowmod.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package openflow13
import (
"encoding/binary"

"antrea-io/libOpenflow/common"
log "github.com/sirupsen/logrus"

"antrea-io/libOpenflow/common"
)

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

func (f *FlowMod) MarshalBinary() (data []byte, err error) {
f.Header.Length = f.Len()
data, err = f.Header.MarshalBinary()
if data, err = f.Header.MarshalBinary(); err != nil {
return
}

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

bytes, err = f.Match.MarshalBinary()
if bytes, err = f.Match.MarshalBinary(); err != nil {
return
}
data = append(data, bytes...)

for _, instr := range f.Instructions {
bytes, err = instr.MarshalBinary()
if bytes, err = instr.MarshalBinary(); err != nil {
return
}
data = append(data, bytes...)
log.Debugf("flowmod instr: %v", bytes)
}
Expand Down Expand Up @@ -212,9 +219,12 @@ func (f *FlowRemoved) Len() (n uint16) {

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

bytes, err := f.Header.MarshalBinary()
if bytes, err = f.Header.MarshalBinary(); err != nil {
return
}
copy(data[next:], bytes)
next += int(f.Header.Len())

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

bytes, err = f.Match.MarshalBinary()
if bytes, err = f.Match.MarshalBinary(); err != nil {
return
}
copy(data[next:], bytes)
next += int(f.Match.Len())
return
}

func (f *FlowRemoved) UnmarshalBinary(data []byte) error {
next := 0
var err error
err = f.Header.UnmarshalBinary(data[next:])
if err := f.Header.UnmarshalBinary(data[next:]); err != nil {
return err
}
next += int(f.Header.Len())

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

err = f.Match.UnmarshalBinary(data[next:])
if err := f.Match.UnmarshalBinary(data[next:]); err != nil {
return err
}
next += int(f.Match.Len())

return err
return nil
}

// ofp_flow_removed_reason 1.3
Expand Down
3 changes: 2 additions & 1 deletion openflow13/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ package openflow13
import (
"encoding/binary"

"antrea-io/libOpenflow/common"
log "github.com/sirupsen/logrus"

"antrea-io/libOpenflow/common"
)

const (
Expand Down
Loading

0 comments on commit bdd82da

Please sign in to comment.