github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/node_eligibility_test.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/nomad/ci"
     9  	"github.com/hashicorp/nomad/testutil"
    10  	"github.com/mitchellh/cli"
    11  	"github.com/posener/complete"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestNodeEligibilityCommand_Implements(t *testing.T) {
    16  	ci.Parallel(t)
    17  	var _ cli.Command = &NodeEligibilityCommand{}
    18  }
    19  
    20  func TestNodeEligibilityCommand_Fails(t *testing.T) {
    21  	ci.Parallel(t)
    22  	srv, _, url := testServer(t, false, nil)
    23  	defer srv.Shutdown()
    24  
    25  	ui := cli.NewMockUi()
    26  	cmd := &NodeEligibilityCommand{Meta: Meta{Ui: ui}}
    27  
    28  	// Fails on misuse
    29  	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
    30  		t.Fatalf("expected exit code 1, got: %d", code)
    31  	}
    32  	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
    33  		t.Fatalf("expected help output, got: %s", out)
    34  	}
    35  	ui.ErrorWriter.Reset()
    36  
    37  	// Fails on connection failure
    38  	if code := cmd.Run([]string{"-address=nope", "-enable", "12345678-abcd-efab-cdef-123456789abc"}); code != 1 {
    39  		t.Fatalf("expected exit code 1, got: %d", code)
    40  	}
    41  	expected := "Error updating scheduling eligibility"
    42  	if out := ui.ErrorWriter.String(); !strings.Contains(out, expected) {
    43  		t.Fatalf("expected %q, got: %s", expected, out)
    44  	}
    45  	ui.ErrorWriter.Reset()
    46  
    47  	// Fails on non-existent node
    48  	if code := cmd.Run([]string{"-address=" + url, "-enable", "12345678-abcd-efab-cdef-123456789abc"}); code != 1 {
    49  		t.Fatalf("expected exit 1, got: %d", code)
    50  	}
    51  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "No node(s) with prefix or id") {
    52  		t.Fatalf("expected not exist error, got: %s", out)
    53  	}
    54  	ui.ErrorWriter.Reset()
    55  
    56  	// Fails if both enable and disable specified
    57  	if code := cmd.Run([]string{"-enable", "-disable", "12345678-abcd-efab-cdef-123456789abc"}); code != 1 {
    58  		t.Fatalf("expected exit 1, got: %d", code)
    59  	}
    60  	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
    61  		t.Fatalf("expected help output, got: %s", out)
    62  	}
    63  	ui.ErrorWriter.Reset()
    64  
    65  	// Fails if neither enable or disable specified
    66  	if code := cmd.Run([]string{"12345678-abcd-efab-cdef-123456789abc"}); code != 1 {
    67  		t.Fatalf("expected exit 1, got: %d", code)
    68  	}
    69  	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
    70  		t.Fatalf("expected help output, got: %s", out)
    71  	}
    72  	ui.ErrorWriter.Reset()
    73  
    74  	// Fail on identifier with too few characters
    75  	if code := cmd.Run([]string{"-address=" + url, "-enable", "1"}); code != 1 {
    76  		t.Fatalf("expected exit 1, got: %d", code)
    77  	}
    78  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "must contain at least two characters.") {
    79  		t.Fatalf("expected too few characters error, got: %s", out)
    80  	}
    81  	ui.ErrorWriter.Reset()
    82  
    83  	// Identifiers with uneven length should produce a query result
    84  	if code := cmd.Run([]string{"-address=" + url, "-enable", "123"}); code != 1 {
    85  		t.Fatalf("expected exit 1, got: %d", code)
    86  	}
    87  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "No node(s) with prefix or id") {
    88  		t.Fatalf("expected not exist error, got: %s", out)
    89  	}
    90  	ui.ErrorWriter.Reset()
    91  }
    92  
    93  func TestNodeEligibilityCommand_AutocompleteArgs(t *testing.T) {
    94  	ci.Parallel(t)
    95  	assert := assert.New(t)
    96  
    97  	srv, client, url := testServer(t, true, nil)
    98  	defer srv.Shutdown()
    99  
   100  	// Wait for a node to appear
   101  	var nodeID string
   102  	testutil.WaitForResult(func() (bool, error) {
   103  		nodes, _, err := client.Nodes().List(nil)
   104  		if err != nil {
   105  			return false, err
   106  		}
   107  		if len(nodes) == 0 {
   108  			return false, fmt.Errorf("missing node")
   109  		}
   110  		nodeID = nodes[0].ID
   111  		return true, nil
   112  	}, func(err error) {
   113  		t.Fatalf("err: %s", err)
   114  	})
   115  
   116  	ui := cli.NewMockUi()
   117  	cmd := &NodeEligibilityCommand{Meta: Meta{Ui: ui, flagAddress: url}}
   118  
   119  	prefix := nodeID[:len(nodeID)-5]
   120  	args := complete.Args{Last: prefix}
   121  	predictor := cmd.AutocompleteArgs()
   122  
   123  	res := predictor.Predict(args)
   124  	assert.Equal(1, len(res))
   125  	assert.Equal(nodeID, res[0])
   126  }