Skip to content

Commit

Permalink
fix: issue with headless installation (#7958)
Browse files Browse the repository at this point in the history
fix: issue with headless installation (#7958)

Signed-off-by: pashavictorovich <pavel@codefresh.io>
  • Loading branch information
pasha-codefresh authored and alexmt committed Dec 16, 2021
1 parent a7e7f32 commit 84f949f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
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)
}

0 comments on commit 84f949f

Please sign in to comment.