Skip to content

Commit

Permalink
Reject unknown sub-commands for k0s airgap
Browse files Browse the repository at this point in the history
Cobra has the property of not reporting any unrecognized sub-commands
and simply displaying the help page for any command that doesn't have a
run function set. Overwrite this for k0s's airgap sub-command.

Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed Dec 13, 2024
1 parent e338b28 commit 6fe4f05
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
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

0 comments on commit 6fe4f05

Please sign in to comment.