Merge branch 'master' into dependabot/go_modules/google.golang.org/protobuf-1.33.0

This commit is contained in:
Pavel 2024-06-06 14:37:54 +03:00 committed by GitHub
commit 14cdfa7075
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 87 additions and 71 deletions

View File

@ -22,20 +22,20 @@ jobs:
- name: Set up stable Go version - name: Set up stable Go version
uses: actions/setup-go@v3 uses: actions/setup-go@v3
with: with:
go-version: 'stable' go-version: '1.22'
- name: Get dependencies - name: Get dependencies
run: go mod tidy run: go mod tidy
- name: Lint code with golangci-lint - name: Lint code with golangci-lint
uses: golangci/golangci-lint-action@v3 uses: golangci/golangci-lint-action@v3
with: with:
version: v1.50.1 version: v1.55.2
only-new-issues: true only-new-issues: true
tests: tests:
name: Tests name: Tests
runs-on: ubuntu-latest runs-on: ubuntu-latest
strategy: strategy:
matrix: matrix:
go-version: ['1.18', '1.19', 'stable'] go-version: ['1.18', '1.19', '1.20', '1.21', '1.22', 'stable']
steps: steps:
- name: Set up Go ${{ matrix.go-version }} - name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v3 uses: actions/setup-go@v3
@ -46,7 +46,7 @@ jobs:
- name: Get dependencies - name: Get dependencies
run: go mod tidy run: go mod tidy
- name: Tests - name: Tests
run: go test ./... -v -cpu 2 -timeout 10s -race -cover -coverprofile=coverage.txt -covermode=atomic run: go test ./... -v -cpu 2 -timeout 30s -race -cover -coverprofile=coverage.txt -covermode=atomic
- name: Coverage - name: Coverage
run: | run: |
go install github.com/axw/gocov/gocov@latest go install github.com/axw/gocov/gocov@latest

View File

@ -6,26 +6,9 @@ output:
format: colored-line-number format: colored-line-number
sort-results: true sort-results: true
# Linters below do not support go1.18 yet because of generics.
# See https://github.com/golangci/golangci-lint/issues/2649
# - bodyclose
# - sqlclosecheck
linters: linters:
disable-all: true disable-all: true
enable: enable:
- paralleltest
- tparallel
- asciicheck
- asasalint
- varnamelen
- reassign
- nilnil
- nilerr
- nakedret
- goprintffuncname
- typecheck
- errchkjson
- errcheck - errcheck
- gosimple - gosimple
- govet - govet
@ -33,6 +16,7 @@ linters:
- staticcheck - staticcheck
- unused - unused
- unparam - unparam
- bodyclose
- dogsled - dogsled
- dupl - dupl
- errorlint - errorlint
@ -46,6 +30,7 @@ linters:
- godot - godot
- goimports - goimports
- revive - revive
- gomnd
- gosec - gosec
- lll - lll
- makezero - makezero
@ -53,9 +38,10 @@ linters:
- nestif - nestif
- prealloc - prealloc
- predeclared - predeclared
- exportloopref - sqlclosecheck
- unconvert - unconvert
- whitespace - whitespace
- unused
linters-settings: linters-settings:
govet: govet:
@ -64,11 +50,9 @@ linters-settings:
enable: enable:
- assign - assign
- atomic - atomic
- atomicalign
- bools - bools
- buildtag - buildtag
- copylocks - copylocks
- fieldalignment
- httpresponse - httpresponse
- loopclosure - loopclosure
- lostcancel - lostcancel
@ -80,6 +64,7 @@ linters-settings:
- unmarshal - unmarshal
- unreachable - unreachable
- unsafeptr - unsafeptr
- unused
settings: settings:
printf: printf:
funcs: funcs:
@ -147,20 +132,18 @@ linters-settings:
threshold: 200 threshold: 200
errorlint: errorlint:
errorf: true errorf: true
asserts: false
comparison: false
exhaustive: exhaustive:
check-generated: false check-generated: false
default-signifies-exhaustive: false default-signifies-exhaustive: false
funlen: funlen:
lines: 90 lines: 60
statements: 40 statements: 40
gocognit: gocognit:
min-complexity: 25 min-complexity: 25
gocyclo: gocyclo:
min-complexity: 25 min-complexity: 25
goimports: goimports:
local-prefixes: github.com/retailcrm/mg-transport-core local-prefixes: github.com/retailcrm/mg-transport-api-client-go
lll: lll:
line-length: 120 line-length: 120
misspell: misspell:
@ -170,25 +153,23 @@ linters-settings:
whitespace: whitespace:
multi-if: false multi-if: false
multi-func: false multi-func: false
varnamelen:
max-distance: 10
ignore-map-index-ok: true
ignore-type-assert-ok: true
ignore-chan-recv-ok: true
ignore-decls:
- t *testing.T
- e error
- i int
issues: issues:
exclude-rules: exclude-rules:
- path: _test\.go - path: _test\.go
linters: linters:
- dupl
- gomnd
- lll - lll
- bodyclose
- errcheck - errcheck
- sqlclosecheck
- misspell - misspell
- ineffassign - ineffassign
- whitespace - whitespace
- makezero - makezero
- maligned
- ifshort
- errcheck - errcheck
- funlen - funlen
- goconst - goconst
@ -196,10 +177,6 @@ issues:
- gocyclo - gocyclo
- godot - godot
- unused - unused
- errchkjson
- varnamelen
- path: \.go
text: "Error return value of `io.WriteString` is not checked"
exclude-use-default: true exclude-use-default: true
exclude-case-sensitive: false exclude-case-sensitive: false
max-issues-per-linter: 0 max-issues-per-linter: 0
@ -211,4 +188,4 @@ severity:
case-sensitive: false case-sensitive: false
service: service:
golangci-lint-version: 1.50.x golangci-lint-version: 1.55.x

