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

     1  package volume
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"testing"
     7  
     8  	"github.com/docker/cli/internal/test"
     9  	. "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
    10  	"github.com/docker/docker/api/types"
    11  	"github.com/pkg/errors"
    12  	"gotest.tools/v3/assert"
    13  	"gotest.tools/v3/golden"
    14  )
    15  
    16  func TestVolumeInspectErrors(t *testing.T) {
    17  	testCases := []struct {
    18  		args              []string
    19  		flags             map[string]string
    20  		volumeInspectFunc func(volumeID string) (types.Volume, error)
    21  		expectedError     string
    22  	}{
    23  		{
    24  			expectedError: "requires at least 1 argument",
    25  		},
    26  		{
    27  			args: []string{"foo"},
    28  			volumeInspectFunc: func(volumeID string) (types.Volume, error) {
    29  				return types.Volume{}, errors.Errorf("error while inspecting the volume")
    30  			},
    31  			expectedError: "error while inspecting the volume",
    32  		},
    33  		{
    34  			args: []string{"foo"},
    35  			flags: map[string]string{
    36  				"format": "{{invalid format}}",
    37  			},
    38  			expectedError: "Template parsing error",
    39  		},
    40  		{
    41  			args: []string{"foo", "bar"},
    42  			volumeInspectFunc: func(volumeID string) (types.Volume, error) {
    43  				if volumeID == "foo" {
    44  					return types.Volume{
    45  						Name: "foo",
    46  					}, nil
    47  				}
    48  				return types.Volume{}, errors.Errorf("error while inspecting the volume")
    49  			},
    50  			expectedError: "error while inspecting the volume",
    51  		},
    52  	}
    53  	for _, tc := range testCases {
    54  		cmd := newInspectCommand(
    55  			test.NewFakeCli(&fakeClient{
    56  				volumeInspectFunc: tc.volumeInspectFunc,
    57  			}),
    58  		)
    59  		cmd.SetArgs(tc.args)
    60  		for key, value := range tc.flags {
    61  			cmd.Flags().Set(key, value)
    62  		}
    63  		cmd.SetOut(ioutil.Discard)
    64  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
    65  	}
    66  }
    67  
    68  func TestVolumeInspectWithoutFormat(t *testing.T) {
    69  	testCases := []struct {
    70  		name              string
    71  		args              []string
    72  		volumeInspectFunc func(volumeID string) (types.Volume, error)
    73  	}{
    74  		{
    75  			name: "single-volume",
    76  			args: []string{"foo"},
    77  			volumeInspectFunc: func(volumeID string) (types.Volume, error) {
    78  				if volumeID != "foo" {
    79  					return types.Volume{}, errors.Errorf("Invalid volumeID, expected %s, got %s", "foo", volumeID)
    80  				}
    81  				return *Volume(), nil
    82  			},
    83  		},
    84  		{
    85  			name: "multiple-volume-with-labels",
    86  			args: []string{"foo", "bar"},
    87  			volumeInspectFunc: func(volumeID string) (types.Volume, error) {
    88  				return *Volume(VolumeName(volumeID), VolumeLabels(map[string]string{
    89  					"foo": "bar",
    90  				})), nil
    91  			},
    92  		},
    93  	}
    94  	for _, tc := range testCases {
    95  		cli := test.NewFakeCli(&fakeClient{
    96  			volumeInspectFunc: tc.volumeInspectFunc,
    97  		})
    98  		cmd := newInspectCommand(cli)
    99  		cmd.SetArgs(tc.args)
   100  		assert.NilError(t, cmd.Execute())
   101  		golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("volume-inspect-without-format.%s.golden", tc.name))
   102  	}
   103  }
   104  
   105  func TestVolumeInspectWithFormat(t *testing.T) {
   106  	volumeInspectFunc := func(volumeID string) (types.Volume, error) {
   107  		return *Volume(VolumeLabels(map[string]string{
   108  			"foo": "bar",
   109  		})), nil
   110  	}
   111  	testCases := []struct {
   112  		name              string
   113  		format            string
   114  		args              []string
   115  		volumeInspectFunc func(volumeID string) (types.Volume, error)
   116  	}{
   117  		{
   118  			name:              "simple-template",
   119  			format:            "{{.Name}}",
   120  			args:              []string{"foo"},
   121  			volumeInspectFunc: volumeInspectFunc,
   122  		},
   123  		{
   124  			name:              "json-template",
   125  			format:            "{{json .Labels}}",
   126  			args:              []string{"foo"},
   127  			volumeInspectFunc: volumeInspectFunc,
   128  		},
   129  	}
   130  	for _, tc := range testCases {
   131  		cli := test.NewFakeCli(&fakeClient{
   132  			volumeInspectFunc: tc.volumeInspectFunc,
   133  		})
   134  		cmd := newInspectCommand(cli)
   135  		cmd.SetArgs(tc.args)
   136  		cmd.Flags().Set("format", tc.format)
   137  		assert.NilError(t, cmd.Execute())
   138  		golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("volume-inspect-with-format.%s.golden", tc.name))
   139  	}
   140  }