2020-01-30 00:26:31 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-03-17 13:34:57 +00:00
|
|
|
"github.com/hashicorp/nomad/api/internal/testutil"
|
2023-01-01 18:57:26 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2020-01-30 00:26:31 +00:00
|
|
|
)
|
|
|
|
|
2020-02-21 21:23:30 +00:00
|
|
|
func TestScalingPolicies_ListPolicies(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2020-01-30 00:26:31 +00:00
|
|
|
|
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
scaling := c.Scaling()
|
|
|
|
jobs := c.Jobs()
|
|
|
|
|
|
|
|
// Check that we don't have any scaling policies before registering a job that has one
|
|
|
|
policies, _, err := scaling.ListPolicies(nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.SliceEmpty(t, policies)
|
2020-01-30 00:26:31 +00:00
|
|
|
|
|
|
|
// Register a job with a scaling policy
|
|
|
|
job := testJob()
|
2020-03-22 11:54:04 +00:00
|
|
|
job.TaskGroups[0].Scaling = &ScalingPolicy{
|
2022-08-17 16:26:34 +00:00
|
|
|
Max: pointerOf(int64(100)),
|
2020-03-22 11:54:04 +00:00
|
|
|
}
|
2020-01-30 00:26:31 +00:00
|
|
|
_, _, err = jobs.Register(job, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2020-01-30 00:26:31 +00:00
|
|
|
|
|
|
|
// Check that we have a scaling policy now
|
|
|
|
policies, _, err = scaling.ListPolicies(nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.Len(t, 1, policies)
|
2020-01-30 00:26:31 +00:00
|
|
|
|
|
|
|
policy := policies[0]
|
|
|
|
|
2020-02-21 21:23:30 +00:00
|
|
|
// Check that the scaling policy references the right namespace
|
|
|
|
namespace := DefaultNamespace
|
|
|
|
if job.Namespace != nil && *job.Namespace != "" {
|
|
|
|
namespace = *job.Namespace
|
|
|
|
}
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, policy.Target["Namespace"], namespace)
|
2020-02-21 21:23:30 +00:00
|
|
|
|
2020-01-30 00:26:31 +00:00
|
|
|
// Check that the scaling policy references the right job
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, policy.Target["Job"], *job.ID)
|
2020-01-30 00:26:31 +00:00
|
|
|
|
2020-02-21 21:23:30 +00:00
|
|
|
// Check that the scaling policy references the right group
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, policy.Target["Group"], *job.TaskGroups[0].Name)
|
2020-09-29 21:57:46 +00:00
|
|
|
|
|
|
|
// Check that the scaling policy has the right type
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, ScalingPolicyTypeHorizontal, policy.Type)
|
2020-01-30 00:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestScalingPolicies_GetPolicy(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2020-01-30 00:26:31 +00:00
|
|
|
|
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
scaling := c.Scaling()
|
|
|
|
jobs := c.Jobs()
|
|
|
|
|
|
|
|
// Empty ID should return 404
|
|
|
|
_, _, err := scaling.GetPolicy("", nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.ErrorContains(t, err, "404")
|
2020-01-30 00:26:31 +00:00
|
|
|
|
2023-01-01 18:57:26 +00:00
|
|
|
// Non-existent ID should return 404
|
|
|
|
_, _, err = scaling.GetPolicy("i-do-not-exist", nil)
|
|
|
|
must.ErrorContains(t, err, "404")
|
2020-01-30 00:26:31 +00:00
|
|
|
|
|
|
|
// Register a job with a scaling policy
|
|
|
|
job := testJob()
|
|
|
|
policy := &ScalingPolicy{
|
2022-08-17 16:26:34 +00:00
|
|
|
Enabled: pointerOf(true),
|
|
|
|
Min: pointerOf(int64(1)),
|
|
|
|
Max: pointerOf(int64(1)),
|
2020-01-30 00:26:31 +00:00
|
|
|
Policy: map[string]interface{}{
|
|
|
|
"key": "value",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
job.TaskGroups[0].Scaling = policy
|
|
|
|
_, _, err = jobs.Register(job, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2020-01-30 00:26:31 +00:00
|
|
|
|
|
|
|
// Find newly created scaling policy ID
|
|
|
|
var policyID string
|
|
|
|
policies, _, err := scaling.ListPolicies(nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2020-01-30 00:26:31 +00:00
|
|
|
for _, p := range policies {
|
2020-02-21 21:23:30 +00:00
|
|
|
if p.Target["Job"] == *job.ID {
|
2020-01-30 00:26:31 +00:00
|
|
|
policyID = p.ID
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if policyID == "" {
|
|
|
|
t.Fatalf("unable to find scaling policy for job %s", *job.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch scaling policy
|
|
|
|
resp, _, err := scaling.GetPolicy(policyID, nil)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.NoError(t, err)
|
2020-01-30 00:26:31 +00:00
|
|
|
|
|
|
|
// Check that the scaling policy fields match
|
2020-02-21 21:23:30 +00:00
|
|
|
namespace := DefaultNamespace
|
|
|
|
if job.Namespace != nil && *job.Namespace != "" {
|
|
|
|
namespace = *job.Namespace
|
|
|
|
}
|
|
|
|
expectedTarget := map[string]string{
|
|
|
|
"Namespace": namespace,
|
|
|
|
"Job": *job.ID,
|
|
|
|
"Group": *job.TaskGroups[0].Name,
|
|
|
|
}
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, expectedTarget, resp.Target)
|
|
|
|
must.Eq(t, policy.Policy, resp.Policy)
|
|
|
|
must.Eq(t, policy.Enabled, resp.Enabled)
|
|
|
|
must.Eq(t, *policy.Min, *resp.Min)
|
|
|
|
must.Eq(t, policy.Max, resp.Max)
|
|
|
|
must.Eq(t, ScalingPolicyTypeHorizontal, resp.Type)
|
2020-01-30 00:26:31 +00:00
|
|
|
}
|