@@ -801,7 +801,6 @@ func TestTemplateInsights_Golden(t *testing.T) {
801
801
for _ , user := range users {
802
802
for _ , workspace := range user .workspaces {
803
803
workspace .user = user
804
- workspace .agentID = uuid .New ()
805
804
for _ , app := range workspace .template .apps {
806
805
app := workspaceApp (app )
807
806
workspace .apps = append (workspace .apps , & app )
@@ -813,24 +812,27 @@ func TestTemplateInsights_Golden(t *testing.T) {
813
812
}
814
813
815
814
prepare := func (t * testing.T , templates []* testTemplate , users []* testUser , testData map [* testWorkspace ]testDataGen ) * codersdk.Client {
816
- logger := slogtest .Make (t , nil ).Leveled (slog .LevelDebug )
815
+ logger := slogtest .Make (t , & slogtest. Options { IgnoreErrors : false } ).Leveled (slog .LevelDebug )
817
816
opts := & coderdtest.Options {
818
817
Logger : & logger ,
819
818
IncludeProvisionerDaemon : true ,
820
- AgentStatsRefreshInterval : time .Millisecond * 100 ,
819
+ AgentStatsRefreshInterval : time .Hour , // Not relevant for this test.
821
820
}
822
821
client , _ , coderdAPI := coderdtest .NewWithAPI (t , opts )
823
822
firstUser := coderdtest .CreateFirstUser (t , client )
824
823
825
824
// Prepare all test users.
826
- for _ , u := range users {
827
- u .client , u .sdk = coderdtest .CreateAnotherUserMutators (t , client , firstUser .OrganizationID , nil , func (r * codersdk.CreateUserRequest ) {
828
- r .Username = u .name
825
+ for _ , user := range users {
826
+ user .client , user .sdk = coderdtest .CreateAnotherUserMutators (t , client , firstUser .OrganizationID , nil , func (r * codersdk.CreateUserRequest ) {
827
+ r .Username = user .name
829
828
})
829
+ user .client .SetLogger (logger .Named ("user" ).With (slog.Field {Name : "name" , Value : user .name }))
830
830
}
831
831
832
832
// Prepare all the templates.
833
833
for _ , template := range templates {
834
+ template := template
835
+
834
836
var parameters []* proto.RichParameter
835
837
for _ , parameter := range template .parameters {
836
838
var options []* proto.RichParameterOption
@@ -861,7 +863,10 @@ func TestTemplateInsights_Golden(t *testing.T) {
861
863
)
862
864
var resources []* proto.Resource
863
865
for _ , user := range users {
866
+ user := user
864
867
for _ , workspace := range user .workspaces {
868
+ workspace := workspace
869
+
865
870
if workspace .template != template {
866
871
continue
867
872
}
@@ -885,7 +890,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
885
890
Name : "example" ,
886
891
Type : "aws_instance" ,
887
892
Agents : []* proto.Agent {{
888
- Id : workspace . agentID . String (),
893
+ Id : uuid . NewString (), // Doesn't matter, not used in DB.
889
894
Name : "dev" ,
890
895
Auth : & proto.Agent_Token {
891
896
Token : authToken .String (),
@@ -901,6 +906,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
901
906
Value : buildParameter .value ,
902
907
})
903
908
}
909
+
904
910
createWorkspaces = append (createWorkspaces , func (templateID uuid.UUID ) {
905
911
// Create workspace using the users client.
906
912
createdWorkspace := coderdtest .CreateWorkspace (t , user .client , firstUser .OrganizationID , templateID , func (cwr * codersdk.CreateWorkspaceRequest ) {
@@ -909,6 +915,11 @@ func TestTemplateInsights_Golden(t *testing.T) {
909
915
workspace .id = createdWorkspace .ID
910
916
waitWorkspaces = append (waitWorkspaces , func () {
911
917
coderdtest .AwaitWorkspaceBuildJob (t , user .client , createdWorkspace .LatestBuild .ID )
918
+ ctx := testutil .Context (t , testutil .WaitShort )
919
+ ws , err := user .client .Workspace (ctx , workspace .id )
920
+ require .NoError (t , err , "want no error getting workspace" )
921
+
922
+ workspace .agentID = ws .LatestBuild .Resources [0 ].Agents [0 ].ID
912
923
})
913
924
})
914
925
}
@@ -1044,14 +1055,30 @@ func TestTemplateInsights_Golden(t *testing.T) {
1044
1055
},
1045
1056
},
1046
1057
appUsage : []appUsage {
1047
- {
1048
- app : users [0 ].workspaces [0 ].apps [0 ],
1049
- startedAt : time .Now ().UTC ().Add (- time .Hour ),
1050
- endedAt : time .Now ().UTC (),
1051
- requests : 1 ,
1052
- },
1058
+ // { // One hour of usage.
1059
+ // app: users[0].workspaces[0].apps[0],
1060
+ // startedAt: weekAgo,
1061
+ // endedAt: weekAgo.Add(time.Hour),
1062
+ // requests: 1,
1063
+ // },
1064
+ // { // used an app on the last day, counts as active user.
1065
+ // app: users[0].workspaces[0].apps[2],
1066
+ // startedAt: weekAgo.AddDate(0, 0, 6),
1067
+ // endedAt: weekAgo.AddDate(0, 0, 6).Add(12 * time.Minute),
1068
+ // requests: 1,
1069
+ // },
1053
1070
},
1054
1071
},
1072
+ // users[0].workspaces[1]: {
1073
+ // appUsage: []appUsage{
1074
+ // { // One hour of usage, but same user and same template app, only count once.
1075
+ // app: users[0].workspaces[1].apps[0],
1076
+ // startedAt: weekAgo,
1077
+ // endedAt: weekAgo.Add(time.Hour),
1078
+ // requests: 1,
1079
+ // },
1080
+ // },
1081
+ // },
1055
1082
}
1056
1083
}
1057
1084
type testRequest struct {
@@ -1135,7 +1162,18 @@ func TestTemplateInsights_Golden(t *testing.T) {
1135
1162
// Sanity check.
1136
1163
for ws , data := range testData {
1137
1164
for _ , usage := range data .appUsage {
1138
- require .Contains (t , ws .apps , usage .app , "test bug: app %q not in workspace %q" , usage .app .name , ws .name )
1165
+ found := false
1166
+ wrongWorkspace := false
1167
+ for _ , app := range ws .apps {
1168
+ if usage .app == app { // Pointer equality
1169
+ found = true
1170
+ break
1171
+ }
1172
+ if * usage .app == * app {
1173
+ wrongWorkspace = true
1174
+ }
1175
+ }
1176
+ require .True (t , found , "test bug: app %q not in workspace %q [wrongWorkspace=%v]" , usage .app .name , ws .name , wrongWorkspace )
1139
1177
}
1140
1178
}
1141
1179
@@ -1145,7 +1183,9 @@ func TestTemplateInsights_Golden(t *testing.T) {
1145
1183
req := req
1146
1184
t .Run (req .name , func (t * testing.T ) {
1147
1185
t .Parallel ()
1186
+
1148
1187
ctx := testutil .Context (t , testutil .WaitMedium )
1188
+
1149
1189
report , err := client .TemplateInsights (ctx , req .makeRequest (templates ))
1150
1190
require .NoError (t , err , "want no error getting template insights" )
1151
1191
0 commit comments