github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/utils/archive/compress_test.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 //nolint 16 package archive 17 18 import ( 19 "fmt" 20 "io" 21 "io/ioutil" 22 "os" 23 "path/filepath" 24 "testing" 25 26 "golang.org/x/sys/unix" 27 ) 28 29 const basePath = "/tmp" 30 31 const fileContent = "content" 32 33 type fileDef struct { 34 name string 35 content string 36 } 37 38 type dirDef struct { 39 path string 40 files []fileDef 41 subDir []dirDef 42 } 43 44 var filesToCreate = []dirDef{ 45 { 46 path: "testDirA", 47 files: []fileDef{ 48 { 49 name: "testFileA", 50 content: fileContent, 51 }, 52 { 53 name: "testFileB", 54 content: fileContent, 55 }, 56 }, 57 subDir: []dirDef{ 58 { 59 path: "testDirC", 60 files: []fileDef{ 61 { 62 name: "testFileA", 63 content: fileContent, 64 }, 65 { 66 name: "testFileB", 67 content: fileContent, 68 }, 69 }, 70 }, 71 }, 72 }, 73 { 74 path: "testDirB", 75 files: []fileDef{ 76 { 77 name: "testFileA", 78 content: fileContent, 79 }, 80 { 81 name: "testFileB", 82 content: fileContent, 83 }, 84 }, 85 }, 86 } 87 88 func makeDir(root string, d dirDef) error { 89 currentDir := filepath.Join(root, d.path) 90 err := os.MkdirAll(currentDir, 0755) 91 if err != nil { 92 return err 93 } 94 95 for _, file := range d.files { 96 _, err = os.Create(filepath.Join(currentDir, file.name)) 97 if err != nil { 98 return err 99 } 100 } 101 102 for _, sub := range d.subDir { 103 err = makeDir(currentDir, sub) 104 if err != nil { 105 return err 106 } 107 } 108 return nil 109 } 110 111 func TestTarWithoutRootDir(t *testing.T) { 112 digest, _, err := TarCanonicalDigest("/Users/eric/Workspace/src/sealer/empty") 113 if err != nil { 114 t.Error(err) 115 } 116 fmt.Println(digest) 117 } 118 119 func TestTarWithRootDir(t *testing.T) { 120 reader, err := TarWithRootDir("./hash.go") 121 if err != nil { 122 t.Error(err) 123 } 124 125 tmp, err := ioutil.TempFile("/tmp", "tar") 126 _, err = io.Copy(tmp, reader) 127 if err != nil { 128 t.Error(err) 129 } 130 } 131 132 func TestName(t *testing.T) { 133 //err := os.Mkdir("abc", 0755) 134 //if err != nil { 135 // t.Error(err) 136 //} 137 err := unix.Setxattr("abc", "trusted.overlay.opaque", []byte{'y'}, 0) 138 if err != nil { 139 t.Error(err) 140 } 141 //fmt.Println(fm.String()) 142 }