Skip to content

Commit

Permalink
Prevent extra snapshot with --use-new-run (#2943)
Browse files Browse the repository at this point in the history
* Prevent extra snapshot when using new run

* Add unit tests for initializing snapshotter

There should be no snapshot for RunV2.  Added a test for SingleSnapshot
as well to prove that the test actually works (rather than `initialized`
just not being read or set properly).
  • Loading branch information
code-asher authored Jan 17, 2024
1 parent 16ed6b2 commit 398ebfb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (s *stageBuilder) build() error {
}

initSnapshotTaken := false
if s.opts.SingleSnapshot || s.opts.RunV2 {
if s.opts.SingleSnapshot {
if err := s.initSnapshotWithTimings(); err != nil {
return err
}
Expand Down
19 changes: 17 additions & 2 deletions pkg/executor/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ func Test_stageBuilder_optimize(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cf := &v1.ConfigFile{}
snap := fakeSnapShotter{}
snap := &fakeSnapShotter{}
lc := &fakeLayerCache{retrieve: tc.retrieve}
sb := &stageBuilder{opts: tc.opts, cf: cf, snapshotter: snap, layerCache: lc,
args: dockerfile.NewBuildArgs([]string{})}
Expand Down Expand Up @@ -896,6 +896,7 @@ func Test_stageBuilder_build(t *testing.T) {
stage config.KanikoStage
crossStageDeps map[int][]string
mockGetFSFromImage func(root string, img v1.Image, extract util.ExtractFunction) ([]string, error)
shouldInitSnapshot bool
}

testCases := []testcase{
Expand Down Expand Up @@ -995,6 +996,15 @@ func Test_stageBuilder_build(t *testing.T) {
rootDir: dir,
}
}(),
{
description: "use new run",
opts: &config.KanikoOptions{RunV2: true},
},
{
description: "single snapshot",
opts: &config.KanikoOptions{SingleSnapshot: true},
shouldInitSnapshot: true,
},
{
description: "fake command cache disabled and key not in cache",
opts: &config.KanikoOptions{Cache: false},
Expand Down Expand Up @@ -1437,7 +1447,7 @@ RUN foobar
}
}

snap := fakeSnapShotter{file: fileName}
snap := &fakeSnapShotter{file: fileName}
lc := tc.layerCache
if lc == nil {
lc = &fakeLayerCache{}
Expand Down Expand Up @@ -1474,6 +1484,11 @@ RUN foobar
if err != nil {
t.Errorf("Expected error to be nil but was %v", err)
}
if tc.shouldInitSnapshot && !snap.initialized {
t.Errorf("Snapshotter was not initialized but should have been")
} else if !tc.shouldInitSnapshot && snap.initialized {
t.Errorf("Snapshotter was initialized but should not have been")
}
assertCacheKeys(t, tc.expectedCacheKeys, lc.receivedKeys, "receive")
assertCacheKeys(t, tc.pushedCacheKeys, keys, "push")

Expand Down
14 changes: 9 additions & 5 deletions pkg/executor/fakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ import (
)

type fakeSnapShotter struct {
file string
tarPath string
file string
tarPath string
initialized bool
}

func (f fakeSnapShotter) Init() error { return nil }
func (f fakeSnapShotter) TakeSnapshotFS() (string, error) {
func (f *fakeSnapShotter) Init() error {
f.initialized = true
return nil
}
func (f *fakeSnapShotter) TakeSnapshotFS() (string, error) {
return f.tarPath, nil
}
func (f fakeSnapShotter) TakeSnapshot(_ []string, _, _ bool) (string, error) {
func (f *fakeSnapShotter) TakeSnapshot(_ []string, _, _ bool) (string, error) {
return f.tarPath, nil
}

Expand Down

0 comments on commit 398ebfb

Please sign in to comment.