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

Reject unknown sub-commands for k0s airgap #5354

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions cmd/airgap/airgap.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func NewAirgapCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "airgap",
Short: "Manage airgap setup",
Args: cobra.NoArgs,
Run: func(*cobra.Command, []string) { /* Enforce arg validation. */ },
}

cmd.AddCommand(NewAirgapListImagesCmd())
Expand Down
41 changes: 41 additions & 0 deletions cmd/airgap/airgap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2024 k0s authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package airgap_test

import (
"errors"
"strings"
"testing"
"testing/iotest"

"github.com/k0sproject/k0s/cmd"
"github.com/stretchr/testify/assert"
)

func TestAirgapCmd_RejectsUnknownCommands(t *testing.T) {
var stdout, stderr strings.Builder
underTest := cmd.NewRootCmd()
underTest.SetArgs([]string{"airgap", "bogus"})
underTest.SetIn(iotest.ErrReader(errors.New("unexpected read from standard input")))
underTest.SetOut(&stdout)
underTest.SetErr(&stderr)

msg := `unknown command "bogus" for "k0s airgap"`
assert.ErrorContains(t, underTest.Execute(), msg)
assert.Equal(t, "Error: "+msg+"\n", stderr.String())
assert.Empty(t, stdout.String())
}
1 change: 1 addition & 0 deletions cmd/airgap/listimages.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func NewAirgapListImagesCmd() *cobra.Command {
Use: "list-images",
Short: "List image names and version needed for air-gap install",
Example: `k0s airgap list-images`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
opts, err := config.GetCmdOpts(cmd)
if err != nil {
Expand Down
21 changes: 14 additions & 7 deletions cmd/airgap/listimages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package airgap
package airgap_test

import (
"errors"
Expand All @@ -26,6 +26,7 @@ import (
"testing"
"testing/iotest"

"github.com/k0sproject/k0s/cmd"
internalio "github.com/k0sproject/k0s/internal/io"
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"

Expand All @@ -44,17 +45,23 @@ func TestAirgapListImages(t *testing.T) {

defaultImage := v1beta1.DefaultEnvoyProxyImage().URI()

t.Run("RejectsUnknownCommands", func(t *testing.T) {
underTest, stdout, stderr := newAirgapListImagesCmdWithConfig(t, "", "bogus")

msg := `unknown command "bogus" for "k0s airgap list-images"`
assert.ErrorContains(t, underTest.Execute(), msg)
assert.Equal(t, "Error: "+msg+"\n", stderr.String())
assert.Empty(t, stdout.String())
})

t.Run("HonorsIOErrors", func(t *testing.T) {
var writes uint
underTest := NewAirgapListImagesCmd()
underTest.SetIn(iotest.ErrReader(errors.New("unexpected read from standard input")))
underTest, _, stderr := newAirgapListImagesCmdWithConfig(t, "")
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")
Expand Down Expand Up @@ -127,8 +134,8 @@ func newAirgapListImagesCmdWithConfig(t *testing.T, config string, args ...strin
require.NoError(t, os.WriteFile(configFile, []byte(config), 0644))

out, err = new(strings.Builder), new(strings.Builder)
cmd := NewAirgapListImagesCmd()
cmd.SetArgs(append([]string{"--config=" + configFile}, args...))
cmd := cmd.NewRootCmd()
cmd.SetArgs(append([]string{"airgap", "--config=" + configFile, "list-images"}, args...))
cmd.SetIn(iotest.ErrReader(errors.New("unexpected read from standard input")))
cmd.SetOut(out)
cmd.SetErr(err)
Expand Down
Loading