Reorganize logic to allow a poison pill (#2317)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2022-08-03 19:29:17 -04:00
1 parent 9b6b767edb
commit 8800ec9c94
18 files changed
+469 -214

No files matched your search

+19 -12
View File
@@ -4,6 +4,7 @@ import (
"context"
"os"
"path/filepath"
"sync"
"testing"
"github.com/google/go-cmp/cmp"
@@ -24,10 +25,12 @@ func TestController_New(t *testing.T) {
testCtx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
tc := &TestController{
t: t,
ctx: ctx,
cancel: cancel,
opts: nil,
t: t,
ctx: ctx,
cancel: cancel,
opts: nil,
shutdownDoneCh: make(chan struct{}),
shutdownOnce: new(sync.Once),
}
// TestControllerConfig(...) will create initial scopes
@@ -207,10 +210,12 @@ func TestControllerNewListenerConfig(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
tc := &TestController{
t: t,
ctx: ctx,
cancel: cancel,
opts: nil,
t: t,
ctx: ctx,
cancel: cancel,
opts: nil,
shutdownDoneCh: make(chan struct{}),
shutdownOnce: new(sync.Once),
}
conf := TestControllerConfig(t, ctx, tc, nil)
conf.Listeners = tt.listeners
@@ -234,10 +239,12 @@ func TestController_NewPluginsConfig(t *testing.T) {
testCtx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
tc := &TestController{
t: t,
ctx: ctx,
cancel: cancel,
opts: nil,
t: t,
ctx: ctx,
cancel: cancel,
opts: nil,
shutdownDoneCh: make(chan struct{}),
shutdownOnce: new(sync.Once),
}
initialConfig, err := config.DevController()
+4 -16
View File
@@ -12,13 +12,10 @@ import (
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/daemon/cluster/handlers"
"github.com/hashicorp/boundary/internal/daemon/common"
"github.com/hashicorp/boundary/internal/daemon/controller/internal/metric"
"github.com/hashicorp/boundary/internal/errors"
pbs "github.com/hashicorp/boundary/internal/gen/controller/servers/services"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/nodeenrollment/multihop"
nodeenet "github.com/hashicorp/nodeenrollment/net"
"github.com/hashicorp/nodeenrollment/protocol"
"google.golang.org/grpc"
@@ -212,20 +209,11 @@ func (c *Controller) configureForCluster(ln *base.ServerListener) (func(), error
),
)
workerService := handlers.NewWorkerServiceServer(c.ServersRepoFn, c.SessionRepoFn, c.ConnectionRepoFn,
c.workerStatusUpdateTimes, c.kms)
pbs.RegisterServerCoordinationServiceServer(workerServer, workerService)
pbs.RegisterSessionServiceServer(workerServer, workerService)
multihopService, err := handlers.NewMultihopServiceServer(
workerAuthStorage,
true,
nil,
)
if err != nil {
return nil, fmt.Errorf("%s: error creating multihop service handler: %w", op, err)
for _, fn := range controllerGrpcServiceRegistrationFunctions {
if err := fn(c.baseContext, c, workerServer); err != nil {
return nil, err
}
}
multihop.RegisterMultihopServiceServer(workerServer, multihopService)
metric.InitializeClusterCollectors(c.conf.PrometheusRegisterer, workerServer)
+9 -1
View File
@@ -16,6 +16,7 @@ import (
"net/http"
"os"
"strconv"
"sync"
"testing"
"time"
@@ -235,7 +236,14 @@ func TestStartListeners(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
tc := &TestController{t: t, ctx: ctx, cancel: cancel, opts: &TestControllerOpts{}}
tc := &TestController{
t: t,
ctx: ctx,
cancel: cancel,
shutdownDoneCh: make(chan struct{}),
shutdownOnce: new(sync.Once),
opts: new(TestControllerOpts),
}
t.Cleanup(func() { tc.Shutdown() })
conf := TestControllerConfig(t, ctx, tc, nil)
@@ -0,0 +1,87 @@
package controller
import (
"context"
"fmt"
"github.com/hashicorp/boundary/internal/daemon/cluster/handlers"
pbs "github.com/hashicorp/boundary/internal/gen/controller/servers/services"
"github.com/hashicorp/nodeenrollment"
"github.com/hashicorp/nodeenrollment/multihop"
"google.golang.org/grpc"
)
var controllerGrpcServiceRegistrationFunctions []func(context.Context, *Controller, *grpc.Server) error
func init() {
controllerGrpcServiceRegistrationFunctions = append(controllerGrpcServiceRegistrationFunctions,
registerControllerServerCoordinationService,
registerControllerSessionService,
registerControllerMultihopService,
)
}
func registerControllerServerCoordinationService(ctx context.Context, c *Controller, server *grpc.Server) error {
const op = "controller.registerControllerServerCoordinationService"
switch {
case nodeenrollment.IsNil(ctx):
return fmt.Errorf("%s: context is nil", op)
case c == nil:
return fmt.Errorf("%s: controller is nil", op)
case server == nil:
return fmt.Errorf("%s: server is nil", op)
}
workerService := handlers.NewWorkerServiceServer(c.ServersRepoFn, c.SessionRepoFn, c.ConnectionRepoFn,
c.workerStatusUpdateTimes, c.kms)
pbs.RegisterServerCoordinationServiceServer(server, workerService)
return nil
}
func registerControllerSessionService(ctx context.Context, c *Controller, server *grpc.Server) error {
const op = "controller.registerControllerSessionService"
switch {
case nodeenrollment.IsNil(ctx):
return fmt.Errorf("%s: context is nil", op)
case c == nil:
return fmt.Errorf("%s: controller is nil", op)
case server == nil:
return fmt.Errorf("%s: server is nil", op)
}
workerService := handlers.NewWorkerServiceServer(c.ServersRepoFn, c.SessionRepoFn, c.ConnectionRepoFn,
c.workerStatusUpdateTimes, c.kms)
pbs.RegisterSessionServiceServer(server, workerService)
return nil
}
func registerControllerMultihopService(ctx context.Context, c *Controller, server *grpc.Server) error {
const op = "controller.registerControllerMultihopService"
switch {
case nodeenrollment.IsNil(ctx):
return fmt.Errorf("%s: context is nil", op)
case c == nil:
return fmt.Errorf("%s: controller is nil", op)
case server == nil:
return fmt.Errorf("%s: server is nil", op)
}
workerAuthStorage, err := c.WorkerAuthRepoStorageFn()
if err != nil {
return fmt.Errorf("%s: error fetching worker auth storage: %w", op, err)
}
multihopService, err := handlers.NewMultihopServiceServer(
workerAuthStorage,
true,
nil,
)
if err != nil {
return fmt.Errorf("%s: error creating multihop service handler: %w", op, err)
}
multihop.RegisterMultihopServiceServer(server, multihopService)
return nil
}
+56 -32
View File
@@ -9,6 +9,7 @@ import (
"os"
"strconv"
"strings"
"sync"
"testing"
"time"
@@ -58,16 +59,18 @@ const (
// fully-programmatic controller for tests. Error checking (for instance, for
// valid config) is not stringent at the moment.
type TestController struct {
b *base.Server
c *Controller
t testing.TB
apiAddrs []string // The address the Controller API is listening on
clusterAddrs []string
client *api.Client
ctx context.Context
cancel context.CancelFunc
name string
opts *TestControllerOpts
b *base.Server
c *Controller
t testing.TB
apiAddrs []string // The address the Controller API is listening on
clusterAddrs []string
client *api.Client
ctx context.Context
cancel context.CancelFunc
name string
opts *TestControllerOpts
shutdownDoneCh chan struct{}
shutdownOnce *sync.Once
}
// Server returns the underlying base server
@@ -282,29 +285,33 @@ func (tc *TestController) buildClient() {
// Shutdown runs any cleanup functions; be sure to run this after your test is
// done
func (tc *TestController) Shutdown() {
if tc.b != nil {
close(tc.b.ShutdownCh)
}
tc.cancel()
if tc.c != nil {
if err := tc.c.Shutdown(); err != nil {
tc.t.Error(err)
tc.shutdownOnce.Do(func() {
if tc.b != nil {
close(tc.b.ShutdownCh)
}
}
if tc.b != nil {
if err := tc.b.RunShutdownFuncs(); err != nil {
tc.t.Error(err)
tc.cancel()
if tc.c != nil {
if err := tc.c.Shutdown(); err != nil {
tc.t.Error(err)
}
}
if !tc.opts.DisableDatabaseDestruction {
if tc.b.DestroyDevDatabase(tc.ctx) != nil {
if err := tc.b.DestroyDevDatabase(tc.ctx); err != nil {
tc.t.Error(err)
if tc.b != nil {
if err := tc.b.RunShutdownFuncs(); err != nil {
tc.t.Error(err)
}
if !tc.opts.DisableDatabaseDestruction {
if tc.b.DestroyDevDatabase(tc.ctx) != nil {
if err := tc.b.DestroyDevDatabase(tc.ctx); err != nil {
tc.t.Error(err)
}
}
}
}
}
close(tc.shutdownDoneCh)
})
}
type TestControllerOpts struct {
@@ -434,10 +441,12 @@ func NewTestController(t testing.TB, opts *TestControllerOpts) *TestController {
}
tc := &TestController{
t: t,
ctx: ctx,
cancel: cancel,
opts: opts,
t: t,
ctx: ctx,
cancel: cancel,
opts: opts,
shutdownDoneCh: make(chan struct{}),
shutdownOnce: new(sync.Once),
}
conf := TestControllerConfig(t, ctx, tc, opts)
@@ -450,6 +459,21 @@ func NewTestController(t testing.TB, opts *TestControllerOpts) *TestController {
tc.buildClient()
// The real server functions will listen for shutdown cues and act so mimic
// that here, and ensure that channels get drained
go func() {
for {
select {
case <-tc.b.ShutdownCh:
tc.Shutdown()
case <-tc.b.ServerSideShutdownCh:
tc.Shutdown()
case <-tc.shutdownDoneCh:
return
}
}
}()
if !opts.DisableAutoStart {
if err := tc.c.Start(); err != nil {
tc.Shutdown()