Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Jan 6, 2025
1 parent bd248e2 commit 891f699
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions internal/configtypes/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package configtypes

import (
"crypto/tls"
"errors"
"os"
"testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -27,10 +25,6 @@ func mockStatFileSuccess(name string) (os.FileInfo, error) {
return nil, nil // Mock successful stat
}

func mockStatFileFailure(name string) (os.FileInfo, error) {
return nil, errors.New("failed to stat file")
}

// Sample valid PEM strings
const validPEM = `-----BEGIN CERTIFICATE-----
MIIEbjCCAtagAwIBAgIRAN1ZJEYl5ZNIOHsbJizQpucwDQYJKoZIhvcNAQELBQAw
Expand Down Expand Up @@ -94,8 +88,8 @@ func TestToGoTLSConfig_Disabled(t *testing.T) {
Enabled: false,
}
tlsCfg, err := cfg.ToGoTLSConfig("test-entity")
assert.NoError(t, err)
assert.Nil(t, tlsCfg)
require.NoError(t, err)
require.Nil(t, tlsCfg)
}

func TestToGoTLSConfig_Valid(t *testing.T) {
Expand All @@ -113,8 +107,8 @@ func TestToGoTLSConfig_Valid(t *testing.T) {
tlsCfg, err := makeTLSConfig(cfg, logger, mockReadFileSuccess, mockStatFileSuccess)
require.NoError(t, err)
require.NotNil(t, tlsCfg)
assert.Equal(t, "test.server", tlsCfg.ServerName)
assert.False(t, tlsCfg.InsecureSkipVerify)
require.Equal(t, "test.server", tlsCfg.ServerName)
require.False(t, tlsCfg.InsecureSkipVerify)
}

func TestMakeTLSConfig_InvalidCertKey(t *testing.T) {
Expand All @@ -128,8 +122,7 @@ func TestMakeTLSConfig_InvalidCertKey(t *testing.T) {

_, err := makeTLSConfig(cfg, logger, mockReadFileSuccess, mockStatFileSuccess)
require.Error(t, err)
t.Logf(err.Error())
assert.Contains(t, err.Error(), "error load certificate")
require.Contains(t, err.Error(), "error load certificate")
}

func TestMakeTLSConfig_InsecureSkipVerify(t *testing.T) {
Expand All @@ -145,42 +138,42 @@ func TestMakeTLSConfig_InsecureSkipVerify(t *testing.T) {
tlsCfg, err := makeTLSConfig(cfg, logger, mockReadFileSuccess, mockStatFileSuccess)
require.NoError(t, err)
require.NotNil(t, tlsCfg)
assert.True(t, tlsCfg.InsecureSkipVerify)
require.True(t, tlsCfg.InsecureSkipVerify)
}

func TestLoadCertificate_NoPEM(t *testing.T) {
logger := zerolog.Nop()
tlsCfg := &tls.Config{}

err := loadCertificate(TLSConfig{}, logger, tlsCfg, mockReadFileSuccess, mockStatFileSuccess)
assert.NoError(t, err)
assert.Empty(t, tlsCfg.Certificates)
require.NoError(t, err)
require.Empty(t, tlsCfg.Certificates)
}

func TestLoadServerCA_NoPEM(t *testing.T) {
logger := zerolog.Nop()
tlsCfg := &tls.Config{}

err := loadServerCA(TLSConfig{}, logger, tlsCfg, mockReadFileSuccess, mockStatFileSuccess)
assert.NoError(t, err)
assert.Nil(t, tlsCfg.RootCAs)
require.NoError(t, err)
require.Nil(t, tlsCfg.RootCAs)
}

func TestLoadClientCA_NoPEM(t *testing.T) {
logger := zerolog.Nop()
tlsCfg := &tls.Config{}

err := loadClientCA(TLSConfig{}, logger, tlsCfg, mockReadFileSuccess, mockStatFileSuccess)
assert.NoError(t, err)
assert.Nil(t, tlsCfg.ClientCAs)
require.NoError(t, err)
require.Nil(t, tlsCfg.ClientCAs)
}

func TestNewCertPoolFromPEM_Invalid(t *testing.T) {
_, err := newCertPoolFromPEM([]byte("invalid"))
assert.Error(t, err)
require.Error(t, err)
}

func TestNewCertPoolFromPEM_Valid(t *testing.T) {
_, err := newCertPoolFromPEM([]byte(validPEM))
assert.NoError(t, err)
require.NoError(t, err)
}

0 comments on commit 891f699

Please sign in to comment.