github.com/GoogleContainerTools/kaniko@v1.23.0/pkg/buildcontext/tar_test.go (about) 1 /* 2 Copyright 2018 Google LLC 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package buildcontext 18 19 import ( 20 "bytes" 21 "compress/gzip" 22 "fmt" 23 "os" 24 "path/filepath" 25 "runtime" 26 "sync" 27 "testing" 28 29 "github.com/GoogleContainerTools/kaniko/pkg/util" 30 "github.com/GoogleContainerTools/kaniko/testutil" 31 ) 32 33 func TestBuildWithLocalTar(t *testing.T) { 34 _, ex, _, _ := runtime.Caller(0) 35 cwd := filepath.Dir(ex) 36 37 testDir := "test_dir" 38 testDirLongPath := filepath.Join(cwd, testDir) 39 dirUnpack := filepath.Join(testDirLongPath, "dir_where_to_unpack") 40 41 if err := os.MkdirAll(dirUnpack, 0750); err != nil { 42 t.Errorf("Failed to create dir_where_to_extract: %v", err) 43 } 44 45 validDockerfile := "Dockerfile_valid" 46 invalidDockerfile := "Dockerfile_invalid" 47 nonExistingDockerfile := "Dockerfile_non_existing" 48 49 files := map[string]string{ 50 validDockerfile: "FROM debian:10.13\nRUN echo \"valid\"", 51 invalidDockerfile: "FROM debian:10.13\nRUN echo \"invalid\"", 52 } 53 54 if err := testutil.SetupFiles(testDir, files); err != nil { 55 t.Errorf("Failed to setup files %v on %s: %v", files, testDir, err) 56 } 57 58 if err := os.Chdir(testDir); err != nil { 59 t.Fatalf("Failed to Chdir on %s: %v", testDir, err) 60 } 61 62 validTarPath := fmt.Sprintf("%s.tar.gz", validDockerfile) 63 invalidTarPath := fmt.Sprintf("%s.tar.gz", invalidDockerfile) 64 nonExistingTarPath := fmt.Sprintf("%s.tar.gz", nonExistingDockerfile) 65 66 var wg sync.WaitGroup 67 wg.Add(1) 68 // Create Tar Gz File with dockerfile inside 69 go func(wg *sync.WaitGroup) { 70 defer wg.Done() 71 validTarFile, err := os.Create(validTarPath) 72 if err != nil { 73 t.Errorf("Failed to create %s: %v", validTarPath, err) 74 } 75 defer validTarFile.Close() 76 77 invalidTarFile, err := os.Create(invalidTarPath) 78 if err != nil { 79 t.Errorf("Failed to create %s: %v", invalidTarPath, err) 80 } 81 defer invalidTarFile.Close() 82 83 gw := gzip.NewWriter(validTarFile) 84 defer gw.Close() 85 86 tw := util.NewTar(gw) 87 defer tw.Close() 88 89 if err := tw.AddFileToTar(validDockerfile); err != nil { 90 t.Errorf("Failed to add %s to %s: %v", validDockerfile, validTarPath, err) 91 } 92 }(&wg) 93 94 // Waiting for the Tar Gz file creation to be done before moving on 95 wg.Wait() 96 97 tests := []struct { 98 dockerfile string 99 srcContext string 100 unpackShouldErr bool 101 srcShaShouldErr bool 102 destShaShouldErr bool 103 }{ 104 { 105 dockerfile: validDockerfile, 106 srcContext: filepath.Join(testDir, validTarPath), 107 unpackShouldErr: false, 108 srcShaShouldErr: false, 109 destShaShouldErr: false, 110 }, 111 { 112 dockerfile: invalidDockerfile, 113 srcContext: filepath.Join(testDir, invalidTarPath), 114 unpackShouldErr: true, 115 srcShaShouldErr: false, 116 destShaShouldErr: true, 117 }, 118 { 119 dockerfile: nonExistingDockerfile, 120 srcContext: filepath.Join(testDir, nonExistingTarPath), 121 unpackShouldErr: true, 122 srcShaShouldErr: true, 123 destShaShouldErr: true, 124 }, 125 } 126 127 for _, tt := range tests { 128 t.Run(tt.dockerfile, func(t *testing.T) { 129 err := util.UnpackCompressedTar(filepath.Join(cwd, tt.srcContext), dirUnpack) 130 testutil.CheckError(t, tt.unpackShouldErr, err) 131 srcSHA, err := getSHAFromFilePath(tt.dockerfile) 132 testutil.CheckError(t, tt.srcShaShouldErr, err) 133 destSHA, err := getSHAFromFilePath(filepath.Join(dirUnpack, tt.dockerfile)) 134 testutil.CheckError(t, tt.destShaShouldErr, err) 135 if err == nil { 136 testutil.CheckDeepEqual(t, srcSHA, destSHA) 137 } 138 }) 139 } 140 141 if err := os.RemoveAll(testDirLongPath); err != nil { 142 t.Errorf("Failed to remove %s: %v", testDirLongPath, err) 143 } 144 } 145 146 func getSHAFromFilePath(f string) (string, error) { 147 data, err := os.ReadFile(f) 148 if err != nil { 149 return "", err 150 } 151 sha, err := util.SHA256(bytes.NewReader(data)) 152 if err != nil { 153 return "", err 154 } 155 156 return sha, nil 157 }