Skip to content

Commit

Permalink
ci: limit gotestsum to circle ci
Browse files Browse the repository at this point in the history
Part 2 of breaking up #12255

This PR makes it so gotestsum is invoked only in CircleCI. Also the
HCLogger(t) is plumbed more correctly in TestServer and TestAgent so
that they respect NOMAD_TEST_LOG_LEVEL.

The reason for these is we'll want to disable logging in GHA,
where spamming the disk with logs really drags performance.
  • Loading branch information
shoenig committed Mar 18, 2022
1 parent 4bf0dec commit a44c55a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ executors:
GOBIN: c:\gopath\bin
GOTESTSUM_PATH: c:\tmp\test-reports
GOLANG_VERSION: 1.17.5
GOTESTSUM_VERSION: 0.4.2
GOTESTSUM_VERSION: 1.7.0
VAULT_VERSION: 1.4.1

workflows:
Expand Down
12 changes: 5 additions & 7 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ ifndef NOMAD_NO_UI
GO_TAGS := ui $(GO_TAGS)
endif

ifeq ($(CIRCLECI),true)
GO_TEST_CMD = $(if $(shell command -v gotestsum 2>/dev/null),gotestsum --,go test)
else
GO_TEST_CMD = go test
endif

ifeq ($(origin GOTEST_PKGS_EXCLUDE), undefined)
GOTEST_PKGS ?= "./..."
Expand All @@ -49,12 +53,6 @@ LAST_RELEASE ?= v1.2.6

default: help

ifeq ($(CI),true)
$(info Running in a CI environment, verbose mode is disabled)
else
VERBOSE="true"
endif

