Skip to content

Commit

Permalink
Fix lint & update CI lint
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Aug 21, 2021
1 parent 02a934d commit d422f41
Show file tree
Hide file tree
Showing 25 changed files with 113 additions and 89 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/non-regression.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@ name: Build container and validate lint/tests
on: [push]

jobs:
lint-validation:
name: Validate Go code linting
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
checkout-code-and-validate:
name: Checkout code
needs: lint-validation
name: Checkout code and validate tests
runs-on: ubuntu-latest
steps:
- name: Checkout code
Expand All @@ -19,8 +30,6 @@ jobs:
run: cp Dockerfile-dev Dockerfile && cp docker-compose.yml.test docker-compose.yml
- name: Build the stack
run: docker-compose up -d --build --force-recreate --remove-orphans
- name: golangci-lint
uses: reviewdog/action-golangci-lint@v1
- name: Test
run: docker-compose exec -T souin go test -v ./...
validate-prod-container-building:
Expand Down
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.PHONY: build-and-run-caddy build-and-run-caddy-json build-and-run-traefik build-and-run-tyk build-app build-caddy build-dev coverage create-network down env-dev env-prod gatling generate-plantUML health-check-prod help lint log tests up validate
.PHONY: build-and-run-caddy build-and-run-caddy-json build-and-run-traefik build-and-run-tyk build-app build-caddy \
build-dev coverage create-network down env-dev env-prod gatling generate-plantUML health-check-prod help lint log \
tests up validate vendor-plugins

DC=docker-compose
DC_BUILD=$(DC) build
Expand Down Expand Up @@ -72,3 +74,7 @@ up: ## Up containers
$(DC) up -d --remove-orphans

validate: lint tests down health-check-prod ## Run lint, tests and ensure prod can build

