github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/recommendation_info_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/stretchr/testify/require"
    10  
    11  	"github.com/hashicorp/nomad/api"
    12  	"github.com/hashicorp/nomad/testutil"
    13  )
    14  
    15  func TestRecommendationInfoCommand_Run(t *testing.T) {
    16  	ci.Parallel(t)
    17  	require := require.New(t)
    18  	srv, client, url := testServer(t, true, nil)
    19  	defer srv.Shutdown()
    20  	testutil.WaitForResult(func() (bool, error) {
    21  		nodes, _, err := client.Nodes().List(nil)
    22  		if err != nil {
    23  			return false, err
    24  		}
    25  		if len(nodes) == 0 {
    26  			return false, fmt.Errorf("missing node")
    27  		}
    28  		if _, ok := nodes[0].Drivers["mock_driver"]; !ok {
    29  			return false, fmt.Errorf("mock_driver not ready")
    30  		}
    31  		return true, nil
    32  	}, func(err error) {
    33  		t.Fatalf("err: %s", err)
    34  	})
    35  
    36  	ui := cli.NewMockUi()
    37  	cmd := &RecommendationInfoCommand{
    38  		RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
    39  			Meta: Meta{Ui: ui},
    40  		},
    41  	}
    42  
    43  	// Perform an initial call, which should return a not found error.
    44  	code := cmd.Run([]string{"-address=" + url, "2c13f001-f5b6-ce36-03a5-e37afe160df5"})
    45  	if srv.Enterprise {
    46  		require.Equal(1, code)
    47  		out := ui.ErrorWriter.String()
    48  		require.Contains(out, "Recommendation not found")
    49  	} else {
    50  		require.Equal(1, code)
    51  		require.Contains(ui.ErrorWriter.String(), "Nomad Enterprise only endpoint")
    52  	}
    53  
    54  	// Register a test job to write a recommendation against.
    55  	testJob := testJob("recommendation_info")
    56  	regResp, _, err := client.Jobs().Register(testJob, nil)
    57  	require.NoError(err)
    58  	registerCode := waitForSuccess(ui, client, fullId, t, regResp.EvalID)
    59  	require.Equal(0, registerCode)
    60  
    61  	// Write a recommendation.
    62  	rec := api.Recommendation{
    63  		JobID:    *testJob.ID,
    64  		Group:    *testJob.TaskGroups[0].Name,
    65  		Task:     testJob.TaskGroups[0].Tasks[0].Name,
    66  		Resource: "CPU",
    67  		Value:    1050,
    68  		Meta:     map[string]interface{}{"test-meta-entry": "test-meta-value"},
    69  		Stats:    map[string]float64{"p13": 1.13},
    70  	}
    71  	recResp, _, err := client.Recommendations().Upsert(&rec, nil)
    72  	if srv.Enterprise {
    73  		require.NoError(err)
    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  		code = cmd.Run([]string{"-address=" + url, recResp.ID})
    82  		require.Equal(0, code)
    83  		out := ui.OutputWriter.String()
    84  		require.Contains(out, "test-meta-entry")
    85  		require.Contains(out, "p13")
    86  		require.Contains(out, "1.13")
    87  		require.Contains(out, recResp.ID)
    88  	}
    89  }
    90  
    91  func TestRecommendationInfoCommand_AutocompleteArgs(t *testing.T) {
    92  	ci.Parallel(t)
    93  
    94  	srv, client, url := testServer(t, false, nil)
    95  	defer srv.Shutdown()
    96  
    97  	ui := cli.NewMockUi()
    98  	cmd := RecommendationInfoCommand{
    99  		RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
   100  			Meta: Meta{
   101  				Ui:          ui,
   102  				flagAddress: url,
   103  			},
   104  		},
   105  	}
   106  	testRecommendationAutocompleteCommand(t, client, srv, &cmd.RecommendationAutocompleteCommand)
   107  }