Merge pull request #9327 from hashicorp/b-fix-rec-cli

recs cli: fixed bad composition
This commit is contained in:
Chris Baker 2020-11-11 15:54:14 -06:00 committed by GitHub
commit 611502dd5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 71 additions and 32 deletions

View file

@ -655,17 +655,23 @@ func Commands(metaPtr *Meta, agentUi cli.Ui) map[string]cli.CommandFactory {
},
"recommendation apply": func() (cli.Command, error) {
return &RecommendationApplyCommand{
Meta: meta,
RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
Meta: meta,
},
}, nil
},
"recommendation dismiss": func() (cli.Command, error) {
return &RecommendationDismissCommand{
Meta: meta,
RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
Meta: meta,
},
}, nil
},
"recommendation info": func() (cli.Command, error) {
return &RecommendationInfoCommand{
Meta: meta,
RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
Meta: meta,
},
}, nil
},
"recommendation list": func() (cli.Command, error) {

View file

@ -15,7 +15,6 @@ var _ cli.Command = &RecommendationApplyCommand{}
// RecommendationApplyCommand implements cli.Command.
type RecommendationApplyCommand struct {
Meta
RecommendationAutocompleteCommand
}

View file

@ -33,7 +33,13 @@ func TestRecommendationApplyCommand_Run(t *testing.T) {
})
ui := cli.NewMockUi()
cmd := &RecommendationApplyCommand{Meta: Meta{Ui: ui}}
cmd := &RecommendationApplyCommand{
RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
Meta: Meta{
Ui: ui,
},
},
}
// Register a test job to write a recommendation against.
testJob := testJob("recommendation_apply")
@ -86,10 +92,17 @@ func TestRecommendationApplyCommand_Run(t *testing.T) {
}
func TestRecommendationApplyCommand_AutocompleteArgs(t *testing.T) {
srv, client, url := testServer(t, true, nil)
srv, client, url := testServer(t, false, nil)
defer srv.Shutdown()
ui := cli.NewMockUi()
cmd := RecommendationApplyCommand{Meta: Meta{Ui: ui, flagAddress: url}}
testRecommendationAutocompleteCommand(t, client, srv, ui, &cmd.RecommendationAutocompleteCommand)
cmd := RecommendationApplyCommand{
RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
Meta: Meta{
Ui: ui,
flagAddress: url,
},
},
}
testRecommendationAutocompleteCommand(t, client, srv, &cmd.RecommendationAutocompleteCommand)
}

View file

@ -36,7 +36,6 @@ func (r *RecommendationAutocompleteCommand) AutocompleteArgs() complete.Predicto
// RecommendationDismissCommand implements cli.Command.
type RecommendationDismissCommand struct {
Meta
RecommendationAutocompleteCommand
}

View file

@ -4,13 +4,13 @@ import (
"fmt"
"testing"
"github.com/mitchellh/cli"
"github.com/posener/complete"
"github.com/stretchr/testify/require"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/command/agent"
"github.com/hashicorp/nomad/testutil"
"github.com/mitchellh/cli"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRecommendationDismissCommand_Run(t *testing.T) {
@ -35,7 +35,14 @@ func TestRecommendationDismissCommand_Run(t *testing.T) {
})
ui := cli.NewMockUi()
cmd := &RecommendationDismissCommand{Meta: Meta{Ui: ui}}
cmd := &RecommendationDismissCommand{
RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
Meta: Meta{
Ui: ui,
flagAddress: url,
},
},
}
// Register a test job to write a recommendation against.
testJob := testJob("recommendation_dismiss")
@ -85,25 +92,30 @@ func TestRecommendationDismissCommand_Run(t *testing.T) {
}
func TestRecommendationDismissCommand_AutocompleteArgs(t *testing.T) {
srv, client, url := testServer(t, true, nil)
srv, client, url := testServer(t, false, nil)
defer srv.Shutdown()
ui := cli.NewMockUi()
cmd := &RecommendationDismissCommand{Meta: Meta{Ui: ui, flagAddress: url}}
cmd := &RecommendationDismissCommand{
RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
Meta: Meta{
Ui: ui,
flagAddress: url,
},
},
}
testRecommendationAutocompleteCommand(t, client, srv, ui, &cmd.RecommendationAutocompleteCommand)
testRecommendationAutocompleteCommand(t, client, srv, &cmd.RecommendationAutocompleteCommand)
}
func testRecommendationAutocompleteCommand(t *testing.T, client *api.Client, srv *agent.TestAgent, ui *cli.MockUi, cmd *RecommendationAutocompleteCommand) {
assert := assert.New(t)
func testRecommendationAutocompleteCommand(t *testing.T, client *api.Client, srv *agent.TestAgent, cmd *RecommendationAutocompleteCommand) {
require := require.New(t)
t.Parallel()
// Register a test job to write a recommendation against.
testJob := testJob("recommendation_autocomplete")
regResp, _, err := client.Jobs().Register(testJob, nil)
require.NoError(t, err)
registerCode := waitForSuccess(ui, client, fullId, t, regResp.EvalID)
require.Equal(t, 0, registerCode)
_, _, err := client.Jobs().Register(testJob, nil)
require.NoError(err)
// Write a recommendation.
rec := &api.Recommendation{
@ -117,9 +129,9 @@ func testRecommendationAutocompleteCommand(t *testing.T, client *api.Client, srv
}
rec, _, err = client.Recommendations().Upsert(rec, nil)
if srv.Enterprise {
require.NoError(t, err)
require.NoError(err)
} else {
require.Error(t, err, "Nomad Enterprise only endpoint")
require.Error(err, "Nomad Enterprise only endpoint")
return
}
@ -128,6 +140,6 @@ func testRecommendationAutocompleteCommand(t *testing.T, client *api.Client, srv
predictor := cmd.AutocompleteArgs()
res := predictor.Predict(args)
assert.Equal(1, len(res))
assert.Equal(rec.ID, res[0])
require.Equal(1, len(res))
require.Equal(rec.ID, res[0])
}

View file

@ -14,7 +14,6 @@ var _ cli.Command = &RecommendationInfoCommand{}
// RecommendationInfoCommand implements cli.Command.
type RecommendationInfoCommand struct {
Meta
RecommendationAutocompleteCommand
}

View file

@ -33,7 +33,11 @@ func TestRecommendationInfoCommand_Run(t *testing.T) {
})
ui := cli.NewMockUi()
cmd := &RecommendationInfoCommand{Meta: Meta{Ui: ui}}
cmd := &RecommendationInfoCommand{
RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
Meta: Meta{Ui: ui},
},
}
// Perform an initial call, which should return a not found error.
code := cmd.Run([]string{"-address=" + url, "2c13f001-f5b6-ce36-03a5-e37afe160df5"})
@ -84,10 +88,17 @@ func TestRecommendationInfoCommand_Run(t *testing.T) {
}
func TestRecommendationInfoCommand_AutocompleteArgs(t *testing.T) {
srv, client, url := testServer(t, true, nil)
srv, client, url := testServer(t, false, nil)
defer srv.Shutdown()
ui := cli.NewMockUi()
cmd := RecommendationInfoCommand{Meta: Meta{Ui: ui, flagAddress: url}}
testRecommendationAutocompleteCommand(t, client, srv, ui, &cmd.RecommendationAutocompleteCommand)
cmd := RecommendationInfoCommand{
RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
Meta: Meta{
Ui: ui,
flagAddress: url,
},
},
}
testRecommendationAutocompleteCommand(t, client, srv, &cmd.RecommendationAutocompleteCommand)
}