ifeq (Linux,$(THIS_OS))
ALL_TARGETS = linux_386 \
linux_amd64 \
Expand Down Expand Up @@ -131,7 +129,7 @@ deps: ## Install build and development dependencies
go install github.com/hashicorp/go-bindata/go-bindata@bf7910af899725e4938903fb32048c7c0b15f12e
go install github.com/elazarl/go-bindata-assetfs/go-bindata-assetfs@234c15e7648ff35458026de92b34c637bae5e6f7
go install github.com/a8m/tree/cmd/tree@fce18e2a750ea4e7f53ee706b1c3d9cbb22de79c
go install gotest.tools/gotestsum@v0.4.2
go install gotest.tools/gotestsum@v1.7.0
go install github.com/hashicorp/hcl/v2/cmd/hclfmt@v2.5.1
go install github.com/golang/protobuf/protoc-gen-go@v1.3.4
go install github.com/hashicorp/go-msgpack/codec/codecgen@v1.1.5
Expand Down
9 changes: 9 additions & 0 deletions command/agent/testagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
metrics "github.com/armon/go-metrics"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/api"
client "github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/client/fingerprint"
"github.com/hashicorp/nomad/helper/freeport"
"github.com/hashicorp/nomad/helper/testlog"
Expand Down Expand Up @@ -357,6 +358,14 @@ func (a *TestAgent) config() *Config {
config := nomad.DefaultConfig()
conf.NomadConfig = config

// Setup client config
conf.ClientConfig = client.DefaultConfig()

logger := testlog.HCLogger(a.T)
conf.LogLevel = testlog.HCLoggerTestLevel().String()
conf.NomadConfig.Logger = logger
conf.ClientConfig.Logger = logger

// Set the name
conf.NodeName = a.Name

Expand Down
36 changes: 30 additions & 6 deletions helper/testlog/testlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package testlog

import (
"bytes"
"fmt"
"io"
"log"
"os"
Expand Down Expand Up @@ -39,25 +40,48 @@ func WithPrefix(t LogPrinter, prefix string) *log.Logger {
return New(t, prefix, log.Lmicroseconds)
}

// Logger returns a new test logger with the Lmicroseconds flag set and no
// prefix.
// Logger returns a new test logger with the Lmicroseconds flag set and no prefix.
//
// Note: only use this where HCLogger cannot be used (i.e. RPC yamux configuration).
func Logger(t LogPrinter) *log.Logger {
return WithPrefix(t, "")
}

//HCLogger returns a new test hc-logger.
// HCLogger returns a new test hc-logger.
//
// Default log level is TRACE. Set NOMAD_TEST_LOG_LEVEL for custom log level.
func HCLogger(t LogPrinter) hclog.InterceptLogger {
logger, _ := HCLoggerNode(t, -1)
return logger
}

// HCLoggerTestLevel returns the level in which hc log should emit logs.
//
// Default log level is TRACE. Set NOMAD_TEST_LOG_LEVEL for custom log level.
func HCLoggerTestLevel() hclog.Level {
level := hclog.Trace
envLogLevel := os.Getenv("NOMAD_TEST_LOG_LEVEL")
if envLogLevel != "" {
level = hclog.LevelFromString(envLogLevel)
}
return level
}

// HCLoggerNode returns a new hc-logger, but with a prefix indicating the node number
// on each log line. Useful for TestServer in tests with more than one server.
//
// Default log level is TRACE. Set NOMAD_TEST_LOG_LEVEL for custom log level.
func HCLoggerNode(t LogPrinter, node int32) (hclog.InterceptLogger, io.Writer) {
var output io.Writer = os.Stderr
if node > -1 {
output = NewPrefixWriter(t, fmt.Sprintf("node-%03d", node))
}
opts := &hclog.LoggerOptions{
Level: level,
Output: os.Stderr,
Level: HCLoggerTestLevel(),
Output: output,
IncludeLocation: true,
}
return hclog.NewInterceptLogger(opts)
return hclog.NewInterceptLogger(opts), output
}

type prefixStderr struct {
Expand Down
30 changes: 8 additions & 22 deletions nomad/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ import (
"fmt"
"math/rand"
"net"
"os"
"sync/atomic"
"testing"
"time"

testing "github.com/mitchellh/go-testing-interface"
"github.com/pkg/errors"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/command/agent/consul"
"github.com/hashicorp/nomad/helper/freeport"
"github.com/hashicorp/nomad/helper/pluginutils/catalog"
Expand All @@ -20,13 +16,14 @@ import (
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/version"
"github.com/pkg/errors"
)

var (
nodeNumber uint32 = 0
nodeNumber int32 = 0
)

func TestACLServer(t testing.T, cb func(*Config)) (*Server, *structs.ACLToken, func()) {
func TestACLServer(t *testing.T, cb func(*Config)) (*Server, *structs.ACLToken, func()) {
server, cleanup := TestServer(t, func(c *Config) {
c.ACLEnabled = true
if cb != nil {
Expand All @@ -41,33 +38,22 @@ func TestACLServer(t testing.T, cb func(*Config)) (*Server, *structs.ACLToken, f
return server, token, cleanup
}

func TestServer(t testing.T, cb func(*Config)) (*Server, func()) {
func TestServer(t *testing.T, cb func(*Config)) (*Server, func()) {
// Setup the default settings
config := DefaultConfig()

// Setup default enterprise-specific settings, including license
defaultEnterpriseTestConfig(config)

config.Logger = testlog.HCLogger(t)
config.Build = version.Version + "+unittest"
config.DevMode = true
config.EnableEventBroker = true
config.BootstrapExpect = 1
nodeNum := atomic.AddUint32(&nodeNumber, 1)
nodeNum := atomic.AddInt32(&nodeNumber, 1)
config.NodeName = fmt.Sprintf("nomad-%03d", nodeNum)

// configure logger
level := hclog.Trace
if envLogLevel := os.Getenv("NOMAD_TEST_LOG_LEVEL"); envLogLevel != "" {
level = hclog.LevelFromString(envLogLevel)
}
opts := &hclog.LoggerOptions{
Level: level,
Output: testlog.NewPrefixWriter(t, config.NodeName+" "),
IncludeLocation: true,
}
config.Logger = hclog.NewInterceptLogger(opts)
config.LogOutput = opts.Output
config.Logger, config.LogOutput = testlog.HCLoggerNode(t, nodeNum)

// Tighten the Serf timing
config.SerfConfig.MemberlistConfig.BindAddr = "127.0.0.1"
Expand Down Expand Up @@ -168,7 +154,7 @@ func TestServer(t testing.T, cb func(*Config)) (*Server, func()) {
return nil, nil
}

func TestJoin(t testing.T, servers ...*Server) {
func TestJoin(t *testing.T, servers ...*Server) {
for i := 0; i < len(servers)-1; i++ {
addr := fmt.Sprintf("127.0.0.1:%d",
servers[i].config.SerfConfig.MemberlistConfig.BindPort)
Expand Down

0 comments on commit a44c55a

Please sign in to comment.