Skip to content

Commit

Permalink
Merge pull request #88 from solarwinds/misc-test-fixes
Browse files Browse the repository at this point in the history
Misc test fixes
  • Loading branch information
tammy-baylis-swi authored May 9, 2024
2 parents 1eb41c4 + ec9002a commit 37e9560
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ vet:
@go vet -composites=false ./... && echo "Go vet analysis passed."

clean:
@go clean -testcache ./...
@go clean -testcache

sure: clean test examples contrib vet
sure: clean test examples vet

.PHONY: certgen test removecert examples vet contrib clean
.PHONY: certgen test removecert examples vet clean
18 changes: 12 additions & 6 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ package config

import (
"fmt"
"github.com/solarwinds/apm-go/internal/log"
"github.com/solarwinds/apm-go/internal/utils"
"io"
"os"
"path/filepath"
"strings"
"testing"

"github.com/solarwinds/apm-go/internal/log"
"github.com/solarwinds/apm-go/internal/utils"
"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -86,21 +88,21 @@ func TestLoadConfig(t *testing.T) {

func TestConfig_HasLocalSamplingConfig(t *testing.T) {
// Set tracing mode
_ = os.Setenv(envSolarWindsAPMTracingMode, "disabled")
require.NoError(t, os.Setenv(envSolarWindsAPMTracingMode, "disabled"))
Load()
assert.True(t, SamplingConfigured())
assert.Equal(t, "disabled", string(GetTracingMode()))
assert.Equal(t, ToInteger(getFieldDefaultValue(&SamplingConfig{}, "SampleRate")), GetSampleRate())

// No local sampling config
_ = os.Unsetenv(envSolarWindsAPMTracingMode)
require.NoError(t, os.Unsetenv(envSolarWindsAPMTracingMode))
Load()
assert.False(t, SamplingConfigured())
assert.Equal(t, getFieldDefaultValue(&SamplingConfig{}, "TracingMode"), string(GetTracingMode()))
assert.Equal(t, ToInteger(getFieldDefaultValue(&SamplingConfig{}, "SampleRate")), GetSampleRate())

// Set sample rate to 10000
_ = os.Setenv(envSolarWindsAPMSampleRate, "10000")
require.NoError(t, os.Setenv(envSolarWindsAPMSampleRate, "10000"))
Load()
assert.True(t, SamplingConfigured())
assert.Equal(t, getFieldDefaultValue(&SamplingConfig{}, "TracingMode"), string(GetTracingMode()))
Expand Down Expand Up @@ -453,18 +455,22 @@ func TestInvalidConfigFile(t *testing.T) {
writers = append(writers, os.Stderr)

log.SetOutput(io.MultiWriter(writers...))
oldLevel := log.Level()
log.SetLevel(log.INFO)

defer func() {
log.SetOutput(os.Stderr)
log.SetLevel(oldLevel)
}()

ClearEnvs()
os.Setenv("SW_APM_SERVICE_KEY", "ae38315f6116585d64d82ec2455aa3ec61e02fee25d286f74ace9e4fea189217:go")
os.Setenv("SW_APM_CONFIG_FILE", "/tmp/solarwinds-apm-config.json")
_ = os.WriteFile("/tmp/solarwinds-apm-config.json", []byte("hello"), 0644)
require.NoError(t, os.WriteFile("/tmp/solarwinds-apm-config.json", []byte("hello"), 0644))

_ = NewConfig()
assert.Contains(t, buf.String(), ErrUnsupportedFormat.Error())
// OK to ignore this error because we're just making sure this file doesn't exist for the logic below
_ = os.Remove("/tmp/file-not-exist.yaml")

buf.Reset()
Expand Down
10 changes: 7 additions & 3 deletions internal/host/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
package host

import (
"github.com/solarwinds/apm-go/internal/config"
"github.com/solarwinds/apm-go/internal/log"
"github.com/solarwinds/apm-go/internal/utils"
"io"
"net"
"os"
"runtime"
"strings"
"testing"

"github.com/solarwinds/apm-go/internal/config"
"github.com/solarwinds/apm-go/internal/log"
"github.com/solarwinds/apm-go/internal/utils"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -78,9 +79,12 @@ func TestConfiguredHostname(t *testing.T) {
writers = append(writers, &buf)

log.SetOutput(io.MultiWriter(writers...))
oldLevel := log.Level()
log.SetLevel(log.INFO)

defer func() {
log.SetOutput(os.Stderr)
log.SetLevel(oldLevel)
}()

var old string
Expand Down

0 comments on commit 37e9560

Please sign in to comment.