Skip to content

Commit a017960

Browse files
upgrade to go 1.21
1 parent 4a1d667 commit a017960

File tree

6 files changed

+74
-41
lines changed

6 files changed

+74
-41
lines changed

go.mod

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
module github.com/Clever/sphinx
22

3-
go 1.15
3+
go 1.21
44

55
require (
66
github.com/Clever/leakybucket v1.1.0
77
github.com/aws/aws-sdk-go v1.29.34
8+
github.com/pborman/uuid v0.0.0-20180906182336-adf5a7427709
9+
github.com/stretchr/testify v1.5.1
10+
gopkg.in/Clever/kayvee-go.v6 v6.24.0
11+
gopkg.in/tylerb/graceful.v1 v1.2.15
12+
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0
13+
)
14+
15+
require (
16+
github.com/davecgh/go-spew v1.1.1 // indirect
817
github.com/eapache/go-resiliency v1.2.0 // indirect
918
github.com/garyburd/redigo v1.6.0 // indirect
1019
github.com/google/uuid v1.1.1 // indirect
11-
github.com/pborman/uuid v0.0.0-20180906182336-adf5a7427709
20+
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
21+
github.com/pmezard/go-difflib v1.0.0 // indirect
1222
github.com/stretchr/objx v0.2.0 // indirect
13-
github.com/stretchr/testify v1.5.1
1423
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
24+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
1525
github.com/xeipuuv/gojsonschema v1.2.1-0.20200118195451-b537c054d4b4 // indirect
16-
gopkg.in/Clever/kayvee-go.v6 v6.24.0
17-
gopkg.in/tylerb/graceful.v1 v1.2.15
18-
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0
1926
gopkg.in/yaml.v2 v2.2.8 // indirect
2027
)

golang.mk

+45-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This is the default Clever Golang Makefile.
22
# It is stored in the dev-handbook repo, github.com/Clever/dev-handbook
33
# Please do not alter this file directly.
4-
GOLANG_MK_VERSION := 1.0.0
4+
GOLANG_MK_VERSION := 1.2.1
55

66
SHELL := /bin/bash
77
SYSTEM := $(shell uname -a | cut -d" " -f1 | tr '[:upper:]' '[:lower:]')
@@ -11,7 +11,7 @@ SYSTEM := $(shell uname -a | cut -d" " -f1 | tr '[:upper:]' '[:lower:]')
1111
export TZ=UTC
1212

1313
# go build flags for use across all commands which accept them
14-
GO_BUILD_FLAGS := "-mod=mod"
14+
export GOFLAGS := -mod=vendor $(GOFLAGS)
1515

1616
# if the gopath includes several directories, use only the first
1717
GOPATH=$(shell echo $$GOPATH | cut -d: -f1)
@@ -39,17 +39,19 @@ endef
3939
# so we're defended against it breaking or changing in the future.
4040
FGT := $(GOPATH)/bin/fgt
4141
$(FGT):
42-
go get github.com/GeertJohan/fgt@262f7b11eec07dc7b147c44641236f3212fee89d
42+
go install -mod=readonly github.com/GeertJohan/fgt@262f7b11eec07dc7b147c44641236f3212fee89d
4343

4444
golang-ensure-curl-installed:
4545
@command -v curl >/dev/null 2>&1 || { echo >&2 "curl not installed. Please install curl."; exit 1; }
4646

4747
# Golint is a tool for linting Golang code for common errors.
4848
# We pin its version because an update could add a new lint check which would make
4949
# previously passing tests start failing without changing our code.
50+
# this package is deprecated and frozen
51+
# Infra recomendation is to eventaully move to https://github.com/golangci/golangci-lint so don't fail on linting error for now
5052
GOLINT := $(GOPATH)/bin/golint
5153
$(GOLINT):
52-
go get golang.org/x/lint/golint@738671d3881b9731cc63024d5d88cf28db875626
54+
go install -mod=readonly golang.org/x/lint/golint@738671d3881b9731cc63024d5d88cf28db875626
5355

5456
# golang-fmt-deps requires the FGT tool for checking output
5557
golang-fmt-deps: $(FGT)
@@ -74,22 +76,14 @@ endef
7476
# golang-lint-deps-strict requires the golint tool for golang linting.
7577
golang-lint-deps-strict: $(GOLINT) $(FGT)
7678

