github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/cli/command/formatter/volume_test.go (about)

     1  package formatter
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/docker/cli/internal/test"
    11  	"github.com/docker/docker/api/types/volume"
    12  	"github.com/docker/docker/pkg/stringid"
    13  	"gotest.tools/v3/assert"
    14  	is "gotest.tools/v3/assert/cmp"
    15  )
    16  
    17  func TestVolumeContext(t *testing.T) {
    18  	volumeName := stringid.GenerateRandomID()
    19  
    20  	var ctx volumeContext
    21  	cases := []struct {
    22  		volumeCtx volumeContext
    23  		expValue  string
    24  		call      func() string
    25  	}{
    26  		{volumeContext{
    27  			v: volume.Volume{Name: volumeName},
    28  		}, volumeName, ctx.Name},
    29  		{volumeContext{
    30  			v: volume.Volume{Driver: "driver_name"},
    31  		}, "driver_name", ctx.Driver},
    32  		{volumeContext{
    33  			v: volume.Volume{Scope: "local"},
    34  		}, "local", ctx.Scope},
    35  		{volumeContext{
    36  			v: volume.Volume{Mountpoint: "mountpoint"},
    37  		}, "mountpoint", ctx.Mountpoint},
    38  		{volumeContext{
    39  			v: volume.Volume{},
    40  		}, "", ctx.Labels},
    41  		{volumeContext{
    42  			v: volume.Volume{Labels: map[string]string{"label1": "value1", "label2": "value2"}},
    43  		}, "label1=value1,label2=value2", ctx.Labels},
    44  	}
    45  
    46  	for _, c := range cases {
    47  		ctx = c.volumeCtx
    48  		v := c.call()
    49  		if strings.Contains(v, ",") {
    50  			test.CompareMultipleValues(t, v, c.expValue)
    51  		} else if v != c.expValue {
    52  			t.Fatalf("Expected %s, was %s\n", c.expValue, v)
    53  		}
    54  	}
    55  }
    56  
    57  func TestVolumeContextWrite(t *testing.T) {
    58  	cases := []struct {
    59  		context  Context
    60  		expected string
    61  	}{
    62  		// Errors
    63  		{
    64  			Context{Format: "{{InvalidFunction}}"},
    65  			`template parsing error: template: :1: function "InvalidFunction" not defined`,
    66  		},
    67  		{
    68  			Context{Format: "{{nil}}"},
    69  			`template parsing error: template: :1:2: executing "" at <nil>: nil is not a command`,
    70  		},
    71  		// Table format
    72  		{
    73  			Context{Format: NewVolumeFormat("table", false)},
    74  			`DRIVER    VOLUME NAME
    75  foo       foobar_baz
    76  bar       foobar_bar
    77  `,
    78  		},
    79  		{
    80  			Context{Format: NewVolumeFormat("table", true)},
    81  			`foobar_baz
    82  foobar_bar
    83  `,
    84  		},
    85  		{
    86  			Context{Format: NewVolumeFormat("table {{.Name}}", false)},
    87  			`VOLUME NAME
    88  foobar_baz
    89  foobar_bar
    90  `,
    91  		},
    92  		{
    93  			Context{Format: NewVolumeFormat("table {{.Name}}", true)},
    94  			`VOLUME NAME
    95  foobar_baz
    96  foobar_bar
    97  `,
    98  		},
    99  		// Raw Format
   100  		{
   101  			Context{Format: NewVolumeFormat("raw", false)},
   102  			`name: foobar_baz
   103  driver: foo
   104  
   105  name: foobar_bar
   106  driver: bar
   107  
   108  `,
   109  		},
   110  		{
   111  			Context{Format: NewVolumeFormat("raw", true)},
   112  			`name: foobar_baz
   113  name: foobar_bar
   114  `,
   115  		},
   116  		// Custom Format
   117  		{
   118  			Context{Format: NewVolumeFormat("{{.Name}}", false)},
   119  			`foobar_baz
   120  foobar_bar
   121  `,
   122  		},
   123  	}
   124  
   125  	volumes := []*volume.Volume{
   126  		{Name: "foobar_baz", Driver: "foo"},
   127  		{Name: "foobar_bar", Driver: "bar"},
   128  	}
   129  
   130  	for _, tc := range cases {
   131  		tc := tc
   132  		t.Run(string(tc.context.Format), func(t *testing.T) {
   133  			var out bytes.Buffer
   134  			tc.context.Output = &out
   135  			err := VolumeWrite(tc.context, volumes)
   136  			if err != nil {
   137  				assert.Error(t, err, tc.expected)
   138  			} else {
   139  				assert.Equal(t, out.String(), tc.expected)
   140  			}
   141  		})
   142  	}
   143  }
   144  
   145  func TestVolumeContextWriteJSON(t *testing.T) {
   146  	volumes := []*volume.Volume{
   147  		{Driver: "foo", Name: "foobar_baz"},
   148  		{Driver: "bar", Name: "foobar_bar"},
   149  	}
   150  	expectedJSONs := []map[string]interface{}{
   151  		{"Availability": "N/A", "Driver": "foo", "Group": "N/A", "Labels": "", "Links": "N/A", "Mountpoint": "", "Name": "foobar_baz", "Scope": "", "Size": "N/A", "Status": "N/A"},
   152  		{"Availability": "N/A", "Driver": "bar", "Group": "N/A", "Labels": "", "Links": "N/A", "Mountpoint": "", "Name": "foobar_bar", "Scope": "", "Size": "N/A", "Status": "N/A"},
   153  	}
   154  	out := bytes.NewBufferString("")
   155  	err := VolumeWrite(Context{Format: "{{json .}}", Output: out}, volumes)
   156  	if err != nil {
   157  		t.Fatal(err)
   158  	}
   159  	for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
   160  		msg := fmt.Sprintf("Output: line %d: %s", i, line)
   161  		var m map[string]interface{}
   162  		err := json.Unmarshal([]byte(line), &m)
   163  		assert.NilError(t, err, msg)
   164  		assert.Check(t, is.DeepEqual(expectedJSONs[i], m), msg)
   165  	}
   166  }
   167  
   168  func TestVolumeContextWriteJSONField(t *testing.T) {
   169  	volumes := []*volume.Volume{
   170  		{Driver: "foo", Name: "foobar_baz"},
   171  		{Driver: "bar", Name: "foobar_bar"},
   172  	}
   173  	out := bytes.NewBufferString("")
   174  	err := VolumeWrite(Context{Format: "{{json .Name}}", Output: out}, volumes)
   175  	if err != nil {
   176  		t.Fatal(err)
   177  	}
   178  	for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
   179  		msg := fmt.Sprintf("Output: line %d: %s", i, line)
   180  		var s string
   181  		err := json.Unmarshal([]byte(line), &s)
   182  		assert.NilError(t, err, msg)
   183  		assert.Check(t, is.Equal(volumes[i].Name, s), msg)
   184  	}
   185  }