github.com/docker/app@v0.9.1-beta3.0.20210611140623-a48f773ab002/internal/store/digest_test.go (about)

     1  package store
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/docker/app/internal/image"
     9  
    10  	"github.com/deislabs/cnab-go/bundle"
    11  	"gotest.tools/assert"
    12  	"gotest.tools/fs"
    13  )
    14  
    15  func Test_storeByDigest(t *testing.T) {
    16  	dockerConfigDir := fs.NewDir(t, t.Name(), fs.WithMode(0755))
    17  	defer dockerConfigDir.Remove()
    18  	appstore, err := NewApplicationStore(dockerConfigDir.Path())
    19  	assert.NilError(t, err)
    20  	imageStore, err := appstore.ImageStore()
    21  	assert.NilError(t, err)
    22  
    23  	bndl := image.FromBundle(&bundle.Bundle{Name: "bundle-name"})
    24  	_, err = imageStore.Store(bndl, nil)
    25  	assert.NilError(t, err)
    26  
    27  	ids := dockerConfigDir.Join("app", "bundles", "contents", "sha256")
    28  	infos, err := ioutil.ReadDir(ids)
    29  	assert.NilError(t, err)
    30  	assert.Equal(t, len(infos), 1)
    31  	_, err = os.Stat(dockerConfigDir.Join("app", "bundles", "contents", "sha256", infos[0].Name(), image.BundleFilename))
    32  	assert.NilError(t, err)
    33  }
    34  
    35  func TestFromString(t *testing.T) {
    36  	tests := []struct {
    37  		name    string
    38  		args    string
    39  		wantErr bool
    40  	}{
    41  		{
    42  			name: "valid digest",
    43  			args: "c661f4ad1e53d6825c65c01fc994793c3333542bc79c181f0acdc63aa908defc",
    44  		},
    45  		{
    46  			name:    "invalid size",
    47  			args:    "c661f4ad1e53d682",
    48  			wantErr: true,
    49  		},
    50  		{
    51  			name:    "invalid characters",
    52  			args:    "c661f4ad1e53d6825c65c01fc994793c3333542bc79c181f0acdc63a/foo/1.0",
    53  			wantErr: true,
    54  		},
    55  	}
    56  	for _, tt := range tests {
    57  		t.Run(tt.name, func(t *testing.T) {
    58  			got, err := FromString(tt.args)
    59  			if (err != nil) != tt.wantErr {
    60  				t.Errorf("FromString() error = %v, wantErr %v", err, tt.wantErr)
    61  				return
    62  			}
    63  			if err == nil {
    64  				assert.Equal(t, got.String(), tt.args)
    65  			}
    66  		})
    67  	}
    68  }