diff --git a/cmd/harbor/root/cmd.go b/cmd/harbor/root/cmd.go index 4c1a325b..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,7 +90,7 @@ 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)") 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 674e05c3..171223f6 100644 --- a/test/e2e/login_test.go +++ b/test/e2e/login_test.go @@ -1,17 +1,22 @@ package e2e import ( + "testing" + "github.com/goharbor/harbor-cli/cmd/harbor/root" "github.com/stretchr/testify/assert" - "testing" ) -func Test_Success(t *testing.T) { +func initialize(t *testing.T) { cds := root.RootCmd() - cds.Execute() - cmd := root.LoginCommand() - var err error + err := cds.Execute() + assert.NoError(t, err, "Expected no error for Root command") +} + +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", @@ -21,107 +26,55 @@ func Test_Success(t *testing.T) { } 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") }