Skip to content

Commit d55026e

Browse files
committed
Update query/tests to handle offline param or offline status
1 parent fe50c08 commit d55026e

File tree

6 files changed

+95
-57
lines changed

6 files changed

+95
-57
lines changed

cli/provisioners_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,23 @@ func TestProvisioners_Golden(t *testing.T) {
232232
clitest.TestGoldenFile(t, t.Name(), got.Bytes(), replace)
233233
})
234234

235+
t.Run("list provisioner daemons without offline", func(t *testing.T) {
236+
t.Parallel()
237+
238+
var got bytes.Buffer
239+
inv, root := clitest.New(t,
240+
"provisioners",
241+
"list",
242+
"--status=idle,busy",
243+
)
244+
inv.Stdout = &got
245+
clitest.SetupConfig(t, templateAdminClient, root)
246+
err := inv.Run()
247+
require.NoError(t, err)
248+
249+
clitest.TestGoldenFile(t, t.Name(), got.Bytes(), replace)
250+
})
251+
235252
// Test jobs list with template admin as members are currently
236253
// unable to access provisioner jobs. In the future (with RBAC
237254
// changes), we may allow them to view _their_ jobs.
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
CREATED AT LAST SEEN AT KEY NAME NAME VERSION STATUS TAGS
2-
====[timestamp]===== ====[timestamp]===== built-in default-provisioner v0.0.0-devel idle map[owner: scope:organization]
3-
====[timestamp]===== ====[timestamp]===== built-in provisioner-1 v0.0.0 busy map[foo:bar owner: scope:organization]
4-
====[timestamp]===== ====[timestamp]===== built-in provisioner-3 v0.0.0 idle map[owner: scope:organization]
1+
CREATED AT LAST SEEN AT KEY NAME NAME VERSION STATUS TAGS
2+
====[timestamp]===== ====[timestamp]===== built-in default-provisioner v0.0.0-devel idle map[owner: scope:organization]
3+
====[timestamp]===== ====[timestamp]===== built-in provisioner-1 v0.0.0 busy map[foo:bar owner: scope:organization]
4+
====[timestamp]===== ====[timestamp]===== built-in provisioner-2 v0.0.0 offline map[owner: scope:organization]
5+
====[timestamp]===== ====[timestamp]===== built-in provisioner-3 v0.0.0 idle map[owner: scope:organization]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATED AT LAST SEEN AT KEY NAME NAME VERSION STATUS TAGS
2+
====[timestamp]===== ====[timestamp]===== built-in default-provisioner v0.0.0-devel idle map[owner: scope:organization]
3+
====[timestamp]===== ====[timestamp]===== built-in provisioner-1 v0.0.0 busy map[foo:bar owner: scope:organization]
4+
====[timestamp]===== ====[timestamp]===== built-in provisioner-3 v0.0.0 idle map[owner: scope:organization]

coderd/database/querier_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -609,15 +609,16 @@ func TestGetProvisionerDaemonsWithStatusByOrganization(t *testing.T) {
609609
},
610610
expectedNum: 1,
611611
},
612+
// Offline daemons should not be included without Offline param
612613
{
613-
name: "Get all - empty statuses",
614+
name: "Get idle - empty statuses",
614615
statuses: []database.ProvisionerDaemonStatus{},
615-
expectedNum: 2,
616+
expectedNum: 1,
616617
},
617618
{
618-
name: "Get all - nil statuses",
619+
name: "Get idle - nil statuses",
619620
statuses: nil,
620-
expectedNum: 2,
621+
expectedNum: 1,
621622
},
622623
}
623624

