@@ -24,6 +24,7 @@ import (
24
24
"github.com/coder/coder/v2/coderd/coderdtest"
25
25
"github.com/coder/coder/v2/coderd/database/dbauthz"
26
26
"github.com/coder/coder/v2/coderd/rbac"
27
+ "github.com/coder/coder/v2/coderd/util/slice"
27
28
"github.com/coder/coder/v2/coderd/workspaceapps"
28
29
"github.com/coder/coder/v2/codersdk"
29
30
"github.com/coder/coder/v2/codersdk/agentsdk"
@@ -579,36 +580,16 @@ func TestTemplateInsights(t *testing.T) {
579
580
func TestTemplateInsights_Golden (t * testing.T ) {
580
581
t .Parallel ()
581
582
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 )
598
585
for _ , param := range report .Report .ParametersUsage {
599
- for i , id := range param .TemplateIDs {
600
- param .TemplateIDs [i ] = toStableTemplateID (id )
601
- }
586
+ toStableTemplateIDs (param .TemplateIDs )
602
587
}
603
588
for _ , app := range report .Report .AppsUsage {
604
- for i , id := range app .TemplateIDs {
605
- app .TemplateIDs [i ] = toStableTemplateID (id )
606
- }
589
+ toStableTemplateIDs (app .TemplateIDs )
607
590
}
608
591
for _ , intervalReport := range report .IntervalReports {
609
- for i , id := range intervalReport .TemplateIDs {
610
- intervalReport .TemplateIDs [i ] = toStableTemplateID (id )
611
- }
592
+ toStableTemplateIDs (intervalReport .TemplateIDs )
612
593
}
613
594
}
614
595
@@ -811,7 +792,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
811
792
return templates , users
812
793
}
813
794
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 )) {
815
796
logger := slogtest .Make (t , & slogtest.Options {IgnoreErrors : false }).Leveled (slog .LevelDebug )
816
797
opts := & coderdtest.Options {
817
798
Logger : & logger ,
@@ -1024,7 +1005,30 @@ func TestTemplateInsights_Golden(t *testing.T) {
1024
1005
err = reporter .Report (dbauthz .AsSystemRestricted (ctx ), stats )
1025
1006
require .NoError (t , err , "want no error inserting app stats" )
1026
1007
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
+ }
1028
1032
}
1029
1033
1030
1034
// Time range for report, test data will be generated within and
@@ -1126,11 +1130,11 @@ func TestTemplateInsights_Golden(t *testing.T) {
1126
1130
requests []testRequest
1127
1131
}{
1128
1132
{
1129
- name : "multiple users and workspaces week " ,
1133
+ name : "multiple users and workspaces" ,
1130
1134
makeTestData : makeBaseTestData ,
1131
1135
requests : []testRequest {
1132
1136
{
1133
- name : "deployment wide" ,
1137
+ name : "week deployment wide" ,
1134
1138
makeRequest : func (templates []* testTemplate ) codersdk.TemplateInsightsRequest {
1135
1139
return codersdk.TemplateInsightsRequest {
1136
1140
StartTime : weekAgo ,
@@ -1140,7 +1144,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
1140
1144
},
1141
1145
},
1142
1146
{
1143
- name : "all templates" ,
1147
+ name : "week all templates" ,
1144
1148
makeRequest : func (templates []* testTemplate ) codersdk.TemplateInsightsRequest {
1145
1149
return codersdk.TemplateInsightsRequest {
1146
1150
TemplateIDs : []uuid.UUID {templates [0 ].id , templates [1 ].id , templates [2 ].id },
@@ -1151,7 +1155,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
1151
1155
},
1152
1156
},
1153
1157
{
1154
- name : "first template" ,
1158
+ name : "week first template" ,
1155
1159
makeRequest : func (templates []* testTemplate ) codersdk.TemplateInsightsRequest {
1156
1160
return codersdk.TemplateInsightsRequest {
1157
1161
TemplateIDs : []uuid.UUID {templates [0 ].id },
@@ -1162,7 +1166,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
1162
1166
},
1163
1167
},
1164
1168
{
1165
- name : "second template" ,
1169
+ name : "week second template" ,
1166
1170
makeRequest : func (templates []* testTemplate ) codersdk.TemplateInsightsRequest {
1167
1171
return codersdk.TemplateInsightsRequest {
1168
1172
TemplateIDs : []uuid.UUID {templates [1 ].id },
@@ -1173,7 +1177,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
1173
1177
},
1174
1178
},
1175
1179
{
1176
- name : "third template" ,
1180
+ name : "week third template" ,
1177
1181
makeRequest : func (templates []* testTemplate ) codersdk.TemplateInsightsRequest {
1178
1182
return codersdk.TemplateInsightsRequest {
1179
1183
TemplateIDs : []uuid.UUID {templates [2 ].id },
@@ -1212,7 +1216,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
1212
1216
}
1213
1217
}
1214
1218
1215
- client := prepare (t , templates , users , testData )
1219
+ client , toStableTemplateID := prepare (t , templates , users , testData )
1216
1220
1217
1221
for _ , req := range tt .requests {
1218
1222
req := req
@@ -1224,7 +1228,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
1224
1228
report , err := client .TemplateInsights (ctx , req .makeRequest (templates ))
1225
1229
require .NoError (t , err , "want no error getting template insights" )
1226
1230
1227
- stabilizeReportForGoldenComparison (& report )
1231
+ stabilizeReportForGoldenComparison (& report , toStableTemplateID )
1228
1232
1229
1233
partialName := strings .Join (strings .Split (t .Name (), "/" )[1 :], "_" )
1230
1234
goldenFile := filepath .Join ("testdata" , "insights" , partialName + ".json.golden" )
0 commit comments