jason810496 commented on code in PR #69926:
URL: https://github.com/apache/airflow/pull/69926#discussion_r3608490085
##########
go-sdk/pkg/worker/runner_test.go:
##########
@@ -71,6 +75,80 @@ func newTestWorkLoad(id string, dagId string)
api.ExecuteTaskWorkload {
}
}
+func TestStartTaskHTTPErrorDoesNotLogToken(t *testing.T) {
+ secretToken := "super-secret-workload-token"
+ type requestDetails struct {
+ authorization string
+ correlationID string
+ method string
+ path string
+ }
+ requestReceived := make(chan requestDetails, 1)
+ server := httptest.NewServer(http.HandlerFunc(func(w
http.ResponseWriter, r *http.Request) {
+ requestReceived <- requestDetails{
+ authorization: r.Header.Get("Authorization"),
+ correlationID: r.Header.Get("Correlation-Id"),
+ method: r.Method,
+ path: r.URL.Path,
+ }
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusBadRequest)
+ if _, err := w.Write([]byte(`{"detail":"simulated start
failure"}`)); err != nil {
+ t.Errorf("failed to write test response: %v", err)
+ }
+ }))
+ t.Cleanup(server.Close)
+
+ workload := newTestWorkLoad(uuid.New().String(), "start-error-dag")
+ workload.Token = secretToken
+ workload.LogPath = nil
+ registry := bundlev1.New()
+ registry.AddDag(workload.TI.DagId).AddTaskWithName(workload.TI.TaskId,
func() error {
+ return nil
+ })
+
+ var logBuffer bytes.Buffer
+ testWorker, err := worker.NewWithBundle(registry,
slog.New(slog.NewJSONHandler(&logBuffer, nil))).
+ WithServer(server.URL + "/execution/")
+ require.NoError(t, err)
+
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second)
+ defer cancel()
+ err = testWorker.ExecuteTaskWorkload(ctx, workload)
+ require.Error(t, err)
+
+ var request requestDetails
+ select {
+ case request = <-requestReceived:
+ case <-ctx.Done():
+ t.Fatal("start-task request was not received")
+ }
+ require.Equal(t, "Bearer "+secretToken, request.authorization)
+ require.NotEmpty(t, request.correlationID)
+ require.Equal(t, http.MethodPatch, request.method)
+ require.Equal(t,
"/execution/task-instances/"+workload.TI.Id.String()+"/run", request.path)
+
+ var logEntry map[string]any
+ require.NoError(t, json.Unmarshal(logBuffer.Bytes(), &logEntry))
+ require.Equal(t, "ERROR", logEntry["level"])
+ require.Equal(t, "Server reported error when attempting to start task",
logEntry["msg"])
+ delete(logEntry, "time")
+ delete(logEntry, "level")
+ delete(logEntry, "msg")
+ require.Equal(t, map[string]any{
+ "dag_id": workload.TI.DagId,
+ "error": "client error '400 Bad Request'
map[detail:simulated start failure]",
+ "status_code": float64(http.StatusBadRequest),
+ "task_id": workload.TI.TaskId,
+ "ti_id": workload.TI.Id.String(),
+ "request": map[string]any{
+ "correlation_id": request.correlationID,
+ "method": http.MethodPatch,
+ "path": request.path,
+ },
+ }, logEntry)
+}
Review Comment:
Would it be better to just mock the error itself instead of exercising the
whole execution path?
##########
go-sdk/pkg/worker/runner.go:
##########
@@ -273,12 +273,9 @@ func (w *worker) ExecuteTaskWorkload(ctx context.Context,
workload api.ExecuteTa
slog.Int("status_code", resp.StatusCode()),
slog.Group(
"request",
- "url",
- resp.Request.URL,
- "method",
- resp.Request.Method,
- "headers",
- resp.Request.Header,
+ slog.String("path",
resp.Request.RawRequest.URL.Path),
Review Comment:
Any reason to not showing the whole URL?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]