2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-11-06 10:16:24 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2020-11-06 10:16:24 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-11-11 11:39:26 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2020-11-06 10:16:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRecommendationInfoCommand_Run(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-11-06 10:16:24 +00:00
|
|
|
require := require.New(t)
|
|
|
|
srv, client, url := testServer(t, true, nil)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes, _, err := client.Nodes().List(nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
return false, fmt.Errorf("missing node")
|
|
|
|
}
|
|
|
|
if _, ok := nodes[0].Drivers["mock_driver"]; !ok {
|
|
|
|
return false, fmt.Errorf("mock_driver not ready")
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
2020-11-11 20:52:57 +00:00
|
|
|
cmd := &RecommendationInfoCommand{
|
|
|
|
RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
},
|
|
|
|
}
|
2020-11-06 10:16:24 +00:00
|
|
|
|
|
|
|
// Perform an initial call, which should return a not found error.
|
|
|
|
code := cmd.Run([]string{"-address=" + url, "2c13f001-f5b6-ce36-03a5-e37afe160df5"})
|
|
|
|
if srv.Enterprise {
|
|
|
|
require.Equal(1, code)
|
|
|
|
out := ui.ErrorWriter.String()
|
|
|
|
require.Contains(out, "Recommendation not found")
|
|
|
|
} else {
|
|
|
|
require.Equal(1, code)
|
|
|
|
require.Contains(ui.ErrorWriter.String(), "Nomad Enterprise only endpoint")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a test job to write a recommendation against.
|
|
|
|
testJob := testJob("recommendation_info")
|
|
|
|
regResp, _, err := client.Jobs().Register(testJob, nil)
|
|
|
|
require.NoError(err)
|
|
|
|
registerCode := waitForSuccess(ui, client, fullId, t, regResp.EvalID)
|
|
|
|
require.Equal(0, registerCode)
|
|
|
|
|
|
|
|
// Write a recommendation.
|
|
|
|
rec := api.Recommendation{
|
|
|
|
JobID: *testJob.ID,
|
|
|
|
Group: *testJob.TaskGroups[0].Name,
|
|
|
|
Task: testJob.TaskGroups[0].Tasks[0].Name,
|
|
|
|
Resource: "CPU",
|
|
|
|
Value: 1050,
|
|
|
|
Meta: map[string]interface{}{"test-meta-entry": "test-meta-value"},
|
|
|
|
Stats: map[string]float64{"p13": 1.13},
|
|
|
|
}
|
|
|
|
recResp, _, err := client.Recommendations().Upsert(&rec, nil)
|
|
|
|
if srv.Enterprise {
|
|
|
|
require.NoError(err)
|
|
|
|
} else {
|
|
|
|
require.Error(err, "Nomad Enterprise only endpoint")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only perform the call if we are running enterprise tests. Otherwise the
|
|
|
|
// recResp object will be nil.
|
|
|
|
if srv.Enterprise {
|
|
|
|
code = cmd.Run([]string{"-address=" + url, recResp.ID})
|
|
|
|
require.Equal(0, code)
|
|
|
|
out := ui.OutputWriter.String()
|
|
|
|
require.Contains(out, "test-meta-entry")
|
|
|
|
require.Contains(out, "p13")
|
|
|
|
require.Contains(out, "1.13")
|
|
|
|
require.Contains(out, recResp.ID)
|
|
|
|
}
|
|
|
|
}
|
2020-11-11 10:29:42 +00:00
|
|
|
|
|
|
|
func TestRecommendationInfoCommand_AutocompleteArgs(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2020-11-11 20:52:57 +00:00
|
|
|
srv, client, url := testServer(t, false, nil)
|
2020-11-11 10:29:42 +00:00
|
|
|
defer srv.Shutdown()
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
2020-11-11 20:52:57 +00:00
|
|
|
cmd := RecommendationInfoCommand{
|
|
|
|
RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
Ui: ui,
|
|
|
|
flagAddress: url,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testRecommendationAutocompleteCommand(t, client, srv, &cmd.RecommendationAutocompleteCommand)
|
2020-11-11 10:29:42 +00:00
|
|
|
}
|