Skip to content

fix(coderd): use stable sorting for insights and improve test coverage #9250

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 33 commits into from
Aug 24, 2023

Conversation

mafredri
Copy link
Member

@mafredri mafredri commented Aug 22, 2023

This PR introduces a new test suite for insights and makes it easy to add new test scenarios which can then be compared against golden file changes.

A few bugfixes found by the test suite are included as well.

Fixes #9213

@mafredri mafredri force-pushed the mafredri/test-improve-template-insights-tests branch from b4a50c3 to bd15af7 Compare August 23, 2023 12:31
@mafredri mafredri force-pushed the mafredri/test-improve-template-insights-tests branch from bd15af7 to 73367d1 Compare August 23, 2023 15:51
@mafredri
Copy link
Member Author

I think this ready now, I might add some more test further down the line but I'd like to start with optimizations next.

@mafredri mafredri marked this pull request as ready for review August 23, 2023 21:18
@mafredri mafredri requested review from mtojek and johnstcn August 23, 2023 21:18
Comment on lines +1127 to +1135
// TODO(mafredri): This doesn't behave correctly right now
// and will add more usage to the app. This could be
// considered both correct and incorrect behavior.
// { // One hour of usage, but same user and same template app, only count once.
// app: users[0].workspaces[1].apps[0],
// startedAt: frozenWeekAgo,
// endedAt: frozenWeekAgo.Add(time.Hour),
// requests: 1,
// },
Copy link
Member Author

@mafredri mafredri Aug 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something I will fix later on, I wanted to keep query logic changes minimal for now.

Essentially this boils down to:

  • User A has two workspaces in template X
  • User A opens code-server in both workspaces
  • User A closes both code-servers ater an hour
  • We should only show 1h of code-server usage in template X

Currently we show two hours. Motivation for this is that a user can't effectively use multiple code-servers simultaneously. Imagine if this was 10 workspaces, the user would have to have very many skill points in parallel thinking and perhaps get a few extra keyboards and grow some extra limbs to produce 10h of usage in 1h. 😄

Copy link
Member

@mtojek mtojek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better test coverage is always welcome!

@@ -2480,6 +2489,8 @@ func (q *FakeQuerier) GetTemplateParameterInsights(ctx context.Context, arg data
}
}

// NOTE(mafredri): Add sorting if we decide on how to handle PostgreSQL collations.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity: what is the default PostgreSQL behavior considering the "most default" configuration?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will depend on the systems locale. Essentially if e.g. if LC_ALL=C or LC_ALL=en_US.UTF-8 is set when calling initdb, it will produce different results.

@@ -0,0 +1,44 @@
{
"report": {
"start_time": "0001-01-01T00:00:00Z",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we populate this with real timestamps? just to prevent UI from breaking down accidentally

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only for the test and to ensure the golden file isn't updated every day.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was rather thinking about the case when somebody forgets to set this field, and a unit test could potentially catch it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be caught by the other tests in this suite 👍🏻.

Without dbgen, builds are generated "now", so we must request "now" to check them. But "now" keeps changing and can't be used in the golden file. The other data is fixed-in-time and we can request fixed-in-time so the times get tested that way.

}

// Create the template version and template.
version := coderdtest.CreateTemplateVersion(t, client, firstUser.OrganizationID, &echo.Responses{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe we should depend on dbgen as much as possible to shorten the test execution and prevent potential flakes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I evaluated doing that, but we didn't have any methods for generating template/workspace build parameters. Perhaps it could be a future improvement?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

makeRequest func([]*testTemplate) codersdk.TemplateInsightsRequest
ignoreTimes bool
}
tests := []struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think that we can convert-to-table/remove some other insights tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can delete at least one or two. But depending on if we rely primarily on dbgen here or not, we might want to keep some "whole tests" around.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deleted one test TestTemplateInsights, its test cases are now covered by the golden files and then some.

@mafredri mafredri changed the title test(coderd): add extended template insights test suite fix(coderd): use stable sorting for insights and improve test coverage Aug 24, 2023
@mafredri mafredri merged commit 6b69abf into main Aug 24, 2023
@mafredri mafredri deleted the mafredri/test-improve-template-insights-tests branch August 24, 2023 10:36
@github-actions github-actions bot locked and limited conversation to collaborators Aug 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve tests for template insights
3 participants