Skip to content

Commit

Permalink
test: keda prometheus autoscaler
Browse files Browse the repository at this point in the history
  • Loading branch information
crgisch committed Apr 8, 2024
1 parent da460e2 commit 6dbaaa4
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 16 deletions.
53 changes: 38 additions & 15 deletions tsuru/client/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,19 @@ func (s *S) TestAppInfoWithKEDAAutoScale(c *check.C) {
"end":"0 15 * * *",
"timezone":"UTC"
}
],
"prometheus": [
{
"name":"my_metric_id",
"threshold":2,
"query":"my_query{app='my-app'}",
"prometheusAddress":"default.com"
},{
"name":"my_metric_id_2",
"threshold":5,
"query":"my_query_2{app='my-app'}",
"prometheusAddress":"exemple.prometheus.com"
}
]
},
{
Expand Down Expand Up @@ -1730,21 +1743,31 @@ Units [process worker]: 1
Auto Scale:
Process: web (v10), Min Units: 1, Max Units: 10
+----------+---------------------------------+
| Triggers | Trigger details |
+----------+---------------------------------+
| CPU | Target: 50% |
+----------+---------------------------------+
| Schedule | Start: At 06:00 AM (0 6 * * *) |
| | End: At 06:00 PM (0 18 * * *) |
| | Units: 2 |
| | Timezone: UTC |
+----------+---------------------------------+
| Schedule | Start: At 12:00 PM (0 12 * * *) |
| | End: At 03:00 PM (0 15 * * *) |
| | Units: 3 |
| | Timezone: UTC |
+----------+---------------------------------+
+------------+-------------------------------------------+
| Triggers | Trigger details |
+------------+-------------------------------------------+
| CPU | Target: 50% |
+------------+-------------------------------------------+
| Schedule | Start: At 06:00 AM (0 6 * * *) |
| | End: At 06:00 PM (0 18 * * *) |
| | Units: 2 |
| | Timezone: UTC |
+------------+-------------------------------------------+
| Schedule | Start: At 12:00 PM (0 12 * * *) |
| | End: At 03:00 PM (0 15 * * *) |
| | Units: 3 |
| | Timezone: UTC |
+------------+-------------------------------------------+
| Prometheus | Name: my_metric_id |
| | Query: my_query{app='my-app'} |
| | Threshold: 2 |
| | PrometheusAddress: default.com |
+------------+-------------------------------------------+
| Prometheus | Name: my_metric_id_2 |
| | Query: my_query_2{app='my-app'} |
| | Threshold: 5 |
| | PrometheusAddress: exemple.prometheus.com |
+------------+-------------------------------------------+
Process: worker (v10), Min Units: 2, Max Units: 5
+----------+--------------------------------+
Expand Down
55 changes: 54 additions & 1 deletion tsuru/client/autoscale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *S) TestAutoScaleSet(c *check.C) {
c.Assert(stdout.String(), check.Equals, expected)
}

func (s *S) TestKEDAAutoScaleSet(c *check.C) {
func (s *S) TestKEDAScheduleAutoScaleSet(c *check.C) {
var stdout, stderr bytes.Buffer
expected := "Unit auto scale successfully set.\n"
context := cmd.Context{
Expand Down Expand Up @@ -106,6 +106,59 @@ func (s *S) TestKEDAAutoScaleSet(c *check.C) {
c.Assert(stdout.String(), check.Equals, expected)
}

func (s *S) TestKEDAPrometheusAutoScaleSet(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{
MinUnits: 1,
MaxUnits: 5,
Process: "proc1",
Prometheus: []tsuru.AutoScalePrometheus{
{
Name: "prometheus_metric_1",
Threshold: 1,
Query: "my_metric_1(app='my-app')",
},
{
Name: "prometheus_metric_2",
Threshold: 5,
Query: "my_metric_2(app='my-app')",
PrometheusAddress: "exemple.prometheus.com",
},
},
})
return true
},
}
s.setupFakeTransport(&trans)
command := AutoScaleSet{}
command.Info()
command.Flags().Parse(true, []string{
"-a", "myapp", "-p", "proc1", "--min", "1", "--max", "5",
"--prometheus", "{\"name\": \"prometheus_metric_1\", \"threshold\": 1, \"query\": \"my_metric_1(app='my-app')\"}",
"--prometheus", "{\"name\": \"prometheus_metric_2\", \"threshold\": 5, \"query\": \"my_metric_2(app='my-app')\", \"prometheusAddress\": \"exemple.prometheus.com\"}",
})
err := command.Run(&context)
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"
Expand Down

0 comments on commit 6dbaaa4

Please sign in to comment.