Skip to content

Commit

Permalink
osbuildexecutor: show full osbuild exector on json decode errors
Browse files Browse the repository at this point in the history
This is a short term workaround to get better visibility why
the osbuild executor sometimes sends non-json data. When reading
the result from the executor the entire output is now read and
if the json parsing goes wrong it will use the entire body
in the error message for better debug visibility.
  • Loading branch information
mvo5 authored and croissanne committed Aug 5, 2024
1 parent 0a68fe3 commit 86b1143
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 4 additions & 4 deletions internal/osbuildexecutor/runner-impl-aws-ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ func handleBuild(inputArchive, host string) (*osbuild.Result, error) {

var osbuildResult osbuild.Result

err = json.NewDecoder(resp.Body).Decode(&osbuildResult)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Unable to decode response body into osbuild result: %w", err)
return nil, fmt.Errorf("Unable to read response body: %w", err)
}

_, err = io.ReadAll(resp.Body)
err = json.Unmarshal(body, &osbuildResult)
if err != nil {
return nil, fmt.Errorf("Unable to wait for executor to close connection: %w", err)
return nil, fmt.Errorf("Unable to decode response body %q into osbuild result: %w", body, err)
}

return &osbuildResult, nil
Expand Down
18 changes: 18 additions & 0 deletions internal/osbuildexecutor/runner-impl-aws-ec2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ func TestHandleBuild(t *testing.T) {
require.True(t, osbuildResult.Success)
}

func TestHandleBuildNoJSON(t *testing.T) {
buildServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := io.ReadAll(r.Body)
require.NoError(t, err)

w.WriteHeader(http.StatusCreated)
_, err = w.Write([]byte("bad non-json text"))
require.NoError(t, err)
}))

cacheDir := t.TempDir()
inputArchive := filepath.Join(cacheDir, "test.tar")
require.NoError(t, os.WriteFile(inputArchive, []byte("test"), 0600))

_, err := osbuildexecutor.HandleBuild(inputArchive, buildServer.URL)
require.ErrorContains(t, err, `Unable to decode response body "bad non-json text" into osbuild result:`)
}

func TestHandleOutputArchive(t *testing.T) {
serverDir := t.TempDir()
serverOutputDir := filepath.Join(serverDir, "output")
Expand Down

0 comments on commit 86b1143

Please sign in to comment.