Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure we can access layer by diffID on a mutated image #1875

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/v1/mutate/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ func (i *image) LayerByDigest(h v1.Hash) (v1.Layer, error) {
// LayerByDiffID is an analog to LayerByDigest, looking up by "diff id"
// (the uncompressed hash).
func (i *image) LayerByDiffID(h v1.Hash) (v1.Layer, error) {
if err := i.compute(); err != nil {
return nil, err
}
if layer, ok := i.diffIDMap[h]; ok {
return layer, nil
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/v1/mutate/mutate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"

v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/match"
Expand Down Expand Up @@ -200,6 +201,10 @@ func TestAppendLayers(t *testing.T) {
t.Fatalf("failed to append a layer: %v", err)
}

if !canGetLayerByDiffID(t, layer, result) {
t.Fatal("could not get layer by diffID before fetching manifest, config, or layers")
}

if manifestsAreEqual(t, source, result) {
t.Fatal("appending a layer did not mutate the manifest")
}
Expand Down Expand Up @@ -228,6 +233,12 @@ func TestAppendLayers(t *testing.T) {
}
}

func canGetLayerByDiffID(t *testing.T, layer v1.Layer, result v1.Image) bool {
diffID := getDiffID(t, layer)
_, err := result.LayerByDiffID(diffID)
return err == nil
}

func TestMutateConfig(t *testing.T) {
source := sourceImage(t)
cfg, err := source.ConfigFile()
Expand Down Expand Up @@ -700,6 +711,17 @@ func getLayers(t *testing.T, i v1.Image) []v1.Layer {
return l
}

func getDiffID(t *testing.T, l v1.Layer) v1.Hash {
t.Helper()

diffID, err := l.DiffID()
if err != nil {
t.Fatalf("Error fetching layer diffID: %v", err)
}

return diffID
}

func getConfigFile(t *testing.T, i v1.Image) *v1.ConfigFile {
t.Helper()

Expand Down
Loading