View File

@ -6,11 +6,11 @@ import (
"net/url" "net/url"
"testing" "testing"
"github.com/h2non/gock"
retailcrm "github.com/retailcrm/api-client-go/v2" retailcrm "github.com/retailcrm/api-client-go/v2"
"github.com/retailcrm/mg-transport-core/v2/core/util/testutil" "github.com/retailcrm/mg-transport-core/v2/core/util/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"gopkg.in/h2non/gock.v1"
) )
func TestDefaultNotifyFunc(t *testing.T) { // nolint:paralleltest func TestDefaultNotifyFunc(t *testing.T) { // nolint:paralleltest

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
"net/url"
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -16,6 +17,14 @@ import (
"github.com/retailcrm/mg-transport-core/v2/core/logger" "github.com/retailcrm/mg-transport-core/v2/core/logger"
) )
const (
defaultDialerTimeout = 30 * time.Second
defaultIdleConnTimeout = 90 * time.Second
defaultTLSHandshakeTimeout = 10 * time.Second
defaultExpectContinueTimeout = 1 * time.Second
defaultMaxIdleConns = 100
)
// DefaultClient stores original http.DefaultClient. // DefaultClient stores original http.DefaultClient.
var DefaultClient = http.DefaultClient var DefaultClient = http.DefaultClient
@ -24,28 +33,29 @@ var DefaultTransport = http.DefaultTransport
// HTTPClientBuilder builds http client with mocks (if necessary) and timeout. // HTTPClientBuilder builds http client with mocks (if necessary) and timeout.
// Example: // Example:
// // Build HTTP client with timeout = 10 sec, without SSL certificates verification and with mocked google.com
// client, err := NewHTTPClientBuilder().
// SetTimeout(10).
// SetMockAddress("api_mock:3004").
// AddMockedDomain("google.com").
// SetSSLVerification(false).
// Build()
// //
// if err != nil { // // Build HTTP client with timeout = 10 sec, without SSL certificates verification and with mocked google.com
// fmt.Print(err) // client, err := NewHTTPClientBuilder().
// } // SetTimeout(10).
// SetMockAddress("api_mock:3004").
// AddMockedDomain("google.com").
// SetSSLVerification(false).
// Build()
// //
// // Actual response will be returned from "api_mock:3004" (it should provide any ssl certificate) // if err != nil {
// if resp, err := client.Get("https://google.com"); err == nil { // fmt.Print(err)
// if data, err := ioutil.ReadAll(resp.Body); err == nil { // }
// fmt.Printf("Data: %s", string(data)) //
// } else { // // Actual response will be returned from "api_mock:3004" (it should provide any ssl certificate)
// fmt.Print(err) // if resp, err := client.Get("https://google.com"); err == nil {
// } // if data, err := ioutil.ReadAll(resp.Body); err == nil {
// } else { // fmt.Printf("Data: %s", string(data))
// fmt.Print(err) // } else {
// } // fmt.Print(err)
// }
// } else {
// fmt.Print(err)
// }
type HTTPClientBuilder struct { type HTTPClientBuilder struct {
logger logger.Logger logger logger.Logger
httpClient *http.Client httpClient *http.Client
@ -64,11 +74,22 @@ type HTTPClientBuilder struct {
// NewHTTPClientBuilder returns HTTPClientBuilder with default values. // NewHTTPClientBuilder returns HTTPClientBuilder with default values.
func NewHTTPClientBuilder() *HTTPClientBuilder { func NewHTTPClientBuilder() *HTTPClientBuilder {
return &HTTPClientBuilder{ return &HTTPClientBuilder{
built: false, built: false,
httpClient: &http.Client{}, httpClient: &http.Client{},
httpTransport: &http.Transport{}, httpTransport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: defaultDialerTimeout,
KeepAlive: defaultDialerTimeout,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: defaultMaxIdleConns,
IdleConnTimeout: defaultIdleConnTimeout,
TLSHandshakeTimeout: defaultTLSHandshakeTimeout,
ExpectContinueTimeout: defaultExpectContinueTimeout,
},
tlsVersion: tls.VersionTLS12, tlsVersion: tls.VersionTLS12,
timeout: 30 * time.Second, timeout: defaultDialerTimeout,
mockAddress: "", mockAddress: "",
mockedDomains: []string{}, mockedDomains: []string{},
logging: false, logging: false,
@ -147,6 +168,11 @@ func (b *HTTPClientBuilder) SetLogging(flag bool) *HTTPClientBuilder {
return b return b
} }
func (b *HTTPClientBuilder) SetProxy(proxy func(*http.Request) (*url.URL, error)) *HTTPClientBuilder {
b.httpTransport.Proxy = proxy
return b
}
// FromConfig fulfills mock configuration from HTTPClientConfig. // FromConfig fulfills mock configuration from HTTPClientConfig.
func (b *HTTPClientBuilder) FromConfig(config *config.HTTPClientConfig) *HTTPClientBuilder { func (b *HTTPClientBuilder) FromConfig(config *config.HTTPClientConfig) *HTTPClientBuilder {
if config == nil { if config == nil {
@ -212,6 +238,7 @@ func (b *HTTPClientBuilder) buildMocks() error {
b.logf(" - %s\n", domain) b.logf(" - %s\n", domain)
} }
b.httpTransport.Proxy = nil
b.httpTransport.DialContext = func(ctx context.Context, network, addr string) (conn net.Conn, e error) { b.httpTransport.DialContext = func(ctx context.Context, network, addr string) (conn net.Conn, e error) {
var ( var (
host string host string

View File

@ -92,6 +92,13 @@ func (t *HTTPClientBuilderTest) Test_SetCertPool() {
assert.Equal(t.T(), pool, t.builder.httpTransport.TLSClientConfig.RootCAs) assert.Equal(t.T(), pool, t.builder.httpTransport.TLSClientConfig.RootCAs)
} }
func (t *HTTPClientBuilderTest) Test_SetProxy() {
t.builder.SetProxy(nil)
assert.Nil(t.T(), t.builder.httpTransport.Proxy)
t.builder.SetProxy(http.ProxyFromEnvironment)
assert.NotNil(t.T(), t.builder.httpTransport.Proxy)
}
func (t *HTTPClientBuilderTest) Test_FromConfigNil() { func (t *HTTPClientBuilderTest) Test_FromConfigNil() {
defer func() { defer func() {
assert.Nil(t.T(), recover()) assert.Nil(t.T(), recover())
@ -161,6 +168,7 @@ func (t *HTTPClientBuilderTest) Test_Build() {
assert.NoError(t.T(), err) assert.NoError(t.T(), err)
assert.NotNil(t.T(), client) assert.NotNil(t.T(), client)
assert.Nil(t.T(), client.Transport.(*http.Transport).Proxy)
assert.Equal(t.T(), client, http.DefaultClient) assert.Equal(t.T(), client, http.DefaultClient)
assert.Equal(t.T(), timeout*time.Second, client.Timeout) assert.Equal(t.T(), timeout*time.Second, client.Timeout)
assert.Equal(t.T(), pool, client.Transport.(*http.Transport).TLSClientConfig.RootCAs) assert.Equal(t.T(), pool, client.Transport.(*http.Transport).TLSClientConfig.RootCAs)
@ -290,6 +298,7 @@ uf/TQPpjrGW5nxOf94qn6FzV2WSype9BcM5MD7z7rk202Fs7Zqc=
SetSSLVerification(false). SetSSLVerification(false).
Build() Build()
require.NoError(t.T(), err, "cannot build client") require.NoError(t.T(), err, "cannot build client")
assert.Nil(t.T(), client.Transport.(*http.Transport).Proxy)
resp, err := client.Get(mockProto + mockDomainAddr) resp, err := client.Get(mockProto + mockDomainAddr)
if err != nil && strings.Contains(err.Error(), "connection refused") { if err != nil && strings.Contains(err.Error(), "connection refused") {
@ -314,6 +323,7 @@ func (t *HTTPClientBuilderTest) Test_UseTLS10() {
t.Require().NotNil(client.Transport) t.Require().NotNil(client.Transport)
t.Require().NotNil(client.Transport.(*http.Transport).TLSClientConfig) t.Require().NotNil(client.Transport.(*http.Transport).TLSClientConfig)
t.Assert().Equal(uint16(tls.VersionTLS10), client.Transport.(*http.Transport).TLSClientConfig.MinVersion) t.Assert().Equal(uint16(tls.VersionTLS10), client.Transport.(*http.Transport).TLSClientConfig.MinVersion)
t.Assert().NotNil(client.Transport.(*http.Transport).Proxy)
} }
// taken from https://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go // taken from https://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go

View File

@ -5,7 +5,7 @@ import (
"io" "io"
"net/http" "net/http"
"gopkg.in/h2non/gock.v1" "github.com/h2non/gock"
) )
// UnmatchedRequestsTestingT contains all of *testing.T methods which are needed for AssertNoUnmatchedRequests. // UnmatchedRequestsTestingT contains all of *testing.T methods which are needed for AssertNoUnmatchedRequests.

View File

@ -6,8 +6,8 @@ import (
"net/http" "net/http"
"testing" "testing"
"github.com/h2non/gock"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"gopkg.in/h2non/gock.v1"
) )
type testingTMock struct { type testingTMock struct {

View File

@ -8,13 +8,13 @@ import (
"testing" "testing"
"time" "time"
"github.com/h2non/gock"
"github.com/op/go-logging" "github.com/op/go-logging"
retailcrm "github.com/retailcrm/api-client-go/v2" retailcrm "github.com/retailcrm/api-client-go/v2"
v1 "github.com/retailcrm/mg-transport-api-client-go/v1" v1 "github.com/retailcrm/mg-transport-api-client-go/v1"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"gopkg.in/h2non/gock.v1"
"github.com/retailcrm/mg-transport-core/v2/core/config" "github.com/retailcrm/mg-transport-core/v2/core/config"

2
go.mod
View File

@ -17,6 +17,7 @@ require (
github.com/gomarkdown/markdown v0.0.0-20221013030248-663e2500819c github.com/gomarkdown/markdown v0.0.0-20221013030248-663e2500819c
github.com/gorilla/securecookie v1.1.1 github.com/gorilla/securecookie v1.1.1
github.com/gorilla/sessions v1.2.0 github.com/gorilla/sessions v1.2.0
github.com/h2non/gock v1.2.0
github.com/jessevdk/go-flags v1.4.0 github.com/jessevdk/go-flags v1.4.0
github.com/jinzhu/gorm v1.9.11 github.com/jinzhu/gorm v1.9.11
github.com/nicksnyder/go-i18n/v2 v2.0.2 github.com/nicksnyder/go-i18n/v2 v2.0.2
@ -29,7 +30,6 @@ require (
go.uber.org/atomic v1.10.0 go.uber.org/atomic v1.10.0
golang.org/x/text v0.9.0 golang.org/x/text v0.9.0
gopkg.in/gormigrate.v1 v1.6.0 gopkg.in/gormigrate.v1 v1.6.0
gopkg.in/h2non/gock.v1 v1.1.2
gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v2 v2.4.0
) )

2
go.sum
View File

@ -237,6 +237,8 @@ github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+
github.com/gorilla/sessions v1.2.0 h1:S7P+1Hm5V/AT9cjEcUD5uDaQSX0OE577aCXgoaKpYbQ= github.com/gorilla/sessions v1.2.0 h1:S7P+1Hm5V/AT9cjEcUD5uDaQSX0OE577aCXgoaKpYbQ=
github.com/gorilla/sessions v1.2.0/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= github.com/gorilla/sessions v1.2.0/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/h2non/gock v1.2.0 h1:K6ol8rfrRkUOefooBC8elXoaNGYkpp7y2qcxGG6BzUE=
github.com/h2non/gock v1.2.0/go.mod h1:tNhoxHYW2W42cYkYb1WqzdbYIieALC99kpYr7rH/BQk=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw= github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI= github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=