github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/recommendation_apply_test.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/mitchellh/cli"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/hashicorp/nomad/api"
    11  	"github.com/hashicorp/nomad/testutil"
    12  )
    13  
    14  func TestRecommendationApplyCommand_Run(t *testing.T) {
    15  	require := require.New(t)
    16  	t.Parallel()
    17  	srv, client, url := testServer(t, true, nil)
    18  	defer srv.Shutdown()
    19  	testutil.WaitForResult(func() (bool, error) {
    20  		nodes, _, err := client.Nodes().List(nil)
    21  		if err != nil {
    22  			return false, err
    23  		}
    24  		if len(nodes) == 0 {
    25  			return false, fmt.Errorf("missing node")
    26  		}
    27  		if _, ok := nodes[0].Drivers["mock_driver"]; !ok {
    28  			return false, fmt.Errorf("mock_driver not ready")
    29  		}
    30  		return true, nil
    31  	}, func(err error) {
    32  		t.Fatalf("err: %s", err)
    33  	})
    34  
    35  	ui := cli.NewMockUi()
    36  	cmd := &RecommendationApplyCommand{
    37  		RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
    38  			Meta: Meta{
    39  				Ui: ui,
    40  			},
    41  		},
    42  	}
    43  
    44  	// Register a test job to write a recommendation against.
    45  	testJob := testJob("recommendation_apply")
    46  	regResp, _, err := client.Jobs().Register(testJob, nil)
    47  	require.NoError(err)
    48  	registerCode := waitForSuccess(ui, client, fullId, t, regResp.EvalID)
    49  	require.Equal(0, registerCode)
    50  
    51  	// Write a recommendation.
    52  	rec := api.Recommendation{
    53  		JobID:    *testJob.ID,
    54  		Group:    *testJob.TaskGroups[0].Name,
    55  		Task:     testJob.TaskGroups[0].Tasks[0].Name,
    56  		Resource: "CPU",
    57  		Value:    1,
    58  		Meta:     map[string]interface{}{"test-meta-entry": "test-meta-value"},
    59  		Stats:    map[string]float64{"p13": 1.13},
    60  	}
    61  	recResp, _, err := client.Recommendations().Upsert(&rec, nil)
    62  	if srv.Enterprise {
    63  		require.NoError(err)
    64  
    65  		// Read the recommendation out to ensure it is there as a control on
    66  		// later tests.
    67  		recInfo, _, err := client.Recommendations().Info(recResp.ID, nil)
    68  		require.NoError(err)
    69  		require.NotNil(recInfo)
    70  	} else {
    71  		require.Error(err, "Nomad Enterprise only endpoint")
    72  	}
    73  
    74  	// Only perform the call if we are running enterprise tests. Otherwise the
    75  	// recResp object will be nil.
    76  	if !srv.Enterprise {
    77  		return
    78  	}
    79  	code := cmd.Run([]string{"-address=" + url, recResp.ID})
    80  	require.Equal(0, code)
    81  
    82  	// Perform an info call on the recommendation which should return not
    83  	// found.
    84  	recInfo, _, err := client.Recommendations().Info(recResp.ID, nil)
    85  	require.Error(err, "not found")
    86  	require.Nil(recInfo)
    87  
    88  	// Check the new jobspec to see if the resource value has changed.
    89  	jobResp, _, err := client.Jobs().Info(*testJob.ID, nil)
    90  	require.NoError(err)
    91  	require.Equal(1, *jobResp.TaskGroups[0].Tasks[0].Resources.CPU)
    92  }
    93  
    94  func TestRecommendationApplyCommand_AutocompleteArgs(t *testing.T) {
    95  	srv, client, url := testServer(t, false, nil)
    96  	defer srv.Shutdown()
    97  
    98  	ui := cli.NewMockUi()
    99  	cmd := RecommendationApplyCommand{
   100  		RecommendationAutocompleteCommand: RecommendationAutocompleteCommand{
   101  			Meta: Meta{
   102  				Ui:          ui,
   103  				flagAddress: url,
   104  			},
   105  		},
   106  	}
   107  	testRecommendationAutocompleteCommand(t, client, srv, &cmd.RecommendationAutocompleteCommand)
   108  }