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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package command
     5  
     6  import (
     7  	"regexp"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/go-set"
    11  	"github.com/hernad/nomad/api"
    12  	"github.com/hernad/nomad/ci"
    13  	"github.com/mitchellh/cli"
    14  	"github.com/posener/complete"
    15  	"github.com/shoenig/test/must"
    16  )
    17  
    18  var (
    19  	// jsonOutputRaftIndexes is a regex that matches raft index fields in JSON
    20  	// strings. It can be used to remove them to make test results more
    21  	// consistent.
    22  	jsonOutputRaftIndexes = regexp.MustCompile(`(?m)\s*"(?:CreateIndex|ModifyIndex)".*`)
    23  )
    24  
    25  func TestNodePoolCommand_Implements(t *testing.T) {
    26  	ci.Parallel(t)
    27  	var _ cli.Command = &NodePoolCommand{}
    28  }
    29  
    30  func TestMeta_NodePoolPredictor(t *testing.T) {
    31  	ci.Parallel(t)
    32  
    33  	// Start test server.
    34  	srv, client, url := testServer(t, true, nil)
    35  	defer srv.Shutdown()
    36  
    37  	waitForNodes(t, client)
    38  
    39  	// Register some test node pools.
    40  	dev1 := &api.NodePool{Name: "dev-1"}
    41  	_, err := client.NodePools().Register(dev1, nil)
    42  	must.NoError(t, err)
    43  
    44  	dev2 := &api.NodePool{Name: "dev-2"}
    45  	_, err = client.NodePools().Register(dev2, nil)
    46  	must.NoError(t, err)
    47  
    48  	prod := &api.NodePool{Name: "prod"}
    49  	_, err = client.NodePools().Register(prod, nil)
    50  	must.NoError(t, err)
    51  
    52  	testCases := []struct {
    53  		name     string
    54  		args     complete.Args
    55  		filter   *set.Set[string]
    56  		expected []string
    57  	}{
    58  		{
    59  			name: "find with prefix",
    60  			args: complete.Args{
    61  				Last: "de",
    62  			},
    63  			expected: []string{"default", "dev-1", "dev-2"},
    64  		},
    65  		{
    66  			name: "filter",
    67  			args: complete.Args{
    68  				Last: "de",
    69  			},
    70  			filter:   set.From([]string{"default"}),
    71  			expected: []string{"dev-1", "dev-2"},
    72  		},
    73  		{
    74  			name: "find all",
    75  			args: complete.Args{
    76  				Last: "",
    77  			},
    78  			expected: []string{"all", "default", "dev-1", "dev-2", "prod"},
    79  		},
    80  	}
    81  
    82  	for _, tc := range testCases {
    83  		t.Run(tc.name, func(t *testing.T) {
    84  			m := &Meta{flagAddress: url}
    85  			got := nodePoolPredictor(m.Client, tc.filter).Predict(tc.args)
    86  			must.SliceContainsAll(t, tc.expected, got)
    87  		})
    88  	}
    89  }