@@ -627,7 +628,6 @@ func TestGetProvisionerDaemonsWithStatusByOrganization(t *testing.T) {
627628
daemons, err := db.GetProvisionerDaemonsWithStatusByOrganization(context.Background(), database.GetProvisionerDaemonsWithStatusByOrganizationParams{
628629
OrganizationID: org.ID,
629630
StaleIntervalMS: 45 * time.Minute.Milliseconds(),
630-
Offline: sql.NullBool{Bool: true, Valid: true},
631631
Statuses: tc.statuses,
632632
})
633633
require.NoError(t, err)

coderd/database/queries.sql.go

Lines changed: 36 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/provisionerdaemons.sql

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ WHERE
3232
SELECT
3333
sqlc.embed(pd),
3434
CASE
35-
WHEN pd.last_seen_at IS NULL OR pd.last_seen_at < (NOW() - (@stale_interval_ms::bigint || ' ms')::interval)
36-
THEN 'offline'
37-
ELSE CASE
38-
WHEN current_job.id IS NOT NULL THEN 'busy'
39-
ELSE 'idle'
40-
END
41-
END::provisioner_daemon_status AS status,
35+
WHEN current_job.id IS NOT NULL THEN 'busy'::provisioner_daemon_status
36+
WHEN (COALESCE(sqlc.narg('offline')::bool, false) = true
37+
OR 'offline'::provisioner_daemon_status = ANY(@statuses::provisioner_daemon_status[]))
38+
AND (pd.last_seen_at IS NULL OR pd.last_seen_at < (NOW() - (@stale_interval_ms::bigint || ' ms')::interval))
39+
THEN 'offline'::provisioner_daemon_status
40+
ELSE 'idle'::provisioner_daemon_status
41+
END AS status,
4242
pk.name AS key_name,
4343
-- NOTE(mafredri): sqlc.embed doesn't support nullable tables nor renaming them.
4444
current_job.id AS current_job_id,
@@ -110,21 +110,29 @@ WHERE
110110
pd.organization_id = @organization_id::uuid
111111
AND (COALESCE(array_length(@ids::uuid[], 1), 0) = 0 OR pd.id = ANY(@ids::uuid[]))
112112
AND (@tags::tagset = 'null'::tagset OR provisioner_tagset_contains(pd.tags::tagset, @tags::tagset))
113-
-- Filter by status array if any status values are provided
114-
AND (COALESCE(array_length(@statuses::provisioner_daemon_status[], 1), 0) = 0 OR
115-
(CASE
116-
WHEN pd.last_seen_at IS NULL OR pd.last_seen_at < (NOW() - (@stale_interval_ms::bigint || ' ms')::interval)
117-
THEN 'offline'::provisioner_daemon_status
118-
ELSE CASE
119-
WHEN current_job.id IS NOT NULL THEN 'busy'::provisioner_daemon_status
120-
ELSE 'idle'::provisioner_daemon_status
121-
END
122-
END) = ANY(@statuses::provisioner_daemon_status[]))
123-
-- Include offline daemons only if offline is set to true
124113
AND (
125-
COALESCE(sqlc.narg('offline')::bool, false) = true
126-
OR
114+
-- Include daemons that have been seen recently
127115
(pd.last_seen_at IS NOT NULL AND pd.last_seen_at >= (NOW() - (@stale_interval_ms::bigint || ' ms')::interval))
116+
-- Include offline daemons only when offline param is set OR 'offline' is in the list of statuses
117+
OR (
118+
(pd.last_seen_at IS NULL OR pd.last_seen_at < (NOW() - (@stale_interval_ms::bigint || ' ms')::interval))
119+
AND (
120+
COALESCE(sqlc.narg('offline')::bool, false) = true
121+
OR 'offline'::provisioner_daemon_status = ANY(@statuses::provisioner_daemon_status[])
122+
)
123+
)
124+
)
125+
-- Filter daemons by their current status if statuses are provided
126+
AND (
127+
COALESCE(array_length(@statuses::provisioner_daemon_status[], 1), 0) = 0
128+
OR (
129+
(current_job.id IS NOT NULL AND 'busy'::provisioner_daemon_status = ANY(@statuses::provisioner_daemon_status[]))
130+
OR (current_job.id IS NULL AND 'idle'::provisioner_daemon_status = ANY(@statuses::provisioner_daemon_status[]))
131+
)
132+
OR (
133+
(COALESCE(sqlc.narg('offline')::bool, false) = true OR 'offline'::provisioner_daemon_status = ANY(@statuses::provisioner_daemon_status[]))
134+
AND (pd.last_seen_at IS NULL OR pd.last_seen_at < (NOW() - (@stale_interval_ms::bigint || ' ms')::interval))
135+
)
128136
)
129137
ORDER BY
130138
pd.created_at DESC

0 commit comments

Comments
 (0)