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
16 changes: 16 additions & 0 deletions .github/workflows/golang-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: golang-lint
on:
push:
branches:
- master
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
pull_request:
branches:
- master
juzhiyuan marked this conversation as resolved.
Show resolved Hide resolved
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
22 changes: 21 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,31 @@
# limitations under the License.
#

SHELL := /bin/bash -o pipefail


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

### license-check: Check apisix-dashboard source codes for Apache License
.PHONY: license-check
license-check:
ifeq ("$(wildcard .actions/openwhisk-utilities/scancode/scanCode.py)", "")
git clone https://github.com/apache/openwhisk-utilities.git .actions/openwhisk-utilities
cp .actions/ASF* .actions/openwhisk-utilities/scancode/
endif
.actions/openwhisk-utilities/scancode/scanCode.py --config .actions/ASF-Release.cfg ./
.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)" != "" ]; 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 ./...
6 changes: 3 additions & 3 deletions api/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const (
EnvDEV = "dev"
EnvLOCAL = "local"

confPath = "/go/manager-api/conf.json"
schemaPath = "/go/manager-api/schema.json"
confJsonPath = "/go/manager-api/conf.json"
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a hard-coded configuration file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, could be overridden by conf file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And @nic-chen will resolve this in the future for easy to use

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hemm, anyway, we can not hard-coded in code base.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moonming
it's a history issue, we have a plan to refactor the config part in 2.1

schemaPath = "/go/manager-api/schema.json"
)

var (
Expand Down Expand Up @@ -103,7 +103,7 @@ func configurationPath() string {
} else if ENV == EnvLOCAL {
return filepath.Join(filepath.Dir(basePath), "conf.json")
} else {
return confPath
return confJsonPath
}
}

Expand Down
6 changes: 4 additions & 2 deletions api/filter/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package filter

import (
"bytes"
"fmt"
"io/ioutil"
"time"

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))
fmt.Printf("type of param: %#v", paramType)
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
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
53 changes: 28 additions & 25 deletions api/internal/core/store/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"

"github.com/xeipuuv/gojsonschema"
"go.uber.org/zap/buffer"
Expand Down Expand Up @@ -88,14 +89,17 @@ func NewAPISIXJsonSchemaValidator(jsonPath string) (Validator, error) {
}

func getPlugins(reqBody interface{}) map[string]interface{} {
switch reqBody.(type) {
switch bodyType := reqBody.(type) {
case *entity.Route:
log.Printf("type of reqBody: %#v", bodyType)
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
route := reqBody.(*entity.Route)
return route.Plugins
case *entity.Service:
log.Printf("type of reqBody: %#v", bodyType)
service := reqBody.(*entity.Service)
return service.Plugins
case *entity.Consumer:
log.Printf("type of reqBody: %#v", bodyType)
consumer := reqBody.(*entity.Consumer)
return consumer.Plugins
}
Expand All @@ -109,7 +113,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 @@ -187,9 +191,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)
fmt.Printf("type of reqBody: %#v", bodyType)
if err := checkUpstream(route.Upstream); err != nil {
return err
}
Expand Down Expand Up @@ -231,33 +236,31 @@ func (v *APISIXJsonSchemaValidator) Validate(obj interface{}) error {

//check plugin json schema
plugins := getPlugins(obj)
if plugins != nil {
for pluginName, pluginConf := range plugins {
schemaDef := conf.Schema.Get("plugins." + pluginName).String()
if schemaDef == "" {
return fmt.Errorf("scheme validate failed: schema not found, path: %s", "plugins."+pluginName)
}
for pluginName, pluginConf := range plugins {
schemaDef := conf.Schema.Get("plugins." + pluginName).String()
if schemaDef == "" {
return fmt.Errorf("scheme validate failed: schema not found, path: %s", "plugins."+pluginName)
}

s, err := gojsonschema.NewSchema(gojsonschema.NewStringLoader(schemaDef))
if err != nil {
return fmt.Errorf("scheme validate failed: %w", err)
}
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