2020-10-05 13:18:30 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2020-10-05 13:18:30 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-05 13:18:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestScalingPolicyListCommand_Run(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-09-09 22:30:40 +00:00
|
|
|
require := require.New(t)
|
|
|
|
srv, client, url := testServer(t, false, nil)
|
2020-10-05 13:18:30 +00:00
|
|
|
defer srv.Shutdown()
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &ScalingPolicyListCommand{Meta: Meta{Ui: ui}}
|
|
|
|
|
|
|
|
// Perform an initial list, which should return zero results.
|
2020-09-09 22:30:40 +00:00
|
|
|
code := cmd.Run([]string{"-address=" + url})
|
|
|
|
require.Equal(0, code)
|
|
|
|
out := ui.OutputWriter.String()
|
|
|
|
require.Contains(out, "No policies found")
|
2020-10-05 13:18:30 +00:00
|
|
|
|
|
|
|
// Generate two test jobs.
|
|
|
|
jobs := []*api.Job{testJob("scaling_policy_list_1"), testJob("scaling_policy_list_2")}
|
|
|
|
|
|
|
|
// Generate an example scaling policy.
|
|
|
|
scalingPolicy := api.ScalingPolicy{
|
2020-09-09 22:30:40 +00:00
|
|
|
Type: api.ScalingPolicyTypeHorizontal,
|
2020-10-05 13:18:30 +00:00
|
|
|
Enabled: helper.BoolToPtr(true),
|
|
|
|
Min: helper.Int64ToPtr(1),
|
|
|
|
Max: helper.Int64ToPtr(1),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate the jobs, add the scaling policy and register.
|
|
|
|
for _, job := range jobs {
|
|
|
|
job.TaskGroups[0].Scaling = &scalingPolicy
|
2020-09-09 22:30:40 +00:00
|
|
|
_, _, err := client.Jobs().Register(job, nil)
|
|
|
|
require.NoError(err)
|
2020-10-05 13:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Perform a new list which should yield results..
|
2020-09-09 22:30:40 +00:00
|
|
|
code = cmd.Run([]string{"-address=" + url})
|
|
|
|
require.Equal(0, code)
|
|
|
|
out = ui.OutputWriter.String()
|
|
|
|
require.Contains(out, "ID")
|
|
|
|
require.Contains(out, "Enabled")
|
|
|
|
require.Contains(out, "Type")
|
|
|
|
require.Contains(out, "Target")
|
|
|
|
require.Contains(out, "scaling_policy_list_1")
|
|
|
|
require.Contains(out, "scaling_policy_list_2")
|
2020-10-05 13:18:30 +00:00
|
|
|
}
|