worker: ensure connections are closed in local state on controller fail (#1369)

This backports some test work that is being added in #1340 to assert
that the worker is actually marking connections closed in its *local*
state on a failure to contact the controller. This was missed in #1357
during some late-stage refactoring.

Summary of the changes:

* If we can't actually contact the controller during connection closure
to mark the connections as closed, we actually construct a "fake"
response that just simply includes a closed state for all connections
requested. This is then used to update the local connection state.

* Added a 3 second timeout to the CloseConnection request to the
controller to ensure the call to close connections completed in a
timely manner and did not hang indefinitely.

Testing enhancements:

* Added a new helper in TestHelper called LookupSession that allows the
lookup of a session from the worker's internal session state.

* This is used in the new ExpectConnectionStateOnWorker check in the E2E
test, which is part of a larger amount of enhancements to the E2E test
that are coming in #1340.

* Added a ConnectionStatusFromProtoVal helper function to the session
package to allow the conversion of protobuf connection status values to
their higher-level counterparts; essentially it is a reverse of
ConnectionStatus.ProtoVal. This is used in some of our tests (with more
coming in #1340) to just help with keeping things more idiomatic.

Finally, there is some minor error refactoring, and we also change the
timeout on internal status and connection close requests to the
controller to 5 seconds.
This commit is contained in:
Chris Marchesi authored and GitHub committed 2021-06-30 10:53:54 -07:00
1 parent b3a74584ca
commit 48e55f156a
7 files changed
+359 -29

No files matched your search

+60
View File
@@ -5,9 +5,11 @@ import (
"fmt"
"net"
"testing"
"time"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/cmd/config"
pbs "github.com/hashicorp/boundary/internal/gen/controller/servers/services"
"github.com/hashicorp/go-hclog"
wrapping "github.com/hashicorp/go-kms-wrapping"
"github.com/hashicorp/vault/sdk/helper/base62"
@@ -76,6 +78,64 @@ func (tw *TestWorker) ProxyAddrs() []string {
return tw.addrs
}
// TestSessionInfo provides detail about a particular session from
// the worker's local session state. This detail is a point-in-time
// snapshot of what's in sessionInfoMap for a particular session, and
// may not contain all of the information that is contained within
// it, or the underlying connInfoMap. Only details that are really
// important to testing are passed along.
type TestSessionInfo struct {
Id string
Status pbs.SESSIONSTATUS
// Connections is indexed by connection ID, which is also included
// within TestConnectionInfo for convenience.
Connections map[string]TestConnectionInfo
}
// TestConnectionInfo provides detail about a particular connection
// as a part of TestSessionInfo. See that struct for details about
// the purpose of this data and how it's gathered.
type TestConnectionInfo struct {
Id string
Status pbs.CONNECTIONSTATUS
CloseTime time.Time
}
// LookupSession returns session info from the worker's local session
// state.
//
// The return boolean will be true if the session was found, false if
// it wasn't.
//
// See TestSessionInfo for details on how to use this info.
func (tw *TestWorker) LookupSession(id string) (TestSessionInfo, bool) {
var result TestSessionInfo
raw, ok := tw.w.sessionInfoMap.Load(id)
if !ok {
return result, false
}
sess := raw.(*sessionInfo)
sess.RLock()
defer sess.RUnlock()
conns := make(map[string]TestConnectionInfo)
for _, conn := range sess.connInfoMap {
conns[conn.id] = TestConnectionInfo{
Id: conn.id,
Status: conn.status,
CloseTime: conn.closeTime,
}
}
result.Id = sess.id
result.Status = sess.status
result.Connections = conns
return result, true
}
// Shutdown runs any cleanup functions; be sure to run this after your test is
// done
func (tw *TestWorker) Shutdown() {