@@ -2879,113 +2879,122 @@ func TestWorkspaceProvisionerdServerMetrics(t *testing.T) {
2879
2879
t .Parallel ()
2880
2880
2881
2881
// Setup
2882
- log := testutil .Logger (t )
2882
+ clock := quartz .NewMock (t )
2883
+ ctx := testutil .Context (t , testutil .WaitSuperLong )
2884
+ db , pb := dbtestutil .NewDB (t , dbtestutil .WithDumpOnFailure ())
2885
+ logger := testutil .Logger (t )
2883
2886
reg := prometheus .NewRegistry ()
2884
- provisionerdserverMetrics := provisionerdserver .NewMetrics (log )
2887
+ provisionerdserverMetrics := provisionerdserver .NewMetrics (logger )
2885
2888
err := provisionerdserverMetrics .Register (reg )
2886
2889
require .NoError (t , err )
2887
- client , db , owner := coderdenttest .NewWithDatabase (t , & coderdenttest.Options {
2890
+ client , _ , api , owner := coderdenttest .NewWithAPI (t , & coderdenttest.Options {
2888
2891
Options : & coderdtest.Options {
2892
+ Database : db ,
2893
+ Pubsub : pb ,
2889
2894
IncludeProvisionerDaemon : true ,
2895
+ Clock : clock ,
2890
2896
ProvisionerdServerMetrics : provisionerdserverMetrics ,
2891
2897
},
2892
- LicenseOptions : & coderdenttest.LicenseOptions {
2893
- Features : license.Features {
2894
- codersdk .FeatureWorkspacePrebuilds : 1 ,
2895
- },
2896
- },
2897
2898
})
2898
2899
2899
- // Given: a template and a template version with a preset without prebuild instances
2900
- presetNoPrebuildID := uuid .New ()
2901
- versionNoPrebuild := coderdtest .CreateTemplateVersion (t , client , owner .OrganizationID , nil )
2902
- _ = coderdtest .AwaitTemplateVersionJobCompleted (t , client , versionNoPrebuild .ID )
2903
- templateNoPrebuild := coderdtest .CreateTemplate (t , client , owner .OrganizationID , versionNoPrebuild .ID )
2904
- presetNoPrebuild := dbgen .Preset (t , db , database.InsertPresetParams {
2905
- ID : presetNoPrebuildID ,
2906
- TemplateVersionID : versionNoPrebuild .ID ,
2907
- })
2900
+ // Setup Prebuild reconciler
2901
+ cache := files .New (prometheus .NewRegistry (), & coderdtest.FakeAuthorizer {})
2902
+ reconciler := prebuilds .NewStoreReconciler (
2903
+ db , pb , cache ,
2904
+ codersdk.PrebuildsConfig {},
2905
+ logger ,
2906
+ clock ,
2907
+ prometheus .NewRegistry (),
2908
+ notifications .NewNoopEnqueuer (),
2909
+ api .AGPL .BuildUsageChecker ,
2910
+ )
2911
+ var claimer agplprebuilds.Claimer = prebuilds .NewEnterpriseClaimer (db )
2912
+ api .AGPL .PrebuildsClaimer .Store (& claimer )
2913
+
2914
+ organizationName , err := client .Organization (ctx , owner .OrganizationID )
2915
+ require .NoError (t , err )
2916
+ userClient , user := coderdtest .CreateAnotherUser (t , client , owner .OrganizationID , rbac .RoleMember ())
2908
2917
2909
- // Given: a template and a template version with a preset with a prebuild instance
2910
- presetPrebuildID := uuid .New ()
2911
- versionPrebuild := coderdtest .CreateTemplateVersion (t , client , owner .OrganizationID , nil )
2912
- _ = coderdtest .AwaitTemplateVersionJobCompleted (t , client , versionPrebuild .ID )
2918
+ // Setup template and template version with a preset with 1 prebuild instance
2919
+ versionPrebuild := coderdtest .CreateTemplateVersion (t , client , owner .OrganizationID , templateWithAgentAndPresetsWithPrebuilds (1 ))
2920
+ coderdtest .AwaitTemplateVersionJobCompleted (t , client , versionPrebuild .ID )
2913
2921
templatePrebuild := coderdtest .CreateTemplate (t , client , owner .OrganizationID , versionPrebuild .ID )
2914
- presetPrebuild := dbgen .Preset (t , db , database.InsertPresetParams {
2915
- ID : presetPrebuildID ,
2916
- TemplateVersionID : versionPrebuild .ID ,
2917
- DesiredInstances : sql.NullInt32 {Int32 : 1 , Valid : true },
2918
- })
2919
- // Given: a prebuild workspace
2920
- wb := dbfake .WorkspaceBuild (t , db , database.WorkspaceTable {
2921
- OwnerID : database .PrebuildsSystemUserID ,
2922
- TemplateID : templatePrebuild .ID ,
2923
- }).Seed (database.WorkspaceBuild {
2924
- TemplateVersionID : versionPrebuild .ID ,
2925
- TemplateVersionPresetID : uuid.NullUUID {
2926
- UUID : presetPrebuildID ,
2927
- Valid : true ,
2928
- },
2929
- }).WithAgent (func (agent []* proto.Agent ) []* proto.Agent {
2930
- return agent
2931
- }).Do ()
2922
+ presetsPrebuild , err := client .TemplateVersionPresets (ctx , versionPrebuild .ID )
2923
+ require .NoError (t , err )
2924
+ require .Len (t , presetsPrebuild , 1 )
2932
2925
2933
- // Mark the prebuilt workspace's agent as ready so the prebuild can be claimed
2934
- // nolint:gocritic
2935
- ctx := dbauthz .AsSystemRestricted (testutil .Context (t , testutil .WaitLong ))
2936
- agent , err := db .GetWorkspaceAgentAndLatestBuildByAuthToken (ctx , uuid .MustParse (wb .AgentToken ))
2926
+ // Setup template and template version with a preset without prebuild instances
2927
+ versionNoPrebuild := coderdtest .CreateTemplateVersion (t , client , owner .OrganizationID , templateWithAgentAndPresetsWithPrebuilds (0 ))
2928
+ coderdtest .AwaitTemplateVersionJobCompleted (t , client , versionNoPrebuild .ID )
2929
+ templateNoPrebuild := coderdtest .CreateTemplate (t , client , owner .OrganizationID , versionNoPrebuild .ID )
2930
+ presetsNoPrebuild , err := client .TemplateVersionPresets (ctx , versionNoPrebuild .ID )
2937
2931
require .NoError (t , err )
2938
- err = db .UpdateWorkspaceAgentLifecycleStateByID (ctx , database.UpdateWorkspaceAgentLifecycleStateByIDParams {
2939
- ID : agent .WorkspaceAgent .ID ,
2940
- LifecycleState : database .WorkspaceAgentLifecycleStateReady ,
2932
+ require .Len (t , presetsNoPrebuild , 1 )
2933
+
2934
+ // Given: no histogram value for prebuilt workspaces creation
2935
+ prebuildCreationMetric := promhelp .MetricValue (t , reg , "coderd_workspace_creation_duration_seconds" , prometheus.Labels {
2936
+ "organization_name" : organizationName .Name ,
2937
+ "template_name" : templatePrebuild .Name ,
2938
+ "preset_name" : presetsPrebuild [0 ].Name ,
2939
+ "type" : "prebuild" ,
2941
2940
})
2942
- require .NoError (t , err )
2941
+ require .Nil (t , prebuildCreationMetric )
2943
2942
2944
- organizationName , err := client .Organization (ctx , owner .OrganizationID )
2945
- require .NoError (t , err )
2946
- user , err := client .User (ctx , "testUser" )
2947
- require .NoError (t , err )
2943
+ // Given: reconciliation loop runs and starts prebuilt workspace
2944
+ coderdenttest .MustRunReconciliationLoopForPreset (ctx , t , db , reconciler , presetsPrebuild [0 ])
2945
+ runningPrebuilds := coderdenttest .GetRunningPrebuilds (ctx , t , db , 1 )
2946
+ require .Len (t , runningPrebuilds , 1 )
2947
+
2948
+ // Then: the histogram value for prebuilt workspace creation should be updated
2949
+ prebuildCreationHistogram := promhelp .HistogramValue (t , reg , "coderd_workspace_creation_duration_seconds" , prometheus.Labels {
2950
+ "organization_name" : organizationName .Name ,
2951
+ "template_name" : templatePrebuild .Name ,
2952
+ "preset_name" : presetsPrebuild [0 ].Name ,
2953
+ "type" : "prebuild" ,
2954
+ })
2955
+ require .NotNil (t , prebuildCreationHistogram )
2956
+ require .Equal (t , uint64 (1 ), prebuildCreationHistogram .GetSampleCount ())
2957
+
2958
+ // Given: a running prebuilt workspace, ready to be claimed
2959
+ prebuild := coderdtest .MustWorkspace (t , client , runningPrebuilds [0 ].ID )
2960
+ require .Equal (t , codersdk .WorkspaceTransitionStart , prebuild .LatestBuild .Transition )
2961
+ require .Nil (t , prebuild .DormantAt )
2962
+ require .Nil (t , prebuild .DeletingAt )
2948
2963
2949
2964
// Given: no histogram value for prebuilt workspaces claim
2950
- prebuiltWorkspaceHistogramMetric := promhelp .MetricValue (t , reg , "coderd_prebuilt_workspace_claim_duration_seconds" , prometheus.Labels {
2965
+ prebuildClaimMetric := promhelp .MetricValue (t , reg , "coderd_prebuilt_workspace_claim_duration_seconds" , prometheus.Labels {
2951
2966
"organization_name" : organizationName .Name ,
2952
2967
"template_name" : templatePrebuild .Name ,
2953
- "preset_name" : presetPrebuild .Name ,
2968
+ "preset_name" : presetsPrebuild [ 0 ] .Name ,
2954
2969
})
2955
- require .Nil (t , prebuiltWorkspaceHistogramMetric )
2970
+ require .Nil (t , prebuildClaimMetric )
2956
2971
2957
2972
// Given: the prebuilt workspace is claimed by a user
2958
- claimedWorkspace , err := client .CreateUserWorkspace (ctx , user .ID .String (), codersdk.CreateWorkspaceRequest {
2959
- TemplateVersionID : versionPrebuild .ID ,
2960
- TemplateVersionPresetID : presetPrebuildID ,
2961
- Name : coderdtest .RandomUsername (t ),
2962
- })
2963
- require .NoError (t , err )
2964
- coderdtest .AwaitWorkspaceBuildJobCompleted (t , client , claimedWorkspace .LatestBuild .ID )
2965
- require .Equal (t , wb .Workspace .ID , claimedWorkspace .ID )
2973
+ workspace := coderdenttest .MustClaimPrebuild (ctx , t , client , userClient , user .Username , versionPrebuild , presetsPrebuild [0 ].ID )
2974
+ require .Equal (t , prebuild .ID , workspace .ID )
2966
2975
2967
2976
// Then: the histogram value for prebuilt workspace claim should be updated
2968
- prebuiltWorkspaceHistogram := promhelp .HistogramValue (t , reg , "coderd_prebuilt_workspace_claim_duration_seconds" , prometheus.Labels {
2977
+ prebuildClaimHistogram := promhelp .HistogramValue (t , reg , "coderd_prebuilt_workspace_claim_duration_seconds" , prometheus.Labels {
2969
2978
"organization_name" : organizationName .Name ,
2970
2979
"template_name" : templatePrebuild .Name ,
2971
- "preset_name" : presetPrebuild .Name ,
2980
+ "preset_name" : presetsPrebuild [ 0 ] .Name ,
2972
2981
})
2973
- require .NotNil (t , prebuiltWorkspaceHistogram )
2974
- require .Equal (t , uint64 (1 ), prebuiltWorkspaceHistogram .GetSampleCount ())
2982
+ require .NotNil (t , prebuildClaimHistogram )
2983
+ require .Equal (t , uint64 (1 ), prebuildClaimHistogram .GetSampleCount ())
2975
2984
2976
2985
// Given: no histogram value for regular workspaces creation
2977
2986
regularWorkspaceHistogramMetric := promhelp .MetricValue (t , reg , "coderd_workspace_creation_duration_seconds" , prometheus.Labels {
2978
2987
"organization_name" : organizationName .Name ,
2979
2988
"template_name" : templateNoPrebuild .Name ,
2980
- "preset_name" : presetNoPrebuild .Name ,
2989
+ "preset_name" : presetsNoPrebuild [ 0 ] .Name ,
2981
2990
"type" : "regular" ,
2982
2991
})
2983
2992
require .Nil (t , regularWorkspaceHistogramMetric )
2984
2993
2985
2994
// Given: a user creates a regular workspace (without prebuild pool)
2986
2995
regularWorkspace , err := client .CreateUserWorkspace (ctx , user .ID .String (), codersdk.CreateWorkspaceRequest {
2987
2996
TemplateVersionID : versionNoPrebuild .ID ,
2988
- TemplateVersionPresetID : presetNoPrebuildID ,
2997
+ TemplateVersionPresetID : presetsNoPrebuild [ 0 ]. ID ,
2989
2998
Name : coderdtest .RandomUsername (t ),
2990
2999
})
2991
3000
require .NoError (t , err )
@@ -2995,7 +3004,7 @@ func TestWorkspaceProvisionerdServerMetrics(t *testing.T) {
2995
3004
regularWorkspaceHistogram := promhelp .HistogramValue (t , reg , "coderd_workspace_creation_duration_seconds" , prometheus.Labels {
2996
3005
"organization_name" : organizationName .Name ,
2997
3006
"template_name" : templateNoPrebuild .Name ,
2998
- "preset_name" : presetNoPrebuild .Name ,
3007
+ "preset_name" : presetsNoPrebuild [ 0 ] .Name ,
2999
3008
"type" : "regular" ,
3000
3009
})
3001
3010
require .NotNil (t , regularWorkspaceHistogram )
0 commit comments