Skip to content

Commit

Permalink
3008 cli implement end to end tests (#3221)
Browse files Browse the repository at this point in the history
* test

* inital commit

* nit changes

* fix test and linter

* small changes

* Added test for org switch

* changes for output as csv

* added test for service cmd

* linter fix

* test sleep

* increased sleep time to test

* tidy

* test

* added tests for user command

* other commands added with helper

* nit

* removed unwanted comments

* few changes

* nit

* updated printer functions

* nit

* review changes

* nit

* PR changes

* nit

* ci fix
  • Loading branch information
rakeshsharma14317 authored Nov 30, 2023
1 parent e1b2227 commit e5e7883
Show file tree
Hide file tree
Showing 89 changed files with 1,426 additions and 497 deletions.
12 changes: 6 additions & 6 deletions cli/cmd/admin/admin.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package admin

import (
"github.com/rilldata/rill/cli/pkg/config"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/spf13/cobra"
)

// AdminCmd represents the admin command
func AdminCmd(cfg *config.Config) *cobra.Command {
func AdminCmd(ch *cmdutil.Helper) *cobra.Command {
adminCmd := &cobra.Command{
Use: "admin",
Hidden: !cfg.IsDev(),
Hidden: !ch.Config.IsDev(),
Short: "Manage an admin server",
}
adminCmd.AddCommand(PingCmd(cfg))
adminCmd.AddCommand(StartCmd(cfg))
adminCmd.AddCommand(SwitchCmd(cfg))
adminCmd.AddCommand(PingCmd(ch))
adminCmd.AddCommand(StartCmd(ch))
adminCmd.AddCommand(SwitchCmd(ch))
return adminCmd
}
7 changes: 3 additions & 4 deletions cli/cmd/admin/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ package admin

import (
"context"
"fmt"

"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/config"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
"github.com/spf13/cobra"
)

func PingCmd(cfg *config.Config) *cobra.Command {
func PingCmd(ch *cmdutil.Helper) *cobra.Command {
var adminURL string

pingCmd := &cobra.Command{
Use: "ping",
Short: "Ping",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := ch.Config
// Must set here to avoid flag parser overriding it globally
cfg.AdminURL = adminURL

Expand All @@ -31,7 +30,7 @@ func PingCmd(cfg *config.Config) *cobra.Command {
return err
}

fmt.Printf("Pong: %s\n", pong.Time.AsTime().String())
ch.Printer.Printf("Pong: %s\n", pong.Time.AsTime().String())
return nil
},
}
Expand Down
14 changes: 8 additions & 6 deletions cli/cmd/admin/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/rilldata/rill/admin"
"github.com/rilldata/rill/admin/server"
"github.com/rilldata/rill/admin/worker"
"github.com/rilldata/rill/cli/pkg/config"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/runtime/pkg/activity"
"github.com/rilldata/rill/runtime/pkg/email"
"github.com/rilldata/rill/runtime/pkg/graceful"
Expand Down Expand Up @@ -75,20 +75,22 @@ type Config struct {
}

// StartCmd starts an admin server. It only allows configuration using environment variables.
func StartCmd(cliCfg *config.Config) *cobra.Command {
func StartCmd(ch *cmdutil.Helper) *cobra.Command {
startCmd := &cobra.Command{
Use: "start [jobs|server|worker]",
Short: "Start admin service",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cliCfg := ch.Config
printer := ch.Printer
// Load .env (note: fails silently if .env has errors)
_ = godotenv.Load()

// Init config
var conf Config
err := envconfig.Process("rill_admin", &conf)
if err != nil {
fmt.Printf("failed to load config: %s\n", err.Error())
printer.Printf("failed to load config: %s\n", err.Error())
os.Exit(1)
}

Expand All @@ -97,7 +99,7 @@ func StartCmd(cliCfg *config.Config) *cobra.Command {
cfg.Level.SetLevel(conf.LogLevel)
logger, err := cfg.Build()
if err != nil {
fmt.Printf("error: failed to create logger: %s\n", err.Error())
printer.Printf("error: failed to create logger: %s\n", err.Error())
os.Exit(1)
}

Expand All @@ -114,12 +116,12 @@ func StartCmd(cliCfg *config.Config) *cobra.Command {
// Validate frontend and external URLs
_, err = url.Parse(conf.FrontendURL)
if err != nil {
fmt.Printf("error: invalid frontend URL: %s\n", err.Error())
printer.Printf("error: invalid frontend URL: %s\n", err.Error())
os.Exit(1)
}
_, err = url.Parse(conf.ExternalURL)
if err != nil {
fmt.Printf("error: invalid external URL: %s\n", err.Error())
printer.Printf("error: invalid external URL: %s\n", err.Error())
os.Exit(1)
}
_, err = url.Parse(conf.ExternalGRPCURL)
Expand Down
7 changes: 4 additions & 3 deletions cli/cmd/admin/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ const (
devAdminURL = "http://localhost:9090"
)

func SwitchCmd(cfg *config.Config) *cobra.Command {
func SwitchCmd(ch *cmdutil.Helper) *cobra.Command {
var env string
switchCmd := &cobra.Command{
Use: "switch {stage|prod|dev}",
Short: "switch",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg := ch.Config
if len(args) > 0 {
env = args[0]
}
Expand Down Expand Up @@ -61,8 +62,8 @@ func SwitchCmd(cfg *config.Config) *cobra.Command {

cfg.AdminURL = url

cmdutil.PrintlnSuccess(fmt.Sprintf("Set default env to %q, url is %q", env, url))
err = auth.SelectOrgFlow(cmd.Context(), cfg)
ch.Printer.PrintlnSuccess(fmt.Sprintf("Set default env to %q, url is %q", env, url))
err = auth.SelectOrgFlow(cmd.Context(), ch)
if err != nil {
return err
}
Expand Down
31 changes: 15 additions & 16 deletions cli/cmd/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,39 @@ import (
"fmt"
"strings"

"github.com/fatih/color"
"github.com/rilldata/rill/cli/pkg/browser"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/config"
"github.com/rilldata/rill/cli/pkg/deviceauth"
"github.com/rilldata/rill/cli/pkg/dotrill"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
"github.com/spf13/cobra"
)

// LoginCmd is the command for logging into a Rill account.
func LoginCmd(cfg *config.Config) *cobra.Command {
func LoginCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "login",
Short: "Authenticate with the Rill API",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := ch.Config
ctx := cmd.Context()

// updating this as its not required to logout first and login again
if cfg.AdminTokenDefault != "" {
err := Logout(ctx, cfg)
err := Logout(ctx, ch)
if err != nil {
return err
}
}

// Login user
err := Login(ctx, cfg, "")
err := Login(ctx, ch, "")
if err != nil {
return err
}

// Set default org after login
err = SelectOrgFlow(ctx, cfg)
err = SelectOrgFlow(ctx, ch)
if err != nil {
return err
}
Expand All @@ -50,10 +49,11 @@ func LoginCmd(cfg *config.Config) *cobra.Command {
return cmd
}

func Login(ctx context.Context, cfg *config.Config, redirectURL string) error {
func Login(ctx context.Context, ch *cmdutil.Helper, redirectURL string) error {
// In production, the REST and gRPC endpoints are the same, but in development, they're served on different ports.
// We plan to move to connect.build for gRPC, which will allow us to serve both on the same port in development as well.
// Until we make that change, this is a convenient hack for local development (assumes gRPC on port 9090 and REST on port 8080).
cfg := ch.Config
authURL := cfg.AdminURL
if strings.Contains(authURL, "http://localhost:9090") {
authURL = "http://localhost:8080"
Expand All @@ -69,12 +69,10 @@ func Login(ctx context.Context, cfg *config.Config, redirectURL string) error {
return err
}

bold := color.New(color.Bold)
bold.Printf("\nConfirmation Code: ")
boldGreen := color.New(color.FgGreen).Add(color.Bold)
boldGreen.Fprintln(color.Output, deviceVerification.UserCode)
ch.Printer.PrintBold("\nConfirmation Code: ")
ch.Printer.PrintlnSuccess(deviceVerification.UserCode)

bold.Printf("\nOpen this URL in your browser to confirm the login: %s\n\n", deviceVerification.VerificationCompleteURL)
ch.Printer.PrintBold(fmt.Sprintf("\nOpen this URL in your browser to confirm the login: %s\n\n", deviceVerification.VerificationCompleteURL))

_ = browser.Open(deviceVerification.VerificationCompleteURL)

Expand All @@ -89,11 +87,12 @@ func Login(ctx context.Context, cfg *config.Config, redirectURL string) error {
}
// set the default token to the one we just got
cfg.AdminTokenDefault = res1.AccessToken
bold.Print("Successfully logged in. Welcome to Rill!\n")
ch.Printer.PrintBold("Successfully logged in. Welcome to Rill!\n")
return nil
}

func SelectOrgFlow(ctx context.Context, cfg *config.Config) error {
func SelectOrgFlow(ctx context.Context, ch *cmdutil.Helper) error {
cfg := ch.Config
client, err := cmdutil.Client(cfg)
if err != nil {
return err
Expand All @@ -106,7 +105,7 @@ func SelectOrgFlow(ctx context.Context, cfg *config.Config) error {
}

if len(res.Organizations) == 0 {
cmdutil.PrintlnWarn("You are not part of an org. Run `rill org create` or `rill deploy` to create one.")
ch.Printer.PrintlnWarn("You are not part of an org. Run `rill org create` or `rill deploy` to create one.")
return nil
}

Expand All @@ -127,6 +126,6 @@ func SelectOrgFlow(ctx context.Context, cfg *config.Config) error {

cfg.Org = defaultOrg

fmt.Printf("Set default organization to %q. Change using `rill org switch`.\n", defaultOrg)
ch.Printer.Print(fmt.Sprintf("Set default organization to %q. Change using `rill org switch`.\n", defaultOrg))
return nil
}
17 changes: 8 additions & 9 deletions cli/cmd/auth/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,42 @@ package auth

import (
"context"
"fmt"

"github.com/fatih/color"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/config"
"github.com/rilldata/rill/cli/pkg/dotrill"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
"github.com/spf13/cobra"
)

// LogoutCmd is the command for logging out of a Rill account.
func LogoutCmd(cfg *config.Config) *cobra.Command {
func LogoutCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "logout",
Short: "Logout of the Rill API",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := ch.Config
ctx := cmd.Context()

token := cfg.AdminToken()
if token == "" {
cmdutil.PrintlnWarn("You are already logged out.")
ch.Printer.PrintlnWarn("You are already logged out.")
return nil
}

err := Logout(ctx, cfg)
err := Logout(ctx, ch)
if err != nil {
return err
}

color.New(color.FgGreen).Println("Successfully logged out.")
ch.Printer.PrintlnSuccess("Successfully logged out.")
return nil
},
}
return cmd
}

func Logout(ctx context.Context, cfg *config.Config) error {
func Logout(ctx context.Context, ch *cmdutil.Helper) error {
cfg := ch.Config
client, err := cmdutil.Client(cfg)
if err != nil {
return err
Expand All @@ -47,7 +46,7 @@ func Logout(ctx context.Context, cfg *config.Config) error {

_, err = client.RevokeCurrentAuthToken(ctx, &adminv1.RevokeCurrentAuthTokenRequest{})
if err != nil {
fmt.Printf("Failed to revoke token (did you revoke it manually?). Clearing local token anyway.\n")
ch.Printer.Printf("Failed to revoke token (did you revoke it manually?). Clearing local token anyway.\n")
}

err = dotrill.SetAccessToken("")
Expand Down
Loading

0 comments on commit e5e7883

Please sign in to comment.