refactor (events): QOL fix: turn off events for tests. (#3163)

This commit is contained in:
Jim authored and GitHub committed 2023-04-14 20:01:14 -04:00
1 parent 8c265bf7c1
commit a4f54cb98f
15 files changed
+196 -55

No files matched your search

+14 -3
View File
@@ -309,14 +309,20 @@ type Plugins struct {
}
// DevWorker is a Config that is used for dev mode of Boundary
// workers
func DevWorker() (*Config, error) {
// workers. Supported options: WithObservationsEnabled, WithSysEventsEnabled,
// WithAuditEventsEnabled, TestWithErrorEventsEnabled
func DevWorker(opt ...Option) (*Config, error) {
workerAuthStorageKey := DevKeyGeneration()
hclStr := fmt.Sprintf(devConfig+devWorkerExtraConfig, workerAuthStorageKey)
parsed, err := Parse(hclStr)
if err != nil {
return nil, fmt.Errorf("error parsing dev config: %w", err)
}
opts := getOpts(opt...)
parsed.Eventing.AuditEnabled = opts.withAuditEventsEnabled
parsed.Eventing.ObservationsEnabled = opts.withObservationsEnabled
parsed.Eventing.SysEventsEnabled = opts.withSysEventsEnabled
parsed.Eventing.ErrorEventsDisabled = !opts.testWithErrorEventsEnabled
return parsed, nil
}
@@ -339,7 +345,7 @@ func DevKeyGeneration() string {
// DevController is a Config that is used for dev mode of Boundary
// controllers
func DevController() (*Config, error) {
func DevController(opt ...Option) (*Config, error) {
controllerKey := DevKeyGeneration()
workerAuthKey := DevKeyGeneration()
recoveryKey := DevKeyGeneration()
@@ -353,6 +359,11 @@ func DevController() (*Config, error) {
parsed.DevControllerKey = controllerKey
parsed.DevWorkerAuthKey = workerAuthKey
parsed.DevRecoveryKey = recoveryKey
opts := getOpts(opt...)
parsed.Eventing.AuditEnabled = opts.withAuditEventsEnabled
parsed.Eventing.ObservationsEnabled = opts.withObservationsEnabled
parsed.Eventing.SysEventsEnabled = opts.withSysEventsEnabled
parsed.Eventing.ErrorEventsDisabled = !opts.testWithErrorEventsEnabled
return parsed, nil
}
+4 -2
View File
@@ -105,6 +105,9 @@ func TestDevController(t *testing.T) {
},
DevController: true,
}
exp.Eventing.ErrorEventsDisabled = true
exp.Eventing.SysEventsEnabled = false
exp.Eventing.ObservationsEnabled = false
exp.Listeners[0].RawConfig = actual.Listeners[0].RawConfig
exp.Listeners[1].RawConfig = actual.Listeners[1].RawConfig
@@ -191,11 +194,10 @@ func TestDevController(t *testing.T) {
}
func TestDevWorker(t *testing.T) {
actual, err := DevWorker()
actual, err := DevWorker(WithSysEventsEnabled(true), WithObservationsEnabled(true), TestWithErrorEventsEnabled(t, true))
if err != nil {
t.Fatal(err)
}
exp := &Config{
Eventing: event.DefaultEventerConfig(),
SharedConfig: &configutil.SharedConfig{
+58
View File
@@ -0,0 +1,58 @@
// Copyright (c) HashiCorp, Inc.
package config
import "testing"
// getOpts - iterate the inbound Options and return a struct
func getOpts(opt ...Option) options {
opts := getDefaultOptions()
for _, o := range opt {
o(&opts)
}
return opts
}
// Option - how Options are passed as arguments
type Option func(*options)
// options = how options are represented
type options struct {
withSysEventsEnabled bool
withAuditEventsEnabled bool
withObservationsEnabled bool
testWithErrorEventsEnabled bool
}
func getDefaultOptions() options {
return options{}
}
// WithSysEventsEnabled provides an option for enabling system events
func WithSysEventsEnabled(enable bool) Option {
return func(o *options) {
o.withSysEventsEnabled = enable
}
}
// WithAuditEventsEnabled provides an option for enabling audit events
func WithAuditEventsEnabled(enable bool) Option {
return func(o *options) {
o.withAuditEventsEnabled = enable
}
}
// WithObservationsEnabled provides an option for enabling observation events
func WithObservationsEnabled(enable bool) Option {
return func(o *options) {
o.withObservationsEnabled = enable
}
}
// TestWithErrorEventsEnabled provides an option for enabling error events
// during tests.
func TestWithErrorEventsEnabled(_ testing.TB, enable bool) Option {
return func(o *options) {
o.testWithErrorEventsEnabled = enable
}
}