From 41b4c888bd936e948772fa23f3335448625d2267 Mon Sep 17 00:00:00 2001 From: bupd Date: Sun, 28 Jul 2024 14:05:31 +0530 Subject: [PATCH 1/2] fix harbor login Signed-off-by: bupd --- cmd/harbor/root/cmd.go | 9 ++++++--- cmd/harbor/root/login.go | 6 ++++-- test/e2e/login_test.go | 13 +++++++++---- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/cmd/harbor/root/cmd.go b/cmd/harbor/root/cmd.go index 6e474b28..7a3fbf3d 100644 --- a/cmd/harbor/root/cmd.go +++ b/cmd/harbor/root/cmd.go @@ -21,7 +21,7 @@ var ( verbose bool ) -func initConfig() { +func InitConfig() { viper.SetConfigType("yaml") // cfgFile = viper.GetStering("config") @@ -90,13 +90,16 @@ harbor help // }, } - cobra.OnInitialize(initConfig) + cobra.OnInitialize(InitConfig) root.PersistentFlags().StringVarP(&output, "output-format", "o", "", "Output format. One of: json|yaml") root.PersistentFlags().StringVar(&cfgFile, "config", utils.DefaultConfigPath, "config file (default is $HOME/.harbor/config.yaml)") root.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output") - viper.BindPFlag("output-format", root.PersistentFlags().Lookup("output-format")) + err := viper.BindPFlag("output-format", root.PersistentFlags().Lookup("output-format")) + if err != nil { + fmt.Println(err.Error()) + } root.AddCommand( versionCommand(), diff --git a/cmd/harbor/root/login.go b/cmd/harbor/root/login.go index 6fdcce13..3c9d2a25 100644 --- a/cmd/harbor/root/login.go +++ b/cmd/harbor/root/login.go @@ -26,7 +26,6 @@ func LoginCommand() *cobra.Command { Long: "Authenticate with Harbor Registry.", Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - if len(args) > 0 { serverAddress = args[0] } @@ -40,7 +39,8 @@ func LoginCommand() *cobra.Command { var err error - if loginView.Server != "" && loginView.Username != "" && loginView.Password != "" && loginView.Name != "" { + if loginView.Server != "" && loginView.Username != "" && loginView.Password != "" && + loginView.Name != "" { err = runLogin(loginView) } else { err = createLoginView(&loginView) @@ -75,6 +75,8 @@ func createLoginView(loginView *login.LoginView) error { } func runLogin(opts login.LoginView) error { + opts.Server = utils.FormatUrl(opts.Server) + clientConfig := &harbor.ClientSetConfig{ URL: opts.Server, Username: opts.Username, diff --git a/test/e2e/login_test.go b/test/e2e/login_test.go index fff2006e..117fab86 100644 --- a/test/e2e/login_test.go +++ b/test/e2e/login_test.go @@ -1,21 +1,26 @@ package e2e import ( + "testing" + "github.com/goharbor/harbor-cli/cmd/harbor/root" "github.com/stretchr/testify/assert" - "testing" ) func Test_Success(t *testing.T) { - cmd := root.LoginCommand() var err error + cds := root.RootCmd() + err = cds.Execute() + + assert.NoError(t, err) + cmd := root.LoginCommand() + validServerAddresses := []string{ "http://demo.goharbor.io:80", - "https://demo.goharbor.io:8443", + "https://demo.goharbor.io:443", "http://demo.goharbor.io", "https://demo.goharbor.io", - "demo.goharbor.io", } for _, serverAddress := range validServerAddresses { From 3e3d57d52e674d96b0f43fae4ad5a5ecae8b5c98 Mon Sep 17 00:00:00 2001 From: bupd Date: Sun, 28 Jul 2024 14:28:59 +0530 Subject: [PATCH 2/2] rewrite login test Signed-off-by: bupd --- test/e2e/login_test.go | 130 +++++++++++++---------------------------- 1 file changed, 40 insertions(+), 90 deletions(-) diff --git a/test/e2e/login_test.go b/test/e2e/login_test.go index 117fab86..d965e353 100644 --- a/test/e2e/login_test.go +++ b/test/e2e/login_test.go @@ -7,124 +7,74 @@ import ( "github.com/stretchr/testify/assert" ) -func Test_Success(t *testing.T) { - var err error - +func initialize(t *testing.T) { cds := root.RootCmd() - err = cds.Execute() + err := cds.Execute() + assert.NoError(t, err, "Expected no error for Root command") +} - assert.NoError(t, err) - cmd := root.LoginCommand() +func Test_Login_Success(t *testing.T) { + initialize(t) // Initialize the root command + cmd := root.LoginCommand() validServerAddresses := []string{ "http://demo.goharbor.io:80", "https://demo.goharbor.io:443", "http://demo.goharbor.io", "https://demo.goharbor.io", + "demo.goharbor.io", } for _, serverAddress := range validServerAddresses { - args := []string{ - serverAddress, - } - cmd.SetArgs(args) - - err = cmd.Flags().Set("name", "test") - if err != nil { - t.Fatal(err) - } - err = cmd.Flags().Set("username", "admin") - if err != nil { - t.Fatal(err) - } - err = cmd.Flags().Set("password", "Harbor12345") - if err != nil { - t.Fatal(err) - } - - err = cmd.Execute() - - assert.NoError(t, err) + t.Run("ValidServer_"+serverAddress, func(t *testing.T) { + args := []string{serverAddress} + cmd.SetArgs(args) + + assert.NoError(t, cmd.Flags().Set("name", "test")) + assert.NoError(t, cmd.Flags().Set("username", "admin")) + assert.NoError(t, cmd.Flags().Set("password", "Harbor12345")) + + err := cmd.Execute() + assert.NoError(t, err, "Expected no error for server: %s", serverAddress) + }) } } -func Test_Failure_WrongServer(t *testing.T) { +func Test_Login_Failure_WrongServer(t *testing.T) { cmd := root.LoginCommand() - var err error - - args := []string{ - "demo.goharbor.io", - } + args := []string{"wrongserver"} cmd.SetArgs(args) - err = cmd.Flags().Set("name", "test") - if err != nil { - t.Fatal(err) - } - err = cmd.Flags().Set("username", "admin") - if err != nil { - t.Fatal(err) - } - err = cmd.Flags().Set("password", "Harbor12345") - if err != nil { - t.Fatal(err) - } - - err = cmd.Execute() + assert.NoError(t, cmd.Flags().Set("name", "test")) + assert.NoError(t, cmd.Flags().Set("username", "admin")) + assert.NoError(t, cmd.Flags().Set("password", "Harbor12345")) - assert.Error(t, err) + err := cmd.Execute() + assert.Error(t, err, "Expected error for invalid server") } -func Test_Failure_WrongUsername(t *testing.T) { +func Test_Login_Failure_WrongUsername(t *testing.T) { cmd := root.LoginCommand() - var err error - - args := []string{ - "http://demo.goharbor.io", - } + args := []string{"http://demo.goharbor.io"} cmd.SetArgs(args) - err = cmd.Flags().Set("name", "test") - if err != nil { - t.Fatal(err) - } - err = cmd.Flags().Set("username", "wrong") - if err != nil { - t.Fatal(err) - } - err = cmd.Flags().Set("password", "Harbor12345") - if err != nil { - t.Fatal(err) - } - - err = cmd.Execute() + assert.NoError(t, cmd.Flags().Set("name", "test")) + assert.NoError(t, cmd.Flags().Set("username", "wrong")) + assert.NoError(t, cmd.Flags().Set("password", "Harbor12345")) - assert.Error(t, err) + err := cmd.Execute() + assert.Error(t, err, "Expected error for wrong username") } -func Test_Failure_WrongPassword(t *testing.T) { +func Test_Login_Failure_WrongPassword(t *testing.T) { cmd := root.LoginCommand() - var err error - - args := []string{ - "http://demo.goharbor.io", - } + args := []string{"http://demo.goharbor.io"} cmd.SetArgs(args) - err = cmd.Flags().Set("name", "test") - if err != nil { - t.Fatal(err) - } - err = cmd.Flags().Set("username", "admin") - if err != nil { - t.Fatal(err) - } - err = cmd.Flags().Set("password", "wrong") - if err != nil { - t.Fatal(err) - } - - err = cmd.Execute() + assert.NoError(t, cmd.Flags().Set("name", "test")) + assert.NoError(t, cmd.Flags().Set("username", "admin")) + assert.NoError(t, cmd.Flags().Set("password", "wrong")) - assert.Error(t, err) + err := cmd.Execute() + assert.Error(t, err, "Expected error for wrong password") }