Skip to content

Commit

Permalink
Merge pull request #265 from buildpacks/add/err-layer-not-found
Browse files Browse the repository at this point in the history
Add custom error type for layer not found
  • Loading branch information
natalieparellano authored Apr 12, 2024
2 parents e29bd65 + db66ae4 commit cdd1ed7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
20 changes: 20 additions & 0 deletions cnb_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ func (i *CNBImageCore) GetAnnotateRefName() (string, error) {
}

func (i *CNBImageCore) GetLayer(diffID string) (io.ReadCloser, error) {
layerHash, err := v1.NewHash(diffID)
if err != nil {
return nil, err
}
configFile, err := i.ConfigFile()
if err != nil {
return nil, err
}
if !contains(configFile.RootFS.DiffIDs, layerHash) {
return nil, ErrLayerNotFound{DiffID: layerHash.String()}
}
hash, err := v1.NewHash(diffID)
if err != nil {
return nil, err
Expand All @@ -93,6 +104,15 @@ func (i *CNBImageCore) GetLayer(diffID string) (io.ReadCloser, error) {
return layer.Uncompressed()
}

func contains(diffIDs []v1.Hash, hash v1.Hash) bool {
for _, diffID := range diffIDs {
if diffID.String() == hash.String() {
return true
}
}
return false
}

// TBD Deprecated: History
func (i *CNBImageCore) History() ([]v1.History, error) {
configFile, err := getConfigFile(i.Image)
Expand Down
8 changes: 8 additions & 0 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,11 @@ func (e SaveError) Error() string {
}
return fmt.Sprintf("failed to write image to the following tags: %s", strings.Join(errors, ","))
}

type ErrLayerNotFound struct {
DiffID string
}

func (e ErrLayerNotFound) Error() string {
return fmt.Sprintf("failed to find layer with diff ID %q", e.DiffID)
}
14 changes: 7 additions & 7 deletions local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ func (i *Image) GetLayer(diffID string) (io.ReadCloser, error) {
if err != nil {
return nil, err
}
configFile, err := i.ConfigFile()
if err != nil {
return nil, err
}
if !contains(configFile.RootFS.DiffIDs, layerHash) {
return nil, imgutil.ErrLayerNotFound{DiffID: layerHash.String()}
}
layer, err := i.LayerByDiffID(layerHash)
if err == nil {
// this avoids downloading ALL the image layers from the daemon
Expand All @@ -62,13 +69,6 @@ func (i *Image) GetLayer(diffID string) (io.ReadCloser, error) {
return layer.Uncompressed()
}
}
configFile, err := i.ConfigFile()
if err != nil {
return nil, err
}
if !contains(configFile.RootFS.DiffIDs, layerHash) {
return nil, fmt.Errorf("image %q does not contain layer with diff ID %q", i.Name(), layerHash.String())
}
if err = i.ensureLayers(); err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1584,11 +1584,9 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, err)
h.AssertNil(t, err)
_, err = img.GetLayer(someSHA)
h.AssertError(
t,
err,
fmt.Sprintf(`image %q does not contain layer with diff ID "%s"`, repoName, someSHA),
)
h.AssertError(t, err, fmt.Sprintf("failed to find layer with diff ID %q", someSHA))
_, ok := err.(imgutil.ErrLayerNotFound)
h.AssertEq(t, ok, true)
})
})
})
Expand All @@ -1600,7 +1598,9 @@ func testImage(t *testing.T, when spec.G, it spec.S) {

readCloser, err := image.GetLayer(someSHA)
h.AssertNil(t, readCloser)
h.AssertError(t, err, fmt.Sprintf("image %q does not contain layer with diff ID %q", "not-exist", someSHA))
h.AssertError(t, err, fmt.Sprintf("failed to find layer with diff ID %q", someSHA))
_, ok := err.(imgutil.ErrLayerNotFound)
h.AssertEq(t, ok, true)
})
})
})
Expand Down

0 comments on commit cdd1ed7

Please sign in to comment.