Skip to content

Commit

Permalink
Recompute the size in the same places that we recompute the digest. (#…
Browse files Browse the repository at this point in the history
…193)

Fixes: #192
  • Loading branch information
mattmoor authored Jun 2, 2018
1 parent 34a6841 commit fa3ceb6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
18 changes: 16 additions & 2 deletions v1/mutate/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package mutate

import (
"archive/tar"
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -110,10 +111,17 @@ func Append(base v1.Image, adds ...Addendum) (v1.Image, error) {
image.configFile.RootFS.DiffIDs = diffIDs
image.configFile.History = history
image.manifest.Layers = manifestLayers
image.manifest.Config.Digest, err = image.ConfigName()

rcfg, err := image.RawConfigFile()
if err != nil {
return nil, err
}
d, sz, err := v1.SHA256(bytes.NewBuffer(rcfg))
if err != nil {
return nil, err
}
image.manifest.Config.Digest = d
image.manifest.Config.Size = sz

return image, nil
}
Expand All @@ -139,10 +147,16 @@ func Config(base v1.Image, cfg v1.Config) (v1.Image, error) {
digestMap: make(map[v1.Hash]v1.Layer),
}

image.manifest.Config.Digest, err = image.ConfigName()
rcfg, err := image.RawConfigFile()
if err != nil {
return nil, err
}
d, sz, err := v1.SHA256(bytes.NewBuffer(rcfg))
if err != nil {
return nil, err
}
image.manifest.Config.Digest = d
image.manifest.Config.Size = sz
return image, nil
}

Expand Down
27 changes: 24 additions & 3 deletions v1/mutate/mutate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ func TestAppendWithHistory(t *testing.T) {
t.Fatalf("correct layer was not appended (-got, +want) %v", diff)
}

if configSizesAreEqual(t, source, result) {
t.Fatal("adding a layer MUST change the config file size")
}

cf := getConfigFile(t, result)

if diff := cmp.Diff(cf.History[1], addendum.History); diff != "" {
Expand All @@ -194,6 +198,10 @@ func TestAppendLayers(t *testing.T) {
t.Fatal("appending a layer did not mutate the config file")
}

if configSizesAreEqual(t, source, result) {
t.Fatal("adding a layer MUST change the config file size")
}

layers := getLayers(t, result)

if got, want := len(layers), 2; got != want {
Expand Down Expand Up @@ -232,6 +240,10 @@ func TestMutateConfig(t *testing.T) {
t.Fatal("mutating the config did not mutate the config file")
}

if configSizesAreEqual(t, source, result) {
t.Fatal("adding an enviornment variable MUST change the config file size")
}

if !reflect.DeepEqual(cfg.Config.Env, newEnv) {
t.Fatalf("incorrect environment set %v!=%v", cfg.Config.Env, newEnv)
}
Expand All @@ -245,8 +257,8 @@ func TestMutateCreatedAt(t *testing.T) {
t.Fatalf("failed to mutate a config: %v", err)
}

if manifestDigestsAreEqual(t, source, result) {
t.Fatal("mutating the created time MUST mutate the manifest digest")
if configDigestsAreEqual(t, source, result) {
t.Fatal("mutating the created time MUST mutate the config digest")
}

got := getConfigFile(t, result).Created.Time
Expand Down Expand Up @@ -340,7 +352,7 @@ func configFilesAreEqual(t *testing.T, first, second v1.Image) bool {
return cmp.Equal(fc, sc)
}

func manifestDigestsAreEqual(t *testing.T, first, second v1.Image) bool {
func configDigestsAreEqual(t *testing.T, first, second v1.Image) bool {
t.Helper()

fm := getManifest(t, first)
Expand All @@ -349,6 +361,15 @@ func manifestDigestsAreEqual(t *testing.T, first, second v1.Image) bool {
return fm.Config.Digest == sm.Config.Digest
}

func configSizesAreEqual(t *testing.T, first, second v1.Image) bool {
t.Helper()

fm := getManifest(t, first)
sm := getManifest(t, second)

return fm.Config.Size == sm.Config.Size
}

func manifestsAreEqual(t *testing.T, first, second v1.Image) bool {
t.Helper()

Expand Down

0 comments on commit fa3ceb6

Please sign in to comment.