@@ -146,12 +146,67 @@ func TestCancelJobRequestsRunningJobWithoutTerminalOverwrite(t *testing.T) { |
| 146 | } | 146 | } |
| 147 | } | 147 | } |
| 148 | | 148 | |
| | 149 | +func TestListActiveWorkflowRunsForAdminFiltersActiveRuns(t *testing.T) { |
| | 150 | + ctx := context.Background() |
| | 151 | + pool := dbtest.NewTestDB(t) |
| | 152 | + repoID, userID := setupLifecycleRepo(t, pool) |
| | 153 | + q := actionsdb.New() |
| | 154 | + |
| | 155 | + queued := insertLifecycleRun(t, pool, repoID, userID, 1) |
| | 156 | + running := insertLifecycleRun(t, pool, repoID, userID, 2) |
| | 157 | + running, err := q.StartWorkflowRun(ctx, pool, running.ID) |
| | 158 | + if err != nil { |
| | 159 | + t.Fatalf("StartWorkflowRun: %v", err) |
| | 160 | + } |
| | 161 | + completed := insertLifecycleRun(t, pool, repoID, userID, 3) |
| | 162 | + if _, err := q.CompleteWorkflowRun(ctx, pool, actionsdb.CompleteWorkflowRunParams{ |
| | 163 | + ID: completed.ID, |
| | 164 | + Conclusion: actionsdb.CheckConclusionSuccess, |
| | 165 | + }); err != nil { |
| | 166 | + t.Fatalf("CompleteWorkflowRun: %v", err) |
| | 167 | + } |
| | 168 | + otherRepoID, otherUserID := setupNamedLifecycleRepo(t, pool, "bob", "other") |
| | 169 | + otherRepoRun := insertLifecycleRun(t, pool, otherRepoID, otherUserID, 1) |
| | 170 | + |
| | 171 | + all, err := q.ListActiveWorkflowRunsForAdmin(ctx, pool, actionsdb.ListActiveWorkflowRunsForAdminParams{ |
| | 172 | + RepoID: 0, |
| | 173 | + LimitCount: 10, |
| | 174 | + }) |
| | 175 | + if err != nil { |
| | 176 | + t.Fatalf("ListActiveWorkflowRunsForAdmin all: %v", err) |
| | 177 | + } |
| | 178 | + assertRunIDs(t, all, queued.ID, running.ID, otherRepoRun.ID) |
| | 179 | + |
| | 180 | + repoOnly, err := q.ListActiveWorkflowRunsForAdmin(ctx, pool, actionsdb.ListActiveWorkflowRunsForAdminParams{ |
| | 181 | + RepoID: repoID, |
| | 182 | + LimitCount: 10, |
| | 183 | + }) |
| | 184 | + if err != nil { |
| | 185 | + t.Fatalf("ListActiveWorkflowRunsForAdmin repo: %v", err) |
| | 186 | + } |
| | 187 | + assertRunIDs(t, repoOnly, queued.ID, running.ID) |
| | 188 | + |
| | 189 | + limited, err := q.ListActiveWorkflowRunsForAdmin(ctx, pool, actionsdb.ListActiveWorkflowRunsForAdminParams{ |
| | 190 | + RepoID: 0, |
| | 191 | + LimitCount: 1, |
| | 192 | + }) |
| | 193 | + if err != nil { |
| | 194 | + t.Fatalf("ListActiveWorkflowRunsForAdmin limited: %v", err) |
| | 195 | + } |
| | 196 | + assertRunIDs(t, limited, queued.ID) |
| | 197 | +} |
| | 198 | + |
| 149 | func setupLifecycleRepo(t *testing.T, db actionsdb.DBTX) (repoID, userID int64) { | 199 | func setupLifecycleRepo(t *testing.T, db actionsdb.DBTX) (repoID, userID int64) { |
| | 200 | + t.Helper() |
| | 201 | + return setupNamedLifecycleRepo(t, db, "alice", "demo") |
| | 202 | +} |
| | 203 | + |
| | 204 | +func setupNamedLifecycleRepo(t *testing.T, db actionsdb.DBTX, username, repoName string) (repoID, userID int64) { |
| 150 | t.Helper() | 205 | t.Helper() |
| 151 | ctx := context.Background() | 206 | ctx := context.Background() |
| 152 | user, err := usersdb.New().CreateUser(ctx, db, usersdb.CreateUserParams{ | 207 | user, err := usersdb.New().CreateUser(ctx, db, usersdb.CreateUserParams{ |
| 153 | - Username: "alice", | 208 | + Username: username, |
| 154 | - DisplayName: "Alice", | 209 | + DisplayName: username, |
| 155 | PasswordHash: fixtureHash, | 210 | PasswordHash: fixtureHash, |
| 156 | }) | 211 | }) |
| 157 | if err != nil { | 212 | if err != nil { |
@@ -159,7 +214,7 @@ func setupLifecycleRepo(t *testing.T, db actionsdb.DBTX) (repoID, userID int64) |
| 159 | } | 214 | } |
| 160 | repo, err := reposdb.New().CreateRepo(ctx, db, reposdb.CreateRepoParams{ | 215 | repo, err := reposdb.New().CreateRepo(ctx, db, reposdb.CreateRepoParams{ |
| 161 | OwnerUserID: pgtype.Int8{Int64: user.ID, Valid: true}, | 216 | OwnerUserID: pgtype.Int8{Int64: user.ID, Valid: true}, |
| 162 | - Name: "demo", | 217 | + Name: repoName, |
| 163 | DefaultBranch: "trunk", | 218 | DefaultBranch: "trunk", |
| 164 | Visibility: reposdb.RepoVisibilityPublic, | 219 | Visibility: reposdb.RepoVisibilityPublic, |
| 165 | }) | 220 | }) |
@@ -169,6 +224,18 @@ func setupLifecycleRepo(t *testing.T, db actionsdb.DBTX) (repoID, userID int64) |
| 169 | return repo.ID, user.ID | 224 | return repo.ID, user.ID |
| 170 | } | 225 | } |
| 171 | | 226 | |
| | 227 | +func assertRunIDs(t *testing.T, runs []actionsdb.WorkflowRun, want ...int64) { |
| | 228 | + t.Helper() |
| | 229 | + if len(runs) != len(want) { |
| | 230 | + t.Fatalf("got %d runs, want %d: %+v", len(runs), len(want), runs) |
| | 231 | + } |
| | 232 | + for i := range want { |
| | 233 | + if runs[i].ID != want[i] { |
| | 234 | + t.Fatalf("run[%d] id=%d, want %d; runs=%+v", i, runs[i].ID, want[i], runs) |
| | 235 | + } |
| | 236 | + } |
| | 237 | +} |
| | 238 | + |
| 172 | func insertLifecycleRun(t *testing.T, db actionsdb.DBTX, repoID, userID, runIndex int64) actionsdb.WorkflowRun { | 239 | func insertLifecycleRun(t *testing.T, db actionsdb.DBTX, repoID, userID, runIndex int64) actionsdb.WorkflowRun { |
| 173 | t.Helper() | 240 | t.Helper() |
| 174 | run, err := actionsdb.New().InsertWorkflowRun(context.Background(), db, actionsdb.InsertWorkflowRunParams{ | 241 | run, err := actionsdb.New().InsertWorkflowRun(context.Background(), db, actionsdb.InsertWorkflowRunParams{ |