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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package command
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/hernad/nomad/api"
    11  	"github.com/hernad/nomad/ci"
    12  	"github.com/mitchellh/cli"
    13  	"github.com/shoenig/test"
    14  	"github.com/shoenig/test/must"
    15  )
    16  
    17  func TestNodePoolInfoCommand_Implements(t *testing.T) {
    18  	ci.Parallel(t)
    19  	var _ cli.Command = &NodePoolInfoCommand{}
    20  }
    21  
    22  func TestNodePoolInfoCommand_Run(t *testing.T) {
    23  	ci.Parallel(t)
    24  
    25  	// Start test server.
    26  	srv, client, url := testServer(t, true, nil)
    27  	defer srv.Shutdown()
    28  
    29  	waitForNodes(t, client)
    30  
    31  	// Register test node pools.
    32  	dev1 := &api.NodePool{
    33  		Name:        "dev-1",
    34  		Description: "Test pool",
    35  		Meta: map[string]string{
    36  			"env": "test",
    37  		},
    38  	}
    39  	_, err := client.NodePools().Register(dev1, nil)
    40  	must.NoError(t, err)
    41  
    42  	dev1Output := `
    43  Name        = dev-1
    44  Description = Test pool
    45  
    46  Metadata
    47  env = test
    48  
    49  Scheduler Configuration
    50  No scheduler configuration`
    51  
    52  	dev1JsonOutput := `
    53  {
    54      "Description": "Test pool",
    55      "Meta": {
    56          "env": "test"
    57      },
    58      "Name": "dev-1",
    59      "SchedulerConfiguration": null
    60  }`
    61  
    62  	// These two node pools are used to test exact prefix match.
    63  	prod1 := &api.NodePool{Name: "prod-1"}
    64  	_, err = client.NodePools().Register(prod1, nil)
    65  	must.NoError(t, err)
    66  
    67  	prod12 := &api.NodePool{Name: "prod-12"}
    68  	_, err = client.NodePools().Register(prod12, nil)
    69  	must.NoError(t, err)
    70  
    71  	testCases := []struct {
    72  		name         string
    73  		args         []string
    74  		expectedOut  string
    75  		expectedErr  string
    76  		expectedCode int
    77  	}{
    78  		{
    79  			name:         "basic info",
    80  			args:         []string{"dev-1"},
    81  			expectedOut:  dev1Output,
    82  			expectedCode: 0,
    83  		},
    84  		{
    85  			name:         "basic info by prefix",
    86  			args:         []string{"dev"},
    87  			expectedOut:  dev1Output,
    88  			expectedCode: 0,
    89  		},
    90  		{
    91  			name: "exact prefix match",
    92  			args: []string{"prod-1"},
    93  			expectedOut: `
    94  Name        = prod-1
    95  Description = <none>
    96  
    97  Metadata
    98  No metadata
    99  
   100  Scheduler Configuration
   101  No scheduler configuration`,
   102  			expectedCode: 0,
   103  		},
   104  		{
   105  			name:         "json",
   106  			args:         []string{"-json", "dev"},
   107  			expectedOut:  dev1JsonOutput,
   108  			expectedCode: 0,
   109  		},
   110  		{
   111  			name: "template",
   112  			args: []string{
   113  				"-t", "{{.Name}} -> {{.Meta.env}}",
   114  				"dev-1",
   115  			},
   116  			expectedOut:  "dev-1 -> test",
   117  			expectedCode: 0,
   118  		},
   119  		{
   120  			name:         "fail because of missing node pool arg",
   121  			args:         []string{},
   122  			expectedErr:  "This command takes one argument",
   123  			expectedCode: 1,
   124  		},
   125  		{
   126  			name:         "fail because no match",
   127  			args:         []string{"invalid"},
   128  			expectedErr:  `No node pool with prefix "invalid" found`,
   129  			expectedCode: 1,
   130  		},
   131  		{
   132  			name:         "fail because of multiple matches",
   133  			args:         []string{"de"}, // Matches default and dev-1.
   134  			expectedErr:  "Prefix matched multiple node pools",
   135  			expectedCode: 1,
   136  		},
   137  		{
   138  			name: "fail because of invalid template",
   139  			args: []string{
   140  				"-t", "{{.NotValid}}",
   141  				"dev-1",
   142  			},
   143  			expectedErr:  "Error formatting the data",
   144  			expectedCode: 1,
   145  		},
   146  	}
   147  
   148  	for _, tc := range testCases {
   149  		t.Run(tc.name, func(t *testing.T) {
   150  			// Initialize UI and command.
   151  			ui := cli.NewMockUi()
   152  			cmd := &NodePoolInfoCommand{Meta: Meta{Ui: ui}}
   153  
   154  			// Run command.
   155  			args := []string{"-address", url}
   156  			args = append(args, tc.args...)
   157  			code := cmd.Run(args)
   158  
   159  			gotStdout := ui.OutputWriter.String()
   160  			gotStdout = jsonOutputRaftIndexes.ReplaceAllString(gotStdout, "")
   161  
   162  			test.Eq(t, tc.expectedCode, code)
   163  			test.StrContains(t, gotStdout, strings.TrimSpace(tc.expectedOut))
   164  			test.StrContains(t, ui.ErrorWriter.String(), strings.TrimSpace(tc.expectedErr))
   165  		})
   166  	}
   167  }