agent: fix two data race in agent tests
The LogOutput io.Writer used by TestAgent must allow concurrent reads and writes, and a bytes.Buffer does not allow this. The bytes.Buffer must be wrapped with a lock to make this safe.
This commit is contained in:
parent
ff26294d63
commit
2946e42a9e
|
@ -18,6 +18,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -3729,10 +3730,27 @@ func TestAgent_SecurityChecks(t *testing.T) {
|
||||||
defer a.Shutdown()
|
defer a.Shutdown()
|
||||||
|
|
||||||
data := make([]byte, 0, 8192)
|
data := make([]byte, 0, 8192)
|
||||||
bytesBuffer := bytes.NewBuffer(data)
|
buf := &syncBuffer{b: bytes.NewBuffer(data)}
|
||||||
a.LogOutput = bytesBuffer
|
a.LogOutput = buf
|
||||||
assert.NoError(t, a.Start(t))
|
assert.NoError(t, a.Start(t))
|
||||||
assert.Contains(t, bytesBuffer.String(), "using enable-script-checks without ACLs and without allow_write_http_from is DANGEROUS")
|
assert.Contains(t, buf.String(), "using enable-script-checks without ACLs and without allow_write_http_from is DANGEROUS")
|
||||||
|
}
|
||||||
|
|
||||||
|
type syncBuffer struct {
|
||||||
|
lock sync.RWMutex
|
||||||
|
b *bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *syncBuffer) Write(data []byte) (int, error) {
|
||||||
|
b.lock.Lock()
|
||||||
|
defer b.lock.Unlock()
|
||||||
|
return b.b.Write(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *syncBuffer) String() string {
|
||||||
|
b.lock.Lock()
|
||||||
|
defer b.lock.Unlock()
|
||||||
|
return b.b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAgent_ReloadConfigOutgoingRPCConfig(t *testing.T) {
|
func TestAgent_ReloadConfigOutgoingRPCConfig(t *testing.T) {
|
||||||
|
|
|
@ -20,6 +20,11 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/NYTimes/gziphandler"
|
"github.com/NYTimes/gziphandler"
|
||||||
|
cleanhttp "github.com/hashicorp/go-cleanhttp"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"golang.org/x/net/http2"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/agent/config"
|
"github.com/hashicorp/consul/agent/config"
|
||||||
"github.com/hashicorp/consul/agent/structs"
|
"github.com/hashicorp/consul/agent/structs"
|
||||||
tokenStore "github.com/hashicorp/consul/agent/token"
|
tokenStore "github.com/hashicorp/consul/agent/token"
|
||||||
|
@ -27,10 +32,6 @@ import (
|
||||||
"github.com/hashicorp/consul/sdk/testutil"
|
"github.com/hashicorp/consul/sdk/testutil"
|
||||||
"github.com/hashicorp/consul/sdk/testutil/retry"
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
||||||
"github.com/hashicorp/consul/testrpc"
|
"github.com/hashicorp/consul/testrpc"
|
||||||
cleanhttp "github.com/hashicorp/go-cleanhttp"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"golang.org/x/net/http2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHTTPServer_UnixSocket(t *testing.T) {
|
func TestHTTPServer_UnixSocket(t *testing.T) {
|
||||||
|
@ -632,7 +633,7 @@ func TestHTTP_wrap_obfuscateLog(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
buf := new(bytes.Buffer)
|
buf := &syncBuffer{b: new(bytes.Buffer)}
|
||||||
a := StartTestAgent(t, TestAgent{LogOutput: buf})
|
a := StartTestAgent(t, TestAgent{LogOutput: buf})
|
||||||
defer a.Shutdown()
|
defer a.Shutdown()
|
||||||
|
|
||||||
|
|
|
@ -53,6 +53,8 @@ type TestAgent struct {
|
||||||
Config *config.RuntimeConfig
|
Config *config.RuntimeConfig
|
||||||
|
|
||||||
// LogOutput is the sink for the logs. If nil, logs are written to os.Stderr.
|
// LogOutput is the sink for the logs. If nil, logs are written to os.Stderr.
|
||||||
|
// The io.Writer must allow concurrent reads and writes. Note that
|
||||||
|
// bytes.Buffer is not safe for concurrent reads and writes.
|
||||||
LogOutput io.Writer
|
LogOutput io.Writer
|
||||||
|
|
||||||
// DataDir may be set to a directory which exists. If is it not set,
|
// DataDir may be set to a directory which exists. If is it not set,
|
||||||
|
|
Loading…
Reference in New Issue