e2e: Wait for keyring before starting client intro client agents. (#26660)

Ensuring the keyring is ready before starting the Nomad client in
the client intro e2e test speeds up execution. This is because the
client does not have to wait to retry failed registrations due to
the keyring not being ready.
This commit is contained in:
James Rasell
2025-09-01 07:32:40 +01:00
committed by GitHub
parent 87be37e8cc
commit d5f2c0201e

View File

@@ -69,6 +69,14 @@ func testClientIntroEnforcementWarn(t *testing.T) {
must.NoError(t, testServer.Start())
t.Cleanup(func() { _ = testServer.Destroy() })
// Create a Nomad API client to talk to the server. Do it here, so we only
// do this once.
nomadClient, err := testServer.Client()
must.NoError(t, err)
must.NotNil(t, nomadClient)
waitForKeyring(t, nomadClient)
clientCallbackFn := func(c *execagent.AgentTemplateVars) {
c.AgentName = "client-intro-" + uuid.Short()
c.LogLevel = hclog.Warn.String()
@@ -91,12 +99,6 @@ func testClientIntroEnforcementWarn(t *testing.T) {
must.NoError(t, testClient.Start())
t.Cleanup(func() { _ = testClient.Destroy() })
// Create a Nomad API client to talk to the server. Do it here, so we only
// do this once.
nomadClient, err := testServer.Client()
must.NoError(t, err)
must.NotNil(t, nomadClient)
// Wait for the client to show up in the server's node list. We use the node
// name as the identifier to check for since it's unique.
must.Wait(t, wait.InitialSuccess(
@@ -171,7 +173,15 @@ server {
must.NoError(t, testServer.Start())
t.Cleanup(func() { _ = testServer.Destroy() })
//
// Create a Nomad API client to talk to the server. Do it here, so we only
// do this once.
nomadClient, err := testServer.Client()
must.NoError(t, err)
must.NotNil(t, nomadClient)
waitForKeyring(t, nomadClient)
// Generate a unique name for the client node we will be creating.
clientAgentName := "client-intro-" + uuid.Short()
clientCallbackFn := func(c *execagent.AgentTemplateVars) {
@@ -240,12 +250,6 @@ server {
},
)
// Get an API client, so we can create the introduction token that
// the client will use.
nomadClient, err := testServer.Client()
must.NoError(t, err)
must.NotNil(t, nomadClient)
resp, _, err := nomadClient.ACLIdentity().CreateClientIntroductionToken(
&api.ACLIdentityClientIntroductionTokenRequest{
NodeName: clientAgentName,
@@ -295,6 +299,25 @@ server {
))
}
// waitForKeyring blocks until the keyring is initialized. If the keyring is not
// initialized within the timeout period, the test will fail.
func waitForKeyring(t *testing.T, nomadClient *api.Client) {
must.Wait(t, wait.InitialSuccess(
wait.ErrorFunc(func() error {
keyList, _, err := nomadClient.Keyring().List(nil)
if err != nil {
return err
}
if len(keyList) == 0 {
return errors.New("no keys found")
}
return nil
}),
wait.Timeout(10*time.Second),
wait.Gap(500*time.Millisecond),
))
}
// caputreLogger is a simple logger that captures log lines in memory and also
// writes them to stderr. It allows us to caputre output and inspect it for
// testing.