github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/task/formatter_test.go (about)

     1  package task
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/docker/cli/cli/command/formatter"
    10  	"github.com/docker/docker/api/types/swarm"
    11  	"gotest.tools/v3/assert"
    12  	is "gotest.tools/v3/assert/cmp"
    13  	"gotest.tools/v3/golden"
    14  )
    15  
    16  func TestTaskContextWrite(t *testing.T) {
    17  	cases := []struct {
    18  		context  formatter.Context
    19  		expected string
    20  	}{
    21  		{
    22  			formatter.Context{Format: "{{InvalidFunction}}"},
    23  			`Template parsing error: template: :1: function "InvalidFunction" not defined
    24  `,
    25  		},
    26  		{
    27  			formatter.Context{Format: "{{nil}}"},
    28  			`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
    29  `,
    30  		},
    31  		{
    32  			formatter.Context{Format: NewTaskFormat("table", true)},
    33  			`taskID1
    34  taskID2
    35  `,
    36  		},
    37  		{
    38  			formatter.Context{Format: NewTaskFormat("table {{.Name}}\t{{.Node}}\t{{.Ports}}", false)},
    39  			string(golden.Get(t, "task-context-write-table-custom.golden")),
    40  		},
    41  		{
    42  			formatter.Context{Format: NewTaskFormat("table {{.Name}}", true)},
    43  			`NAME
    44  foobar_baz
    45  foobar_bar
    46  `,
    47  		},
    48  		{
    49  			formatter.Context{Format: NewTaskFormat("raw", true)},
    50  			`id: taskID1
    51  id: taskID2
    52  `,
    53  		},
    54  		{
    55  			formatter.Context{Format: NewTaskFormat("{{.Name}} {{.Node}}", false)},
    56  			`foobar_baz foo1
    57  foobar_bar foo2
    58  `,
    59  		},
    60  	}
    61  
    62  	tasks := []swarm.Task{
    63  		{ID: "taskID1"},
    64  		{ID: "taskID2"},
    65  	}
    66  	names := map[string]string{
    67  		"taskID1": "foobar_baz",
    68  		"taskID2": "foobar_bar",
    69  	}
    70  	nodes := map[string]string{
    71  		"taskID1": "foo1",
    72  		"taskID2": "foo2",
    73  	}
    74  
    75  	for _, tc := range cases {
    76  		tc := tc
    77  		t.Run(string(tc.context.Format), func(t *testing.T) {
    78  			var out bytes.Buffer
    79  			tc.context.Output = &out
    80  
    81  			if err := FormatWrite(tc.context, tasks, names, nodes); err != nil {
    82  				assert.Error(t, err, tc.expected)
    83  			} else {
    84  				assert.Equal(t, out.String(), tc.expected)
    85  			}
    86  		})
    87  	}
    88  }
    89  
    90  func TestTaskContextWriteJSONField(t *testing.T) {
    91  	tasks := []swarm.Task{
    92  		{ID: "taskID1"},
    93  		{ID: "taskID2"},
    94  	}
    95  	names := map[string]string{
    96  		"taskID1": "foobar_baz",
    97  		"taskID2": "foobar_bar",
    98  	}
    99  	out := bytes.NewBufferString("")
   100  	err := FormatWrite(formatter.Context{Format: "{{json .ID}}", Output: out}, tasks, names, map[string]string{})
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
   105  		var s string
   106  		if err := json.Unmarshal([]byte(line), &s); err != nil {
   107  			t.Fatal(err)
   108  		}
   109  		assert.Check(t, is.Equal(tasks[i].ID, s))
   110  	}
   111  }