-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Fix unpack tar.gz archive with ADD instruction, issue #2409 * Added unit test for ADD instruction
- Loading branch information
1 parent
390b0ff
commit d5c36a6
Showing
2 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/* | ||
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 commands | ||
|
||
import ( | ||
"compress/gzip" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
"testing" | ||
|
||
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile" | ||
"github.com/GoogleContainerTools/kaniko/pkg/util" | ||
"github.com/GoogleContainerTools/kaniko/testutil" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/moby/buildkit/frontend/dockerfile/instructions" | ||
) | ||
|
||
type TarList struct { | ||
tarName string | ||
directory string | ||
compressed bool | ||
} | ||
|
||
func createFile(tempDir string) error { | ||
fileName := filepath.Join(tempDir, "text.txt") | ||
file, err := os.Create(fileName) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
err = os.WriteFile(fileName, []byte("This is a test!\n"), 0644) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func createTar(tempDir string, toCreate TarList) error { | ||
if toCreate.compressed { | ||
file, err := os.OpenFile(filepath.Join(tempDir, toCreate.tarName), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
gzipWriter := gzip.NewWriter(file) | ||
defer gzipWriter.Close() | ||
|
||
err = util.CreateTarballOfDirectory(filepath.Join(tempDir, toCreate.directory), gzipWriter) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
tarFile, err := os.Create(filepath.Join(tempDir, toCreate.tarName)) | ||
if err != nil { | ||
return err | ||
} | ||
err = util.CreateTarballOfDirectory(filepath.Join(tempDir, toCreate.directory), tarFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func setupAddTest(t *testing.T) string { | ||
tempDir := t.TempDir() | ||
|
||
err := createFile(tempDir) | ||
if err != nil { | ||
t.Errorf("couldn't create the file %v", err) | ||
} | ||
|
||
var tarFiles = []TarList{ | ||
{ | ||
tarName: "a.tar", | ||
directory: "a", | ||
compressed: false, | ||
}, | ||
{ | ||
tarName: "b.tar.gz", | ||
directory: "b", | ||
compressed: true, | ||
}, | ||
} | ||
|
||
// Create directories with files and then create tar | ||
for _, toCreate := range tarFiles { | ||
|
||
err = os.Mkdir(filepath.Join(tempDir, toCreate.directory), 0755) | ||
if err != nil { | ||
t.Errorf("couldn't create directory %v", err) | ||
} | ||
|
||
err = createFile(filepath.Join(tempDir, toCreate.directory)) | ||
if err != nil { | ||
t.Errorf("couldn't create file inside directory %v", err) | ||
} | ||
err = createTar(tempDir, toCreate) | ||
|
||
if err != nil { | ||
t.Errorf("couldn't create the tar %v", err) | ||
} | ||
} | ||
|
||
return tempDir | ||
} | ||
|
||
func Test_AddCommand(t *testing.T) { | ||
tempDir := setupAddTest(t) | ||
|
||
fileContext := util.FileContext{Root: tempDir} | ||
cfg := &v1.Config{ | ||
Cmd: nil, | ||
Env: []string{}, | ||
WorkingDir: tempDir, | ||
} | ||
buildArgs := dockerfile.NewBuildArgs([]string{}) | ||
|
||
var addTests = []struct { | ||
name string | ||
sourcesAndDest []string | ||
expectedDest []string | ||
}{ | ||
{ | ||
name: "add files into tempAddExecuteTest/", | ||
sourcesAndDest: []string{"text.txt", "a.tar", "b.tar.gz", "tempAddExecuteTest/"}, | ||
expectedDest: []string{ | ||
"text.txt", | ||
filepath.Join(tempDir, "a/"), | ||
filepath.Join(tempDir, "a/text.txt"), | ||
filepath.Join(tempDir, "b/"), | ||
filepath.Join(tempDir, "b/text.txt"), | ||
}, | ||
}, | ||
} | ||
|
||
for _, testCase := range addTests { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
c := AddCommand{ | ||
cmd: &instructions.AddCommand{ | ||
SourcesAndDest: instructions.SourcesAndDest{SourcePaths: testCase.sourcesAndDest[0 : len(testCase.sourcesAndDest)-1], | ||
DestPath: testCase.sourcesAndDest[len(testCase.sourcesAndDest)-1]}, | ||
}, | ||
fileContext: fileContext, | ||
} | ||
c.ExecuteCommand(cfg, buildArgs) | ||
|
||
expected := []string{} | ||
resultDir := filepath.Join(tempDir, "tempAddExecuteTest/") | ||
for _, file := range testCase.expectedDest { | ||
expected = append(expected, filepath.Join(resultDir, file)) | ||
} | ||
sort.Strings(expected) | ||
sort.Strings(c.snapshotFiles) | ||
testutil.CheckDeepEqual(t, expected, c.snapshotFiles) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters