feature (events): add TestWithoutEventing(t) (#2137)
This commit is contained in:
6 files changed
+171
-5
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -373,6 +374,11 @@ type TestControllerOpts struct {
|
||||
// If true, the controller will not be started
|
||||
DisableAutoStart bool
|
||||
|
||||
// DisableEventing, if true the test controller will not create events
|
||||
// You must not run the test in parallel (no calls to t.Parallel) since the
|
||||
// this option relies on modifying the system wide default eventer.
|
||||
DisableEventing bool
|
||||
|
||||
// DisableAuthorizationFailures will still cause authz checks to be
|
||||
// performed but they won't cause 403 Forbidden. Useful for API-level
|
||||
// testing to avoid a lot of faff.
|
||||
@@ -543,8 +549,32 @@ func TestControllerConfig(t testing.TB, ctx context.Context, tc *TestController,
|
||||
}
|
||||
opts.Config.Controller.SchedulerRunJobInterval = opts.SchedulerRunJobInterval
|
||||
|
||||
if err := tc.b.SetupEventing(tc.b.Logger, tc.b.StderrLock, opts.Config.Controller.Name, base.WithEventerConfig(opts.Config.Eventing)); err != nil {
|
||||
t.Fatal(err)
|
||||
switch {
|
||||
case opts.DisableEventing:
|
||||
opts.Config.Eventing = &event.EventerConfig{
|
||||
AuditEnabled: false,
|
||||
ObservationsEnabled: false,
|
||||
SysEventsEnabled: false,
|
||||
}
|
||||
testLogger := hclog.New(&hclog.LoggerOptions{
|
||||
Mutex: tc.b.StderrLock,
|
||||
Output: io.Discard,
|
||||
})
|
||||
e, err := event.NewEventer(
|
||||
testLogger,
|
||||
tc.b.StderrLock,
|
||||
opts.Config.Controller.Name,
|
||||
*opts.Config.Eventing,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tc.b.Eventer = e
|
||||
event.TestWithoutEventing(t) // this ensures the sys eventer will also stop eventing
|
||||
default:
|
||||
if err := tc.b.SetupEventing(tc.b.Logger, tc.b.StderrLock, opts.Config.Controller.Name, base.WithEventerConfig(opts.Config.Eventing)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize status grace period
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_TestController(t *testing.T) {
|
||||
@@ -17,4 +22,44 @@ func Test_TestController(t *testing.T) {
|
||||
defer tc1.Shutdown()
|
||||
defer tc2.Shutdown()
|
||||
})
|
||||
t.Run("controller-without-eventing", func(t *testing.T) {
|
||||
const op = "Test_TestWithoutEventing"
|
||||
assert := assert.New(t)
|
||||
|
||||
// this isn't the best solution for capturing stdout but it works for now...
|
||||
captureFn := func(fn func()) string {
|
||||
old := os.Stdout
|
||||
defer func() {
|
||||
os.Stderr = old
|
||||
}()
|
||||
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stderr = w
|
||||
|
||||
{
|
||||
fn()
|
||||
}
|
||||
|
||||
outC := make(chan string)
|
||||
// copy the output in a separate goroutine so writing to stderr can't block indefinitely
|
||||
go func() {
|
||||
var buf bytes.Buffer
|
||||
io.Copy(&buf, r)
|
||||
outC <- buf.String()
|
||||
}()
|
||||
|
||||
// back to normal state
|
||||
w.Close()
|
||||
return <-outC
|
||||
}
|
||||
|
||||
assert.Empty(captureFn(func() {
|
||||
tc := NewTestController(t, &TestControllerOpts{DisableEventing: true})
|
||||
defer tc.Shutdown()
|
||||
}))
|
||||
assert.NotEmpty(captureFn(func() {
|
||||
tc := NewTestController(t, nil)
|
||||
defer tc.Shutdown()
|
||||
}))
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user