Skip to content

Commit f82d6ec

Browse files
committed
add tests for prebuild gauge metrics
make -B fmt
1 parent 67a3215 commit f82d6ec

File tree

5 files changed

+79
-52
lines changed

5 files changed

+79
-52
lines changed

cli/agent.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,10 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
380380
SSHMaxTimeout: sshMaxTimeout,
381381
Subsystems: subsystems,
382382

383-
PrometheusRegistry: prometheusRegistry,
384-
BlockFileTransfer: blockFileTransfer,
385-
Execer: execer,
386-
ContainerLister: containerLister,
383+
PrometheusRegistry: prometheusRegistry,
384+
BlockFileTransfer: blockFileTransfer,
385+
Execer: execer,
386+
ContainerLister: containerLister,
387387
ExperimentalDevcontainersEnabled: experimentalDevcontainersEnabled,
388388
})
389389

coderd/prebuilds/noop.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ func NewNoopReconciler() *NoopReconciler {
1414

1515
func (NoopReconciler) RunLoop(ctx context.Context) {}
1616
func (NoopReconciler) Stop(ctx context.Context, cause error) {}
17-
func (NoopReconciler) SnapshotState(ctx context.Context, store database.Store) (*ReconciliationState, error) { return &ReconciliationState{}, nil }
18-
func (NoopReconciler) DetermineActions(ctx context.Context, state PresetState) (*ReconciliationActions, error) { return &ReconciliationActions{}, nil}
19-
func (NoopReconciler) Reconcile(ctx context.Context, state PresetState, actions ReconciliationActions) error { return nil}
17+
func (NoopReconciler) SnapshotState(ctx context.Context, store database.Store) (*ReconciliationState, error) {
18+
return &ReconciliationState{}, nil
19+
}
20+
func (NoopReconciler) DetermineActions(ctx context.Context, state PresetState) (*ReconciliationActions, error) {
21+
return &ReconciliationActions{}, nil
22+
}
23+
func (NoopReconciler) Reconcile(ctx context.Context, state PresetState, actions ReconciliationActions) error {
24+
return nil
25+
}
2026

2127
var _ ReconciliationOrchestrator = NoopReconciler{}

enterprise/coderd/coderd.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package coderd
33
import (
44
"context"
55
"crypto/ed25519"
6-
"errors"
76
"fmt"
87
"math"
98
"net/http"

enterprise/coderd/prebuilds/metricscollector_test.go

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ import (
2222
"github.com/coder/quartz"
2323
)
2424

25+
type metricCheck struct {
26+
name string
27+
value *float64
28+
isCounter bool
29+
}
30+
2531
func TestMetricsCollector(t *testing.T) {
2632
t.Parallel()
2733

@@ -35,9 +41,12 @@ func TestMetricsCollector(t *testing.T) {
3541
jobStatuses []database.ProvisionerJobStatus
3642
initiatorIDs []uuid.UUID
3743
ownerIDs []uuid.UUID
38-
shouldIncrementPrebuildsCreated *bool
39-
shouldIncrementPrebuildsFailed *bool
40-
shouldIncrementPrebuildsAssigned *bool
44+
shouldIncrementPrebuildsCreated *float64
45+
shouldIncrementPrebuildsFailed *float64
46+
shouldIncrementPrebuildsAssigned *float64
47+
shouldSetDesiredPrebuilds *float64
48+
shouldSetActualPrebuilds *float64
49+
shouldSetEligiblePrebuilds *float64
4150
}
4251

4352
tests := []testCase{
@@ -50,35 +59,57 @@ func TestMetricsCollector(t *testing.T) {
5059
jobStatuses: allJobStatuses,
5160
initiatorIDs: []uuid.UUID{prebuilds.OwnerID},
5261
ownerIDs: []uuid.UUID{prebuilds.OwnerID, uuid.New()},
53-
shouldIncrementPrebuildsCreated: ptr.To(true),
62+
shouldIncrementPrebuildsCreated: ptr.To(1.0),
63+
shouldSetDesiredPrebuilds: ptr.To(1.0),
64+
shouldSetEligiblePrebuilds: ptr.To(0.0),
65+
},
66+
{
67+
name: "prebuild running",
68+
transitions: []database.WorkspaceTransition{database.WorkspaceTransitionStart},
69+
jobStatuses: []database.ProvisionerJobStatus{database.ProvisionerJobStatusSucceeded},
70+
initiatorIDs: []uuid.UUID{prebuilds.OwnerID},
71+
ownerIDs: []uuid.UUID{prebuilds.OwnerID},
72+
shouldIncrementPrebuildsCreated: ptr.To(1.0),
73+
shouldSetDesiredPrebuilds: ptr.To(1.0),
74+
shouldSetActualPrebuilds: ptr.To(1.0),
75+
shouldSetEligiblePrebuilds: ptr.To(0.0),
5476
},
5577
{
5678
name: "prebuild failed",
5779
transitions: allTransitions,
5880
jobStatuses: []database.ProvisionerJobStatus{database.ProvisionerJobStatusFailed},
5981
initiatorIDs: []uuid.UUID{prebuilds.OwnerID},
6082
ownerIDs: []uuid.UUID{prebuilds.OwnerID, uuid.New()},
61-
shouldIncrementPrebuildsCreated: ptr.To(true),
62-
shouldIncrementPrebuildsFailed: ptr.To(true),
83+
shouldIncrementPrebuildsCreated: ptr.To(1.0),
84+
shouldIncrementPrebuildsFailed: ptr.To(1.0),
85+
shouldSetDesiredPrebuilds: ptr.To(1.0),
86+
shouldSetActualPrebuilds: ptr.To(0.0),
87+
shouldSetEligiblePrebuilds: ptr.To(0.0),
6388
},
6489
{
6590
name: "prebuild assigned",
6691
transitions: allTransitions,
6792
jobStatuses: allJobStatuses,
6893
initiatorIDs: []uuid.UUID{prebuilds.OwnerID},
6994
ownerIDs: []uuid.UUID{uuid.New()},
70-
shouldIncrementPrebuildsCreated: ptr.To(true),
71-
shouldIncrementPrebuildsAssigned: ptr.To(true),
95+
shouldIncrementPrebuildsCreated: ptr.To(1.0),
96+
shouldIncrementPrebuildsAssigned: ptr.To(1.0),
97+
shouldSetDesiredPrebuilds: ptr.To(1.0),
98+
shouldSetActualPrebuilds: ptr.To(0.0),
99+
shouldSetEligiblePrebuilds: ptr.To(0.0),
72100
},
73101
{
74102
name: "workspaces that were not created by the prebuilds user are not counted",
75103
transitions: allTransitions,
76104
jobStatuses: allJobStatuses,
77105
initiatorIDs: []uuid.UUID{uuid.New()},
78106
ownerIDs: []uuid.UUID{uuid.New()},
79-
shouldIncrementPrebuildsCreated: ptr.To(false),
80-
shouldIncrementPrebuildsFailed: ptr.To(false),
81-
shouldIncrementPrebuildsAssigned: ptr.To(false),
107+
shouldIncrementPrebuildsCreated: nil,
108+
shouldIncrementPrebuildsFailed: nil,
109+
shouldIncrementPrebuildsAssigned: nil,
110+
shouldSetDesiredPrebuilds: ptr.To(1.0),
111+
shouldSetActualPrebuilds: ptr.To(0.0),
112+
shouldSetEligiblePrebuilds: ptr.To(0.0),
82113
},
83114
}
84115
for _, test := range tests {
@@ -121,7 +152,7 @@ func TestMetricsCollector(t *testing.T) {
121152
for i := 0; i < numTemplates; i++ {
122153
orgID, templateID := setupTestDBTemplate(t, db, ownerID)
123154
templateVersionID := setupTestDBTemplateVersion(t, ctx, db, pubsub, orgID, ownerID, templateID)
124-
preset := setupTestDBPreset(t, ctx, db, pubsub, templateVersionID, 1)
155+
preset := setupTestDBPreset(t, ctx, db, pubsub, templateVersionID, 1, uuid.New().String())
125156
setupTestDBPrebuild(
126157
t, ctx, db, pubsub,
127158
transition, jobStatus, orgID, templateID, templateVersionID, preset.ID, initiatorID, ownerID,
@@ -147,42 +178,31 @@ func TestMetricsCollector(t *testing.T) {
147178
require.Equal(t, 1, len(presets))
148179

149180
for _, preset := range presets {
150-
if test.shouldIncrementPrebuildsCreated != nil {
151-
metric := findMetric(metricsFamilies, "coderd_prebuilds_created", map[string]string{
152-
"template_name": template.Name,
153-
"preset_name": preset.Name,
154-
})
155-
if *test.shouldIncrementPrebuildsCreated {
156-
require.NotNil(t, metric)
157-
require.Equal(t, metric.GetCounter().GetValue(), 1.0)
158-
} else {
159-
require.Nil(t, metric)
160-
}
181+
checks := []metricCheck{
182+
{"coderd_prebuilds_created", test.shouldIncrementPrebuildsCreated, true},
183+
{"coderd_prebuilds_failed", test.shouldIncrementPrebuildsFailed, true},
184+
{"coderd_prebuilds_assigned", test.shouldIncrementPrebuildsAssigned, true},
185+
{"coderd_prebuilds_desired", test.shouldSetDesiredPrebuilds, false},
186+
{"coderd_prebuilds_actual", test.shouldSetActualPrebuilds, false},
187+
{"coderd_prebuilds_eligible", test.shouldSetEligiblePrebuilds, false},
161188
}
162189

163-
if test.shouldIncrementPrebuildsFailed != nil {
164-
metric := findMetric(metricsFamilies, "coderd_prebuilds_failed", map[string]string{
165-
"template_name": template.Name,
166-
"preset_name": preset.Name,
167-
})
168-
if *test.shouldIncrementPrebuildsFailed {
169-
require.NotNil(t, metric)
170-
require.Equal(t, metric.GetCounter().GetValue(), 1.0)
171-
} else {
172-
require.Nil(t, metric)
173-
}
190+
labels := map[string]string{
191+
"template_name": template.Name,
192+
"preset_name": preset.Name,
174193
}
175194

176-
if test.shouldIncrementPrebuildsAssigned != nil {
177-
metric := findMetric(metricsFamilies, "coderd_prebuilds_assigned", map[string]string{
178-
"template_name": template.Name,
179-
"preset_name": preset.Name,
180-
})
181-
if *test.shouldIncrementPrebuildsAssigned {
182-
require.NotNil(t, metric)
183-
require.Equal(t, metric.GetCounter().GetValue(), 1.0)
195+
for _, check := range checks {
196+
metric := findMetric(metricsFamilies, check.name, labels)
197+
if check.value == nil {
198+
continue
199+
}
200+
201+
require.NotNil(t, metric, "metric %s should exist", check.name)
202+
if check.isCounter {
203+
require.Equal(t, *check.value, metric.GetCounter().GetValue(), "counter %s value mismatch", check.name)
184204
} else {
185-
require.Nil(t, metric)
205+
require.Equal(t, *check.value, metric.GetGauge().GetValue(), "gauge %s value mismatch", check.name)
186206
}
187207
}
188208
}

enterprise/coderd/prebuilds/reconcile_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ func TestPrebuildReconciliation(t *testing.T) {
309309
pubsub,
310310
templateVersionID,
311311
1,
312+
uuid.New().String(),
312313
)
313314
prebuildID := setupTestDBPrebuild(
314315
t,
@@ -439,11 +440,12 @@ func setupTestDBPreset(
439440
ps pubsub.Pubsub,
440441
templateVersionID uuid.UUID,
441442
desiredInstances int32,
443+
presetName string,
442444
) database.TemplateVersionPreset {
443445
t.Helper()
444446
preset := dbgen.Preset(t, db, database.InsertPresetParams{
445447
TemplateVersionID: templateVersionID,
446-
Name: "test",
448+
Name: presetName,
447449
})
448450
dbgen.PresetParameter(t, db, database.InsertPresetParametersParams{
449451
TemplateVersionPresetID: preset.ID,

0 commit comments

Comments
 (0)