github.com/hernad/nomad@v1.6.112/command/node_pool_delete_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "testing" 8 9 "github.com/hernad/nomad/api" 10 "github.com/hernad/nomad/ci" 11 "github.com/mitchellh/cli" 12 "github.com/shoenig/test/must" 13 ) 14 15 func TestNodePoolDeleteCommand_Implements(t *testing.T) { 16 ci.Parallel(t) 17 var _ cli.Command = &NodePoolDeleteCommand{} 18 } 19 20 func TestNodePoolDeleteCommand_Run(t *testing.T) { 21 ci.Parallel(t) 22 23 // Start test server. 24 srv, client, url := testServer(t, true, nil) 25 defer srv.Shutdown() 26 27 waitForNodes(t, client) 28 29 // Register test node pools. 30 dev1 := &api.NodePool{Name: "dev-1"} 31 _, err := client.NodePools().Register(dev1, nil) 32 must.NoError(t, err) 33 34 // Initialize UI and command. 35 ui := cli.NewMockUi() 36 cmd := &NodePoolDeleteCommand{Meta: Meta{Ui: ui}} 37 38 // Delete test node pool. 39 args := []string{"-address", url, dev1.Name} 40 code := cmd.Run(args) 41 must.Eq(t, 0, code) 42 must.StrContains(t, ui.OutputWriter.String(), "Successfully deleted") 43 44 // Verify node pool was delete. 45 got, _, err := client.NodePools().Info(dev1.Name, nil) 46 must.ErrorContains(t, err, "404") 47 must.Nil(t, got) 48 } 49 50 func TestNodePoolDeleteCommand_Run_fail(t *testing.T) { 51 ci.Parallel(t) 52 53 // Start test server. 54 srv, client, url := testServer(t, true, nil) 55 defer srv.Shutdown() 56 57 waitForNodes(t, client) 58 59 testCases := []struct { 60 name string 61 args []string 62 expectedErr string 63 expectedCode int 64 }{ 65 { 66 name: "missing pool", 67 args: []string{"-address", url}, 68 expectedCode: 1, 69 expectedErr: "This command takes one argument", 70 }, 71 { 72 name: "invalid pool", 73 args: []string{"-address", url, "invalid"}, 74 expectedCode: 1, 75 expectedErr: "not found", 76 }, 77 { 78 name: "built-in pool", 79 args: []string{"-address", url, "all"}, 80 expectedCode: 1, 81 expectedErr: "not allowed", 82 }, 83 } 84 85 for _, tc := range testCases { 86 t.Run(tc.name, func(t *testing.T) { 87 // Initialize UI and command. 88 ui := cli.NewMockUi() 89 cmd := &NodePoolDeleteCommand{Meta: Meta{Ui: ui}} 90 91 // Run command. 92 code := cmd.Run(tc.args) 93 must.Eq(t, tc.expectedCode, code) 94 must.StrContains(t, ui.ErrorWriter.String(), tc.expectedErr) 95 }) 96 } 97 }