Skip to content

Commit

Permalink
Add tests about AppProcessUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Dec 20, 2023
1 parent f06779a commit b5d9fda
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tsuru/client/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,7 @@ func (c *AppProcessUpdate) Flags() *gnuflag.FlagSet {
if c.fs == nil {
flagSet := gnuflag.NewFlagSet("", gnuflag.ExitOnError)
planMessage := "Changes plan for the app"
planReset := "Reset process to default of app"
planReset := "Reset process to default plan of app"
noRestartMessage := "Prevent tsuru from restarting the application"
flagSet.StringVar(&c.plan, "plan", "", planMessage)
flagSet.StringVar(&c.plan, "p", "", planMessage)
Expand Down
74 changes: 74 additions & 0 deletions tsuru/client/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2979,3 +2979,77 @@ func (s *S) TestAppStop(c *check.C) {
func (s *S) TestAppStopIsAFlaggedCommand(c *check.C) {
var _ cmd.FlaggedCommand = &AppStop{}
}

func (s *S) TestAppProcessUpdate(c *check.C) {
var stdout, stderr bytes.Buffer
expectedOut := "Process \"process02\" of app \"app01\" has been updated!\n"
msg := tsuruIo.SimpleJsonMessage{Message: "stream\n"}
result, err := json.Marshal(msg)
c.Assert(err, check.IsNil)
context := cmd.Context{
Args: []string{"app01", "process02"},
Stdout: &stdout,
Stderr: &stderr,
Stdin: nil,
}
client := cmd.NewClient(&http.Client{
Transport: &cmdtest.ConditionalTransport{
Transport: cmdtest.Transport{Message: string(result), Status: http.StatusOK},
CondFunc: func(r *http.Request) bool {
m := map[string]any{}
err = json.NewDecoder(r.Body).Decode(&m)
c.Assert(err, check.IsNil)
c.Assert(m["processes"], check.DeepEquals, []any{
map[string]any{
"name": "process02",
"plan": "c2m2",
"metadata": map[string]any{},
},
})
return true
},
},
}, nil, manager)
command := AppProcessUpdate{}
command.Flags().Parse(true, []string{"--plan", "c2m2"})
err = command.Run(&context, client)
c.Assert(err, check.IsNil)
c.Assert(stdout.String(), check.Equals, "stream\n"+expectedOut)
}

func (s *S) TestAppProcessUpdateReset(c *check.C) {
var stdout, stderr bytes.Buffer
expectedOut := "Process \"process02\" of app \"app01\" has been updated!\n"
msg := tsuruIo.SimpleJsonMessage{Message: "stream\n"}
result, err := json.Marshal(msg)
c.Assert(err, check.IsNil)
context := cmd.Context{
Args: []string{"app01", "process02"},
Stdout: &stdout,
Stderr: &stderr,
Stdin: nil,
}
client := cmd.NewClient(&http.Client{
Transport: &cmdtest.ConditionalTransport{
Transport: cmdtest.Transport{Message: string(result), Status: http.StatusOK},
CondFunc: func(r *http.Request) bool {
m := map[string]any{}
err = json.NewDecoder(r.Body).Decode(&m)
c.Assert(err, check.IsNil)
c.Assert(m["processes"], check.DeepEquals, []any{
map[string]any{
"name": "process02",
"plan": "$default",
"metadata": map[string]any{},
},
})
return true
},
},
}, nil, manager)
command := AppProcessUpdate{}
command.Flags().Parse(true, []string{"--default-plan"})
err = command.Run(&context, client)
c.Assert(err, check.IsNil)
c.Assert(stdout.String(), check.Equals, "stream\n"+expectedOut)
}

0 comments on commit b5d9fda

Please sign in to comment.