github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/command/node/ps_test.go (about)

     1  package node
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/docker/cli/internal/test"
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/docker/docker/api/types/swarm"
    13  	"github.com/pkg/errors"
    14  	// Import builders to get the builder function as package function
    15  	. "github.com/docker/cli/internal/test/builders"
    16  	"gotest.tools/assert"
    17  	"gotest.tools/golden"
    18  )
    19  
    20  func TestNodePsErrors(t *testing.T) {
    21  	testCases := []struct {
    22  		args            []string
    23  		flags           map[string]string
    24  		infoFunc        func() (types.Info, error)
    25  		nodeInspectFunc func() (swarm.Node, []byte, error)
    26  		taskListFunc    func(options types.TaskListOptions) ([]swarm.Task, error)
    27  		taskInspectFunc func(taskID string) (swarm.Task, []byte, error)
    28  		expectedError   string
    29  	}{
    30  		{
    31  			infoFunc: func() (types.Info, error) {
    32  				return types.Info{}, errors.Errorf("error asking for node info")
    33  			},
    34  			expectedError: "error asking for node info",
    35  		},
    36  		{
    37  			args: []string{"nodeID"},
    38  			nodeInspectFunc: func() (swarm.Node, []byte, error) {
    39  				return swarm.Node{}, []byte{}, errors.Errorf("error inspecting the node")
    40  			},
    41  			expectedError: "error inspecting the node",
    42  		},
    43  		{
    44  			args: []string{"nodeID"},
    45  			taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
    46  				return []swarm.Task{}, errors.Errorf("error returning the task list")
    47  			},
    48  			expectedError: "error returning the task list",
    49  		},
    50  	}
    51  	for _, tc := range testCases {
    52  		cli := test.NewFakeCli(&fakeClient{
    53  			infoFunc:        tc.infoFunc,
    54  			nodeInspectFunc: tc.nodeInspectFunc,
    55  			taskInspectFunc: tc.taskInspectFunc,
    56  			taskListFunc:    tc.taskListFunc,
    57  		})
    58  		cmd := newPsCommand(cli)
    59  		cmd.SetArgs(tc.args)
    60  		for key, value := range tc.flags {
    61  			cmd.Flags().Set(key, value)
    62  		}
    63  		cmd.SetOutput(ioutil.Discard)
    64  		assert.Error(t, cmd.Execute(), tc.expectedError)
    65  	}
    66  }
    67  
    68  func TestNodePs(t *testing.T) {
    69  	testCases := []struct {
    70  		name               string
    71  		args               []string
    72  		flags              map[string]string
    73  		infoFunc           func() (types.Info, error)
    74  		nodeInspectFunc    func() (swarm.Node, []byte, error)
    75  		taskListFunc       func(options types.TaskListOptions) ([]swarm.Task, error)
    76  		taskInspectFunc    func(taskID string) (swarm.Task, []byte, error)
    77  		serviceInspectFunc func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error)
    78  	}{
    79  		{
    80  			name: "simple",
    81  			args: []string{"nodeID"},
    82  			nodeInspectFunc: func() (swarm.Node, []byte, error) {
    83  				return *Node(), []byte{}, nil
    84  			},
    85  			taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
    86  				return []swarm.Task{
    87  					*Task(WithStatus(Timestamp(time.Now().Add(-2*time.Hour)), PortStatus([]swarm.PortConfig{
    88  						{
    89  							TargetPort:    80,
    90  							PublishedPort: 80,
    91  							Protocol:      "tcp",
    92  						},
    93  					}))),
    94  				}, nil
    95  			},
    96  			serviceInspectFunc: func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error) {
    97  				return swarm.Service{
    98  					ID: serviceID,
    99  					Spec: swarm.ServiceSpec{
   100  						Annotations: swarm.Annotations{
   101  							Name: serviceID,
   102  						},
   103  					},
   104  				}, []byte{}, nil
   105  			},
   106  		},
   107  		{
   108  			name: "with-errors",
   109  			args: []string{"nodeID"},
   110  			nodeInspectFunc: func() (swarm.Node, []byte, error) {
   111  				return *Node(), []byte{}, nil
   112  			},
   113  			taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
   114  				return []swarm.Task{
   115  					*Task(TaskID("taskID1"), TaskServiceID("failure"),
   116  						WithStatus(Timestamp(time.Now().Add(-2*time.Hour)), StatusErr("a task error"))),
   117  					*Task(TaskID("taskID2"), TaskServiceID("failure"),
   118  						WithStatus(Timestamp(time.Now().Add(-3*time.Hour)), StatusErr("a task error"))),
   119  					*Task(TaskID("taskID3"), TaskServiceID("failure"),
   120  						WithStatus(Timestamp(time.Now().Add(-4*time.Hour)), StatusErr("a task error"))),
   121  				}, nil
   122  			},
   123  			serviceInspectFunc: func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error) {
   124  				return swarm.Service{
   125  					ID: serviceID,
   126  					Spec: swarm.ServiceSpec{
   127  						Annotations: swarm.Annotations{
   128  							Name: serviceID,
   129  						},
   130  					},
   131  				}, []byte{}, nil
   132  			},
   133  		},
   134  	}
   135  	for _, tc := range testCases {
   136  		cli := test.NewFakeCli(&fakeClient{
   137  			infoFunc:           tc.infoFunc,
   138  			nodeInspectFunc:    tc.nodeInspectFunc,
   139  			taskInspectFunc:    tc.taskInspectFunc,
   140  			taskListFunc:       tc.taskListFunc,
   141  			serviceInspectFunc: tc.serviceInspectFunc,
   142  		})
   143  		cmd := newPsCommand(cli)
   144  		cmd.SetArgs(tc.args)
   145  		for key, value := range tc.flags {
   146  			cmd.Flags().Set(key, value)
   147  		}
   148  		assert.NilError(t, cmd.Execute())
   149  		golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("node-ps.%s.golden", tc.name))
   150  	}
   151  }