From 6edf43c8c0f94851fd1a4e28196e9304e1899e68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilson=20J=C3=BAnior?= Date: Thu, 2 May 2024 16:50:16 -0300 Subject: [PATCH] Simplify routes rebuild command --- tsuru/admin/app.go | 56 ++-------------- tsuru/admin/app_test.go | 141 +++------------------------------------- 2 files changed, 12 insertions(+), 185 deletions(-) diff --git a/tsuru/admin/app.go b/tsuru/admin/app.go index 07cbdeb4..34291c7f 100644 --- a/tsuru/admin/app.go +++ b/tsuru/admin/app.go @@ -5,17 +5,13 @@ package admin import ( - "encoding/json" "fmt" - "io" "net/http" - "sort" "github.com/tsuru/go-tsuruclient/pkg/config" "github.com/tsuru/tsuru-client/tsuru/app" tsuruHTTP "github.com/tsuru/tsuru-client/tsuru/http" "github.com/tsuru/tsuru/cmd" - "github.com/tsuru/tsuru/router/rebuild" ) type AppRoutesRebuild struct { @@ -33,11 +29,6 @@ existing routes to be lost.`, } } -type compatibleRebuildResult struct { - rebuild.RebuildRoutesResult - rebuild.RebuildPrefixResult -} - func (c *AppRoutesRebuild) Run(ctx *cmd.Context) error { appName, err := c.AppName() if err != nil { @@ -56,49 +47,10 @@ func (c *AppRoutesRebuild) Run(ctx *cmd.Context) error { return err } defer rsp.Body.Close() - var allRebuildResult map[string]compatibleRebuildResult - err = json.NewDecoder(rsp.Body).Decode(&allRebuildResult) - if err != nil { - return err - } - if len(allRebuildResult) == 0 { - fmt.Fprintf(ctx.Stdout, "App has no routers.\n") - return nil - } - var routerNames []string - for routerName := range allRebuildResult { - routerNames = append(routerNames, routerName) - } - sort.Strings(routerNames) - for _, routerName := range routerNames { - rebuildResult := allRebuildResult[routerName] - fmt.Fprintf(ctx.Stdout, "Router %v:\n", routerName) - if len(rebuildResult.PrefixResults) == 0 { - printRouterResult(ctx.Stdout, rebuildResult.Added, rebuildResult.Removed) - } - for _, prefixResult := range rebuildResult.PrefixResults { - fmt.Fprintf(ctx.Stdout, " - Prefix %q:\n", prefixResult.Prefix) - printRouterResult(ctx.Stdout, prefixResult.Added, prefixResult.Removed) - } - } - return nil -} -func printRouterResult(w io.Writer, added, removed []string) { - rebuilt := len(added) > 0 || len(removed) > 0 - if len(added) > 0 { - fmt.Fprintf(w, " * Added routes:\n") - for _, added := range added { - fmt.Fprintf(w, " - %s\n", added) - } - } - if len(removed) > 0 { - fmt.Fprintf(w, " * Removed routes:\n") - for _, removed := range removed { - fmt.Fprintf(w, " - %s\n", removed) - } - } - if !rebuilt { - fmt.Fprintf(w, " * Nothing to do, routes already correct.\n") + if rsp.StatusCode == http.StatusOK { + fmt.Fprintln(ctx.Stdout, "routes was rebuilt successfully") } + + return nil } diff --git a/tsuru/admin/app_test.go b/tsuru/admin/app_test.go index 7080933a..18603dd6 100644 --- a/tsuru/admin/app_test.go +++ b/tsuru/admin/app_test.go @@ -6,13 +6,11 @@ package admin import ( "bytes" - "encoding/json" "net/http" "strings" "github.com/tsuru/tsuru/cmd" "github.com/tsuru/tsuru/cmd/cmdtest" - "github.com/tsuru/tsuru/router/rebuild" "gopkg.in/check.v1" ) @@ -26,155 +24,32 @@ func (s *S) TestAppRoutesRebuildRun(c *check.C) { Stdout: &stdout, Stderr: &stderr, } - rebuildResult := `{ -"r1": { - "added": ["r1", "r2"], - "removed": ["r9"] -}, -"r2": { - "removed": ["r9"] -}, -"r3": {} -}` - trans := &cmdtest.ConditionalTransport{ - Transport: cmdtest.Transport{Message: rebuildResult, Status: http.StatusOK}, - CondFunc: func(req *http.Request) bool { - return strings.HasSuffix(req.URL.Path, "/apps/app1/routes") && req.Method == "POST" - }, - } - - s.setupFakeTransport(trans) - - command := AppRoutesRebuild{} - command.Flags().Parse(true, []string{"--app", "app1"}) - err := command.Run(&context) - c.Assert(err, check.IsNil) - c.Assert(stdout.String(), check.Equals, `Router r1: - * Added routes: - - r1 - - r2 - * Removed routes: - - r9 -Router r2: - * Removed routes: - - r9 -Router r3: - * Nothing to do, routes already correct. -`) -} -func (s *S) TestAppRoutesRebuildRunWithPrefixes(c *check.C) { - var stdout, stderr bytes.Buffer - context := cmd.Context{ - Stdout: &stdout, - Stderr: &stderr, - } - rebuildResult := `{ -"r1": { - "PrefixResults": [ - { - "prefix": "", - "added": ["r1", "r2"], - "removed": ["r9"] - }, - { - "prefix": "v1.version", - "added": ["r3"], - "removed": ["r8"] - } - ], - "added": ["r1", "r2"], - "removed": ["r9"] -}, -"r2": { - "PrefixResults": [ - { - "prefix": "", - "removed": ["r9"] - } - ], - "removed": ["r9"] -}, -"r3": { - "PrefixResults": [ - { - "prefix": "" - } - ] -} -}` trans := &cmdtest.ConditionalTransport{ - Transport: cmdtest.Transport{Message: rebuildResult, Status: http.StatusOK}, + Transport: cmdtest.Transport{Status: http.StatusOK}, CondFunc: func(req *http.Request) bool { return strings.HasSuffix(req.URL.Path, "/apps/app1/routes") && req.Method == "POST" }, } - s.setupFakeTransport(trans) - command := AppRoutesRebuild{} - command.Flags().Parse(true, []string{"--app", "app1"}) - err := command.Run(&context) - c.Assert(err, check.IsNil) - c.Assert(stdout.String(), check.Equals, `Router r1: - - Prefix "": - * Added routes: - - r1 - - r2 - * Removed routes: - - r9 - - Prefix "v1.version": - * Added routes: - - r3 - * Removed routes: - - r8 -Router r2: - - Prefix "": - * Removed routes: - - r9 -Router r3: - - Prefix "": - * Nothing to do, routes already correct. -`) -} -func (s *S) TestAppRoutesRebuildRunNothingToDo(c *check.C) { - var stdout, stderr bytes.Buffer - context := cmd.Context{ - Stdout: &stdout, - Stderr: &stderr, - } - rebuildResult := map[string]rebuild.RebuildRoutesResult{ - "r1": {}, - } - data, err := json.Marshal(rebuildResult) - c.Assert(err, check.IsNil) - trans := &cmdtest.ConditionalTransport{ - Transport: cmdtest.Transport{Message: string(data), Status: http.StatusOK}, - CondFunc: func(req *http.Request) bool { - return strings.HasSuffix(req.URL.Path, "/apps/app1/routes") && req.Method == "POST" - }, - } s.setupFakeTransport(trans) command := AppRoutesRebuild{} command.Flags().Parse(true, []string{"--app", "app1"}) - err = command.Run(&context) + err := command.Run(&context) c.Assert(err, check.IsNil) - c.Assert(stdout.String(), check.Equals, `Router r1: - * Nothing to do, routes already correct. + c.Assert(stdout.String(), check.Equals, `routes was rebuilt successfully `) } -func (s *S) TestAppRoutesRebuildRunNoRouters(c *check.C) { +func (s *S) TestAppRoutesRebuildFailed(c *check.C) { var stdout, stderr bytes.Buffer context := cmd.Context{ Stdout: &stdout, Stderr: &stderr, } - rebuildResult := map[string]rebuild.RebuildRoutesResult{} - data, err := json.Marshal(rebuildResult) - c.Assert(err, check.IsNil) trans := &cmdtest.ConditionalTransport{ - Transport: cmdtest.Transport{Message: string(data), Status: http.StatusOK}, + Transport: cmdtest.Transport{Message: "Some error", Status: http.StatusBadGateway}, CondFunc: func(req *http.Request) bool { return strings.HasSuffix(req.URL.Path, "/apps/app1/routes") && req.Method == "POST" }, @@ -183,7 +58,7 @@ func (s *S) TestAppRoutesRebuildRunNoRouters(c *check.C) { command := AppRoutesRebuild{} command.Flags().Parse(true, []string{"--app", "app1"}) - err = command.Run(&context) - c.Assert(err, check.IsNil) - c.Assert(stdout.String(), check.Equals, "App has no routers.\n") + err := command.Run(&context) + c.Assert(err, check.Not(check.IsNil)) + c.Assert(err.Error(), check.Matches, ".*: Some error") }