vendor-plugins: ## Generate and prepare vendors for each plugin
cd plugins/tyk && $(MAKE) vendor
cd plugins/traefik && $(MAKE) vendor
8 changes: 4 additions & 4 deletions api/auth/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestCheckToken(t *testing.T) {
http.SetCookie(w, &http.Cookie{Name: tests.GetTokenName(), Value: "badvalue"})
r = &http.Request{
Header: http.Header{
"Cookie": w.HeaderMap["Set-Cookie"],
"Cookie": w.Header()["Set-Cookie"],
},
}
w = httptest.NewRecorder()
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestCheckToken(t *testing.T) {
})
r = &http.Request{
Header: http.Header{
"Cookie": w.HeaderMap["Set-Cookie"],
"Cookie": w.Header()["Set-Cookie"],
},
}
w = httptest.NewRecorder()
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestCheckToken(t *testing.T) {
})
r = &http.Request{
Header: http.Header{
"Cookie": w.HeaderMap["Set-Cookie"],
"Cookie": w.Header()["Set-Cookie"],
},
}
w = httptest.NewRecorder()
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestCheckToken(t *testing.T) {
http.SetCookie(w, tests.GetValidToken())
r = &http.Request{
Header: http.Header{
"Cookie": w.HeaderMap["Set-Cookie"],
"Cookie": w.Header()["Set-Cookie"],
},
}
jwt, err = CheckToken(security, w, r)
Expand Down
4 changes: 2 additions & 2 deletions api/auth/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func (s *SecurityAPI) HandleRequest(w http.ResponseWriter, r *http.Request) {
} else if regexp.MustCompile(fmt.Sprintf("%s/refresh", s.GetBasePath())).FindString(r.RequestURI) != "" {
s.refresh(w, r)
} else {
w.Write([]byte{})
_, _ = w.Write([]byte{})
}
default:
w.Write([]byte{})
_, _ = w.Write([]byte{})
}
}
2 changes: 1 addition & 1 deletion api/auth/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestSecurityAPI_HandleRequest(t *testing.T) {
http.SetCookie(w, tests.GetValidToken())
r = &http.Request{
Header: http.Header{
"Cookie": w.HeaderMap["Set-Cookie"],
"Cookie": w.Header()["Set-Cookie"],
},
}
security.HandleRequest(w, r)
Expand Down
12 changes: 6 additions & 6 deletions api/souin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (

// SouinAPI object contains informations related to the endpoints
type SouinAPI struct {
basePath string
enabled bool
provider types.AbstractProviderInterface
security *auth.SecurityAPI
basePath string
enabled bool
provider types.AbstractProviderInterface
security *auth.SecurityAPI
ykeyStorage *ykeys.YKeyStorage
}

Expand Down Expand Up @@ -76,7 +76,7 @@ func (s *SouinAPI) HandleRequest(w http.ResponseWriter, r *http.Request) {
res := []byte{}
if s.security != nil {
if _, err := auth.CheckToken(s.security, w, r); err != nil {
w.Write(res)
_, _ = w.Write(res)
return
}
}
Expand All @@ -100,5 +100,5 @@ func (s *SouinAPI) HandleRequest(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
default:
}
w.Write(res)
_, _ = w.Write(res)
}
10 changes: 5 additions & 5 deletions cache/providers/embeddedOlricProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (

// EmbeddedOlric provider type
type EmbeddedOlric struct {
dm *olric.DMap
db *olric.Olric
dm *olric.DMap
db *olric.Olric
}

func tryToLoadConfiguration(olricInstance *config.Config, olricConfiguration t.CacheProvider, logger *zap.Logger) (*config.Config, bool) {
Expand Down Expand Up @@ -79,7 +79,7 @@ func EmbeddedOlricConnectionFactory(configuration t.AbstractConfigurationInterfa
ch := make(chan error, 1)
go func() {
if err = db.Start(); err != nil {
fmt.Println(fmt.Sprintf("Impossible to start the embedded Olric instance: %v", err))
fmt.Printf("Impossible to start the embedded Olric instance: %v\n", err)
ch <- err
}
}()
Expand Down Expand Up @@ -111,7 +111,7 @@ func (provider *EmbeddedOlric) ListKeys() []string {
})
defer c.Close()
if err != nil {
fmt.Println(fmt.Sprintf("An error occurred while trying to list keys in Olric: %s", err))
fmt.Printf("An error occurred while trying to list keys in Olric: %s\n", err)
return []string{}
}

Expand All @@ -133,7 +133,7 @@ func (provider *EmbeddedOlric) Prefix(key string, req *http.Request) []byte {
})

if err != nil {
fmt.Println(fmt.Sprintf("An error occurred while trying to retrieve data in Olric: %s", err))
fmt.Printf("An error occurred while trying to retrieve data in Olric: %s\n", err)
return []byte{}
}
defer c.Close()
Expand Down
6 changes: 3 additions & 3 deletions cache/providers/olricProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// Olric provider type
type Olric struct {
*client.Client
dm *client.DMap
dm *client.DMap
}

// OlricConnectionFactory function create new Olric instance
Expand Down Expand Up @@ -49,7 +49,7 @@ func (provider *Olric) ListKeys() []string {
},
})
if err != nil {
fmt.Println(fmt.Sprintf("An error occurred while trying to list keys in Olric: %s", err))
fmt.Printf("An error occurred while trying to list keys in Olric: %s\n", err)
return []string{}
}
defer c.Close()
Expand All @@ -72,7 +72,7 @@ func (provider *Olric) Prefix(key string, req *http.Request) []byte {
})

if err != nil {
fmt.Println(fmt.Sprintf("An error occurred while trying to retrieve data in Olric: %s", err))
fmt.Printf("An error occurred while trying to retrieve data in Olric: %s\n", err)
return []byte{}
}
defer c.Close()
Expand Down
2 changes: 1 addition & 1 deletion cache/ykeys/ykey.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
// YKeyStorage is the layer for YKey support storage
type YKeyStorage struct {
*ristretto.Cache
Keys map[string]configurationtypes.YKey
Keys map[string]configurationtypes.YKey
}

