github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/cli/command/node/inspect_test.go (about) 1 package node 2 3 import ( 4 "fmt" 5 "io" 6 "testing" 7 8 "github.com/docker/docker/api/types/swarm" 9 "github.com/docker/docker/api/types/system" 10 "github.com/khulnasoft/cli/internal/test" 11 "github.com/khulnasoft/cli/internal/test/builders" 12 "github.com/pkg/errors" 13 "gotest.tools/v3/assert" 14 "gotest.tools/v3/golden" 15 ) 16 17 func TestNodeInspectErrors(t *testing.T) { 18 testCases := []struct { 19 args []string 20 flags map[string]string 21 nodeInspectFunc func() (swarm.Node, []byte, error) 22 infoFunc func() (system.Info, error) 23 expectedError string 24 }{ 25 { 26 expectedError: "requires at least 1 argument", 27 }, 28 { 29 args: []string{"self"}, 30 infoFunc: func() (system.Info, error) { 31 return system.Info{}, errors.Errorf("error asking for node info") 32 }, 33 expectedError: "error asking for node info", 34 }, 35 { 36 args: []string{"nodeID"}, 37 nodeInspectFunc: func() (swarm.Node, []byte, error) { 38 return swarm.Node{}, []byte{}, errors.Errorf("error inspecting the node") 39 }, 40 infoFunc: func() (system.Info, error) { 41 return system.Info{}, errors.Errorf("error asking for node info") 42 }, 43 expectedError: "error inspecting the node", 44 }, 45 { 46 args: []string{"self"}, 47 nodeInspectFunc: func() (swarm.Node, []byte, error) { 48 return swarm.Node{}, []byte{}, errors.Errorf("error inspecting the node") 49 }, 50 infoFunc: func() (system.Info, error) { 51 return system.Info{Swarm: swarm.Info{NodeID: "abc"}}, nil 52 }, 53 expectedError: "error inspecting the node", 54 }, 55 { 56 args: []string{"self"}, 57 flags: map[string]string{ 58 "pretty": "true", 59 }, 60 infoFunc: func() (system.Info, error) { 61 return system.Info{}, errors.Errorf("error asking for node info") 62 }, 63 expectedError: "error asking for node info", 64 }, 65 } 66 for _, tc := range testCases { 67 cmd := newInspectCommand( 68 test.NewFakeCli(&fakeClient{ 69 nodeInspectFunc: tc.nodeInspectFunc, 70 infoFunc: tc.infoFunc, 71 })) 72 cmd.SetArgs(tc.args) 73 for key, value := range tc.flags { 74 assert.Check(t, cmd.Flags().Set(key, value)) 75 } 76 cmd.SetOut(io.Discard) 77 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 78 } 79 } 80 81 func TestNodeInspectPretty(t *testing.T) { 82 testCases := []struct { 83 name string 84 nodeInspectFunc func() (swarm.Node, []byte, error) 85 }{ 86 { 87 name: "simple", 88 nodeInspectFunc: func() (swarm.Node, []byte, error) { 89 return *builders.Node(builders.NodeLabels(map[string]string{ 90 "lbl1": "value1", 91 })), []byte{}, nil 92 }, 93 }, 94 { 95 name: "manager", 96 nodeInspectFunc: func() (swarm.Node, []byte, error) { 97 return *builders.Node(builders.Manager()), []byte{}, nil 98 }, 99 }, 100 { 101 name: "manager-leader", 102 nodeInspectFunc: func() (swarm.Node, []byte, error) { 103 return *builders.Node(builders.Manager(builders.Leader())), []byte{}, nil 104 }, 105 }, 106 } 107 for _, tc := range testCases { 108 cli := test.NewFakeCli(&fakeClient{ 109 nodeInspectFunc: tc.nodeInspectFunc, 110 }) 111 cmd := newInspectCommand(cli) 112 cmd.SetArgs([]string{"nodeID"}) 113 assert.Check(t, cmd.Flags().Set("pretty", "true")) 114 assert.NilError(t, cmd.Execute()) 115 golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("node-inspect-pretty.%s.golden", tc.name)) 116 } 117 }