github.1git.de/docker/cli@v26.1.3+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  			formatter.Context{Format: "{{nil}}"},
    27  			`template parsing error: template: :1:2: executing "" at <nil>: nil is not a command`,
    28  		},
    29  		{
    30  			formatter.Context{Format: NewTaskFormat("table", true)},
    31  			`taskID1
    32  taskID2
    33  `,
    34  		},
    35  		{
    36  			formatter.Context{Format: NewTaskFormat("table {{.Name}}\t{{.Node}}\t{{.Ports}}", false)},
    37  			string(golden.Get(t, "task-context-write-table-custom.golden")),
    38  		},
    39  		{
    40  			formatter.Context{Format: NewTaskFormat("table {{.Name}}", true)},
    41  			`NAME
    42  foobar_baz
    43  foobar_bar
    44  `,
    45  		},
    46  		{
    47  			formatter.Context{Format: NewTaskFormat("raw", true)},
    48  			`id: taskID1
    49  id: taskID2
    50  `,
    51  		},
    52  		{
    53  			formatter.Context{Format: NewTaskFormat("{{.Name}} {{.Node}}", false)},
    54  			`foobar_baz foo1
    55  foobar_bar foo2
    56  `,
    57  		},
    58  	}
    59  
    60  	tasks := []swarm.Task{
    61  		{ID: "taskID1"},
    62  		{ID: "taskID2"},
    63  	}
    64  	names := map[string]string{
    65  		"taskID1": "foobar_baz",
    66  		"taskID2": "foobar_bar",
    67  	}
    68  	nodes := map[string]string{
    69  		"taskID1": "foo1",
    70  		"taskID2": "foo2",
    71  	}
    72  
    73  	for _, tc := range cases {
    74  		tc := tc
    75  		t.Run(string(tc.context.Format), func(t *testing.T) {
    76  			var out bytes.Buffer
    77  			tc.context.Output = &out
    78  
    79  			if err := FormatWrite(tc.context, tasks, names, nodes); err != nil {
    80  				assert.Error(t, err, tc.expected)
    81  			} else {
    82  				assert.Equal(t, out.String(), tc.expected)
    83  			}
    84  		})
    85  	}
    86  }
    87  
    88  func TestTaskContextWriteJSONField(t *testing.T) {
    89  	tasks := []swarm.Task{
    90  		{ID: "taskID1"},
    91  		{ID: "taskID2"},
    92  	}
    93  	names := map[string]string{
    94  		"taskID1": "foobar_baz",
    95  		"taskID2": "foobar_bar",
    96  	}
    97  	out := bytes.NewBufferString("")
    98  	err := FormatWrite(formatter.Context{Format: "{{json .ID}}", Output: out}, tasks, names, map[string]string{})
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  	for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
   103  		var s string
   104  		if err := json.Unmarshal([]byte(line), &s); err != nil {
   105  			t.Fatal(err)
   106  		}
   107  		assert.Check(t, is.Equal(tasks[i].ID, s))
   108  	}
   109  }