github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/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"
    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: types.Volume{Name: volumeName},
    28  		}, volumeName, ctx.Name},
    29  		{volumeContext{
    30  			v: types.Volume{Driver: "driver_name"},
    31  		}, "driver_name", ctx.Driver},
    32  		{volumeContext{
    33  			v: types.Volume{Scope: "local"},
    34  		}, "local", ctx.Scope},
    35  		{volumeContext{
    36  			v: types.Volume{Mountpoint: "mountpoint"},
    37  		}, "mountpoint", ctx.Mountpoint},
    38  		{volumeContext{
    39  			v: types.Volume{},
    40  		}, "", ctx.Labels},
    41  		{volumeContext{
    42  			v: types.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  		{
    69  			Context{Format: "{{nil}}"},
    70  			`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
    71  `,
    72  		},
    73  		// Table format
    74  		{
    75  			Context{Format: NewVolumeFormat("table", false)},
    76  			`DRIVER    VOLUME NAME
    77  foo       foobar_baz
    78  bar       foobar_bar
    79  `,
    80  		},
    81  		{
    82  			Context{Format: NewVolumeFormat("table", true)},
    83  			`foobar_baz
    84  foobar_bar
    85  `,
    86  		},
    87  		{
    88  			Context{Format: NewVolumeFormat("table {{.Name}}", false)},
    89  			`VOLUME NAME
    90  foobar_baz
    91  foobar_bar
    92  `,
    93  		},
    94  		{
    95  			Context{Format: NewVolumeFormat("table {{.Name}}", true)},
    96  			`VOLUME NAME
    97  foobar_baz
    98  foobar_bar
    99  `,
   100  		},
   101  		// Raw Format
   102  		{
   103  			Context{Format: NewVolumeFormat("raw", false)},
   104  			`name: foobar_baz
   105  driver: foo
   106  
   107  name: foobar_bar
   108  driver: bar
   109  
   110  `,
   111  		},
   112  		{
   113  			Context{Format: NewVolumeFormat("raw", true)},
   114  			`name: foobar_baz
   115  name: foobar_bar
   116  `,
   117  		},
   118  		// Custom Format
   119  		{
   120  			Context{Format: NewVolumeFormat("{{.Name}}", false)},
   121  			`foobar_baz
   122  foobar_bar
   123  `,
   124  		},
   125  	}
   126  
   127  	volumes := []*types.Volume{
   128  		{Name: "foobar_baz", Driver: "foo"},
   129  		{Name: "foobar_bar", Driver: "bar"},
   130  	}
   131  
   132  	for _, tc := range cases {
   133  		tc := tc
   134  		t.Run(string(tc.context.Format), func(t *testing.T) {
   135  			var out bytes.Buffer
   136  			tc.context.Output = &out
   137  			err := VolumeWrite(tc.context, volumes)
   138  			if err != nil {
   139  				assert.Error(t, err, tc.expected)
   140  			} else {
   141  				assert.Equal(t, out.String(), tc.expected)
   142  			}
   143  		})
   144  	}
   145  }
   146  
   147  func TestVolumeContextWriteJSON(t *testing.T) {
   148  	volumes := []*types.Volume{
   149  		{Driver: "foo", Name: "foobar_baz"},
   150  		{Driver: "bar", Name: "foobar_bar"},
   151  	}
   152  	expectedJSONs := []map[string]interface{}{
   153  		{"Driver": "foo", "Labels": "", "Links": "N/A", "Mountpoint": "", "Name": "foobar_baz", "Scope": "", "Size": "N/A"},
   154  		{"Driver": "bar", "Labels": "", "Links": "N/A", "Mountpoint": "", "Name": "foobar_bar", "Scope": "", "Size": "N/A"},
   155  	}
   156  	out := bytes.NewBufferString("")
   157  	err := VolumeWrite(Context{Format: "{{json .}}", Output: out}, volumes)
   158  	if err != nil {
   159  		t.Fatal(err)
   160  	}
   161  	for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
   162  		msg := fmt.Sprintf("Output: line %d: %s", i, line)
   163  		var m map[string]interface{}
   164  		err := json.Unmarshal([]byte(line), &m)
   165  		assert.NilError(t, err, msg)
   166  		assert.Check(t, is.DeepEqual(expectedJSONs[i], m), msg)
   167  	}
   168  }
   169  
   170  func TestVolumeContextWriteJSONField(t *testing.T) {
   171  	volumes := []*types.Volume{
   172  		{Driver: "foo", Name: "foobar_baz"},
   173  		{Driver: "bar", Name: "foobar_bar"},
   174  	}
   175  	out := bytes.NewBufferString("")
   176  	err := VolumeWrite(Context{Format: "{{json .Name}}", Output: out}, volumes)
   177  	if err != nil {
   178  		t.Fatal(err)
   179  	}
   180  	for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
   181  		msg := fmt.Sprintf("Output: line %d: %s", i, line)
   182  		var s string
   183  		err := json.Unmarshal([]byte(line), &s)
   184  		assert.NilError(t, err, msg)
   185  		assert.Check(t, is.Equal(volumes[i].Name, s), msg)
   186  	}
   187  }