-
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.
feat(kaniko): add unit test about valid, invalid and non existing tar.gz
- Loading branch information
1 parent
f3d6b23
commit 785b390
Showing
9 changed files
with
196 additions
and
139 deletions.
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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
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 buildcontext | ||
|
||
import ( | ||
"compress/gzip" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/GoogleContainerTools/kaniko/pkg/util" | ||
"github.com/GoogleContainerTools/kaniko/testutil" | ||
) | ||
|
||
func TestBuildWithLocalTar(t *testing.T) { | ||
_, ex, _, _ := runtime.Caller(0) | ||
cwd := filepath.Dir(ex) | ||
|
||
testDir := "test_dir" | ||
|
||
testDirLongPath := filepath.Join(cwd, testDir) | ||
|
||
directoryUncompress := filepath.Join(testDirLongPath, "dir_to_uncompress") | ||
|
||
if err := os.MkdirAll(directoryUncompress, 0750); err != nil { | ||
t.Errorf("Failed to create dir_to_uncompress: %v", err) | ||
} | ||
|
||
validDockerfile := "Dockerfile_valid" | ||
invalidDockerfile := "Dockerfile_invalid" | ||
nonExistingDockerfile := "Dockerfile_non_existing" | ||
|
||
files := map[string]string{ | ||
validDockerfile: "FROM debian:9.11\nRUN echo \"valid\"", | ||
invalidDockerfile: "FROM debian:9.11\nRUN echo \"invalid\"", | ||
nonExistingDockerfile: "FROM debian:9.11\nRUN echo \"non_existing\"", | ||
} | ||
|
||
if err := testutil.SetupFiles(testDir, files); err != nil { | ||
t.Errorf("Failed to setup files %v on %s: %v", files, testDir, err) | ||
} | ||
|
||
if err := os.Chdir(testDir); err != nil { | ||
t.Fatalf("Failed to Chdir on %s: %v", testDir, err) | ||
} | ||
|
||
validTarPath := fmt.Sprintf("%s.tar.gz", validDockerfile) | ||
invalidTarPath := fmt.Sprintf("%s.tar.gz", invalidDockerfile) | ||
nonExistingTarPath := fmt.Sprintf("%s.tar.gz", nonExistingDockerfile) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
// Create Tar Gz File with dockerfile inside | ||
go func(wg *sync.WaitGroup) { | ||
defer wg.Done() | ||
validTarFile, err := os.Create(validTarPath) | ||
if err != nil { | ||
t.Errorf("Failed to create %s: %v", validTarPath, err) | ||
} | ||
defer validTarFile.Close() | ||
|
||
invalidTarFile, err := os.Create(invalidTarPath) | ||
if err != nil { | ||
t.Errorf("Failed to create %s: %v", invalidTarPath, err) | ||
} | ||
defer invalidTarFile.Close() | ||
|
||
gw := gzip.NewWriter(validTarFile) | ||
defer gw.Close() | ||
|
||
tw := util.NewTar(gw) | ||
defer tw.Close() | ||
|
||
if err := tw.AddFileToTar(validDockerfile); err != nil { | ||
t.Errorf("Failed to add %s to %s: %v", validDockerfile, validTarPath, err) | ||
} | ||
}(&wg) | ||
|
||
// Waiting for the Tar Gz file creation to be done before moving on | ||
wg.Wait() | ||
|
||
tests := []struct { | ||
dockerfile string | ||
srcContext string | ||
shouldErr bool | ||
expectedErr string | ||
}{ | ||
{ | ||
dockerfile: validDockerfile, | ||
srcContext: filepath.Join(testDir, validTarPath), | ||
shouldErr: false, | ||
expectedErr: "<nil>", | ||
}, | ||
{ | ||
dockerfile: invalidDockerfile, | ||
srcContext: filepath.Join(testDir, invalidTarPath), | ||
shouldErr: true, | ||
expectedErr: "EOF", | ||
}, | ||
{ | ||
dockerfile: nonExistingDockerfile, | ||
srcContext: filepath.Join(testDir, nonExistingTarPath), | ||
shouldErr: true, | ||
expectedErr: fmt.Sprintf("open %s: no such file or directory", filepath.Join(testDirLongPath, nonExistingTarPath)), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.dockerfile, func(t *testing.T) { | ||
err := util.UnpackCompressedTar(filepath.Join(cwd, tt.srcContext), directoryUncompress) | ||
if err != nil { | ||
if !tt.shouldErr { | ||
t.Errorf("Unexpected error for %s: %v", tt.dockerfile, err) | ||
} | ||
if err.Error() != tt.expectedErr { | ||
t.Errorf("error '%s' and '%s' aren't equal", err.Error(), tt.expectedErr) | ||
} | ||
} else { | ||
if tt.shouldErr { | ||
t.Errorf("Expected error, but returned none for %s", tt.dockerfile) | ||
} | ||
sourceSha, err := getSha(tt.dockerfile) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
destSha, err := getSha(filepath.Join(directoryUncompress, tt.dockerfile)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if sourceSha != destSha { | ||
t.Errorf("'%s' and '%s' aren't equal", sourceSha, destSha) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
if err := os.RemoveAll(testDirLongPath); err != nil { | ||
t.Errorf("Failed to remove %s: %v", testDirLongPath, err) | ||
} | ||
} | ||
|
||
func getSha(path string) (string, error) { | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer f.Close() | ||
|
||
hasher := sha256.New() | ||
if _, err := io.Copy(hasher, f); err != nil { | ||
return "", err | ||
} | ||
value := hex.EncodeToString(hasher.Sum(nil)) | ||
|
||
return value, nil | ||
} |
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
Oops, something went wrong.