github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/task/print_test.go (about)

     1  package task
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/docker/cli/cli/command/formatter"
     9  	"github.com/docker/cli/cli/command/idresolver"
    10  	"github.com/docker/cli/internal/test"
    11  	. "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
    12  	"github.com/docker/docker/api/types"
    13  	"github.com/docker/docker/api/types/swarm"
    14  	"gotest.tools/v3/assert"
    15  	"gotest.tools/v3/golden"
    16  )
    17  
    18  func TestTaskPrintSorted(t *testing.T) {
    19  	apiClient := &fakeClient{
    20  		serviceInspectWithRaw: func(ref string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) {
    21  			if ref == "service-id-one" {
    22  				return *Service(ServiceName("service-name-1")), nil, nil
    23  			}
    24  			return *Service(ServiceName("service-name-10")), nil, nil
    25  		},
    26  	}
    27  
    28  	cli := test.NewFakeCli(apiClient)
    29  	tasks := []swarm.Task{
    30  		*Task(
    31  			TaskID("id-foo"),
    32  			TaskServiceID("service-id-ten"),
    33  			TaskNodeID("id-node"),
    34  			WithTaskSpec(TaskImage("myimage:mytag")),
    35  			TaskDesiredState(swarm.TaskStateReady),
    36  			WithStatus(TaskState(swarm.TaskStateFailed), Timestamp(time.Now().Add(-2*time.Hour))),
    37  		),
    38  		*Task(
    39  			TaskID("id-bar"),
    40  			TaskServiceID("service-id-one"),
    41  			TaskNodeID("id-node"),
    42  			WithTaskSpec(TaskImage("myimage:mytag")),
    43  			TaskDesiredState(swarm.TaskStateReady),
    44  			WithStatus(TaskState(swarm.TaskStateFailed), Timestamp(time.Now().Add(-2*time.Hour))),
    45  		),
    46  	}
    47  
    48  	err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, false), false, false, formatter.TableFormatKey)
    49  	assert.NilError(t, err)
    50  	golden.Assert(t, cli.OutBuffer().String(), "task-print-sorted.golden")
    51  }
    52  
    53  func TestTaskPrintWithQuietOption(t *testing.T) {
    54  	quiet := true
    55  	trunc := false
    56  	noResolve := true
    57  	apiClient := &fakeClient{}
    58  	cli := test.NewFakeCli(apiClient)
    59  	tasks := []swarm.Task{*Task(TaskID("id-foo"))}
    60  	err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, formatter.TableFormatKey)
    61  	assert.NilError(t, err)
    62  	golden.Assert(t, cli.OutBuffer().String(), "task-print-with-quiet-option.golden")
    63  }
    64  
    65  func TestTaskPrintWithNoTruncOption(t *testing.T) {
    66  	quiet := false
    67  	trunc := false
    68  	noResolve := true
    69  	apiClient := &fakeClient{}
    70  	cli := test.NewFakeCli(apiClient)
    71  	tasks := []swarm.Task{
    72  		*Task(TaskID("id-foo-yov6omdek8fg3k5stosyp2m50")),
    73  	}
    74  	err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .ID }}")
    75  	assert.NilError(t, err)
    76  	golden.Assert(t, cli.OutBuffer().String(), "task-print-with-no-trunc-option.golden")
    77  }
    78  
    79  func TestTaskPrintWithGlobalService(t *testing.T) {
    80  	quiet := false
    81  	trunc := false
    82  	noResolve := true
    83  	apiClient := &fakeClient{}
    84  	cli := test.NewFakeCli(apiClient)
    85  	tasks := []swarm.Task{
    86  		*Task(TaskServiceID("service-id-foo"), TaskNodeID("node-id-bar"), TaskSlot(0)),
    87  	}
    88  	err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .Name }}")
    89  	assert.NilError(t, err)
    90  	golden.Assert(t, cli.OutBuffer().String(), "task-print-with-global-service.golden")
    91  }
    92  
    93  func TestTaskPrintWithReplicatedService(t *testing.T) {
    94  	quiet := false
    95  	trunc := false
    96  	noResolve := true
    97  	apiClient := &fakeClient{}
    98  	cli := test.NewFakeCli(apiClient)
    99  	tasks := []swarm.Task{
   100  		*Task(TaskServiceID("service-id-foo"), TaskSlot(1)),
   101  	}
   102  	err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .Name }}")
   103  	assert.NilError(t, err)
   104  	golden.Assert(t, cli.OutBuffer().String(), "task-print-with-replicated-service.golden")
   105  }
   106  
   107  func TestTaskPrintWithIndentation(t *testing.T) {
   108  	quiet := false
   109  	trunc := false
   110  	noResolve := false
   111  	apiClient := &fakeClient{
   112  		serviceInspectWithRaw: func(ref string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) {
   113  			return *Service(ServiceName("service-name-foo")), nil, nil
   114  		},
   115  		nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) {
   116  			return *Node(NodeName("node-name-bar")), nil, nil
   117  		},
   118  	}
   119  	cli := test.NewFakeCli(apiClient)
   120  	tasks := []swarm.Task{
   121  		*Task(
   122  			TaskID("id-foo"),
   123  			TaskServiceID("service-id-foo"),
   124  			TaskNodeID("id-node"),
   125  			WithTaskSpec(TaskImage("myimage:mytag")),
   126  			TaskDesiredState(swarm.TaskStateReady),
   127  			WithStatus(TaskState(swarm.TaskStateFailed), Timestamp(time.Now().Add(-2*time.Hour))),
   128  		),
   129  		*Task(
   130  			TaskID("id-bar"),
   131  			TaskServiceID("service-id-foo"),
   132  			TaskNodeID("id-node"),
   133  			WithTaskSpec(TaskImage("myimage:mytag")),
   134  			TaskDesiredState(swarm.TaskStateReady),
   135  			WithStatus(TaskState(swarm.TaskStateFailed), Timestamp(time.Now().Add(-2*time.Hour))),
   136  		),
   137  	}
   138  	err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, formatter.TableFormatKey)
   139  	assert.NilError(t, err)
   140  	golden.Assert(t, cli.OutBuffer().String(), "task-print-with-indentation.golden")
   141  }
   142  
   143  func TestTaskPrintWithResolution(t *testing.T) {
   144  	quiet := false
   145  	trunc := false
   146  	noResolve := false
   147  	apiClient := &fakeClient{
   148  		serviceInspectWithRaw: func(ref string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) {
   149  			return *Service(ServiceName("service-name-foo")), nil, nil
   150  		},
   151  		nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) {
   152  			return *Node(NodeName("node-name-bar")), nil, nil
   153  		},
   154  	}
   155  	cli := test.NewFakeCli(apiClient)
   156  	tasks := []swarm.Task{
   157  		*Task(TaskServiceID("service-id-foo"), TaskSlot(1)),
   158  	}
   159  	err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .Name }} {{ .Node }}")
   160  	assert.NilError(t, err)
   161  	golden.Assert(t, cli.OutBuffer().String(), "task-print-with-resolution.golden")
   162  }