-
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 dd2ef60
Showing
8 changed files
with
140 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,125 @@ | ||
package buildcontext | ||
|
||
import ( | ||
"compress/gzip" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/GoogleContainerTools/kaniko/pkg/util" | ||
"github.com/GoogleContainerTools/kaniko/testutil" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func TestBuildWithLocalTar(t *testing.T) { | ||
_, ex, _, _ := runtime.Caller(0) | ||
cwd := filepath.Dir(ex) | ||
|
||
testDir := "test_dir" | ||
|
||
directoryCompress := filepath.Join(cwd, testDir, "dir_to_compress") | ||
|
||
if err := os.MkdirAll(directoryCompress, 0750); err != nil { | ||
t.Errorf("Failed to create dir_to_uncompress: %v", err) | ||
} | ||
|
||
directoryUncompress := filepath.Join(cwd, testDir, "dir_to_uncompress") | ||
|
||
if err := os.MkdirAll(directoryUncompress, 0750); err != nil { | ||
t.Errorf("Failed to create dir_to_uncompress: %v", err) | ||
} | ||
|
||
if err := os.Chdir(directoryCompress); err != nil { | ||
t.Fatalf("Failed to Chdir on %s: %v", directoryCompress, 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(directoryCompress, files); err != nil { | ||
t.Errorf("Failed to setup files %v: %v", files, 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 { | ||
dockerfilePath string | ||
srcContext string | ||
shouldErr bool | ||
expectedErr string | ||
}{ | ||
{ | ||
dockerfilePath: validDockerfile, | ||
srcContext: filepath.Join(directoryCompress, validTarPath), | ||
shouldErr: false, | ||
expectedErr: "<nil>", | ||
}, | ||
{ | ||
dockerfilePath: invalidDockerfile, | ||
srcContext: filepath.Join(directoryCompress, invalidTarPath), | ||
shouldErr: true, | ||
expectedErr: "EOF", | ||
}, | ||
{ | ||
dockerfilePath: nonExistingDockerfile, | ||
srcContext: filepath.Join(directoryCompress, nonExistingTarPath), | ||
shouldErr: true, | ||
expectedErr: fmt.Sprintf("open tar://%s: no such file or directory", nonExistingTarPath), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.dockerfilePath, func(t *testing.T) { | ||
logrus.Warnf("cwd is %s", cwd) | ||
err := util.UnpackCompressedTar(tt.srcContext, directoryUncompress) | ||
testutil.CheckErrorAndDeepEqual(t, tt.shouldErr, err, tt.expectedErr, err.Error()) | ||
}) | ||
} | ||
|
||
// if err := os.RemoveAll(directoryCompress); err != nil { | ||
// t.Errorf("Failed to remove %s: %v", directoryCompress, err) | ||
// } | ||
} |
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