77-
# golang-lint-strict calls golint on all golang files in the pkg and fails if any lint
78-
# errors are found.
79-
# arg1: pkg path
80-
define golang-lint-strict
81-
@echo "LINTING $(1)..."
82-
@PKG_PATH=$$(go list -f '{{.Dir}}' $(1)); find $${PKG_PATH}/*.go -type f | grep -v gen_ | xargs $(FGT) $(GOLINT)
83-
endef
84-
8579
# golang-test-deps is here for consistency
8680
golang-test-deps:
8781

8882
# golang-test uses the Go toolchain to run all tests in the pkg.
8983
# arg1: pkg path
9084
define golang-test
9185
@echo "TESTING $(1)..."
92-
@go test $(GO_BUILD_FLAGS) -v $(1)
86+
@go test -v $(1)
9387
endef
9488

9589
# golang-test-strict-deps is here for consistency
@@ -99,7 +93,22 @@ golang-test-strict-deps:
9993
# arg1: pkg path
10094
define golang-test-strict
10195
@echo "TESTING $(1)..."
102-
@go test -v $(GO_BUILD_FLAGS) -race $(1)
96+
@go test -v -race $(1)
97+
endef
98+
99+
# golang-test-strict-cover-deps is here for consistency
100+
golang-test-strict-cover-deps:
101+
102+
# golang-test-strict-cover uses the Go toolchain to run all tests in the pkg with the race and cover flag.
103+
# appends coverage results to coverage.txt
104+
# arg1: pkg path
105+
define golang-test-strict-cover
106+
@echo "TESTING $(1)..."
107+
@go test -v -race -cover -coverprofile=profile.tmp -covermode=atomic $(1)
108+
@if [ -f profile.tmp ]; then \
109+
cat profile.tmp | tail -n +2 >> coverage.txt; \
110+
rm profile.tmp; \
111+
fi;
103112
endef
104113

105114
# golang-vet-deps is here for consistency
@@ -109,7 +118,7 @@ golang-vet-deps:
109118
# arg1: pkg path
110119
define golang-vet
111120
@echo "VETTING $(1)..."
112-
@go vet $(GO_BUILD_FLAGS) $(1)
121+
@go vet $(1)
113122
endef
114123

115124
# golang-test-all-deps installs all dependencies needed for different test cases.
@@ -132,24 +141,41 @@ golang-test-all-strict-deps: golang-fmt-deps golang-lint-deps-strict golang-test
132141
# arg1: pkg path
133142
define golang-test-all-strict
134143
$(call golang-fmt,$(1))
135-
$(call golang-lint-strict,$(1))
144+
$(call golang-lint,$(1))
136145
$(call golang-vet,$(1))
137146
$(call golang-test-strict,$(1))
138147
endef
139148

149+
# golang-test-all-strict-cover-deps: installs all dependencies needed for different test cases.
150+
golang-test-all-strict-cover-deps: golang-fmt-deps golang-lint-deps-strict golang-test-strict-cover-deps golang-vet-deps
151+
152+
# golang-test-all-strict-cover calls fmt, lint, vet and test on the specified pkg with strict and cover
153+
# requirements that no errors are thrown while linting.
154+
# arg1: pkg path
155+
define golang-test-all-strict-cover
156+
$(call golang-fmt,$(1))
157+
$(call golang-lint,$(1))
158+
$(call golang-vet,$(1))
159+
$(call golang-test-strict-cover,$(1))
160+
endef
161+
140162
# golang-build: builds a golang binary. ensures CGO build is done during CI. This is needed to make a binary that works with a Docker alpine image.
141163
# arg1: pkg path
142164
# arg2: executable name
143165
define golang-build
144-
@echo "BUILDING..."
166+
@echo "BUILDING $(2)..."
145167
@if [ -z "$$CI" ]; then \
146-
go build $(GO_BUILD_FLAGS) -o bin/$(2) $(1); \
168+
go build -o bin/$(2) $(1); \
147169
else \
148170
echo "-> Building CGO binary"; \
149-
CGO_ENABLED=0 go build $(GO_BUILD_FLAGS) -installsuffix cgo -o bin/$(2) $(1); \
171+
CGO_ENABLED=0 go build -installsuffix cgo -o bin/$(2) $(1); \
150172
fi;
151173
endef
152174

175+
# golang-setup-coverage: set up the coverage file
176+
golang-setup-coverage:
177+
@echo "mode: atomic" > coverage.txt
178+
153179
# golang-update-makefile downloads latest version of golang.mk
154180
golang-update-makefile:
155181
@wget https://raw.githubusercontent.com/Clever/dev-handbook/master/make/golang-v1.mk -O /tmp/golang.mk 2>/dev/null

handlers/http_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ func constructHTTPRateLimiter() httpRateLimiter {
101101

102102
func TestParsesHeaders(t *testing.T) {
103103
request := common.HTTPToSphinxRequest(constructMockRequestWithHeaders(map[string][]string{
104-
"Authorization": []string{"Bearer 12345"},
105-
"X-Forwarded-For": []string{"IP1", "IP2"},
104+
"Authorization": {"Bearer 12345"},
105+
"X-Forwarded-For": {"IP1", "IP2"},
106106
}))
107107
if len(request["headers"].(http.Header)) != 2 {
108108
t.Fatalf("expected 2 headers, recevied %d", len(request["headers"].(http.Header)))

limitkeys/headerlimitkey_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestKeysWithHeaders(t *testing.T) {
1717
}
1818

1919
request := getRequest(map[string][]string{
20-
"Authorization": []string{"Bearer 12345"},
20+
"Authorization": {"Bearer 12345"},
2121
})
2222
if key, err := limitkey.Key(request); err != nil || key != "Authorization:Bearer 12345" {
2323
t.Error("HeaderKey did not match")
@@ -31,7 +31,7 @@ func TestKeysWithHeaders(t *testing.T) {
3131
}
3232

3333
request = getRequest(map[string][]string{
34-
"X-Forwarded-For": []string{"127.0.0.1", "172.0.0.1"},
34+
"X-Forwarded-For": {"127.0.0.1", "172.0.0.1"},
3535
})
3636
if key, err := limitkey.Key(request); err != nil || key !=
3737
"X-Forwarded-For:127.0.0.1;172.0.0.1" {
@@ -46,7 +46,7 @@ func TestKeysWithHeadersSalt(t *testing.T) {
4646
}
4747

4848
request := getRequest(map[string][]string{
49-
"Authorization": []string{"Bearer 12345"},
49+
"Authorization": {"Bearer 12345"},
5050
})
5151
if key, err := limitkey.Key(request); err != nil {
5252
t.Fatal(err)
@@ -62,7 +62,7 @@ func TestKeysWithHeadersSalt(t *testing.T) {
6262
}
6363

6464
request = getRequest(map[string][]string{
65-
"X-Forwarded-For": []string{"127.0.0.1", "172.0.0.1"},
65+
"X-Forwarded-For": {"127.0.0.1", "172.0.0.1"},
6666
})
6767
if key, err := limitkey.Key(request); err != nil || key !=
6868
"X-Forwarded-For:127.0.0.1;172.0.0.1" {
@@ -78,7 +78,7 @@ func TestKeysWithoutHeaders(t *testing.T) {
7878

7979
// returns custom error when no key is found
8080
request := getRequest(map[string][]string{
81-
"X-Forwarded-For": []string{"127.0.0.1"},
81+
"X-Forwarded-For": {"127.0.0.1"},
8282
})
8383
key, err := limitkey.Key(request)
8484
if key != "" {

matchers/headermatcher_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -96,29 +96,29 @@ headers:
9696
t.Fatalf("Failed to create HeaderMatcher: %s", err)
9797
}
9898
request := getRequest(map[string][]string{
99-
"Authorization": []string{"Bearer 12345"},
99+
"Authorization": {"Bearer 12345"},
100100
})
101101

102102
if !headermatcher.Match(request) {
103103
t.Fatalf("Should have matched Header Authorization")
104104
}
105105

106106
request = getRequest(map[string][]string{
107-
"Authorization": []string{"Basic 12345"},
107+
"Authorization": {"Basic 12345"},
108108
})
109109
if headermatcher.Match(request) {
110110
t.Fatalf("Should NOT have matched Header Authorization Basic")
111111
}
112112

113113
request = getRequest(map[string][]string{
114-
"X-Forwarded-For": []string{"192.0.0.1", "127.0.0.1"},
115-
"Authorization": []string{"Basic 12345"},
114+
"X-Forwarded-For": {"192.0.0.1", "127.0.0.1"},
115+
"Authorization": {"Basic 12345"},
116116
})
117117
if !headermatcher.Match(request) {
118118
t.Fatalf("Should have matched X-Forwarded-For")
119119
}
120120
request = getRequest(map[string][]string{
121-
"Authorization": []string{"Basic 12345"},
121+
"Authorization": {"Basic 12345"},
122122
})
123123
if headermatcher.Match(request) {
124124
t.Fatalf("Should NOT have matched Header Authorization Basic")
@@ -137,14 +137,14 @@ headers:
137137
}
138138

139139
request := getRequest(map[string][]string{
140-
"Authorization": []string{"Bearer 12345"},
140+
"Authorization": {"Bearer 12345"},
141141
})
142142
if !headermatcher.Match(request) {
143143
t.Fatalf("Should have matched Header Authorization")
144144
}
145145

146146
request = getRequest(map[string][]string{
147-
"X-Forwarded-For": []string{"192.0.0.1"},
147+
"X-Forwarded-For": {"192.0.0.1"},
148148
})
149149
if headermatcher.Match(request) {
150150
t.Fatalf("Should NOT have matched Header X-Forwarded-For")

ratelimiter/ratelimiter_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestSimpleAdd(t *testing.T) {
8080
}
8181
if err = checkLastStatusForRequests(
8282
ratelimiter, request, 5, []Status{
83-
Status{Remaining: 195, Name: "bearer-special"}}); err != nil {
83+
{Remaining: 195, Name: "bearer-special"}}); err != nil {
8484
t.Error(err)
8585
}
8686

@@ -93,7 +93,7 @@ func TestSimpleAdd(t *testing.T) {
9393

9494
if err = checkLastStatusForRequests(
9595
ratelimiter, request, 1, []Status{
96-
Status{Remaining: 195, Name: "basic-simple"}}); err != nil {
96+
{Remaining: 195, Name: "basic-simple"}}); err != nil {
9797
t.Error(err)
9898
}
9999

0 commit comments

Comments
 (0)