Skip to content

Commit

Permalink
Polish tests and repair goimports linter
Browse files Browse the repository at this point in the history
  • Loading branch information
vasayxtx committed Aug 9, 2024
1 parent aa00cdb commit 104adc5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ linters-settings:
lll:
line-length: 140
goimports:
local-prefixes: git.acronis.com/abc/go-libs/
local-prefixes: github.com/acronis/go-libs/
gocritic:
enabled-tags:
- diagnostic
Expand Down
30 changes: 14 additions & 16 deletions httpserver/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,16 @@ func generateCertificate(certFilePath, privKeyPath string) error {
}

func TestHTTPServer_Start_SecureServer(t *testing.T) {
var (
dirName = "testdata/tls"
certFilePath = filepath.Join(dirName, "cert.pem")
privKeyFile = filepath.Join(dirName, "key.pem")
)
tmpDirPath := t.TempDir()
certFilePath := filepath.Join(tmpDirPath, "cert.pem")
privKeyFilePath := filepath.Join(tmpDirPath, "key.pem")

addr := testutil.GetLocalAddrWithFreeTCPPort()
fatalErr := make(chan error, 1)
err := os.MkdirAll(dirName, 0755)
require.NoError(t, err)

err = generateCertificate(certFilePath, privKeyFile)
require.NoError(t, err)
require.NoError(t, generateCertificate(certFilePath, privKeyFilePath))

cfg := TLSConfig{Enabled: true, Certificate: certFilePath, Key: privKeyFile}
cfg := TLSConfig{Enabled: true, Certificate: certFilePath, Key: privKeyFilePath}
httpServer, err := New(&Config{Address: addr, TLS: cfg}, logtest.NewLogger(), Opts{})
require.NoError(t, err)

Expand All @@ -122,15 +118,17 @@ func TestHTTPServer_Start_SecureServer(t *testing.T) {
testutil.RequireNoErrorInChannel(t, fatalErr)
}()

client := buildClient(cfg.Certificate)
client, err := buildClient(cfg.Certificate)
require.NoError(t, err)

resp, err := client.Get(httpServer.URL + "/healthz")
defer func() { require.NoError(t, resp.Body.Close()) }()

require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
}

func buildClient(certPath string) *http.Client {
func buildClient(certPath string) (*http.Client, error) {
// Set up our own certificate pool
tlsConfig := &tls.Config{RootCAs: x509.NewCertPool()}
transport := &http.Transport{TLSClientConfig: tlsConfig}
Expand All @@ -139,18 +137,18 @@ func buildClient(certPath string) *http.Client {
// Load our trusted certificate path
pemData, err := os.ReadFile(certPath)
if err != nil {
panic(err)
return nil, err
}
ok := tlsConfig.RootCAs.AppendCertsFromPEM(pemData)
if !ok {
panic("Couldn't load PEM data")
return nil, err
}

return client
return client, nil
}

func TestHTTPServer_StartWithStaticPort(t *testing.T) {
testHTTPServerStart(t, "127.0.0.1", testutil.GetLocalFreeTCPPort(), "")
testHTTPServerStart(t, "127.0.0.1", testutil.MustGetLocalFreeTCPPort(), "")
}

func TestHTTPServer_StartWithDynamicPort(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions httpserver/middleware/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ func (h *mockLoggingNextHandler) ServeHTTP(rw http.ResponseWriter, r *http.Reque
h.lastContextLogger = GetLoggerFromContext(r.Context())
h.lastContextLoggingParams = GetLoggingParamsFromContext(r.Context())
rw.WriteHeader(h.respStatusCode)
if _, err := rw.Write([]byte(http.StatusText(h.respStatusCode))); err != nil {
panic(err)
}
_, _ = rw.Write([]byte(http.StatusText(h.respStatusCode)))
}

func TestLoggingHandler_ServeHTTP(t *testing.T) {
Expand Down
19 changes: 13 additions & 6 deletions testutil/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,28 @@ import (
)

// GetLocalFreeTCPPort returns free (not listening by somebody) TCP port on the 127.0.0.1 network interface.
func GetLocalFreeTCPPort() int {
listener, err := net.Listen("tcp", "127.0.0.1:0")
func GetLocalFreeTCPPort() (int, error) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
panic(err)
return 0, err
}
port := listener.Addr().(*net.TCPAddr).Port
if err := listener.Close(); err != nil {
port := ln.Addr().(*net.TCPAddr).Port
return port, ln.Close()
}

// MustGetLocalFreeTCPPort returns free (not listening by somebody) TCP port on the 127.0.0.1 network interface.
// It panics if an error occurs.
func MustGetLocalFreeTCPPort() int {
port, err := GetLocalFreeTCPPort()
if err != nil {
panic(err)
}
return port
}

// GetLocalAddrWithFreeTCPPort returns 127.0.0.1:<free-tcp-port> address.
func GetLocalAddrWithFreeTCPPort() string {
return fmt.Sprintf("127.0.0.1:%d", GetLocalFreeTCPPort())
return fmt.Sprintf("127.0.0.1:%d", MustGetLocalFreeTCPPort())
}

// WaitListeningServer waits until the server is ready to accept TCP connection on the passing address.
Expand Down

0 comments on commit 104adc5

Please sign in to comment.