Skip to content
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

Honor IO errors in list-images sub-command #5330

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cmd/airgap/listimages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
40 changes: 24 additions & 16 deletions cmd/airgap/listimages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ limitations under the License.
package airgap

import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"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"
Expand All @@ -44,11 +44,28 @@ 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")

require.NoError(t, underTest.Execute())
lines := intoLines(out)
lines := strings.Split(out.String(), "\n")
if runtime.GOARCH == "arm" {
assert.NotContains(t, lines, defaultImage)
} else {
Expand Down Expand Up @@ -88,7 +105,7 @@ spec:

require.NoError(t, underTest.Execute())

lines := intoLines(out)
lines := strings.Split(out.String(), "\n")
for _, contained := range test.contained {
if runtime.GOARCH == "arm" {
assert.NotContains(t, lines, contained)
Expand All @@ -105,24 +122,15 @@ spec:
})
}

func newAirgapListImagesCmdWithConfig(t *testing.T, config string, args ...string) (_ *cobra.Command, out, err *bytes.Buffer) {
func newAirgapListImagesCmdWithConfig(t *testing.T, config string, args ...string) (_ *cobra.Command, out, err *strings.Builder) {
configFile := filepath.Join(t.TempDir(), "k0s.yaml")
require.NoError(t, os.WriteFile(configFile, []byte(config), 0644))

out, err = new(bytes.Buffer), new(bytes.Buffer)
out, err = new(strings.Builder), new(strings.Builder)
cmd := NewAirgapListImagesCmd()
cmd.SetArgs(append([]string{"--config=" + configFile}, args...))
cmd.SetIn(iotest.ErrReader(errors.New("unexpected read from standard input")))
cmd.SetOut(out)
cmd.SetErr(err)
return cmd, out, err
}

func intoLines(in io.Reader) (lines []string) {
scanner := bufio.NewScanner(in)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return
}
Loading