From 13673efbf759e015aa870555076203716c3f4e26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilson=20J=C3=BAnior?= Date: Thu, 2 May 2024 16:36:09 -0300 Subject: [PATCH] Drop deprecated swap command --- tsuru/client/swap.go | 102 -------------------------- tsuru/client/swap_test.go | 147 -------------------------------------- tsuru/main.go | 1 - tsuru/main_test.go | 7 -- 4 files changed, 257 deletions(-) delete mode 100644 tsuru/client/swap.go delete mode 100644 tsuru/client/swap_test.go diff --git a/tsuru/client/swap.go b/tsuru/client/swap.go deleted file mode 100644 index a5f38bca..00000000 --- a/tsuru/client/swap.go +++ /dev/null @@ -1,102 +0,0 @@ -// Copyright 2016 tsuru-client authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package client - -import ( - "fmt" - "io" - "net/http" - "net/url" - "strconv" - "strings" - - "github.com/tsuru/gnuflag" - "github.com/tsuru/go-tsuruclient/pkg/config" - tsuruHTTP "github.com/tsuru/tsuru-client/tsuru/http" - "github.com/tsuru/tsuru/cmd" - "github.com/tsuru/tsuru/errors" -) - -type AppSwap struct { - cmd.Command - force bool - cnameOnly bool - fs *gnuflag.FlagSet -} - -func (s *AppSwap) Info() *cmd.Info { - return &cmd.Info{ - Name: "app-swap", - Usage: "app swap [-f/--force] [-c/--cname-only]", - Desc: `Swaps routing between two apps. This allows zero downtime and makes rollback -as simple as swapping the applications back. - -Use [[--force]] if you want to swap applications with a different number of -units or different platform without confirmation. - -Use [[--cname-only]] if you want to swap all cnames except the default -cname of application`, - MinArgs: 2, - } -} - -func (s *AppSwap) Flags() *gnuflag.FlagSet { - if s.fs == nil { - s.fs = gnuflag.NewFlagSet("", gnuflag.ExitOnError) - s.fs.BoolVar(&s.force, "force", false, "Force Swap among apps with different number of units or different platform.") - s.fs.BoolVar(&s.force, "f", false, "Force Swap among apps with different number of units or different platform.") - s.fs.BoolVar(&s.cnameOnly, "cname-only", false, "Swap all cnames except the default cname.") - s.fs.BoolVar(&s.cnameOnly, "c", false, "Swap all cnames except the default cname.") - } - return s.fs -} - -func (s *AppSwap) Run(context *cmd.Context) error { - v := url.Values{} - v.Set("app1", context.Args[0]) - v.Set("app2", context.Args[1]) - v.Set("force", strconv.FormatBool(s.force)) - v.Set("cnameOnly", strconv.FormatBool(s.cnameOnly)) - u, err := config.GetURL("/swap") - if err != nil { - return err - } - err = makeSwap(u, strings.NewReader(v.Encode())) - if err != nil { - err = tsuruHTTP.UnwrapErr(err) - if e, ok := err.(*errors.HTTP); ok && e.Code == http.StatusPreconditionFailed { - var answer string - fmt.Fprintf(context.Stdout, "WARNING: %s.\nSwap anyway? (y/n) ", strings.TrimRight(e.Message, "\n")) - fmt.Fscanf(context.Stdin, "%s", &answer) - if answer == "y" || answer == "yes" { - v = url.Values{} - v.Set("app1", context.Args[0]) - v.Set("app2", context.Args[1]) - v.Set("force", "true") - v.Set("cnameOnly", strconv.FormatBool(s.cnameOnly)) - u, err = config.GetURL("/swap") - if err != nil { - return err - } - return makeSwap(u, strings.NewReader(v.Encode())) - } - fmt.Fprintln(context.Stdout, "swap aborted.") - return nil - } - return err - } - fmt.Fprintln(context.Stdout, "Apps successfully swapped!") - return err -} - -func makeSwap(url string, body io.Reader) error { - request, err := http.NewRequest("POST", url, body) - if err != nil { - return err - } - request.Header.Set("Content-Type", "application/x-www-form-urlencoded") - _, err = tsuruHTTP.AuthenticatedClient.Do(request) - return err -} diff --git a/tsuru/client/swap_test.go b/tsuru/client/swap_test.go deleted file mode 100644 index 30dc4229..00000000 --- a/tsuru/client/swap_test.go +++ /dev/null @@ -1,147 +0,0 @@ -// Copyright 2016 tsuru-client authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package client - -import ( - "bytes" - "net/http" - "strings" - - "github.com/tsuru/tsuru/cmd" - "github.com/tsuru/tsuru/cmd/cmdtest" - "gopkg.in/check.v1" -) - -func (s *S) TestSwapInfo(c *check.C) { - command := AppSwap{} - c.Assert(command.Info(), check.NotNil) -} - -func (s *S) TestSwap(c *check.C) { - var buf bytes.Buffer - var called bool - transport := cmdtest.ConditionalTransport{ - Transport: cmdtest.Transport{Status: http.StatusOK, Message: ""}, - CondFunc: func(r *http.Request) bool { - called = true - app1Name := r.FormValue("app1") == "app1" - app2Name := r.FormValue("app2") == "app2" - forceSwap := r.FormValue("force") == "false" - cnameOnly := r.FormValue("cnameOnly") == "false" - method := r.Method == "POST" - url := strings.HasSuffix(r.URL.Path, "/swap") - return url && method && cnameOnly && forceSwap && app2Name && app1Name - }, - } - context := cmd.Context{ - Args: []string{"app1", "app2"}, - Stdout: &buf, - } - s.setupFakeTransport(&transport) - command := AppSwap{} - err := command.Run(&context) - c.Assert(err, check.IsNil) - c.Assert(called, check.Equals, true) - expected := "Apps successfully swapped!\n" - c.Assert(buf.String(), check.Equals, expected) -} - -func (s *S) TestSwapFlags(c *check.C) { - command := AppSwap{} - flagset := command.Flags() - c.Assert(flagset, check.NotNil) - flagset.Parse(true, []string{"app1", "app2", "-f", "-c"}) - forces := flagset.Lookup("force") - c.Check(forces, check.NotNil) - c.Check(forces.Name, check.Equals, "force") - c.Check(forces.Usage, check.Equals, "Force Swap among apps with different number of units or different platform.") - c.Check(forces.Value.String(), check.Equals, "true") - c.Check(forces.DefValue, check.Equals, "false") - cname := flagset.Lookup("cname-only") - c.Check(cname, check.NotNil) - c.Check(cname.Name, check.Equals, "cname-only") - c.Check(cname.Usage, check.Equals, "Swap all cnames except the default cname.") - c.Check(cname.Value.String(), check.Equals, "true") - c.Check(cname.DefValue, check.Equals, "false") -} - -func (s *S) TestSwapCnameOnlyFlag(c *check.C) { - var buf bytes.Buffer - var called bool - transport := cmdtest.ConditionalTransport{ - Transport: cmdtest.Transport{Status: http.StatusOK, Message: ""}, - CondFunc: func(r *http.Request) bool { - called = true - app1Name := r.FormValue("app1") == "app1" - app2Name := r.FormValue("app2") == "app2" - forceSwap := r.FormValue("force") == "false" - cnameOnly := r.FormValue("cnameOnly") == "true" - method := r.Method == "POST" - url := strings.HasSuffix(r.URL.Path, "/swap") - return url && method && cnameOnly && forceSwap && app2Name && app1Name - }, - } - context := cmd.Context{ - Args: []string{"app1", "app2"}, - Stdout: &buf, - } - s.setupFakeTransport(&transport) - command := AppSwap{} - command.Flags().Parse(true, []string{"-c"}) - err := command.Run(&context) - c.Assert(err, check.IsNil) - c.Assert(called, check.Equals, true) - expected := "Apps successfully swapped!\n" - c.Assert(buf.String(), check.Equals, expected) -} - -func (s *S) TestSwapWhenAppsAreNotEqual(c *check.C) { - var buf bytes.Buffer - var called int - stdin := bytes.NewBufferString("yes") - transportError := cmdtest.ConditionalTransport{ - Transport: cmdtest.Transport{Status: http.StatusPreconditionFailed, Message: "Apps are not equal."}, - CondFunc: func(r *http.Request) bool { - called++ - app1Name := r.FormValue("app1") == "app1" - app2Name := r.FormValue("app2") == "app2" - forceSwap := r.FormValue("force") == "false" - cnameOnly := r.FormValue("cnameOnly") == "false" - method := r.Method == "POST" - url := strings.HasSuffix(r.URL.Path, "/swap") - return url && method && cnameOnly && forceSwap && app2Name && app1Name - }, - } - transportOk := cmdtest.ConditionalTransport{ - Transport: cmdtest.Transport{Status: http.StatusOK, Message: ""}, - CondFunc: func(r *http.Request) bool { - called++ - app1Name := r.FormValue("app1") == "app1" - app2Name := r.FormValue("app2") == "app2" - forceSwap := r.FormValue("force") == "true" - cnameOnly := r.FormValue("cnameOnly") == "false" - method := r.Method == "POST" - url := strings.HasSuffix(r.URL.Path, "/swap") - return url && method && cnameOnly && forceSwap && app2Name && app1Name - }, - } - multiTransport := cmdtest.MultiConditionalTransport{ - ConditionalTransports: []cmdtest.ConditionalTransport{transportError, transportOk}, - } - context := cmd.Context{ - Args: []string{"app1", "app2"}, - Stdout: &buf, - Stdin: stdin, - } - s.setupFakeTransport(&multiTransport) - command := AppSwap{} - err := command.Run(&context) - c.Assert(err, check.IsNil) - c.Assert(called, check.Equals, 2) -} - -func (s *S) TestSwapIsACommand(c *check.C) { - var _ cmd.Command = &AppSwap{} -} diff --git a/tsuru/main.go b/tsuru/main.go index e8300cca..fe14b9c8 100644 --- a/tsuru/main.go +++ b/tsuru/main.go @@ -169,7 +169,6 @@ Services aren’t managed by tsuru, but by their creators.`) m.Register(&client.PluginRemove{}) m.Register(&client.PluginList{}) m.Register(&client.PluginBundle{}) - m.Register(&client.AppSwap{}) m.Register(&client.AppDeploy{}) m.Register(&client.AppBuild{}) m.Register(&client.PlanList{}) diff --git a/tsuru/main_test.go b/tsuru/main_test.go index 8a581295..63c13d19 100644 --- a/tsuru/main_test.go +++ b/tsuru/main_test.go @@ -242,13 +242,6 @@ func (s *S) TestPlatformAddIsRegistered(c *check.C) { c.Assert(plat, check.FitsTypeOf, &admin.PlatformAdd{}) } -func (s *S) TestAppSwapIsRegistered(c *check.C) { - manager = buildManager("tsuru") - cmd, ok := manager.Commands["app-swap"] - c.Assert(ok, check.Equals, true) - c.Assert(cmd, check.FitsTypeOf, &client.AppSwap{}) -} - func (s *S) TestAppStartIsRegistered(c *check.C) { manager = buildManager("tsuru") start, ok := manager.Commands["app-start"]