diff --git a/.nextchanges/air/list-skip-purged-runs.md b/.nextchanges/air/list-skip-purged-runs.md new file mode 100644 index 00000000000..dabb64e7138 --- /dev/null +++ b/.nextchanges/air/list-skip-purged-runs.md @@ -0,0 +1 @@ +* `air list --all-status` no longer fails when a listed run has been purged. ([#7523](https://github.com/databricks/cli/pull/7523)) diff --git a/cmd/air/joblist.go b/cmd/air/joblist.go index dd786d8b496..0f3ce4a9740 100644 --- a/cmd/air/joblist.go +++ b/cmd/air/joblist.go @@ -146,7 +146,8 @@ func fetchJobRun(ctx context.Context, w *databricks.WorkspaceClient, runID int64 // hydrateJobRuns fetches the given run ids concurrently via runs/get, preserving // input order. runs/get enforces per-run view ACLs, so an id the caller can't -// view (403) or that has been purged (404) is dropped; any other error is +// view (403) or that has been purged (404, or the 400 "Run does not exist." +// that the SDK maps to ErrResourceDoesNotExist) is dropped; any other error is // systemic and fails the whole batch. func hydrateJobRuns(ctx context.Context, w *databricks.WorkspaceClient, ids []int64) ([]*jobs.Run, error) { runs := make([]*jobs.Run, len(ids)) @@ -156,11 +157,14 @@ func hydrateJobRuns(ctx context.Context, w *databricks.WorkspaceClient, ids []in g.Go(func() error { run, err := fetchJobRun(gctx, w, id) if err != nil { + if errors.Is(err, apierr.ErrResourceDoesNotExist) { + return nil // purged: drop this id + } if apiErr, ok := errors.AsType[*apierr.APIError](err); ok && (apiErr.StatusCode == http.StatusForbidden || apiErr.StatusCode == http.StatusNotFound) { return nil // not viewable or purged: drop this id } - return fmt.Errorf("failed to get run %d: %w", id, err) + return err } runs[i] = run return nil diff --git a/cmd/air/joblist_test.go b/cmd/air/joblist_test.go index 11e5efac8e9..33c69970dbc 100644 --- a/cmd/air/joblist_test.go +++ b/cmd/air/joblist_test.go @@ -5,6 +5,7 @@ import ( "net/http/httptest" "testing" + "github.com/databricks/databricks-sdk-go/service/jobs" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -45,3 +46,76 @@ func TestFetchJobRunParsesAiRuntimeTask(t *testing.T) { assert.Equal(t, "GPU_1xA10", gpuType) assert.Equal(t, 1, count) } + +type runGetResponse struct { + status int + body string +} + +// runGetByIDServer serves runs/get per run_id and a stub for the SDK config +// probe; ids missing from responses return a run with that id. +func runGetByIDServer(t *testing.T, responses map[string]runGetResponse) *httptest.Server { + t.Helper() + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/2.2/jobs/runs/get" { + _, _ = w.Write([]byte(`{}`)) + return + } + id := r.URL.Query().Get("run_id") + resp, ok := responses[id] + if !ok { + resp = runGetResponse{status: http.StatusOK, body: `{"run_id": ` + id + `}`} + } + w.WriteHeader(resp.status) + _, _ = w.Write([]byte(resp.body)) + })) + t.Cleanup(srv.Close) + return srv +} + +func runIDs(runs []*jobs.Run) []int64 { + ids := make([]int64, len(runs)) + for i, r := range runs { + ids[i] = r.RunId + } + return ids +} + +func TestHydrateJobRunsDropsUnviewableAndPurgedRuns(t *testing.T) { + srv := runGetByIDServer(t, map[string]runGetResponse{ + "2": {http.StatusBadRequest, `{"error_code": "INVALID_PARAMETER_VALUE", "message": "Run 2 does not exist."}`}, + "4": {http.StatusForbidden, `{"error_code": "PERMISSION_DENIED", "message": "denied"}`}, + "5": {http.StatusNotFound, `{"error_code": "NOT_FOUND", "message": "not found"}`}, + }) + + runs, err := hydrateJobRuns(t.Context(), newTestWorkspaceClient(t, srv.URL), []int64{1, 2, 3, 4, 5, 6}) + require.NoError(t, err) + assert.Equal(t, []int64{1, 3, 6}, runIDs(runs)) +} + +func TestHydrateJobRunsFailsOnOtherErrors(t *testing.T) { + tests := []struct { + name string + resp runGetResponse + wantErr string + }{ + { + name: "internal error", + resp: runGetResponse{http.StatusInternalServerError, `{"error_code": "INTERNAL_ERROR", "message": "boom"}`}, + wantErr: "failed to get run 2: boom", + }, + { + name: "other invalid parameter", + resp: runGetResponse{http.StatusBadRequest, `{"error_code": "INVALID_PARAMETER_VALUE", "message": "run_id must be positive"}`}, + wantErr: "failed to get run 2: run_id must be positive", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + srv := runGetByIDServer(t, map[string]runGetResponse{"2": tt.resp}) + + _, err := hydrateJobRuns(t.Context(), newTestWorkspaceClient(t, srv.URL), []int64{1, 2, 3}) + assert.EqualError(t, err, tt.wantErr) + }) + } +}