// InitializeYKeys will initialize the ykey storage system
Expand Down
36 changes: 18 additions & 18 deletions cache/ykeys/ykey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ func TestYKeyStorage_AddToTags(t *testing.T) {
url := "http://the.url.com"

r := InitializeYKeys(baseYkeys)
time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
r.AddToTags(url, []string{FirstKey})
time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
res, _ := r.Cache.Get(FirstKey)
if !strings.Contains(res.(string), url) {
errors.GenerateError(t, fmt.Sprintf("R1 => The key %s should contains the url %s, %s given", FirstKey, url, res))
Expand All @@ -61,7 +61,7 @@ func TestYKeyStorage_AddToTags(t *testing.T) {
r.AddToTags(url, []string{FirstKey, SecondKey})
r.AddToTags("http://domain.com", []string{FirstKey})

time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
res, _ = r.Cache.Get(FirstKey)
if !strings.Contains(res.(string), url) {
errors.GenerateError(t, fmt.Sprintf("R2 => The key %s should contains the url %s, %s given", FirstKey, url, res))
Expand All @@ -70,7 +70,7 @@ func TestYKeyStorage_AddToTags(t *testing.T) {
errors.GenerateError(t, fmt.Sprintf("R2 => The key %s should contains 2 records, %d given", FirstKey, len(strings.Split(res.(string), ","))))
}

time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
res, _ = r.Cache.Get(SecondKey)
if !strings.Contains(res.(string), url) {
errors.GenerateError(t, fmt.Sprintf("R2 => The key %s should contains the url %s, %s given", SecondKey, url, res))
Expand All @@ -82,7 +82,7 @@ func TestYKeyStorage_AddToTags(t *testing.T) {
r.AddToTags(url, []string{FirstKey})
r.AddToTags(url, []string{FirstKey})
r.AddToTags("http://domain.com", []string{FirstKey})
time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
res, _ = r.Cache.Get(FirstKey)
if len(strings.Split(res.(string), ",")) != 2 {
errors.GenerateError(t, fmt.Sprintf("R3 => The key %s should contains 2 records, %d given", FirstKey, len(strings.Split(res.(string), ","))))
Expand All @@ -97,7 +97,7 @@ func TestYKeyStorage_InvalidateTags(t *testing.T) {
url4 := "http://the.url4.com"

r := InitializeYKeys(baseYkeys)
time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
r.AddToTags(url1, []string{FirstKey, SecondKey})
r.AddToTags(url2, []string{FirstKey, SecondKey})
r.AddToTags(url3, []string{FirstKey, ThirdKey})
Expand All @@ -109,25 +109,25 @@ func TestYKeyStorage_InvalidateTags(t *testing.T) {
errors.GenerateError(t, fmt.Sprintf("It should have 3 urls to remove (e.g. [http://the.url1.com http://the.url2.com http://the.url3.com]), %v given", urls))
}

time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
res, _ := r.Cache.Get(FirstKey)
if res.(string) != "" {
errors.GenerateError(t, "The FIRST_KEY should be empty")
}

time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
res, _ = r.Cache.Get(SecondKey)
if res.(string) != "" {
errors.GenerateError(t, "The SECOND_KEY should be empty")
}

time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
res, _ = r.Cache.Get(ThirdKey)
if res.(string) != url4 {
errors.GenerateError(t, "The THIRD_KEY should be equals to http://the.url4.com")
}

time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
res, _ = r.Cache.Get(FourthKey)
if res.(string) != url4 {
errors.GenerateError(t, "The FOURTH_KEY should be equals to http://the.url4.com")
Expand All @@ -142,39 +142,39 @@ func TestYKeyStorage_InvalidateTagURLs(t *testing.T) {
url4 := "http://the.url4.com"

r := InitializeYKeys(baseYkeys)
time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
r.AddToTags(url1, []string{FirstKey, SecondKey})
time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
r.AddToTags(url2, []string{FirstKey, SecondKey})
time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
r.AddToTags(url3, []string{FirstKey, ThirdKey})
time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
r.AddToTags(url4, []string{ThirdKey, FourthKey})

urls := r.InvalidateTagURLs(fmt.Sprintf("%s,%s", url1, url3))
if len(urls) != 2 {
errors.GenerateError(t, "It should have 2 urls to remove (e.g. [http://the.url1.com http://the.url3.com])")
}

time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
res, _ := r.Cache.Get(FirstKey)
if res.(string) != url2 {
errors.GenerateError(t, "The FIRST_KEY should be equals to http://the.url2.com")
}

time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
res, _ = r.Cache.Get(SecondKey)
if res.(string) != url2 {
errors.GenerateError(t, "The SECOND_KEY should be equals to http://the.url2.com")
}

time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
res, _ = r.Cache.Get(ThirdKey)
if res.(string) != url4 {
errors.GenerateError(t, "The THIRD_KEY should be equals to http://the.url4.com")
}

time.Sleep(200*time.Millisecond)
time.Sleep(200 * time.Millisecond)
res, _ = r.Cache.Get(FourthKey)
if res.(string) != url4 {
errors.GenerateError(t, "The FOURTH_KEY should be equals to http://the.url4.com")
Expand Down
12 changes: 6 additions & 6 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (

//Configuration holder
type Configuration struct {
DefaultCache *configurationtypes.DefaultCache `yaml:"default_cache"`
API configurationtypes.API `yaml:"api"`
ReverseProxyURL string `yaml:"reverse_proxy_url"`
SSLProviders []string `yaml:"ssl_providers"`
URLs map[string]configurationtypes.URL `yaml:"urls"`
LogLevel string `yaml:"log_level"`
DefaultCache *configurationtypes.DefaultCache `yaml:"default_cache"`
API configurationtypes.API `yaml:"api"`
ReverseProxyURL string `yaml:"reverse_proxy_url"`
SSLProviders []string `yaml:"ssl_providers"`
URLs map[string]configurationtypes.URL `yaml:"urls"`
LogLevel string `yaml:"log_level"`
logger *zap.Logger
Ykeys map[string]configurationtypes.YKey `yaml:"ykeys"`
}
Expand Down
1 change: 1 addition & 0 deletions errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ func GenerateError(t *testing.T, text string) {
t.Errorf("An error occurred : %s", text)
}

// CanceledRequestContextError is the error to handle request cancellation
type CanceledRequestContextError struct{}

func (c *CanceledRequestContextError) Error() string {
Expand Down
20 changes: 11 additions & 9 deletions plugins/traefik/Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
.PHONY: prepare
.PHONY: copy-file-to copy-to prepare replace vendor

SOUIN=vendor/github.com/darkweak/souin
CACHE=$(SOUIN)/cache

prepare: ## Prepare tyk plugin
go mod tidy
go mod download
go mod vendor
$(MAKE) replace
cp docker-compose.yml.test docker-compose.yml
docker-compose up -d

copy-to: ## Copy from to
rm -rf $(base)/$(target)
scp -r override/$(target) $(base)
Expand All @@ -19,10 +11,20 @@ copy-file-to: ## Copy file from to
rm -rf $(base)/$(target)
scp -r override/$(target) $(base)/$(target)

prepare: vendor ## Prepare tyk plugin
cp docker-compose.yml.test docker-compose.yml
docker-compose up -d

# TODO find another way to do that
replace: ## Replace sources in the vendor folder deeper than the go mod replace
$(MAKE) copy-to base=$(CACHE) target=coalescing
$(MAKE) copy-to base=$(CACHE) target=providers
$(MAKE) copy-to base=$(CACHE) target=types
$(MAKE) copy-to base=$(CACHE) target=ykeys
$(MAKE) copy-file-to base=$(SOUIN) target=rfc/vary.go

vendor: ## Generate vendors for the plugin
go mod tidy
go mod download
go mod vendor
$(MAKE) replace
Loading

0 comments on commit d422f41

Please sign in to comment.