From 868a10982a823b4491f1971fd0b423a0e7c42ed8 Mon Sep 17 00:00:00 2001 From: Tom Wieczorek Date: Mon, 9 Dec 2024 11:12:25 +0100 Subject: [PATCH] Honor IO errors in list-images sub-command Previously, the sub-command did not interrupt the loop and returned successfully, even when it encountered IO errors while writing to standard out. Signed-off-by: Tom Wieczorek --- cmd/airgap/listimages.go | 5 ++++- cmd/airgap/listimages_test.go | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/cmd/airgap/listimages.go b/cmd/airgap/listimages.go index 1291de8a22e7..34cf42a7adc0 100644 --- a/cmd/airgap/listimages.go +++ b/cmd/airgap/listimages.go @@ -48,8 +48,11 @@ func NewAirgapListImagesCmd() *cobra.Command { return fmt.Errorf("failed to get config: %w", err) } + out := cmd.OutOrStdout() for _, uri := range airgap.GetImageURIs(clusterConfig.Spec, all) { - fmt.Fprintln(cmd.OutOrStdout(), uri) + if _, err := fmt.Fprintln(out, uri); err != nil { + return err + } } return nil }, diff --git a/cmd/airgap/listimages_test.go b/cmd/airgap/listimages_test.go index 171f62b50518..be20ffc85a87 100644 --- a/cmd/airgap/listimages_test.go +++ b/cmd/airgap/listimages_test.go @@ -26,7 +26,9 @@ import ( "testing" "testing/iotest" + internalio "github.com/k0sproject/k0s/internal/io" "github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1" + "github.com/spf13/cobra" "github.com/stretchr/testify/assert" @@ -42,6 +44,23 @@ func TestAirgapListImages(t *testing.T) { defaultImage := v1beta1.DefaultEnvoyProxyImage().URI() + t.Run("HonorsIOErrors", func(t *testing.T) { + var writes uint + underTest := NewAirgapListImagesCmd() + underTest.SetIn(iotest.ErrReader(errors.New("unexpected read from standard input"))) + underTest.SilenceUsage = true // Cobra writes usage to stdout on errors 🤔 + underTest.SetOut(internalio.WriterFunc(func(p []byte) (int, error) { + writes++ + return 0, assert.AnError + })) + var stderr strings.Builder + underTest.SetErr(&stderr) + + assert.Same(t, assert.AnError, underTest.Execute()) + assert.Equal(t, uint(1), writes, "Expected a single write to stdout") + assert.Equal(t, fmt.Sprintf("Error: %v\n", assert.AnError), stderr.String()) + }) + t.Run("All", func(t *testing.T) { underTest, out, err := newAirgapListImagesCmdWithConfig(t, "{}", "--all")