Prevent extra snapshot with --use-new-run (#2943)
* 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).
This commit is contained in:
parent
16ed6b2428
commit
398ebfb6da
|
|
@ -344,7 +344,7 @@ func (s *stageBuilder) build() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
initSnapshotTaken := false
|
initSnapshotTaken := false
|
||||||
if s.opts.SingleSnapshot || s.opts.RunV2 {
|
if s.opts.SingleSnapshot {
|
||||||
if err := s.initSnapshotWithTimings(); err != nil {
|
if err := s.initSnapshotWithTimings(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -616,7 +616,7 @@ func Test_stageBuilder_optimize(t *testing.T) {
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
cf := &v1.ConfigFile{}
|
cf := &v1.ConfigFile{}
|
||||||
snap := fakeSnapShotter{}
|
snap := &fakeSnapShotter{}
|
||||||
lc := &fakeLayerCache{retrieve: tc.retrieve}
|
lc := &fakeLayerCache{retrieve: tc.retrieve}
|
||||||
sb := &stageBuilder{opts: tc.opts, cf: cf, snapshotter: snap, layerCache: lc,
|
sb := &stageBuilder{opts: tc.opts, cf: cf, snapshotter: snap, layerCache: lc,
|
||||||
args: dockerfile.NewBuildArgs([]string{})}
|
args: dockerfile.NewBuildArgs([]string{})}
|
||||||
|
|
@ -896,6 +896,7 @@ func Test_stageBuilder_build(t *testing.T) {
|
||||||
stage config.KanikoStage
|
stage config.KanikoStage
|
||||||
crossStageDeps map[int][]string
|
crossStageDeps map[int][]string
|
||||||
mockGetFSFromImage func(root string, img v1.Image, extract util.ExtractFunction) ([]string, error)
|
mockGetFSFromImage func(root string, img v1.Image, extract util.ExtractFunction) ([]string, error)
|
||||||
|
shouldInitSnapshot bool
|
||||||
}
|
}
|
||||||
|
|
||||||
testCases := []testcase{
|
testCases := []testcase{
|
||||||
|
|
@ -995,6 +996,15 @@ func Test_stageBuilder_build(t *testing.T) {
|
||||||
rootDir: dir,
|
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",
|
description: "fake command cache disabled and key not in cache",
|
||||||
opts: &config.KanikoOptions{Cache: false},
|
opts: &config.KanikoOptions{Cache: false},
|
||||||
|
|
@ -1437,7 +1447,7 @@ RUN foobar
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
snap := fakeSnapShotter{file: fileName}
|
snap := &fakeSnapShotter{file: fileName}
|
||||||
lc := tc.layerCache
|
lc := tc.layerCache
|
||||||
if lc == nil {
|
if lc == nil {
|
||||||
lc = &fakeLayerCache{}
|
lc = &fakeLayerCache{}
|
||||||
|
|
@ -1474,6 +1484,11 @@ RUN foobar
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected error to be nil but was %v", err)
|
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.expectedCacheKeys, lc.receivedKeys, "receive")
|
||||||
assertCacheKeys(t, tc.pushedCacheKeys, keys, "push")
|
assertCacheKeys(t, tc.pushedCacheKeys, keys, "push")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,13 +31,17 @@ import (
|
||||||
type fakeSnapShotter struct {
|
type fakeSnapShotter struct {
|
||||||
file string
|
file string
|
||||||
tarPath string
|
tarPath string
|
||||||
|
initialized bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f fakeSnapShotter) Init() error { return nil }
|
func (f *fakeSnapShotter) Init() error {
|
||||||
func (f fakeSnapShotter) TakeSnapshotFS() (string, error) {
|
f.initialized = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakeSnapShotter) TakeSnapshotFS() (string, error) {
|
||||||
return f.tarPath, nil
|
return f.tarPath, nil
|
||||||
}
|
}
|
||||||
func (f fakeSnapShotter) TakeSnapshot(_ []string, _, _ bool) (string, error) {
|
func (f *fakeSnapShotter) TakeSnapshot(_ []string, _, _ bool) (string, error) {
|
||||||
return f.tarPath, nil
|
return f.tarPath, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue