Skip to content

Commit db6ae5a

Browse files
committed
improve stability of template id
1 parent 502747b commit db6ae5a

File tree

2 files changed

+47
-43
lines changed

2 files changed

+47
-43
lines changed

coderd/insights_test.go

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/coder/coder/v2/coderd/coderdtest"
2525
"github.com/coder/coder/v2/coderd/database/dbauthz"
2626
"github.com/coder/coder/v2/coderd/rbac"
27+
"github.com/coder/coder/v2/coderd/util/slice"
2728
"github.com/coder/coder/v2/coderd/workspaceapps"
2829
"github.com/coder/coder/v2/codersdk"
2930
"github.com/coder/coder/v2/codersdk/agentsdk"
@@ -579,36 +580,16 @@ func TestTemplateInsights(t *testing.T) {
579580
func TestTemplateInsights_Golden(t *testing.T) {
580581
t.Parallel()
581582

582-
stabilizeReportForGoldenComparison := func(report *codersdk.TemplateInsightsResponse) {
583-
var stableTemplateIDs []uuid.UUID
584-
stableTemplateIDMap := make(map[uuid.UUID]uuid.UUID)
585-
toStableTemplateID := func(id uuid.UUID) uuid.UUID {
586-
if stableID, ok := stableTemplateIDMap[id]; ok {
587-
return stableID
588-
}
589-
stableTemplateIDs = append(stableTemplateIDs, uuid.MustParse(fmt.Sprintf("00000000-0000-0000-0000-%012d", len(stableTemplateIDs)+1)))
590-
stableID := stableTemplateIDs[len(stableTemplateIDs)-1]
591-
stableTemplateIDMap[id] = stableID
592-
return stableID
593-
}
594-
595-
for i, id := range report.Report.TemplateIDs {
596-
report.Report.TemplateIDs[i] = toStableTemplateID(id)
597-
}
583+
stabilizeReportForGoldenComparison := func(report *codersdk.TemplateInsightsResponse, toStableTemplateIDs func([]uuid.UUID)) {
584+
toStableTemplateIDs(report.Report.TemplateIDs)
598585
for _, param := range report.Report.ParametersUsage {
599-
for i, id := range param.TemplateIDs {
600-
param.TemplateIDs[i] = toStableTemplateID(id)
601-
}
586+
toStableTemplateIDs(param.TemplateIDs)
602587
}
603588
for _, app := range report.Report.AppsUsage {
604-
for i, id := range app.TemplateIDs {
605-
app.TemplateIDs[i] = toStableTemplateID(id)
606-
}
589+
toStableTemplateIDs(app.TemplateIDs)
607590
}
608591
for _, intervalReport := range report.IntervalReports {
609-
for i, id := range intervalReport.TemplateIDs {
610-
intervalReport.TemplateIDs[i] = toStableTemplateID(id)
611-
}
592+
toStableTemplateIDs(intervalReport.TemplateIDs)
612593
}
613594
}
614595

@@ -811,7 +792,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
811792
return templates, users
812793
}
813794

814-
prepare := func(t *testing.T, templates []*testTemplate, users []*testUser, testData map[*testWorkspace]testDataGen) *codersdk.Client {
795+
prepare := func(t *testing.T, templates []*testTemplate, users []*testUser, testData map[*testWorkspace]testDataGen) (*codersdk.Client, func([]uuid.UUID)) {
815796
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: false}).Leveled(slog.LevelDebug)
816797
opts := &coderdtest.Options{
817798
Logger: &logger,
@@ -1024,7 +1005,30 @@ func TestTemplateInsights_Golden(t *testing.T) {
10241005
err = reporter.Report(dbauthz.AsSystemRestricted(ctx), stats)
10251006
require.NoError(t, err, "want no error inserting app stats")
10261007

1027-
return client
1008+
var stableTemplateIDs []uuid.UUID
1009+
stableTemplateIDMap := make(map[uuid.UUID]uuid.UUID)
1010+
toStableTemplateID := func(id uuid.UUID) uuid.UUID {
1011+
if stableID, ok := stableTemplateIDMap[id]; ok {
1012+
return stableID
1013+
}
1014+
stableTemplateIDs = append(stableTemplateIDs, uuid.MustParse(fmt.Sprintf("00000000-0000-0000-0000-%012d", len(stableTemplateIDs)+1)))
1015+
stableID := stableTemplateIDs[len(stableTemplateIDs)-1]
1016+
stableTemplateIDMap[id] = stableID
1017+
return stableID
1018+
}
1019+
// Prime the map.
1020+
for _, template := range templates {
1021+
_ = toStableTemplateID(template.id)
1022+
}
1023+
1024+
return client, func(ids []uuid.UUID) {
1025+
for i, id := range ids {
1026+
ids[i] = toStableTemplateID(id)
1027+
}
1028+
slices.SortFunc(ids, func(a, b uuid.UUID) int {
1029+
return slice.Ascending(a.String(), b.String())
1030+
})
1031+
}
10281032
}
10291033

10301034
// Time range for report, test data will be generated within and
@@ -1126,11 +1130,11 @@ func TestTemplateInsights_Golden(t *testing.T) {
11261130
requests []testRequest
11271131
}{
11281132
{
1129-
name: "multiple users and workspaces week",
1133+
name: "multiple users and workspaces",
11301134
makeTestData: makeBaseTestData,
11311135
requests: []testRequest{
11321136
{
1133-
name: "deployment wide",
1137+
name: "week deployment wide",
11341138
makeRequest: func(templates []*testTemplate) codersdk.TemplateInsightsRequest {
11351139
return codersdk.TemplateInsightsRequest{
11361140
StartTime: weekAgo,
@@ -1140,7 +1144,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
11401144
},
11411145
},
11421146
{
1143-
name: "all templates",
1147+
name: "week all templates",
11441148
makeRequest: func(templates []*testTemplate) codersdk.TemplateInsightsRequest {
11451149
return codersdk.TemplateInsightsRequest{
11461150
TemplateIDs: []uuid.UUID{templates[0].id, templates[1].id, templates[2].id},
@@ -1151,7 +1155,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
11511155
},
11521156
},
11531157
{
1154-
name: "first template",
1158+
name: "week first template",
11551159
makeRequest: func(templates []*testTemplate) codersdk.TemplateInsightsRequest {
11561160
return codersdk.TemplateInsightsRequest{
11571161
TemplateIDs: []uuid.UUID{templates[0].id},
@@ -1162,7 +1166,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
11621166
},
11631167
},
11641168
{
1165-
name: "second template",
1169+
name: "week second template",
11661170
makeRequest: func(templates []*testTemplate) codersdk.TemplateInsightsRequest {
11671171
return codersdk.TemplateInsightsRequest{
11681172
TemplateIDs: []uuid.UUID{templates[1].id},
@@ -1173,7 +1177,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
11731177
},
11741178
},
11751179
{
1176-
name: "third template",
1180+
name: "week third template",
11771181
makeRequest: func(templates []*testTemplate) codersdk.TemplateInsightsRequest {
11781182
return codersdk.TemplateInsightsRequest{
11791183
TemplateIDs: []uuid.UUID{templates[2].id},
@@ -1212,7 +1216,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
12121216
}
12131217
}
12141218

1215-
client := prepare(t, templates, users, testData)
1219+
client, toStableTemplateID := prepare(t, templates, users, testData)
12161220

12171221
for _, req := range tt.requests {
12181222
req := req
@@ -1224,7 +1228,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
12241228
report, err := client.TemplateInsights(ctx, req.makeRequest(templates))
12251229
require.NoError(t, err, "want no error getting template insights")
12261230

1227-
stabilizeReportForGoldenComparison(&report)
1231+
stabilizeReportForGoldenComparison(&report, toStableTemplateID)
12281232

12291233
partialName := strings.Join(strings.Split(t.Name(), "/")[1:], "_")
12301234
goldenFile := filepath.Join("testdata", "insights", partialName+".json.golden")

coderd/testdata/insights/multiple_users_and_workspaces_week_second_template.json.golden

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
"start_time": "2023-08-15T00:00:00Z",
44
"end_time": "2023-08-22T00:00:00Z",
55
"template_ids": [
6-
"00000000-0000-0000-0000-000000000001"
6+
"00000000-0000-0000-0000-000000000002"
77
],
88
"active_users": 1,
99
"apps_usage": [
1010
{
1111
"template_ids": [
12-
"00000000-0000-0000-0000-000000000001"
12+
"00000000-0000-0000-0000-000000000002"
1313
],
1414
"type": "builtin",
1515
"display_name": "Visual Studio Code",
@@ -19,7 +19,7 @@
1919
},
2020
{
2121
"template_ids": [
22-
"00000000-0000-0000-0000-000000000001"
22+
"00000000-0000-0000-0000-000000000002"
2323
],
2424
"type": "builtin",
2525
"display_name": "JetBrains",
@@ -29,7 +29,7 @@
2929
},
3030
{
3131
"template_ids": [
32-
"00000000-0000-0000-0000-000000000001"
32+
"00000000-0000-0000-0000-000000000002"
3333
],
3434
"type": "builtin",
3535
"display_name": "Web Terminal",
@@ -39,7 +39,7 @@
3939
},
4040
{
4141
"template_ids": [
42-
"00000000-0000-0000-0000-000000000001"
42+
"00000000-0000-0000-0000-000000000002"
4343
],
4444
"type": "builtin",
4545
"display_name": "SSH",
@@ -49,7 +49,7 @@
4949
},
5050
{
5151
"template_ids": [
52-
"00000000-0000-0000-0000-000000000001"
52+
"00000000-0000-0000-0000-000000000002"
5353
],
5454
"type": "app",
5555
"display_name": "app1",
@@ -65,7 +65,7 @@
6565
"start_time": "2023-08-15T00:00:00Z",
6666
"end_time": "2023-08-16T00:00:00Z",
6767
"template_ids": [
68-
"00000000-0000-0000-0000-000000000001"
68+
"00000000-0000-0000-0000-000000000002"
6969
],
7070
"interval": "day",
7171
"active_users": 1
@@ -81,7 +81,7 @@
8181
"start_time": "2023-08-17T00:00:00Z",
8282
"end_time": "2023-08-18T00:00:00Z",
8383
"template_ids": [
84-
"00000000-0000-0000-0000-000000000001"
84+
"00000000-0000-0000-0000-000000000002"
8585
],
8686
"interval": "day",
8787
"active_users": 1

0 commit comments

Comments
 (0)