github.com/docker/app@v0.9.1-beta3.0.20210611140623-a48f773ab002/internal/commands/image/list_test.go (about)

     1  package image
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"fmt"
     7  	"testing"
     8  
     9  	"github.com/docker/app/internal/image"
    10  
    11  	"gotest.tools/assert"
    12  
    13  	"github.com/deislabs/cnab-go/bundle"
    14  	"github.com/docker/app/internal/store"
    15  	"github.com/docker/cli/cli/command"
    16  	"github.com/docker/distribution/reference"
    17  )
    18  
    19  type imageStoreStubForListCmd struct {
    20  	refMap map[reference.Reference]*image.AppImage
    21  	// in order to keep the reference in the same order between tests
    22  	refList []reference.Reference
    23  }
    24  
    25  func (b *imageStoreStubForListCmd) Store(bndl *image.AppImage, ref reference.Reference) (reference.Digested, error) {
    26  	b.refMap[ref] = bndl
    27  	b.refList = append(b.refList, ref)
    28  	return store.FromAppImage(bndl)
    29  }
    30  
    31  func (b *imageStoreStubForListCmd) Read(ref reference.Reference) (*image.AppImage, error) {
    32  	bndl, ok := b.refMap[ref]
    33  	if ok {
    34  		return bndl, nil
    35  	}
    36  	return nil, fmt.Errorf("AppImage not found")
    37  }
    38  
    39  func (b *imageStoreStubForListCmd) List() ([]reference.Reference, error) {
    40  	return b.refList, nil
    41  }
    42  
    43  func (b *imageStoreStubForListCmd) Remove(ref reference.Reference, force bool) error {
    44  	return nil
    45  }
    46  
    47  func (b *imageStoreStubForListCmd) LookUp(refOrID string) (reference.Reference, error) {
    48  	return nil, nil
    49  }
    50  
    51  func TestListCmd(t *testing.T) {
    52  	ref, err := store.FromString("a855ac937f2ed375ba4396bbc49c4093e124da933acd2713fb9bc17d7562a087")
    53  	assert.NilError(t, err)
    54  	refs := []reference.Reference{
    55  		parseReference(t, "foo/bar@sha256:b59492bb814012ca3d2ce0b6728242d96b4af41687cc82166a4b5d7f2d9fb865"),
    56  		parseReference(t, "foo/bar:1.0"),
    57  		ref,
    58  	}
    59  	bundles := []image.AppImage{
    60  		{
    61  			Bundle: &bundle.Bundle{
    62  				Name: "Digested App",
    63  			},
    64  		},
    65  		{
    66  			Bundle: &bundle.Bundle{
    67  				Version:       "1.0.0",
    68  				SchemaVersion: "1.0.0",
    69  				Name:          "Foo App",
    70  			},
    71  		},
    72  		{
    73  			Bundle: &bundle.Bundle{
    74  				Name: "Quiet App",
    75  			},
    76  		},
    77  	}
    78  
    79  	testCases := []struct {
    80  		name           string
    81  		expectedOutput string
    82  		options        imageListOption
    83  	}{
    84  		{
    85  			name: "TestList",
    86  			expectedOutput: `REPOSITORY          TAG                 APP IMAGE ID        APP NAME            
    87  foo/bar             <none>              3f825b2d0657        Digested App        
    88  foo/bar             1.0                 9aae408ee04f        Foo App             
    89  <none>              <none>              a855ac937f2e        Quiet App           
    90  `,
    91  			options: imageListOption{format: "table"},
    92  		},
    93  		{
    94  			name: "TestTemplate",
    95  			expectedOutput: `APP IMAGE ID        DIGEST
    96  3f825b2d0657        sha256:b59492bb814012ca3d2ce0b6728242d96b4af41687cc82166a4b5d7f2d9fb865
    97  9aae408ee04f        <none>
    98  a855ac937f2e        sha256:a855ac937f2ed375ba4396bbc49c4093e124da933acd2713fb9bc17d7562a087
    99  `,
   100  			options: imageListOption{format: "table {{.ID}}", digests: true},
   101  		},
   102  		{
   103  			name: "TestListWithDigests",
   104  			//nolint:lll
   105  			expectedOutput: `REPOSITORY          TAG                 DIGEST                                                                    APP IMAGE ID        APP NAME                                
   106  foo/bar             <none>              sha256:b59492bb814012ca3d2ce0b6728242d96b4af41687cc82166a4b5d7f2d9fb865   3f825b2d0657        Digested App                            
   107  foo/bar             1.0                 <none>                                                                    9aae408ee04f        Foo App                                 
   108  <none>              <none>              sha256:a855ac937f2ed375ba4396bbc49c4093e124da933acd2713fb9bc17d7562a087   a855ac937f2e        Quiet App                               
   109  `,
   110  			options: imageListOption{format: "table", digests: true},
   111  		},
   112  		{
   113  			name: "TestListWithQuiet",
   114  			expectedOutput: `3f825b2d0657
   115  9aae408ee04f
   116  a855ac937f2e
   117  `,
   118  			options: imageListOption{format: "table", quiet: true},
   119  		},
   120  	}
   121  
   122  	for _, tc := range testCases {
   123  		t.Run(tc.name, func(t *testing.T) {
   124  			testRunList(t, refs, bundles, tc.options, tc.expectedOutput)
   125  		})
   126  	}
   127  }
   128  
   129  func parseReference(t *testing.T, s string) reference.Reference {
   130  	ref, err := reference.Parse(s)
   131  	assert.NilError(t, err)
   132  	return ref
   133  }
   134  
   135  func testRunList(t *testing.T, refs []reference.Reference, bundles []image.AppImage, options imageListOption, expectedOutput string) {
   136  	var buf bytes.Buffer
   137  	w := bufio.NewWriter(&buf)
   138  	dockerCli, err := command.NewDockerCli(command.WithOutputStream(w))
   139  	assert.NilError(t, err)
   140  	imageStore := &imageStoreStubForListCmd{
   141  		refMap:  make(map[reference.Reference]*image.AppImage),
   142  		refList: []reference.Reference{},
   143  	}
   144  	for i, ref := range refs {
   145  		_, err = imageStore.Store(&bundles[i], ref)
   146  		assert.NilError(t, err)
   147  	}
   148  	err = runList(dockerCli, options, imageStore)
   149  	assert.NilError(t, err)
   150  	w.Flush()
   151  	assert.Equal(t, buf.String(), expectedOutput)
   152  }