github.com/hernad/nomad@v1.6.112/command/node_pool_list_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 TestNodePoolListCommand_Implements(t *testing.T) { 18 ci.Parallel(t) 19 var _ cli.Command = &NodePoolListCommand{} 20 } 21 22 func TestNodePoolListCommand_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{Name: "dev-1", Description: "Pool dev-1"} 33 _, err := client.NodePools().Register(dev1, nil) 34 must.NoError(t, err) 35 36 prod1 := &api.NodePool{Name: "prod-1"} 37 _, err = client.NodePools().Register(prod1, nil) 38 must.NoError(t, err) 39 40 prod2 := &api.NodePool{Name: "prod-2", Description: "Pool prod-2"} 41 _, err = client.NodePools().Register(prod2, nil) 42 must.NoError(t, err) 43 44 testCases := []struct { 45 name string 46 args []string 47 expectedOut string 48 expectedErr string 49 expectedCode int 50 }{ 51 { 52 name: "list all", 53 args: []string{}, 54 expectedOut: ` 55 Name Description 56 all Node pool with all nodes in the cluster. 57 default Default node pool. 58 dev-1 Pool dev-1 59 prod-1 <none> 60 prod-2 Pool prod-2`, 61 expectedCode: 0, 62 }, 63 { 64 name: "filter", 65 args: []string{ 66 "-filter", `Name contains "prod"`, 67 }, 68 expectedOut: ` 69 Name Description 70 prod-1 <none> 71 prod-2 Pool prod-2`, 72 expectedCode: 0, 73 }, 74 { 75 name: "paginate", 76 args: []string{ 77 "-per-page", "2", 78 }, 79 expectedOut: ` 80 Name Description 81 all Node pool with all nodes in the cluster. 82 default Default node pool.`, 83 expectedCode: 0, 84 }, 85 { 86 name: "paginate page 2", 87 args: []string{ 88 "-per-page", "2", 89 "-page-token", "dev-1", 90 }, 91 expectedOut: ` 92 Name Description 93 dev-1 Pool dev-1 94 prod-1 <none>`, 95 expectedCode: 0, 96 }, 97 { 98 name: "json", 99 args: []string{ 100 "-json", 101 "-filter", `Name == "prod-1"`, 102 }, 103 expectedOut: ` 104 [ 105 { 106 "Description": "", 107 "Meta": null, 108 "Name": "prod-1", 109 "SchedulerConfiguration": null 110 } 111 ]`, 112 expectedCode: 0, 113 }, 114 { 115 name: "template", 116 args: []string{ 117 "-t", "{{range .}}{{.Name}} {{end}}", 118 }, 119 expectedOut: "all default dev-1 prod-1 prod-2", 120 expectedCode: 0, 121 }, 122 { 123 name: "fail because of arg", 124 args: []string{"invalid"}, 125 expectedErr: "This command takes no arguments", 126 expectedCode: 1, 127 }, 128 { 129 name: "fail because of invalid template", 130 args: []string{ 131 "-t", "{{.NotValid}}", 132 }, 133 expectedErr: "Error formatting the data", 134 expectedCode: 1, 135 }, 136 } 137 138 for _, tc := range testCases { 139 t.Run(tc.name, func(t *testing.T) { 140 // Initialize UI and command. 141 ui := cli.NewMockUi() 142 cmd := &NodePoolListCommand{Meta: Meta{Ui: ui}} 143 144 // Run command. 145 args := []string{"-address", url} 146 args = append(args, tc.args...) 147 code := cmd.Run(args) 148 149 gotStdout := ui.OutputWriter.String() 150 gotStdout = jsonOutputRaftIndexes.ReplaceAllString(gotStdout, "") 151 152 test.Eq(t, tc.expectedCode, code) 153 test.StrContains(t, gotStdout, strings.TrimSpace(tc.expectedOut)) 154 test.StrContains(t, ui.ErrorWriter.String(), strings.TrimSpace(tc.expectedErr)) 155 }) 156 } 157 }