Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add linter for manager api #655

Merged
merged 15 commits into from
Nov 3, 2020
20 changes: 20 additions & 0 deletions .github/workflows/golang-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: golang-lint
on:
push:
branches:
- master
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
- v2.0
pull_request:
branches:
- master
juzhiyuan marked this conversation as resolved.
Show resolved Hide resolved
- v2.0

jobs:
juzhiyuan marked this conversation as resolved.
Show resolved Hide resolved
golangci:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: run lint
run: make golang-lint
26 changes: 19 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
# limitations under the License.
#

SHELL := /bin/bash -o pipefail
UNAME ?= $(shell uname)

### help: Show Makefile rules
.PHONY: help
help:
@echo Makefile rules:
@echo
@grep -E '^### [-A-Za-z0-9_]+:' Makefile | sed 's/###/ /'

export GO111MODULE=on

### license-check: Check apisix-dashboard source codes for Apache License
Expand All @@ -26,16 +36,18 @@ ifeq ("$(wildcard .actions/openwhisk-utilities/scancode/scanCode.py)", "")
endif
.actions/openwhisk-utilities/scancode/scanCode.py --config .actions/ASF-Release.cfg ./

nic-chen marked this conversation as resolved.
Show resolved Hide resolved
### golang-lint: Lint Go source code
.PHONY: golang-lint
golang-lint: ## Run the golangci-lint application (install if not found)
@#Brew - MacOS
@if [ "$(shell command -v golangci-lint)" = "" ] && [ "$(shell command -v brew)" != "" ] && [ "$(UNAME)" = "Darwin" ]; then brew install golangci-lint; fi;
@#has sudo
@if [ "$(shell command -v golangci-lint)" = "" ]; then curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.32.0 && sudo cp ./bin/golangci-lint $(go env GOPATH)/bin/; fi;
@echo "running golangci-lint..."
@cd api && golangci-lint run --tests=false ./...

### api-test: Run the tests of manager-api
.PHONY: api-test
api-test:
cd api/ && go test -v -race -cover -coverprofile=coverage.txt -covermode=atomic ./...


### help: Show Makefile rules
.PHONY: help
help: default
@echo Makefile rules:
@echo
@grep -E '^### [-A-Za-z0-9_]+:' Makefile | sed 's/###/ /'
6 changes: 4 additions & 2 deletions api/filter/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/shiningrush/droplet/log"
"github.com/sirupsen/logrus"
)

