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

Raising error if arguments are provided to version and install commands #2809

Merged
merged 1 commit into from
Jan 31, 2020
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/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 @@ -60,6 +60,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)
}
})
}
}