forked from cnabio/duffle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(*): add digest to invocation image on build
resolves cnabio#690
- Loading branch information
Michelle Noorali
committed
Apr 1, 2019
1 parent
9551a6d
commit b2f3aee
Showing
8 changed files
with
161 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package digester | ||
|
||
import ( | ||
"golang.org/x/net/context" | ||
|
||
"github.com/docker/docker/client" | ||
"github.com/opencontainers/go-digest" | ||
) | ||
|
||
type Digester struct { | ||
Client client.ImageAPIClient | ||
Image string | ||
Context context.Context | ||
} | ||
|
||
// NewDigester returns a Digester given the args client, image, ctx | ||
// | ||
// client allows us to talk to the Docker client | ||
// image is a string identifier of the image we want to compute digest of | ||
// ctx is context to use and pass to Docker client | ||
func NewDigester(client client.ImageAPIClient, image string, ctx context.Context) *Digester { | ||
return &Digester{ | ||
Client: client, | ||
Image: image, | ||
Context: ctx, | ||
} | ||
} | ||
|
||
// Digest returns the digest of the image tar | ||
func (d *Digester) Digest() (string, error) { | ||
reader, err := d.Client.ImageSave(d.Context, []string{d.Image}) | ||
computedDigest, err := digest.Canonical.FromReader(reader) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer reader.Close() | ||
|
||
return computedDigest.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package digester | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/filters" | ||
"github.com/docker/docker/api/types/image" | ||
"github.com/docker/docker/api/types/registry" | ||
) | ||
|
||
func TestDigest(t *testing.T) { | ||
d := Digester{ | ||
Client: &mockDockerClient{}, | ||
Image: "mock-image", | ||
} | ||
digestStr, err := d.Digest() | ||
if err != nil { | ||
t.Errorf("Not expecting error computing digest but got error: %s", err) | ||
} | ||
expectedDigestStr := "sha256:5dffd8ab8b1b8db6fbb2a62f5059d930fe79f3e3d7b2e65d331af5b8de03c93c" | ||
if digestStr != expectedDigestStr { | ||
t.Errorf("Expected digest %s, got %s", expectedDigestStr, digestStr) | ||
} | ||
} | ||
|
||
type mockDockerClient struct{} | ||
|
||
func (m *mockDockerClient) ImageSave(ctx context.Context, imageIDs []string) (io.ReadCloser, error) { | ||
return ioutil.NopCloser(bytes.NewReader([]byte("mock-context-for-digest"))), nil | ||
} | ||
|
||
func (m *mockDockerClient) ImageBuild(ctx context.Context, context io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error) { | ||
return types.ImageBuildResponse{}, nil | ||
} | ||
func (m *mockDockerClient) BuildCachePrune(ctx context.Context, opts types.BuildCachePruneOptions) (*types.BuildCachePruneReport, error) { | ||
return nil, nil | ||
} | ||
func (m *mockDockerClient) BuildCancel(ctx context.Context, id string) error { | ||
return nil | ||
} | ||
|
||
func (m *mockDockerClient) ImageCreate(ctx context.Context, parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) { | ||
return nil, nil | ||
} | ||
|
||
func (m *mockDockerClient) ImageHistory(ctx context.Context, image string) ([]image.HistoryResponseItem, error) { | ||
return nil, nil | ||
} | ||
func (m *mockDockerClient) ImageImport(ctx context.Context, source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error) { | ||
return nil, nil | ||
} | ||
func (m *mockDockerClient) ImageInspectWithRaw(ctx context.Context, image string) (types.ImageInspect, []byte, error) { | ||
return types.ImageInspect{}, nil, nil | ||
} | ||
func (m *mockDockerClient) ImageList(ctx context.Context, options types.ImageListOptions) ([]types.ImageSummary, error) { | ||
return nil, nil | ||
} | ||
func (m *mockDockerClient) ImageLoad(ctx context.Context, input io.Reader, quiet bool) (types.ImageLoadResponse, error) { | ||
return types.ImageLoadResponse{}, nil | ||
} | ||
func (m *mockDockerClient) ImagePull(ctx context.Context, ref string, options types.ImagePullOptions) (io.ReadCloser, error) { | ||
return nil, nil | ||
} | ||
func (m *mockDockerClient) ImagePush(ctx context.Context, ref string, options types.ImagePushOptions) (io.ReadCloser, error) { | ||
return nil, nil | ||
} | ||
func (m *mockDockerClient) ImageRemove(ctx context.Context, image string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error) { | ||
return nil, nil | ||
} | ||
func (m *mockDockerClient) ImageSearch(ctx context.Context, term string, options types.ImageSearchOptions) ([]registry.SearchResult, error) { | ||
return nil, nil | ||
} | ||
func (m *mockDockerClient) ImageTag(ctx context.Context, image, ref string) error { | ||
return nil | ||
} | ||
func (m *mockDockerClient) ImagesPrune(ctx context.Context, pruneFilter filters.Args) (types.ImagesPruneReport, error) { | ||
return types.ImagesPruneReport{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters