mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-22 09:37:48 +02:00
When building Docker images, layers were previously stored in memory. This caused obvious issues when manipulating large layers, which could cause Kaniko to crash.
233 lines
5.8 KiB
Go
233 lines
5.8 KiB
Go
/*
|
|
Copyright 2018 Google LLC
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
package snapshot
|
|
|
|
import (
|
|
"archive/tar"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/GoogleContainerTools/kaniko/pkg/util"
|
|
"github.com/GoogleContainerTools/kaniko/testutil"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func TestSnapshotFSFileChange(t *testing.T) {
|
|
testDir, snapshotter, cleanup, err := setUpTestDir()
|
|
defer cleanup()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Make some changes to the filesystem
|
|
newFiles := map[string]string{
|
|
"foo": "newbaz1",
|
|
"bar/bat": "baz",
|
|
}
|
|
if err := testutil.SetupFiles(testDir, newFiles); err != nil {
|
|
t.Fatalf("Error setting up fs: %s", err)
|
|
}
|
|
// Take another snapshot
|
|
tarPath, err := snapshotter.TakeSnapshotFS()
|
|
if err != nil {
|
|
t.Fatalf("Error taking snapshot of fs: %s", err)
|
|
}
|
|
|
|
f, err := os.Open(tarPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Check contents of the snapshot, make sure contents is equivalent to snapshotFiles
|
|
tr := tar.NewReader(f)
|
|
fooPath := filepath.Join(testDir, "foo")
|
|
batPath := filepath.Join(testDir, "bar/bat")
|
|
snapshotFiles := map[string]string{
|
|
fooPath: "newbaz1",
|
|
batPath: "baz",
|
|
}
|
|
numFiles := 0
|
|
for {
|
|
hdr, err := tr.Next()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
numFiles++
|
|
if _, isFile := snapshotFiles[hdr.Name]; !isFile {
|
|
t.Fatalf("File %s unexpectedly in tar", hdr.Name)
|
|
}
|
|
contents, _ := ioutil.ReadAll(tr)
|
|
if string(contents) != snapshotFiles[hdr.Name] {
|
|
t.Fatalf("Contents of %s incorrect, expected: %s, actual: %s", hdr.Name, snapshotFiles[hdr.Name], string(contents))
|
|
}
|
|
}
|
|
if numFiles != 2 {
|
|
t.Fatalf("Incorrect number of files were added, expected: 2, actual: %v", numFiles)
|
|
}
|
|
}
|
|
|
|
func TestSnapshotFSChangePermissions(t *testing.T) {
|
|
testDir, snapshotter, cleanup, err := setUpTestDir()
|
|
defer cleanup()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Change permissions on a file
|
|
batPath := filepath.Join(testDir, "bar/bat")
|
|
if err := os.Chmod(batPath, 0600); err != nil {
|
|
t.Fatalf("Error changing permissions on %s: %v", batPath, err)
|
|
}
|
|
// Take another snapshot
|
|
tarPath, err := snapshotter.TakeSnapshotFS()
|
|
if err != nil {
|
|
t.Fatalf("Error taking snapshot of fs: %s", err)
|
|
}
|
|
f, err := os.Open(tarPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Check contents of the snapshot, make sure contents is equivalent to snapshotFiles
|
|
tr := tar.NewReader(f)
|
|
snapshotFiles := map[string]string{
|
|
batPath: "baz2",
|
|
}
|
|
numFiles := 0
|
|
for {
|
|
hdr, err := tr.Next()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
numFiles++
|
|
if _, isFile := snapshotFiles[hdr.Name]; !isFile {
|
|
t.Fatalf("File %s unexpectedly in tar", hdr.Name)
|
|
}
|
|
contents, _ := ioutil.ReadAll(tr)
|
|
if string(contents) != snapshotFiles[hdr.Name] {
|
|
t.Fatalf("Contents of %s incorrect, expected: %s, actual: %s", hdr.Name, snapshotFiles[hdr.Name], string(contents))
|
|
}
|
|
}
|
|
if numFiles != 1 {
|
|
t.Fatalf("Incorrect number of files were added, expected: 1, got: %v", numFiles)
|
|
}
|
|
}
|
|
|
|
func TestSnapshotFiles(t *testing.T) {
|
|
testDir, snapshotter, cleanup, err := setUpTestDir()
|
|
defer cleanup()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Make some changes to the filesystem
|
|
newFiles := map[string]string{
|
|
"foo": "newbaz1",
|
|
}
|
|
if err := testutil.SetupFiles(testDir, newFiles); err != nil {
|
|
t.Fatalf("Error setting up fs: %s", err)
|
|
}
|
|
filesToSnapshot := []string{
|
|
filepath.Join(testDir, "foo"),
|
|
}
|
|
tarPath, err := snapshotter.TakeSnapshot(filesToSnapshot)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.Remove(tarPath)
|
|
|
|
expectedFiles := []string{"/", "/tmp", filepath.Join(testDir, "foo")}
|
|
|
|
f, err := os.Open(tarPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Check contents of the snapshot, make sure contents is equivalent to snapshotFiles
|
|
tr := tar.NewReader(f)
|
|
var actualFiles []string
|
|
for {
|
|
hdr, err := tr.Next()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
actualFiles = append(actualFiles, hdr.Name)
|
|
}
|
|
testutil.CheckErrorAndDeepEqual(t, false, nil, expectedFiles, actualFiles)
|
|
}
|
|
|
|
func TestEmptySnapshotFS(t *testing.T) {
|
|
_, snapshotter, cleanup, err := setUpTestDir()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer cleanup()
|
|
|
|
// Take snapshot with no changes
|
|
tarPath, err := snapshotter.TakeSnapshotFS()
|
|
if err != nil {
|
|
t.Fatalf("Error taking snapshot of fs: %s", err)
|
|
}
|
|
|
|
f, err := os.Open(tarPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tr := tar.NewReader(f)
|
|
|
|
if _, err := tr.Next(); err != io.EOF {
|
|
t.Fatal("no files expected in tar, found files.")
|
|
}
|
|
}
|
|
|
|
func setUpTestDir() (string, *Snapshotter, func(), error) {
|
|
testDir, err := ioutil.TempDir("", "")
|
|
if err != nil {
|
|
return "", nil, nil, errors.Wrap(err, "setting up temp dir")
|
|
}
|
|
|
|
snapshotPath, err := ioutil.TempDir("", "")
|
|
if err != nil {
|
|
return "", nil, nil, errors.Wrap(err, "setting up temp dir")
|
|
}
|
|
|
|
snapshotPathPrefix = snapshotPath
|
|
|
|
files := map[string]string{
|
|
"foo": "baz1",
|
|
"bar/bat": "baz2",
|
|
"kaniko/file": "file",
|
|
}
|
|
// Set up initial files
|
|
if err := testutil.SetupFiles(testDir, files); err != nil {
|
|
return "", nil, nil, errors.Wrap(err, "setting up file system")
|
|
}
|
|
|
|
// Take the initial snapshot
|
|
l := NewLayeredMap(util.Hasher(), util.CacheHasher())
|
|
snapshotter := NewSnapshotter(l, testDir)
|
|
if err := snapshotter.Init(); err != nil {
|
|
return "", nil, nil, errors.Wrap(err, "initializing snapshotter")
|
|
}
|
|
|
|
cleanup := func() {
|
|
os.RemoveAll(snapshotPath)
|
|
os.RemoveAll(testDir)
|
|
}
|
|
|
|
return testDir, snapshotter, cleanup, nil
|
|
}
|