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

     1  package image
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"testing"
     7  
     8  	"github.com/docker/cli/internal/test"
     9  	"github.com/docker/docker/api/types"
    10  	"gotest.tools/v3/assert"
    11  	is "gotest.tools/v3/assert/cmp"
    12  	"gotest.tools/v3/golden"
    13  )
    14  
    15  func TestNewInspectCommandErrors(t *testing.T) {
    16  	testCases := []struct {
    17  		name          string
    18  		args          []string
    19  		expectedError string
    20  	}{
    21  		{
    22  			name:          "wrong-args",
    23  			args:          []string{},
    24  			expectedError: "requires at least 1 argument.",
    25  		},
    26  	}
    27  	for _, tc := range testCases {
    28  		cmd := newInspectCommand(test.NewFakeCli(&fakeClient{}))
    29  		cmd.SetOut(io.Discard)
    30  		cmd.SetArgs(tc.args)
    31  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
    32  	}
    33  }
    34  
    35  func TestNewInspectCommandSuccess(t *testing.T) {
    36  	imageInspectInvocationCount := 0
    37  	testCases := []struct {
    38  		name             string
    39  		args             []string
    40  		imageCount       int
    41  		imageInspectFunc func(image string) (types.ImageInspect, []byte, error)
    42  	}{
    43  		{
    44  			name:       "simple",
    45  			args:       []string{"image"},
    46  			imageCount: 1,
    47  			imageInspectFunc: func(image string) (types.ImageInspect, []byte, error) {
    48  				imageInspectInvocationCount++
    49  				assert.Check(t, is.Equal("image", image))
    50  				return types.ImageInspect{}, nil, nil
    51  			},
    52  		},
    53  		{
    54  			name:       "format",
    55  			imageCount: 1,
    56  			args:       []string{"--format='{{.ID}}'", "image"},
    57  			imageInspectFunc: func(image string) (types.ImageInspect, []byte, error) {
    58  				imageInspectInvocationCount++
    59  				return types.ImageInspect{ID: image}, nil, nil
    60  			},
    61  		},
    62  		{
    63  			name:       "simple-many",
    64  			args:       []string{"image1", "image2"},
    65  			imageCount: 2,
    66  			imageInspectFunc: func(image string) (types.ImageInspect, []byte, error) {
    67  				imageInspectInvocationCount++
    68  				if imageInspectInvocationCount == 1 {
    69  					assert.Check(t, is.Equal("image1", image))
    70  				} else {
    71  					assert.Check(t, is.Equal("image2", image))
    72  				}
    73  				return types.ImageInspect{}, nil, nil
    74  			},
    75  		},
    76  	}
    77  	for _, tc := range testCases {
    78  		tc := tc
    79  		t.Run(tc.name, func(t *testing.T) {
    80  			imageInspectInvocationCount = 0
    81  			cli := test.NewFakeCli(&fakeClient{imageInspectFunc: tc.imageInspectFunc})
    82  			cmd := newInspectCommand(cli)
    83  			cmd.SetOut(io.Discard)
    84  			cmd.SetArgs(tc.args)
    85  			err := cmd.Execute()
    86  			assert.NilError(t, err)
    87  			golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("inspect-command-success.%s.golden", tc.name))
    88  			assert.Check(t, is.Equal(imageInspectInvocationCount, tc.imageCount))
    89  		})
    90  	}
    91  }