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

fix: issue with headless installation #7958

Merged
merged 7 commits into from
Dec 16, 2021
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
14 changes: 10 additions & 4 deletions cmd/argocd/commands/headless/headless.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/argoproj/argo-cd/v2/util/cli"
"github.com/argoproj/argo-cd/v2/util/io"
"github.com/argoproj/argo-cd/v2/util/localconfig"

flag "github.com/spf13/pflag"
)

func testAPI(clientOpts *argoapi.ClientOptions) error {
Expand All @@ -43,6 +45,13 @@ func testAPI(clientOpts *argoapi.ClientOptions) error {
return err
}

func retrieveContextIfChanged(contextFlag *flag.Flag) string {
if contextFlag != nil && contextFlag.Changed {
return contextFlag.Value.String()
}
return ""
}

// InitCommand allows executing command in a headless mode: on the fly starts Argo CD API server and
// changes provided client options to use started API server port
func InitCommand(cmd *cobra.Command, clientOpts *argoapi.ClientOptions, port *int) *cobra.Command {
Expand Down Expand Up @@ -108,10 +117,7 @@ func InitCommand(cmd *cobra.Command, clientOpts *argoapi.ClientOptions, port *in
return err
}

var context string
if cmd.Flag("context").Changed {
context = cmd.Flag("context").Value.String()
}
context := retrieveContextIfChanged(cmd.Flag("context"))

mr, err := miniredis.Run()
if err != nil {
Expand Down
80 changes: 80 additions & 0 deletions cmd/argocd/commands/headless/headless_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package headless

import (
"testing"

flag "github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)

type StringFlag struct {
// The exact value provided on the flag
value string
}

func (f StringFlag) String() string {
return f.value
}

func (f *StringFlag) Set(value string) error {
f.value = value
return nil
}

func (f *StringFlag) Type() string {
return "string"
}

func Test_FlagContextNotChanged(t *testing.T) {
res := retrieveContextIfChanged(&flag.Flag{
Name: "",
Shorthand: "",
Usage: "",
Value: &StringFlag{value: "test"},
DefValue: "",
Changed: false,
NoOptDefVal: "",
Deprecated: "",
Hidden: false,
ShorthandDeprecated: "",
Annotations: nil,
})

assert.Equal(t, "", res)
}

func Test_FlagContextChanged(t *testing.T) {
res := retrieveContextIfChanged(&flag.Flag{
Name: "",
Shorthand: "",
Usage: "",
Value: &StringFlag{value: "test"},
DefValue: "",
Changed: true,
NoOptDefVal: "",
Deprecated: "",
Hidden: false,
ShorthandDeprecated: "",
Annotations: nil,
})

assert.Equal(t, "test", res)
}

func Test_FlagContextNil(t *testing.T) {
res := retrieveContextIfChanged(&flag.Flag{
Name: "",
Shorthand: "",
Usage: "",
Value: nil,
DefValue: "",
Changed: false,
NoOptDefVal: "",
Deprecated: "",
Hidden: false,
ShorthandDeprecated: "",
Annotations: nil,
})

assert.Equal(t, "", res)
}