github.com/hernad/nomad@v1.6.112/command/node_eligibility_test.go (about)

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