Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unit tests on Windows #983

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions config/configgrpc/configgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,14 @@ func GrpcSettingsToDialOptions(settings GRPCSettings) ([]grpc.DialOption, error)

// LoadTLSConfig loads TLS certificates and returns a tls.Config.
func (c TLSConfig) LoadTLSConfig() (*tls.Config, error) {
certPool, err := c.loadCertPool()
if err != nil {
return nil, fmt.Errorf("failed to load CA CertPool: %w", err)
var err error
var certPool *x509.CertPool
if len(c.CaCert) != 0 {
// setup user specified truststore
certPool, err = c.loadCert(c.CaCert)
if err != nil {
return nil, fmt.Errorf("failed to load CA CertPool: %w", err)
}
}
// #nosec G402
tlsCfg := &tls.Config{
Expand All @@ -157,20 +162,6 @@ func (c TLSConfig) LoadTLSConfig() (*tls.Config, error) {
return tlsCfg, nil
}

var systemCertPool = x509.SystemCertPool // to allow overriding in unit test

func (c TLSConfig) loadCertPool() (*x509.CertPool, error) {
if len(c.CaCert) == 0 { // no truststore given, use SystemCertPool
certPool, err := systemCertPool()
if err != nil {
return nil, fmt.Errorf("failed to load SystemCertPool: %w", err)
}
return certPool, nil
}
// setup user specified truststore
return c.loadCert(c.CaCert)
}

func (c TLSConfig) loadCert(caPath string) (*x509.CertPool, error) {
caPEM, err := ioutil.ReadFile(filepath.Clean(caPath))
if err != nil {
Expand Down
26 changes: 4 additions & 22 deletions config/configgrpc/configgrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package configgrpc

import (
"crypto/x509"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -46,7 +44,7 @@ func TestInvalidPemFile(t *testing.T) {
err string
}{
{
err: "open /doesnt/exist: no such file or directory",
err: "^open /doesnt/exist:",
settings: GRPCSettings{
Headers: nil,
Endpoint: "",
Expand All @@ -60,7 +58,7 @@ func TestInvalidPemFile(t *testing.T) {
},
},
{
err: "failed to load TLS config: failed to load CA CertPool: failed to load CA /doesnt/exist: open /doesnt/exist: no such file or directory",
err: "^failed to load TLS config: failed to load CA CertPool: failed to load CA /doesnt/exist:",
settings: GRPCSettings{
Headers: nil,
Endpoint: "",
Expand All @@ -74,7 +72,7 @@ func TestInvalidPemFile(t *testing.T) {
},
},
{
err: "failed to load TLS config: for client auth via TLS, either both client certificate and key must be supplied, or neither",
err: "^failed to load TLS config: for client auth via TLS, either both client certificate and key must be supplied, or neither$",
settings: GRPCSettings{
Headers: nil,
Endpoint: "",
Expand All @@ -91,7 +89,7 @@ func TestInvalidPemFile(t *testing.T) {
for _, test := range tests {
t.Run(test.err, func(t *testing.T) {
_, err := GrpcSettingsToDialOptions(test.settings)
assert.EqualError(t, err, test.err)
assert.Regexp(t, test.err, err)
})
}
}
Expand All @@ -117,19 +115,12 @@ func TestOptionsToConfig(t *testing.T) {
tests := []struct {
name string
options TLSConfig
fakeSysPool bool
expectError string
}{
{
name: "should load system CA",
options: TLSConfig{CaCert: ""},
},
{
name: "should fail with fake system CA",
fakeSysPool: true,
options: TLSConfig{CaCert: ""},
expectError: "fake system pool",
},
bogdandrutu marked this conversation as resolved.
Show resolved Hide resolved
{
name: "should load custom CA",
options: TLSConfig{CaCert: "testdata/testCA.pem"},
Expand Down Expand Up @@ -210,15 +201,6 @@ func TestOptionsToConfig(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.fakeSysPool {
saveSystemCertPool := systemCertPool
systemCertPool = func() (*x509.CertPool, error) {
return nil, fmt.Errorf("fake system pool")
}
defer func() {
systemCertPool = saveSystemCertPool
}()
}
cfg, err := test.options.LoadTLSConfig()
if test.expectError != "" {
require.Error(t, err)
Expand Down
12 changes: 4 additions & 8 deletions exporter/opencensusexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package opencensusexporter

import (
"crypto/x509"
"fmt"

"contrib.go.opencensus.io/exporter/ocagent"
Expand Down Expand Up @@ -95,16 +94,13 @@ func (f *Factory) OCAgentOptions(logger *zap.Logger, ocac *Config) ([]ocagent.Ex
}
opts = append(opts, ocagent.WithTLSCredentials(creds))
} else if ocac.TLSConfig.UseSecure {
certPool, err := x509.SystemCertPool()
tlsConf, err := ocac.TLSConfig.LoadTLSConfig()
if err != nil {
return nil, &ocExporterError{
code: errUnableToGetTLSCreds,
msg: fmt.Sprintf(
"OpenCensus exporter unable to read certificates from system pool: %v", err),
}
return nil, fmt.Errorf("OpenCensus exporter failed to load TLS config: %w", err)
}
creds := credentials.NewClientTLSFromCert(certPool, "")
creds := credentials.NewTLS(tlsConf)
opts = append(opts, ocagent.WithTLSCredentials(creds))

} else {
opts = append(opts, ocagent.WithInsecure())
}
Expand Down
5 changes: 2 additions & 3 deletions receiver/opencensusreceiver/opencensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,9 @@ func TestStartWithoutConsumersShouldFail(t *testing.T) {
func tempSocketName(t *testing.T) string {
tmpfile, err := ioutil.TempFile("", "sock")
require.NoError(t, err)
require.NoError(t, tmpfile.Close())
socket := tmpfile.Name()
err = os.Remove(socket)
require.NoError(t, err)

require.NoError(t, os.Remove(socket))
return socket
}

Expand Down
5 changes: 2 additions & 3 deletions receiver/otlpreceiver/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,9 @@ func TestStartWithoutConsumersShouldFail(t *testing.T) {
func tempSocketName(t *testing.T) string {
tmpfile, err := ioutil.TempFile("", "sock")
require.NoError(t, err)
require.NoError(t, tmpfile.Close())
socket := tmpfile.Name()
err = os.Remove(socket)
require.NoError(t, err)

require.NoError(t, os.Remove(socket))
return socket
}

Expand Down