-
Notifications
You must be signed in to change notification settings - Fork 899
feat(coderd/database/dbtestutil): set default database timezone to non-UTC in unit tests #9672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
355b16b
feat(coderd/database/dbtestutil): set a random timezone in the db
johnstcn 986db8b
remove check for tz after set
johnstcn fc3ba52
clitest: fix timestamp regex to match non-UTC timezones
johnstcn b3c559a
set fixed timezone in some existing unit tests
johnstcn f57dc9d
fixup! set fixed timezone in some existing unit tests
johnstcn d3d1274
fix timezone bug in ActivityBumpWorkspace query
johnstcn 9f1f5c6
refactor to separate package, allow specifying tz
johnstcn 7c2ff2f
set db timezone to canada/newfoundland by default
johnstcn eecfcdb
test activity bump in multiple timezones
johnstcn d327131
remove unnecessary IN TIME ZONE UTC in activitybump query
johnstcn 7680984
clarify use of multiple timezones in test
johnstcn d051bbb
remove deprecation and add linter rule
johnstcn da059dc
fix typo
johnstcn 0d425ba
bump allowed withinDuration for TestWorkpaceActivityBump
johnstcn d97ff2f
make Test_ActivityBumpWorkspace more flake-resistant
johnstcn f18c6b5
replace conditional sleep with a slower unconditional sleep that shou…
johnstcn 5a8aa8b
modify logic slightly in TestWorkspaceActivityBump from "deadline mus…
johnstcn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
refactor to separate package, allow specifying tz
- Loading branch information
commit 9f1f5c65a21925e9529bb6f8e3b40e51d4644641
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -13,6 +13,7 @@ import ( | |
|
||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/database/dbfake" | ||
"github.com/coder/coder/v2/coderd/database/dbtestutil/randtz" | ||
"github.com/coder/coder/v2/coderd/database/postgres" | ||
"github.com/coder/coder/v2/coderd/database/pubsub" | ||
) | ||
|
@@ -23,17 +24,18 @@ func WillUsePostgres() bool { | |
} | ||
|
||
type options struct { | ||
fixedTimezone bool | ||
fixedTimezone string | ||
} | ||
|
||
type Option func(*options) | ||
|
||
// WithFixedTimezone disables the random timezone setting for the database. | ||
// WithTimezone sets the database to the defined timezone instead of | ||
// to a random one. | ||
// | ||
// DEPRECATED: If you need to use this, you may have a timezone-related bug. | ||
func WithFixedTimezone() Option { | ||
// Deprecated: If you need to use this, you may have a timezone-related bug. | ||
func WithTimezone(tz string) Option { | ||
return func(o *options) { | ||
o.fixedTimezone = true | ||
o.fixedTimezone = tz | ||
} | ||
} | ||
|
||
|
@@ -59,11 +61,13 @@ func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) { | |
t.Cleanup(closePg) | ||
} | ||
|
||
if !o.fixedTimezone { | ||
// To make sure we find timezone-related issues, we set the timezone of the database to a random one. | ||
dbName := dbNameFromConnectionURL(t, connectionURL) | ||
setRandDBTimezone(t, connectionURL, dbName) | ||
if o.fixedTimezone == "" { | ||
// To make sure we find timezone-related issues, we set the timezone | ||
// of the database to a random one. | ||
o.fixedTimezone = randtz.Name(t) | ||
} | ||
dbName := dbNameFromConnectionURL(t, connectionURL) | ||
setDBTimezone(t, connectionURL, dbName, o.fixedTimezone) | ||
|
||
sqlDB, err := sql.Open("postgres", connectionURL) | ||
require.NoError(t, err) | ||
|
@@ -83,7 +87,7 @@ func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) { | |
} | ||
|
||
// setRandDBTimezone sets the timezone of the database to the given timezone. | ||
func setRandDBTimezone(t testing.TB, dbURL, dbname string) { | ||
func setDBTimezone(t testing.TB, dbURL, dbname, tz string) { | ||
t.Helper() | ||
|
||
sqlDB, err := sql.Open("postgres", dbURL) | ||
|
@@ -94,15 +98,7 @@ func setRandDBTimezone(t testing.TB, dbURL, dbname string) { | |
_ = sqlDB.Close() | ||
}() | ||
|
||
// Pick a random timezone. We can simply pick from pg_timezone_names. | ||
var tz string | ||
err = sqlDB.QueryRow("SELECT name FROM pg_timezone_names ORDER BY RANDOM() LIMIT 1").Scan(&tz) | ||
require.NoError(t, err) | ||
|
||
// Set the timezone for the database. | ||
t.Logf("setting timezone of database %q to %q", dbname, tz) | ||
// We apparently can't use placeholders here, sadly. | ||
// nolint: gosec // This is not user input and this is only executed in tests | ||
// nolint: gosec // This unfortunately does not work with placeholders. | ||
_, err = sqlDB.Exec(fmt.Sprintf("ALTER DATABASE %s SET TIMEZONE TO %q", dbname, tz)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. self-review: I hate this but it appears necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's fine in tests, IMO. It's an unfortunate limitation of some PostgreSQL queries.
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
require.NoError(t, err, "failed to set timezone for database") | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.