-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix] Refactor some flakey tests (#10777)
* [fix] Fix flakey test by avoiding full table lock * [fix] Refactor some flakey tests - Stop using Eventually() in SessionReaper tests, and use a channel to signal when the reaper has run instead. - Start using random users where we previously used APIEmailAdmin. - Remove a full table lock and replace it with a selective delete which should reduce some lock contention * Clean up user/session methods
- Loading branch information
1 parent
8ed253b
commit 57fec36
Showing
8 changed files
with
122 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -257,17 +257,20 @@ func TestShell_DestroyExternalInitiator_NotFound(t *testing.T) { | |
func TestShell_RemoteLogin(t *testing.T) { | ||
|
||
app := startNewApplicationV2(t, nil) | ||
orm := app.SessionORM() | ||
|
||
u := cltest.NewUserWithSession(t, orm) | ||
|
||
tests := []struct { | ||
name, file string | ||
email, pwd string | ||
wantError bool | ||
}{ | ||
{"success prompt", "", cltest.APIEmailAdmin, cltest.Password, false}, | ||
{"success prompt", "", u.Email, cltest.Password, false}, | ||
{"success file", "../internal/fixtures/apicredentials", "", "", false}, | ||
{"failure prompt", "", "[email protected]", "wrongpwd", true}, | ||
{"failure file", "/tmp/doesntexist", "", "", true}, | ||
{"failure file w correct prompt", "/tmp/doesntexist", cltest.APIEmailAdmin, cltest.Password, true}, | ||
{"failure file w correct prompt", "/tmp/doesntexist", u.Email, cltest.Password, true}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
|
@@ -297,7 +300,8 @@ func TestShell_RemoteBuildCompatibility(t *testing.T) { | |
t.Parallel() | ||
|
||
app := startNewApplicationV2(t, nil) | ||
enteredStrings := []string{cltest.APIEmailAdmin, cltest.Password} | ||
u := cltest.NewUserWithSession(t, app.SessionORM()) | ||
enteredStrings := []string{u.Email, cltest.Password} | ||
prompter := &cltest.MockCountingPrompter{T: t, EnteredStrings: append(enteredStrings, enteredStrings...)} | ||
client := app.NewAuthenticatingShell(prompter) | ||
|
||
|
@@ -335,6 +339,7 @@ func TestShell_CheckRemoteBuildCompatibility(t *testing.T) { | |
t.Parallel() | ||
|
||
app := startNewApplicationV2(t, nil) | ||
u := cltest.NewUserWithSession(t, app.SessionORM()) | ||
tests := []struct { | ||
name string | ||
remoteVersion, remoteSha string | ||
|
@@ -349,7 +354,7 @@ func TestShell_CheckRemoteBuildCompatibility(t *testing.T) { | |
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
enteredStrings := []string{cltest.APIEmailAdmin, cltest.Password} | ||
enteredStrings := []string{u.Email, cltest.Password} | ||
prompter := &cltest.MockCountingPrompter{T: t, EnteredStrings: enteredStrings} | ||
client := app.NewAuthenticatingShell(prompter) | ||
|
||
|
@@ -410,8 +415,9 @@ func TestShell_ChangePassword(t *testing.T) { | |
t.Parallel() | ||
|
||
app := startNewApplicationV2(t, nil) | ||
u := cltest.NewUserWithSession(t, app.SessionORM()) | ||
|
||
enteredStrings := []string{cltest.APIEmailAdmin, cltest.Password} | ||
enteredStrings := []string{u.Email, cltest.Password} | ||
prompter := &cltest.MockCountingPrompter{T: t, EnteredStrings: enteredStrings} | ||
|
||
client := app.NewAuthenticatingShell(prompter) | ||
|
@@ -459,7 +465,8 @@ func TestShell_Profile_InvalidSecondsParam(t *testing.T) { | |
t.Parallel() | ||
|
||
app := startNewApplicationV2(t, nil) | ||
enteredStrings := []string{cltest.APIEmailAdmin, cltest.Password} | ||
u := cltest.NewUserWithSession(t, app.SessionORM()) | ||
enteredStrings := []string{u.Email, cltest.Password} | ||
prompter := &cltest.MockCountingPrompter{T: t, EnteredStrings: enteredStrings} | ||
|
||
client := app.NewAuthenticatingShell(prompter) | ||
|
@@ -489,7 +496,8 @@ func TestShell_Profile(t *testing.T) { | |
t.Parallel() | ||
|
||
app := startNewApplicationV2(t, nil) | ||
enteredStrings := []string{cltest.APIEmailAdmin, cltest.Password} | ||
u := cltest.NewUserWithSession(t, app.SessionORM()) | ||
enteredStrings := []string{u.Email, cltest.Password} | ||
prompter := &cltest.MockCountingPrompter{T: t, EnteredStrings: enteredStrings} | ||
|
||
client := app.NewAuthenticatingShell(prompter) | ||
|
@@ -656,7 +664,7 @@ func TestShell_AutoLogin(t *testing.T) { | |
require.NoError(t, err) | ||
|
||
// Expire the session and then try again | ||
pgtest.MustExec(t, app.GetSqlxDB(), "TRUNCATE sessions") | ||
pgtest.MustExec(t, app.GetSqlxDB(), "delete from sessions where email = $1", user.Email) | ||
err = client.ListJobs(cli.NewContext(nil, fs, nil)) | ||
require.NoError(t, err) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,8 +34,8 @@ func TestORM_FindUser(t *testing.T) { | |
t.Parallel() | ||
|
||
db, orm := setupORM(t) | ||
user1 := cltest.MustNewUser(t, "[email protected]", cltest.Password) | ||
user2 := cltest.MustNewUser(t, "[email protected]", cltest.Password) | ||
user1 := cltest.MustRandomUser(t) | ||
user2 := cltest.MustRandomUser(t) | ||
|
||
require.NoError(t, orm.CreateUser(&user1)) | ||
require.NoError(t, orm.CreateUser(&user2)) | ||
|
@@ -56,20 +56,19 @@ func TestORM_AuthorizedUserWithSession(t *testing.T) { | |
sessionID string | ||
sessionDuration time.Duration | ||
wantError string | ||
wantEmail string | ||
}{ | ||
{"authorized", "correctID", cltest.MustParseDuration(t, "3m"), "", "have@email"}, | ||
{"expired", "correctID", cltest.MustParseDuration(t, "0m"), sessions.ErrUserSessionExpired.Error(), ""}, | ||
{"incorrect", "wrong", cltest.MustParseDuration(t, "3m"), sessions.ErrUserSessionExpired.Error(), ""}, | ||
{"empty", "", cltest.MustParseDuration(t, "3m"), sessions.ErrEmptySessionID.Error(), ""}, | ||
{"authorized", "correctID", cltest.MustParseDuration(t, "3m"), ""}, | ||
{"expired", "correctID", cltest.MustParseDuration(t, "0m"), sessions.ErrUserSessionExpired.Error()}, | ||
{"incorrect", "wrong", cltest.MustParseDuration(t, "3m"), sessions.ErrUserSessionExpired.Error()}, | ||
{"empty", "", cltest.MustParseDuration(t, "3m"), sessions.ErrEmptySessionID.Error()}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
db := pgtest.NewSqlxDB(t) | ||
orm := sessions.NewORM(db, test.sessionDuration, logger.TestLogger(t), pgtest.NewQConfig(true), &audit.AuditLoggerService{}) | ||
|
||
user := cltest.MustNewUser(t, "have@email", cltest.Password) | ||
user := cltest.MustRandomUser(t) | ||
require.NoError(t, orm.CreateUser(&user)) | ||
|
||
prevSession := cltest.NewSession("correctID") | ||
|
@@ -83,7 +82,7 @@ func TestORM_AuthorizedUserWithSession(t *testing.T) { | |
require.EqualError(t, err, test.wantError) | ||
} else { | ||
require.NoError(t, err) | ||
assert.Equal(t, test.wantEmail, actual.Email) | ||
assert.Equal(t, user.Email, actual.Email) | ||
var bumpedSession sessions.Session | ||
err = db.Get(&bumpedSession, "SELECT * FROM sessions WHERE ID = $1", prevSession.ID) | ||
require.NoError(t, err) | ||
|
@@ -97,13 +96,13 @@ func TestORM_DeleteUser(t *testing.T) { | |
t.Parallel() | ||
_, orm := setupORM(t) | ||
|
||
_, err := orm.FindUser(cltest.APIEmailAdmin) | ||
require.NoError(t, err) | ||
u := cltest.MustRandomUser(t) | ||
require.NoError(t, orm.CreateUser(&u)) | ||
|
||
err = orm.DeleteUser(cltest.APIEmailAdmin) | ||
err := orm.DeleteUser(u.Email) | ||
require.NoError(t, err) | ||
|
||
_, err = orm.FindUser(cltest.APIEmailAdmin) | ||
_, err = orm.FindUser(u.Email) | ||
require.Error(t, err) | ||
} | ||
|
||
|
@@ -112,14 +111,17 @@ func TestORM_DeleteUserSession(t *testing.T) { | |
|
||
db, orm := setupORM(t) | ||
|
||
u := cltest.MustRandomUser(t) | ||
require.NoError(t, orm.CreateUser(&u)) | ||
|
||
session := sessions.NewSession() | ||
_, err := db.Exec("INSERT INTO sessions (id, email, last_used, created_at) VALUES ($1, $2, now(), now())", session.ID, cltest.APIEmailAdmin) | ||
_, err := db.Exec("INSERT INTO sessions (id, email, last_used, created_at) VALUES ($1, $2, now(), now())", session.ID, u.Email) | ||
require.NoError(t, err) | ||
|
||
err = orm.DeleteUserSession(session.ID) | ||
require.NoError(t, err) | ||
|
||
_, err = orm.FindUser(cltest.APIEmailAdmin) | ||
_, err = orm.FindUser(u.Email) | ||
require.NoError(t, err) | ||
|
||
sessions, err := orm.Sessions(0, 10) | ||
|
@@ -130,14 +132,17 @@ func TestORM_DeleteUserSession(t *testing.T) { | |
func TestORM_DeleteUserCascade(t *testing.T) { | ||
db, orm := setupORM(t) | ||
|
||
u := cltest.MustRandomUser(t) | ||
require.NoError(t, orm.CreateUser(&u)) | ||
|
||
session := sessions.NewSession() | ||
_, err := db.Exec("INSERT INTO sessions (id, email, last_used, created_at) VALUES ($1, $2, now(), now())", session.ID, cltest.APIEmailAdmin) | ||
_, err := db.Exec("INSERT INTO sessions (id, email, last_used, created_at) VALUES ($1, $2, now(), now())", session.ID, u.Email) | ||
require.NoError(t, err) | ||
|
||
err = orm.DeleteUser(cltest.APIEmailAdmin) | ||
err = orm.DeleteUser(u.Email) | ||
require.NoError(t, err) | ||
|
||
_, err = orm.FindUser(cltest.APIEmailAdmin) | ||
_, err = orm.FindUser(u.Email) | ||
require.Error(t, err) | ||
|
||
sessions, err := orm.Sessions(0, 10) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package sessions | ||
|
||
func (sr *sessionReaper) RunSignal() <-chan struct{} { | ||
return sr.runSignal | ||
} |
Oops, something went wrong.