Skip to content

Commit

Permalink
test: (attempt to) fix "address already in use" on coordinator restart
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasten committed Sep 25, 2023
1 parent 065eede commit 070511c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
23 changes: 12 additions & 11 deletions test/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ func (c CoordinatorConfig) Cleanup() {
}

// StartCoordinator starts the Coordinator defined by the given config.
func (i IntegrationTest) StartCoordinator(ctx context.Context, cfg CoordinatorConfig) {
// The returned func cancels the Coordinator and waits until it exited.
func (i IntegrationTest) StartCoordinator(ctx context.Context, cfg CoordinatorConfig) func() {
var cmd *exec.Cmd
if i.NoEnclave {
cmd = exec.CommandContext(ctx, filepath.Join(i.BuildDir, "coordinator-noenclave"))
Expand Down Expand Up @@ -174,7 +175,10 @@ func (i IntegrationTest) StartCoordinator(ctx context.Context, cfg CoordinatorCo
i.t.Log("Coordinator started")
resp.Body.Close()
i.require.Equal(http.StatusOK, resp.StatusCode)
return
return func() {
_ = cmd.Cancel()
<-cmdErr
}
}
}
}
Expand Down Expand Up @@ -455,16 +459,15 @@ func (i IntegrationTest) TriggerRecovery(coordinatorCfg CoordinatorConfig, cance

// Restart server, we should be in recovery mode
i.t.Log("Restarting the old instance")
ctx, cancel := context.WithCancel(i.Ctx)
i.StartCoordinator(ctx, coordinatorCfg)
cancelCoordinator = i.StartCoordinator(i.Ctx, coordinatorCfg)

// Query status API, check if status response begins with Code 1 (recovery state)
i.t.Log("Checking status...")
statusResponse, err := i.GetStatus()
i.require.NoError(err)
i.assert.EqualValues(1, gjson.Get(statusResponse, "data.StatusCode").Int(), "Server is not in recovery state, but should be.")

return cancel, cert
return cancelCoordinator, cert
}

// VerifyCertAfterRecovery verifies the certificate after a recovery.
Expand All @@ -489,8 +492,7 @@ func (i IntegrationTest) VerifyCertAfterRecovery(cert string, cancelCoordinator

// Restart server, we should be in recovery mode
i.t.Log("Restarting the old instance")
ctx, cancel := context.WithCancel(i.Ctx)
i.StartCoordinator(ctx, cfg)
cancelCoordinator = i.StartCoordinator(i.Ctx, cfg)

// Finally, check if we survive a restart.
i.t.Log("Restarted instance, now let's see if the state can be restored again successfully.")
Expand All @@ -507,7 +509,7 @@ func (i IntegrationTest) VerifyCertAfterRecovery(cert string, cancelCoordinator
resp.Body.Close()
i.require.Equal(http.StatusOK, resp.StatusCode)

return cancel
return cancelCoordinator
}

// VerifyResetAfterRecovery verifies the Coordinator after a recovery as been reset by setting a new manifest.
Expand All @@ -525,16 +527,15 @@ func (i IntegrationTest) VerifyResetAfterRecovery(cancelCoordinator func(), cfg

// Restart server, we should be in recovery mode
i.t.Log("Restarting the old instance")
ctx, cancel := context.WithCancel(i.Ctx)
i.StartCoordinator(ctx, cfg)
cancelCoordinator = i.StartCoordinator(i.Ctx, cfg)

// Finally, check if we survive a restart.
i.t.Log("Restarted instance, now let's see if the new state can be decrypted successfully...")
statusResponse, err = i.GetStatus()
i.require.NoError(err)
i.assert.EqualValues(3, gjson.Get(statusResponse, "data.StatusCode").Int(), "Server is in wrong status after recovery.")

return cancel
return cancelCoordinator
}

// MakeEnv returns a string that can be used as an environment variable.
Expand Down
9 changes: 3 additions & 6 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ func TestRestart(t *testing.T) {
t.Log("Starting a coordinator enclave...")
cfg := framework.NewCoordinatorConfig()
defer cfg.Cleanup()
coordinatorCtx, cancelCoordinator := context.WithCancel(f.Ctx)
f.StartCoordinator(coordinatorCtx, cfg)
cancelCoordinator := f.StartCoordinator(f.Ctx, cfg)

// set Manifest
_, err := f.SetManifest(f.TestManifest)
Expand Down Expand Up @@ -296,8 +295,7 @@ func TestRecoveryRestoreKey(t *testing.T) {
t.Log("Starting a coordinator enclave")
cfg := framework.NewCoordinatorConfig()
defer cfg.Cleanup()
coordinatorCtx, cancelCoordinator := context.WithCancel(f.Ctx)
f.StartCoordinator(coordinatorCtx, cfg)
cancelCoordinator := f.StartCoordinator(f.Ctx, cfg)

// set Manifest
t.Log("Setting the Manifest")
Expand Down Expand Up @@ -339,8 +337,7 @@ func TestRecoveryReset(t *testing.T) {
t.Log("Starting a coordinator enclave")
cfg := framework.NewCoordinatorConfig()
defer cfg.Cleanup()
coordinatorCtx, cancelCoordinator := context.WithCancel(f.Ctx)
f.StartCoordinator(coordinatorCtx, cfg)
cancelCoordinator := f.StartCoordinator(f.Ctx, cfg)

// set Manifest
t.Log("Setting the Manifest")
Expand Down

0 comments on commit 070511c

Please sign in to comment.