diff --git a/tsuru/client/apps_test.go b/tsuru/client/apps_test.go index 97d061b8..3c19571f 100644 --- a/tsuru/client/apps_test.go +++ b/tsuru/client/apps_test.go @@ -1606,12 +1606,154 @@ Units [process worker]: 1 +--------+---------+------+------+ Auto Scale: -+--------------+-----+-----+------------+ -| Process | Min | Max | Target CPU | -+--------------+-----+-----+------------+ -| web (v10) | 1 | 10 | 50% | -| worker (v10) | 2 | 5 | 200% | -+--------------+-----+-----+------------+ + +Process: web (v10), Min Units: 1, Max Units: 10 ++----------+-----------------+ +| Triggers | Trigger details | ++----------+-----------------+ +| CPU | Target: 20% | ++----------+-----------------+ + +Process: worker (v10), Min Units: 2, Max Units: 5 ++----------+-----------------+ +| Triggers | Trigger details | ++----------+-----------------+ +| CPU | Target: 20% | ++----------+-----------------+ + +` + context := cmd.Context{ + Stdout: &stdout, + Stderr: &stderr, + } + client := cmd.NewClient(&http.Client{Transport: &cmdtest.Transport{Message: result, Status: http.StatusOK}}, nil, manager) + command := AppInfo{} + command.Flags().Parse(true, []string{"--app", "app1"}) + err := command.Run(&context, client) + c.Assert(err, check.IsNil) + c.Assert(stdout.String(), check.Equals, expected) +} + +func (s *S) TestAppInfoWithKEDAAutoScale(c *check.C) { + var stdout, stderr bytes.Buffer + result := `{ + "name": "app1", + "teamowner": "myteam", + "cname": [ + "" + ], + "ip": "myapp.tsuru.io", + "platform": "php", + "repository": "git@git.com:php.git", + "state": "dead", + "units": [ + { + "ID": "app1/0", + "Status": "started", + "ProcessName": "web" + }, + { + "ID": "app1/1", + "Status": "started", + "ProcessName": "worker" + } + ], + "teams": [ + "tsuruteam", + "crane" + ], + "owner": "myapp_owner", + "deploys": 7, + "router": "planb", + "autoscale": [ + { + "process":"web", + "minUnits":1, + "maxUnits":10, + "averageCPU":"500m", + "version":10, + "schedules": [ + { + "minReplicas":2, + "start":"0 6 * * *", + "end":"0 18 * * *", + "timezone":"UTC" + },{ + "minReplicas":3, + "start":"0 12 * * *", + "end":"0 15 * * *", + "timezone":"UTC" + } + ] + }, + { + "process":"worker", + "minUnits":2, + "maxUnits":5, + "averageCPU":"2", + "version":10, + "schedules": [ + { + "minReplicas":1, + "start":"0 0 * * *", + "end":"0 6 * * *", + "timezone":"UTC" + } + ] + } + ] +}` + expected := `Application: app1 +Platform: php +Router: planb +Teams: myteam (owner), tsuruteam, crane +External Addresses: myapp.tsuru.io +Created by: myapp_owner +Deploys: 7 +Pool: +Quota: 0/0 units + +Units [process web]: 1 ++--------+---------+------+------+ +| Name | Status | Host | Port | ++--------+---------+------+------+ +| app1/0 | started | | | ++--------+---------+------+------+ + +Units [process worker]: 1 ++--------+---------+------+------+ +| Name | Status | Host | Port | ++--------+---------+------+------+ +| app1/1 | started | | | ++--------+---------+------+------+ + +Auto Scale: + +Process: web (v10), Min Units: 1, Max Units: 10 ++----------+---------------------------------+ +| Triggers | Trigger details | ++----------+---------------------------------+ +| CPU | Target: 20% | ++----------+---------------------------------+ +| Schedule | Start: At 06:00 AM (0 6 * * *) | +| | End: At 06:00 PM (0 18 * * *) | +| | Units: 2 | ++----------+---------------------------------+ +| Schedule | Start: At 12:00 PM (0 12 * * *) | +| | End: At 03:00 PM (0 15 * * *) | +| | Units: 3 | ++----------+---------------------------------+ + +Process: worker (v10), Min Units: 2, Max Units: 5 ++----------+--------------------------------+ +| Triggers | Trigger details | ++----------+--------------------------------+ +| CPU | Target: 20% | ++----------+--------------------------------+ +| Schedule | Start: At 12:00 AM (0 0 * * *) | +| | End: At 06:00 AM (0 6 * * *) | +| | Units: 1 | ++----------+--------------------------------+ ` context := cmd.Context{ diff --git a/tsuru/client/autoscale_test.go b/tsuru/client/autoscale_test.go index 7cf94d64..2aca98a5 100644 --- a/tsuru/client/autoscale_test.go +++ b/tsuru/client/autoscale_test.go @@ -53,6 +53,59 @@ func (s *S) TestAutoScaleSet(c *check.C) { c.Assert(stdout.String(), check.Equals, expected) } +func (s *S) TestKEDAAutoScaleSet(c *check.C) { + var stdout, stderr bytes.Buffer + expected := "Unit auto scale successfully set.\n" + context := cmd.Context{ + Stdout: &stdout, + Stderr: &stderr, + Args: []string{}, + } + trans := cmdtest.ConditionalTransport{ + Transport: cmdtest.Transport{Message: "", Status: http.StatusOK}, + CondFunc: func(r *http.Request) bool { + c.Assert(r.URL.Path, check.Equals, "/1.9/apps/myapp/units/autoscale") + c.Assert(r.Method, check.Equals, "POST") + var ret tsuru.AutoScaleSpec + c.Assert(r.Header.Get("Content-Type"), check.Equals, "application/json") + data, err := io.ReadAll(r.Body) + c.Assert(err, check.IsNil) + err = json.Unmarshal(data, &ret) + c.Assert(err, check.IsNil) + c.Assert(ret, check.DeepEquals, tsuru.AutoScaleSpec{ + AverageCPU: "30%", + MinUnits: 2, + MaxUnits: 5, + Process: "proc1", + Schedules: []tsuru.AutoScaleSchedule{ + { + MinReplicas: 2, + Start: "0 6 * * *", + End: "0 18 * * *", + }, + { + MinReplicas: 1, + Start: "0 18 * * *", + End: "0 0 * * *", + }, + }, + }) + return true + }, + } + client := cmd.NewClient(&http.Client{Transport: &trans}, nil, manager) + command := AutoScaleSet{} + command.Info() + command.Flags().Parse(true, []string{ + "-a", "myapp", "-p", "proc1", "--min", "2", "--max", "5", "--cpu", "30%", + "--schedule", "{\"minReplicas\": 2, \"start\": \"0 6 * * *\", \"end\": \"0 18 * * *\"}", + "--schedule", "{\"minReplicas\": 1, \"start\": \"0 18 * * *\", \"end\": \"0 0 * * *\"}", + }) + err := command.Run(&context, client) + c.Assert(err, check.IsNil) + c.Assert(stdout.String(), check.Equals, expected) +} + func (s *S) TestAutoScaleUnset(c *check.C) { var stdout, stderr bytes.Buffer expected := "Unit auto scale successfully unset.\n"