Skip to content

Commit

Permalink
Simplify routes rebuild command
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed May 2, 2024
1 parent 13673ef commit 6edf43c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 185 deletions.
56 changes: 4 additions & 52 deletions tsuru/admin/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
141 changes: 8 additions & 133 deletions tsuru/admin/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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"
},
Expand All @@ -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")
}

0 comments on commit 6edf43c

Please sign in to comment.