@@ -17,12 +17,15 @@ limitations under the License.
1717package util
1818
1919import (
20+ "archive/tar"
21+ "bytes"
2022 "compress/gzip"
2123 "io"
2224 "io/ioutil"
2325 "os"
2426 "path/filepath"
2527 "testing"
28+ "time"
2629
2730 "github.com/GoogleContainerTools/kaniko/testutil"
2831)
@@ -57,6 +60,39 @@ func Test_IsLocalTarArchive(t *testing.T) {
5760 }
5861}
5962
63+ func Test_AddFileToTar (t * testing.T ) {
64+ testDir , err := ioutil .TempDir ("" , "" )
65+ if err != nil {
66+ t .Fatalf ("err setting up temp dir: %v" , err )
67+ }
68+ defer os .RemoveAll (testDir )
69+
70+ path := filepath .Join (testDir , regularFiles [0 ])
71+ if err := ioutil .WriteFile (path , []byte ("hello" ), os .ModePerm ); err != nil {
72+ t .Fatal (err )
73+ }
74+ // use a pre-determined time with non-zero microseconds to avoid flakiness
75+ mtime := time .UnixMicro (1635533172891395 )
76+ if err := os .Chtimes (path , mtime , mtime ); err != nil {
77+ t .Fatal (err )
78+ }
79+
80+ buf := new (bytes.Buffer )
81+ tarw := NewTar (buf )
82+ if err := tarw .AddFileToTar (path ); err != nil {
83+ t .Fatal (err )
84+ }
85+ tarw .Close ()
86+
87+ // Check that the mtime is correct (#1808)
88+ tarReader := tar .NewReader (buf )
89+ hdr , err := tarReader .Next ()
90+ if err != nil {
91+ t .Fatal (err )
92+ }
93+ testutil .CheckDeepEqual (t , mtime , hdr .ModTime )
94+ }
95+
6096func setUpFilesAndTars (testDir string ) error {
6197 regularFilesAndContents := map [string ]string {
6298 regularFiles [0 ]: "" ,
0 commit comments