Expand All @@ -42,16 +43,17 @@ func RequestLogHandler() gin.HandlerFunc {

param, _ := c.Get("requestBody")

switch param.(type) {
switch paramType := param.(type) {
case []byte:
param = string(param.([]byte))
log.Infof("type of param: %#v", paramType)
default:
}

blw := &bodyLogWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer}
c.Writer = blw
c.Next()
latency := time.Now().Sub(start) / 1000000
latency := time.Since(start) / 1000000
statusCode := c.Writer.Status()
respBody := blw.body.String()
if uuid == "" {
Expand Down
4 changes: 3 additions & 1 deletion api/internal/core/entity/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ func TestConsumer(t *testing.T) {
"127.0.0.1:8080": 1
}`
nodesMap := map[string]float64{}
json.Unmarshal([]byte(nodesStr), &nodesMap)
err := json.Unmarshal([]byte(nodesStr), &nodesMap)
assert.Nil(t, err)

res := NodesFormat(nodesMap)
nodes := res.([]*Node)

Expand Down
6 changes: 4 additions & 2 deletions api/internal/core/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,8 @@ func TestGenericStore_Create(t *testing.T) {
createCalled = true
assert.Equal(t, tc.wantKey, args[1], tc.caseDesc)
input := TestStruct{}
_ = json.Unmarshal([]byte(args[2].(string)), &input)
err := json.Unmarshal([]byte(args[2].(string)), &input)
assert.Nil(t, err)
assert.Equal(t, tc.giveObj.Field1, input.Field1, tc.caseDesc)
assert.Equal(t, tc.giveObj.Field2, input.Field2, tc.caseDesc)
assert.NotEqual(t, 0, len(input.ID), tc.caseDesc)
Expand Down Expand Up @@ -689,7 +690,8 @@ func TestGenericStore_Update(t *testing.T) {
createCalled = true
assert.Equal(t, tc.wantKey, args[1], tc.caseDesc)
input := TestStruct{}
_ = json.Unmarshal([]byte(args[2].(string)), &input)
err := json.Unmarshal([]byte(args[2].(string)), &input)
assert.Nil(t, err)
assert.Equal(t, tc.giveObj.Field1, input.Field1, tc.caseDesc)
assert.Equal(t, tc.giveObj.Field2, input.Field2, tc.caseDesc)
assert.NotEqual(t, 0, input.UpdateTime, tc.caseDesc)
Expand Down
64 changes: 33 additions & 31 deletions api/internal/core/store/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io/ioutil"
"regexp"

"github.com/shiningrush/droplet/log"
"github.com/xeipuuv/gojsonschema"
"go.uber.org/zap/buffer"

Expand Down Expand Up @@ -89,14 +90,17 @@ func NewAPISIXJsonSchemaValidator(jsonPath string) (Validator, error) {
}

func getPlugins(reqBody interface{}) (map[string]interface{}, string) {
switch reqBody.(type) {
switch bodyType := reqBody.(type) {
case *entity.Route:
log.Infof("type of reqBody: %#v", bodyType)
route := reqBody.(*entity.Route)
return route.Plugins, "schema"
case *entity.Service:
log.Infof("type of reqBody: %#v", bodyType)
service := reqBody.(*entity.Service)
return service.Plugins, "schema"
case *entity.Consumer:
log.Infof("type of reqBody: %#v", bodyType)
consumer := reqBody.(*entity.Consumer)
return consumer.Plugins, "consumer_schema"
}
Expand All @@ -110,7 +114,7 @@ func cHashKeySchemaCheck(upstream *entity.UpstreamDef) error {
if upstream.HashOn != "vars" &&
upstream.HashOn != "header" &&
upstream.HashOn != "cookie" {
fmt.Errorf("invalid hash_on type: %s", upstream.HashOn)
return fmt.Errorf("invalid hash_on type: %s", upstream.HashOn)
}

var schemaDef string
Expand Down Expand Up @@ -188,9 +192,10 @@ func checkUpstream(upstream *entity.UpstreamDef) error {
}

func checkConf(reqBody interface{}) error {
switch reqBody.(type) {
switch bodyType := reqBody.(type) {
case *entity.Route:
route := reqBody.(*entity.Route)
log.Infof("type of reqBody: %#v", bodyType)
if err := checkUpstream(route.Upstream); err != nil {
return err
}
Expand Down Expand Up @@ -230,42 +235,39 @@ func (v *APISIXJsonSchemaValidator) Validate(obj interface{}) error {
return err
}

//check plugin json schema
plugins, schemaType := getPlugins(obj)
//fix lua json.encode transform lua{properties={}} to json{"properties":[]}
reg := regexp.MustCompile(`\"properties\":\[\]`)
if plugins != nil {
for pluginName, pluginConf := range plugins {
var schemaDef string
schemaDef = conf.Schema.Get("plugins." + pluginName + "." + schemaType).String()
if (schemaDef == "" && schemaType == "consumer_schema") {
schemaDef = conf.Schema.Get("plugins." + pluginName + ".schema").String()
}
if schemaDef == "" {
return fmt.Errorf("scheme validate failed: schema not found, path: %s", "plugins."+pluginName)
}
for pluginName, pluginConf := range plugins {
var schemaDef string
schemaDef = conf.Schema.Get("plugins." + pluginName + "." + schemaType).String()
if schemaDef == "" && schemaType == "consumer_schema" {
schemaDef = conf.Schema.Get("plugins." + pluginName + ".schema").String()
}
if schemaDef == "" {
return fmt.Errorf("scheme validate failed: schema not found, path: %s", "plugins."+pluginName)
}

schemaDef = reg.ReplaceAllString(schemaDef, `"properties":{}`)
s, err := gojsonschema.NewSchema(gojsonschema.NewStringLoader(schemaDef))
if err != nil {
return fmt.Errorf("scheme validate failed: %w", err)
}
schemaDef = reg.ReplaceAllString(schemaDef, `"properties":{}`)
s, err := gojsonschema.NewSchema(gojsonschema.NewStringLoader(schemaDef))
if err != nil {
return fmt.Errorf("scheme validate failed: %w", err)
}

ret, err := s.Validate(gojsonschema.NewGoLoader(pluginConf))
if err != nil {
return fmt.Errorf("scheme validate failed: %w", err)
}
ret, err := s.Validate(gojsonschema.NewGoLoader(pluginConf))
if err != nil {
return fmt.Errorf("scheme validate failed: %w", err)
}

if !ret.Valid() {
errString := buffer.Buffer{}
for i, vErr := range ret.Errors() {
if i != 0 {
errString.AppendString("\n")
}
errString.AppendString(vErr.String())
if !ret.Valid() {
errString := buffer.Buffer{}
for i, vErr := range ret.Errors() {
if i != 0 {
errString.AppendString("\n")
}
return fmt.Errorf("scheme validate failed: %s", errString.String())
errString.AppendString(vErr.String())
}
return fmt.Errorf("scheme validate failed: %s", errString.String())
}
}

Expand Down
Loading