Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2809 from prometherion/issues/2808
Browse files Browse the repository at this point in the history
Raising error if arguments are provided to version and install commands
  • Loading branch information
2opremio authored Jan 31, 2020
2 parents 0810999 + cc6f328 commit ce7fc6b
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cmd/fluxctl/install_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (opts *installOpts) Command() *cobra.Command {
Use: "install",
Short: "Print and tweak Kubernetes manifests needed to install Flux in a Cluster",
Example: `# Install Flux and make it use Git repository [email protected]:<your username>/flux-get-started
fluxctl install --git-url '[email protected]:<your username>/flux-get-started' | kubectl -f -`,
fluxctl install --git-url '[email protected]:<your username>/flux-get-started' --git-email=<your_git_email> | kubectl -f -`,
RunE: opts.RunE,
}
cmd.Flags().StringVar(&opts.GitURL, "git-url", "",
Expand Down Expand Up @@ -61,6 +61,9 @@ fluxctl install --git-url '[email protected]:<your username>/flux-get-started' | ku
}

func (opts *installOpts) RunE(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return errorWantedNoArgs
}
if opts.GitURL == "" {
return fmt.Errorf("please supply a valid --git-url argument")
}
Expand Down
63 changes: 63 additions & 0 deletions cmd/fluxctl/install_cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"bytes"
"fmt"
"testing"
)

func TestInstallCommand_ExtraArgumentFailure(t *testing.T) {
for k, v := range [][]string{
{"foo"},
{"foo", "bar"},
{"foo", "bar", "bizz"},
{"foo", "bar", "bizz", "buzz"},
} {
t.Run(fmt.Sprintf("%d", k), func(t *testing.T) {
cmd := newInstall().Command()
buf := new(bytes.Buffer)
cmd.SetOut(buf)
cmd.SetArgs(v)
_ = cmd.Flags().Set("git-url", "[email protected]:testcase/flux-get-started")
_ = cmd.Flags().Set("git-email", "[email protected]")
if err := cmd.Execute(); err == nil {
t.Fatalf("expecting error, got nil")
}
})
}
}

func TestInstallCommand_MissingRequiredFlag(t *testing.T) {
for k, v := range map[string]string{
"git-url": "[email protected]:testcase/flux-get-started",
"git-email": "[email protected]",
} {
t.Run(fmt.Sprintf("only --%s", k), func(t *testing.T) {
cmd := newInstall().Command()
buf := new(bytes.Buffer)
cmd.SetOut(buf)
cmd.SetArgs([]string{})
_ = cmd.Flags().Set(k, v)
if err := cmd.Execute(); err == nil {
t.Fatalf("expecting error, got nil")
}
})
}
}

func TestInstallCommand_Success(t *testing.T) {
f := make(map[string]string)
f["git-url"] = "[email protected]:testcase/flux-get-started"
f["git-email"] = "[email protected]"

cmd := newInstall().Command()
buf := new(bytes.Buffer)
cmd.SetOut(buf)
cmd.SetArgs([]string{})
for k, v := range f {
_ = cmd.Flags().Set(k, v)
}
if err := cmd.Execute(); err != nil {
t.Fatalf("expecting nil, got error (%s)", err.Error())
}
}
3 changes: 3 additions & 0 deletions cmd/fluxctl/version_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ func newVersionCommand() *cobra.Command {
Use: "version",
Short: "Output the version of fluxctl",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return errorWantedNoArgs
}
if version == "" {
version = "unversioned"
}
Expand Down
59 changes: 59 additions & 0 deletions cmd/fluxctl/version_cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"bytes"
"fmt"
"strings"
"testing"
)

func TestVersionCommand_InputFailure(t *testing.T) {
for k, v := range [][]string{
{"foo"},
{"foo", "bar"},
{"foo", "bar", "bizz"},
{"foo", "bar", "bizz", "buzz"},
} {
t.Run(fmt.Sprintf("%d", k), func(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newVersionCommand()
cmd.SetOut(buf)
cmd.SetArgs(v)
if err := cmd.Execute(); err == nil {
t.Fatalf("Expecting error: command is not expecting extra arguments")
}
})
}
}

func TestVersionCommand_Success(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newVersionCommand()
cmd.SetOut(buf)
cmd.SetArgs([]string{})
if err := cmd.Execute(); err != nil {
t.Fatalf("Expecting nil, got error (%s)", err.Error())
}
}

func TestVersionCommand_SuccessCheckVersion(t *testing.T) {
for _, e := range []string{
"v1.0.0",
"v2.0.0",
} {
t.Run(e, func(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newVersionCommand()
version = e
cmd.SetOut(buf)
cmd.SetArgs([]string{})
if err := cmd.Execute(); err != nil {
t.Fatalf("Expecting nil, got error (%s)", err.Error())
}
if g := strings.TrimRight(buf.String(), "\n"); e != g {
println(e == g)
t.Fatalf("Expected %s, got %s", e, g)
}
})
}
}

0 comments on commit ce7fc6b

Please sign in to comment.