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) { "recommendation apply": func() (cli.Command, error) {
return &RecommendationApplyCommand{ return &RecommendationApplyCommand{
RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
Meta: meta, Meta: meta,
},
}, nil }, nil
}, },
"recommendation dismiss": func() (cli.Command, error) { "recommendation dismiss": func() (cli.Command, error) {
return &RecommendationDismissCommand{ return &RecommendationDismissCommand{
RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
Meta: meta, Meta: meta,
},
}, nil }, nil
}, },
"recommendation info": func() (cli.Command, error) { "recommendation info": func() (cli.Command, error) {
return &RecommendationInfoCommand{ return &RecommendationInfoCommand{
RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
Meta: meta, Meta: meta,
},
}, nil }, nil
}, },
"recommendation list": func() (cli.Command, error) { "recommendation list": func() (cli.Command, error) {

View file

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

View file

@ -33,7 +33,13 @@ func TestRecommendationApplyCommand_Run(t *testing.T) {
}) })
ui := cli.NewMockUi() 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. // Register a test job to write a recommendation against.
testJob := testJob("recommendation_apply") testJob := testJob("recommendation_apply")
@ -86,10 +92,17 @@ func TestRecommendationApplyCommand_Run(t *testing.T) {
} }
func TestRecommendationApplyCommand_AutocompleteArgs(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() defer srv.Shutdown()
ui := cli.NewMockUi() ui := cli.NewMockUi()
cmd := RecommendationApplyCommand{Meta: Meta{Ui: ui, flagAddress: url}} cmd := RecommendationApplyCommand{
testRecommendationAutocompleteCommand(t, client, srv, ui, &cmd.RecommendationAutocompleteCommand) 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. // RecommendationDismissCommand implements cli.Command.
type RecommendationDismissCommand struct { type RecommendationDismissCommand struct {
Meta
RecommendationAutocompleteCommand RecommendationAutocompleteCommand
} }

View file

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

View file

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

View file

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