Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 28e3e45

Browse files
deansheatherpull[bot]
authored andcommitted
chore: fix flake in create-admin-user test (#6103)
1 parent a1e6c45 commit 28e3e45

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

cli/server_createadminuser_test.go

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,17 @@ func TestServerCreateAdminUser(t *testing.T) {
8686
connectionURL, closeFunc, err := postgres.Open()
8787
require.NoError(t, err)
8888
defer closeFunc()
89-
ctx, cancelFunc := context.WithCancel(context.Background())
90-
defer cancelFunc()
9189

9290
sqlDB, err := sql.Open("postgres", connectionURL)
9391
require.NoError(t, err)
9492
defer sqlDB.Close()
9593
db := database.New(sqlDB)
9694

95+
// Sometimes generating SSH keys takes a really long time if there isn't
96+
// enough entropy. We don't want the tests to fail in these cases.
97+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitSuperLong)
98+
defer cancel()
99+
97100
pingCtx, pingCancel := context.WithTimeout(ctx, testutil.WaitShort)
98101
defer pingCancel()
99102
_, err = db.Ping(pingCtx)
@@ -135,14 +138,14 @@ func TestServerCreateAdminUser(t *testing.T) {
135138
errC <- err
136139
}()
137140

138-
pty.ExpectMatch("Creating user...")
139-
pty.ExpectMatch("Generating user SSH key...")
140-
pty.ExpectMatch(fmt.Sprintf("Adding user to organization %q (%s) as admin...", org1Name, org1ID.String()))
141-
pty.ExpectMatch(fmt.Sprintf("Adding user to organization %q (%s) as admin...", org2Name, org2ID.String()))
142-
pty.ExpectMatch("User created successfully.")
143-
pty.ExpectMatch(username)
144-
pty.ExpectMatch(email)
145-
pty.ExpectMatch("****")
141+
pty.ExpectMatchContext(ctx, "Creating user...")
142+
pty.ExpectMatchContext(ctx, "Generating user SSH key...")
143+
pty.ExpectMatchContext(ctx, fmt.Sprintf("Adding user to organization %q (%s) as admin...", org1Name, org1ID.String()))
144+
pty.ExpectMatchContext(ctx, fmt.Sprintf("Adding user to organization %q (%s) as admin...", org2Name, org2ID.String()))
145+
pty.ExpectMatchContext(ctx, "User created successfully.")
146+
pty.ExpectMatchContext(ctx, username)
147+
pty.ExpectMatchContext(ctx, email)
148+
pty.ExpectMatchContext(ctx, "****")
146149

147150
require.NoError(t, <-errC)
148151

@@ -158,11 +161,14 @@ func TestServerCreateAdminUser(t *testing.T) {
158161
connectionURL, closeFunc, err := postgres.Open()
159162
require.NoError(t, err)
160163
defer closeFunc()
161-
ctx, cancelFunc := context.WithCancel(context.Background())
162-
defer cancelFunc()
164+
165+
// Sometimes generating SSH keys takes a really long time if there isn't
166+
// enough entropy. We don't want the tests to fail in these cases.
167+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitSuperLong)
168+
defer cancel()
163169

164170
t.Setenv("CODER_POSTGRES_URL", connectionURL)
165-
t.Setenv("CODER_SSH_KEYGEN_ALGORITHM", "ecdsa")
171+
t.Setenv("CODER_SSH_KEYGEN_ALGORITHM", "ed25519")
166172
t.Setenv("CODER_USERNAME", username)
167173
t.Setenv("CODER_EMAIL", email)
168174
t.Setenv("CODER_PASSWORD", password)
@@ -178,10 +184,10 @@ func TestServerCreateAdminUser(t *testing.T) {
178184
errC <- err
179185
}()
180186

181-
pty.ExpectMatch("User created successfully.")
182-
pty.ExpectMatch(username)
183-
pty.ExpectMatch(email)
184-
pty.ExpectMatch("****")
187+
pty.ExpectMatchContext(ctx, "User created successfully.")
188+
pty.ExpectMatchContext(ctx, username)
189+
pty.ExpectMatchContext(ctx, email)
190+
pty.ExpectMatchContext(ctx, "****")
185191

186192
require.NoError(t, <-errC)
187193

@@ -198,13 +204,16 @@ func TestServerCreateAdminUser(t *testing.T) {
198204
connectionURL, closeFunc, err := postgres.Open()
199205
require.NoError(t, err)
200206
defer closeFunc()
201-
ctx, cancelFunc := context.WithCancel(context.Background())
202-
defer cancelFunc()
207+
208+
// Sometimes generating SSH keys takes a really long time if there isn't
209+
// enough entropy. We don't want the tests to fail in these cases.
210+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitSuperLong)
211+
defer cancel()
203212

204213
root, _ := clitest.New(t,
205214
"server", "create-admin-user",
206215
"--postgres-url", connectionURL,
207-
"--ssh-keygen-algorithm", "rsa4096",
216+
"--ssh-keygen-algorithm", "ed25519",
208217
)
209218
pty := ptytest.New(t)
210219
root.SetIn(pty.Input())
@@ -217,19 +226,19 @@ func TestServerCreateAdminUser(t *testing.T) {
217226
errC <- err
218227
}()
219228

220-
pty.ExpectMatch("> Username")
229+
pty.ExpectMatchContext(ctx, "> Username")
221230
pty.WriteLine(username)
222-
pty.ExpectMatch("> Email")
231+
pty.ExpectMatchContext(ctx, "> Email")
223232
pty.WriteLine(email)
224-
pty.ExpectMatch("> Password")
233+
pty.ExpectMatchContext(ctx, "> Password")
225234
pty.WriteLine(password)
226-
pty.ExpectMatch("> Confirm password")
235+
pty.ExpectMatchContext(ctx, "> Confirm password")
227236
pty.WriteLine(password)
228237

229-
pty.ExpectMatch("User created successfully.")
230-
pty.ExpectMatch(username)
231-
pty.ExpectMatch(email)
232-
pty.ExpectMatch("****")
238+
pty.ExpectMatchContext(ctx, "User created successfully.")
239+
pty.ExpectMatchContext(ctx, username)
240+
pty.ExpectMatchContext(ctx, email)
241+
pty.ExpectMatchContext(ctx, "****")
233242

234243
require.NoError(t, <-errC)
235244

0 commit comments

Comments
 (0)