-
Notifications
You must be signed in to change notification settings - Fork 9
fix: do not show flashing instructions when build failed #294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bennyz
merged 1 commit into
centos-automotive-suite:main
from
bennyz:fix-flash-instruction
May 28, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,280 @@ | ||
| package buildcmd | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| buildapitypes "github.com/centos-automotive-suite/automotive-dev-operator/internal/buildapi" | ||
| buildapiclient "github.com/centos-automotive-suite/automotive-dev-operator/internal/buildapi/client" | ||
| ) | ||
|
|
||
| func newTestOpts() Options { | ||
| opts := newTestDiskOpts() | ||
| var ( | ||
| workspace string | ||
| extraRepos []string | ||
| flashCmd string | ||
| exporterSelector string | ||
| ) | ||
| opts.Workspace = &workspace | ||
| opts.ExtraRepos = &extraRepos | ||
| opts.FlashCmd = &flashCmd | ||
| opts.ExporterSelector = &exporterSelector | ||
| return opts | ||
| } | ||
|
|
||
| // fakeBuildServer creates an httptest.Server that responds to /v1/builds/<name> | ||
| // with the given BuildResponse sequence. Each call to GetBuild returns the next | ||
| // response; once exhausted it repeats the last one. Progress endpoint returns 404. | ||
| func fakeBuildServer(t *testing.T, responses []buildapitypes.BuildResponse) *httptest.Server { | ||
| t.Helper() | ||
| callIdx := 0 | ||
| return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if strings.Contains(r.URL.Path, "/progress") { | ||
| w.WriteHeader(http.StatusNotFound) | ||
| return | ||
| } | ||
| idx := callIdx | ||
| if idx < len(responses)-1 { | ||
| callIdx++ | ||
| } | ||
| w.Header().Set("Content-Type", "application/json") | ||
| if err := json.NewEncoder(w).Encode(responses[idx]); err != nil { | ||
| t.Errorf("failed to encode response: %v", err) | ||
| } | ||
| })) | ||
| } | ||
|
|
||
| func TestWaitForBuildCompletion_BuildFailedNoDiskImage(t *testing.T) { | ||
| responses := []buildapitypes.BuildResponse{ | ||
| { | ||
| Name: "test-build", | ||
| Phase: "Failed", | ||
| Message: `"step-build-image" exited with code 1`, | ||
| }, | ||
| } | ||
| srv := fakeBuildServer(t, responses) | ||
| defer srv.Close() | ||
|
|
||
| opts := newTestOpts() | ||
| *opts.ServerURL = srv.URL | ||
| timeout := 1 | ||
| opts.Timeout = &timeout | ||
|
|
||
| var capturedErr error | ||
| opts.HandleError = func(err error) { capturedErr = err } | ||
| h := NewHandler(opts) | ||
|
|
||
| api, err := buildapiclient.New(srv.URL) | ||
| if err != nil { | ||
| t.Fatalf("failed to create client: %v", err) | ||
| } | ||
|
|
||
| // Capture stdout to verify no flash instructions printed | ||
| old := os.Stdout | ||
| r, w, _ := os.Pipe() | ||
| os.Stdout = w | ||
|
|
||
| waitErr := h.waitForBuildCompletion(t.Context(), api, "test-build") | ||
|
|
||
| _ = w.Close() | ||
| out, _ := io.ReadAll(r) | ||
| os.Stdout = old | ||
|
|
||
| if waitErr == nil { | ||
| t.Fatal("expected error from waitForBuildCompletion") | ||
| } | ||
|
|
||
| output := string(out) | ||
| if strings.Contains(output, "Flash failed") { | ||
| t.Errorf("should not show flash instructions when build itself failed, got: %s", output) | ||
| } | ||
| if strings.Contains(output, "flash manually") { | ||
| t.Errorf("should not show manual flash guidance when build failed, got: %s", output) | ||
| } | ||
| if capturedErr == nil { | ||
| t.Fatal("expected HandleError to be called") | ||
| } | ||
| if !strings.Contains(capturedErr.Error(), "step-build-image") { | ||
| t.Errorf("error should contain build failure message, got: %v", capturedErr) | ||
| } | ||
| } | ||
|
|
||
| func TestWaitForBuildCompletion_BuildFailedWithDiskImage_NotFlashFailure(t *testing.T) { | ||
| // Edge case: server returns DiskImage on a build failure (shouldn't happen | ||
| // with server fix, but tests client-side defense-in-depth). | ||
| responses := []buildapitypes.BuildResponse{ | ||
| { | ||
| Name: "test-build", | ||
| Phase: "Failed", | ||
| Message: `"step-build-image" exited with code 1`, | ||
| DiskImage: "registry.example.com/img:disk", | ||
| }, | ||
| } | ||
| srv := fakeBuildServer(t, responses) | ||
| defer srv.Close() | ||
|
|
||
| opts := newTestOpts() | ||
| *opts.ServerURL = srv.URL | ||
| *opts.FlashAfterBuild = true | ||
| timeout := 1 | ||
| opts.Timeout = &timeout | ||
|
|
||
| var capturedErr error | ||
| opts.HandleError = func(err error) { capturedErr = err } | ||
| h := NewHandler(opts) | ||
|
|
||
| api, err := buildapiclient.New(srv.URL) | ||
| if err != nil { | ||
| t.Fatalf("failed to create client: %v", err) | ||
| } | ||
|
|
||
| old := os.Stdout | ||
| r, w, _ := os.Pipe() | ||
| os.Stdout = w | ||
|
|
||
| waitErr := h.waitForBuildCompletion(t.Context(), api, "test-build") | ||
|
|
||
| _ = w.Close() | ||
| out, _ := io.ReadAll(r) | ||
| os.Stdout = old | ||
|
|
||
| if waitErr == nil { | ||
| t.Fatal("expected error from waitForBuildCompletion") | ||
| } | ||
|
|
||
| output := string(out) | ||
| // Even though DiskImage is set and FlashAfterBuild is true, the message | ||
| // does not indicate a flash failure, so no flash instructions should appear. | ||
| if strings.Contains(output, "Flash failed") { | ||
| t.Errorf("should not show flash instructions for build failure (not flash failure), got: %s", output) | ||
| } | ||
| if capturedErr == nil { | ||
| t.Fatal("expected HandleError to be called") | ||
| } | ||
| } | ||
|
|
||
| func TestWaitForBuildCompletion_FlashFailure_ShowsFlashInstructions(t *testing.T) { | ||
| responses := []buildapitypes.BuildResponse{ | ||
| { | ||
| Name: "test-build", | ||
| Phase: "Failed", | ||
| Message: "Flash to device failed: timeout waiting for device", | ||
| DiskImage: "registry.example.com/ns/test-build:disk", | ||
| Jumpstarter: &buildapitypes.JumpstarterInfo{ | ||
| Available: true, | ||
| ExporterSelector: "board-type=renesas-rcar-s4,enabled=true", | ||
| FlashCmd: "j storage flash oci://registry.example.com/ns/test-build:disk", | ||
| }, | ||
| }, | ||
| } | ||
| srv := fakeBuildServer(t, responses) | ||
| defer srv.Close() | ||
|
|
||
| opts := newTestOpts() | ||
| *opts.ServerURL = srv.URL | ||
| *opts.FlashAfterBuild = true | ||
| timeout := 1 | ||
| opts.Timeout = &timeout | ||
|
|
||
| var capturedErr error | ||
| opts.HandleError = func(err error) { capturedErr = err } | ||
| h := NewHandler(opts) | ||
|
|
||
| api, err := buildapiclient.New(srv.URL) | ||
| if err != nil { | ||
| t.Fatalf("failed to create client: %v", err) | ||
| } | ||
|
|
||
| old := os.Stdout | ||
| r, w, _ := os.Pipe() | ||
| os.Stdout = w | ||
|
|
||
| waitErr := h.waitForBuildCompletion(t.Context(), api, "test-build") | ||
|
|
||
| _ = w.Close() | ||
| out, _ := io.ReadAll(r) | ||
| os.Stdout = old | ||
|
|
||
| if waitErr == nil { | ||
| t.Fatal("expected error from waitForBuildCompletion") | ||
| } | ||
|
|
||
| output := string(out) | ||
| if !strings.Contains(output, "Flash failed") { | ||
| t.Errorf("expected flash failure message, got: %s", output) | ||
| } | ||
| if !strings.Contains(output, "flash manually") { | ||
| t.Errorf("expected manual flash guidance, got: %s", output) | ||
| } | ||
| if !strings.Contains(output, "j storage flash") { | ||
| t.Errorf("expected flash command in output, got: %s", output) | ||
| } | ||
| if capturedErr == nil { | ||
| t.Fatal("expected HandleError to be called") | ||
| } | ||
| if !strings.Contains(capturedErr.Error(), "Flash to device failed") { | ||
| t.Errorf("error should contain flash failure message, got: %v", capturedErr) | ||
| } | ||
| } | ||
|
|
||
| func TestWaitForBuildCompletion_FlashFailure_NoJumpstarter(t *testing.T) { | ||
| // Flash failure detected but no Jumpstarter info — should still call | ||
| // handleFlashError (which calls handleError) but not print flash instructions. | ||
| responses := []buildapitypes.BuildResponse{ | ||
| { | ||
| Name: "test-build", | ||
| Phase: "Failed", | ||
| Message: "Flash to device failed: connection refused", | ||
| DiskImage: "registry.example.com/ns/test-build:disk", | ||
| }, | ||
| } | ||
| srv := fakeBuildServer(t, responses) | ||
| defer srv.Close() | ||
|
|
||
| opts := newTestOpts() | ||
| *opts.ServerURL = srv.URL | ||
| *opts.FlashAfterBuild = true | ||
| timeout := 1 | ||
| opts.Timeout = &timeout | ||
|
|
||
| var capturedErr error | ||
| opts.HandleError = func(err error) { capturedErr = err } | ||
| h := NewHandler(opts) | ||
|
|
||
| api, err := buildapiclient.New(srv.URL) | ||
| if err != nil { | ||
| t.Fatalf("failed to create client: %v", err) | ||
| } | ||
|
|
||
| old := os.Stdout | ||
| r, w, _ := os.Pipe() | ||
| os.Stdout = w | ||
|
|
||
| waitErr := h.waitForBuildCompletion(t.Context(), api, "test-build") | ||
|
|
||
| _ = w.Close() | ||
| out, _ := io.ReadAll(r) | ||
| os.Stdout = old | ||
|
|
||
| if waitErr == nil { | ||
| t.Fatal("expected error from waitForBuildCompletion") | ||
| } | ||
|
|
||
| output := string(out) | ||
| // No Jumpstarter info, so no "flash manually" instructions should appear | ||
| if strings.Contains(output, "flash manually") { | ||
| t.Errorf("should not show flash instructions without Jumpstarter info, got: %s", output) | ||
| } | ||
| if capturedErr == nil { | ||
| t.Fatal("expected HandleError to be called") | ||
| } | ||
| if !strings.Contains(capturedErr.Error(), "Flash to device failed") { | ||
| t.Errorf("error should contain flash failure message, got: %v", capturedErr) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: centos-automotive-suite/automotive-dev-operator
Length of output: 205
🏁 Script executed:
Repository: centos-automotive-suite/automotive-dev-operator
Length of output: 131
🏁 Script executed:
Repository: centos-automotive-suite/automotive-dev-operator
Length of output: 4353
🏁 Script executed:
Repository: centos-automotive-suite/automotive-dev-operator
Length of output: 6974
🏁 Script executed:
Repository: centos-automotive-suite/automotive-dev-operator
Length of output: 4990
🏁 Script executed:
Repository: centos-automotive-suite/automotive-dev-operator
Length of output: 8112
🏁 Script executed:
Repository: centos-automotive-suite/automotive-dev-operator
Length of output: 254
🏁 Script executed:
Repository: centos-automotive-suite/automotive-dev-operator
Length of output: 864
Add tests covering flash-specific
phaseFailedhandling (hasImage && isFlashFailure)cmd/caib/buildcmd/logs.gogates the flash-error path inwaitForBuildCompletionwithhasImage := st.DiskImage != "" || st.ContainerImage != ""andisFlashFailure := ..., then callsdisplayBuildResults+handleFlashErroronly whenhasImage && isFlashFailure. Current tests undercmd/caib/buildcmd/*_test.godon’t exercise thephaseFailedbranch or assert any flash error/results output (no coverage ofwaitForBuildCompletion,handleFlashError, ordisplayBuildResults).🤖 Prompt for AI Agents