github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/recommendation_dismiss_test.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/nomad/ci" 8 "github.com/mitchellh/cli" 9 "github.com/posener/complete" 10 "github.com/stretchr/testify/require" 11 12 "github.com/hashicorp/nomad/api" 13 "github.com/hashicorp/nomad/command/agent" 14 "github.com/hashicorp/nomad/testutil" 15 ) 16 17 func TestRecommendationDismissCommand_Run(t *testing.T) { 18 ci.Parallel(t) 19 require := require.New(t) 20 srv, client, url := testServer(t, true, nil) 21 defer srv.Shutdown() 22 testutil.WaitForResult(func() (bool, error) { 23 nodes, _, err := client.Nodes().List(nil) 24 if err != nil { 25 return false, err 26 } 27 if len(nodes) == 0 { 28 return false, fmt.Errorf("missing node") 29 } 30 if _, ok := nodes[0].Drivers["mock_driver"]; !ok { 31 return false, fmt.Errorf("mock_driver not ready") 32 } 33 return true, nil 34 }, func(err error) { 35 t.Fatalf("err: %s", err) 36 }) 37 38 ui := cli.NewMockUi() 39 cmd := &RecommendationDismissCommand{ 40 RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{ 41 Meta: Meta{ 42 Ui: ui, 43 flagAddress: url, 44 }, 45 }, 46 } 47 48 // Register a test job to write a recommendation against. 49 testJob := testJob("recommendation_dismiss") 50 regResp, _, err := client.Jobs().Register(testJob, nil) 51 require.NoError(err) 52 registerCode := waitForSuccess(ui, client, fullId, t, regResp.EvalID) 53 require.Equal(0, registerCode) 54 55 // Write a recommendation. 56 rec := api.Recommendation{ 57 JobID: *testJob.ID, 58 Group: *testJob.TaskGroups[0].Name, 59 Task: testJob.TaskGroups[0].Tasks[0].Name, 60 Resource: "CPU", 61 Value: 1050, 62 Meta: map[string]interface{}{"test-meta-entry": "test-meta-value"}, 63 Stats: map[string]float64{"p13": 1.13}, 64 } 65 recResp, _, err := client.Recommendations().Upsert(&rec, nil) 66 if srv.Enterprise { 67 require.NoError(err) 68 69 // Read the recommendation out to ensure it is there as a control on 70 // later tests. 71 recInfo, _, err := client.Recommendations().Info(recResp.ID, nil) 72 require.NoError(err) 73 require.NotNil(recInfo) 74 } else { 75 require.Error(err, "Nomad Enterprise only endpoint") 76 } 77 78 // Only perform the call if we are running enterprise tests. Otherwise the 79 // recResp object will be nil. 80 if !srv.Enterprise { 81 return 82 } 83 code := cmd.Run([]string{"-address=" + url, recResp.ID}) 84 require.Equal(0, code) 85 out := ui.OutputWriter.String() 86 require.Contains(out, "Successfully dismissed recommendation") 87 88 // Perform an info call on the recommendation which should return not 89 // found. 90 recInfo, _, err := client.Recommendations().Info(recResp.ID, nil) 91 require.Error(err, "not found") 92 require.Nil(recInfo) 93 } 94 95 func TestRecommendationDismissCommand_AutocompleteArgs(t *testing.T) { 96 srv, client, url := testServer(t, false, nil) 97 defer srv.Shutdown() 98 99 ui := cli.NewMockUi() 100 cmd := &RecommendationDismissCommand{ 101 RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{ 102 Meta: Meta{ 103 Ui: ui, 104 flagAddress: url, 105 }, 106 }, 107 } 108 109 testRecommendationAutocompleteCommand(t, client, srv, &cmd.RecommendationAutocompleteCommand) 110 } 111 112 func testRecommendationAutocompleteCommand(t *testing.T, client *api.Client, srv *agent.TestAgent, cmd *RecommendationAutocompleteCommand) { 113 ci.Parallel(t) 114 require := require.New(t) 115 116 // Register a test job to write a recommendation against. 117 testJob := testJob("recommendation_autocomplete") 118 _, _, err := client.Jobs().Register(testJob, nil) 119 require.NoError(err) 120 121 // Write a recommendation. 122 rec := &api.Recommendation{ 123 JobID: *testJob.ID, 124 Group: *testJob.TaskGroups[0].Name, 125 Task: testJob.TaskGroups[0].Tasks[0].Name, 126 Resource: "CPU", 127 Value: 1050, 128 Meta: map[string]interface{}{"test-meta-entry": "test-meta-value"}, 129 Stats: map[string]float64{"p13": 1.13}, 130 } 131 rec, _, err = client.Recommendations().Upsert(rec, nil) 132 if srv.Enterprise { 133 require.NoError(err) 134 } else { 135 require.Error(err, "Nomad Enterprise only endpoint") 136 return 137 } 138 139 prefix := rec.ID[:5] 140 args := complete.Args{Last: prefix} 141 predictor := cmd.AutocompleteArgs() 142 143 res := predictor.Predict(args) 144 require.Equal(1, len(res)) 145 require.Equal(rec.ID, res[0]) 146 }