github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/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  	"io"
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  )
    24  
    25  const basePath = "/tmp"
    26  
    27  const fileContent = "content"
    28  
    29  type fileDef struct {
    30  	name    string
    31  	content string
    32  }
    33  
    34  type dirDef struct {
    35  	path   string
    36  	files  []fileDef
    37  	subDir []dirDef
    38  }
    39  
    40  var filesToCreate = []dirDef{
    41  	{
    42  		path: "testDirA",
    43  		files: []fileDef{
    44  			{
    45  				name:    "testFileA",
    46  				content: fileContent,
    47  			},
    48  			{
    49  				name:    "testFileB",
    50  				content: fileContent,
    51  			},
    52  		},
    53  		subDir: []dirDef{
    54  			{
    55  				path: "testDirC",
    56  				files: []fileDef{
    57  					{
    58  						name:    "testFileA",
    59  						content: fileContent,
    60  					},
    61  					{
    62  						name:    "testFileB",
    63  						content: fileContent,
    64  					},
    65  				},
    66  			},
    67  		},
    68  	},
    69  	{
    70  		path: "testDirB",
    71  		files: []fileDef{
    72  			{
    73  				name:    "testFileA",
    74  				content: fileContent,
    75  			},
    76  			{
    77  				name:    "testFileB",
    78  				content: fileContent,
    79  			},
    80  		},
    81  	},
    82  }
    83  
    84  func makeDir(root string, d dirDef) error {
    85  	currentDir := filepath.Join(root, d.path)
    86  	err := os.MkdirAll(currentDir, 0755)
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	for _, file := range d.files {
    92  		_, err = os.Create(filepath.Join(currentDir, file.name))
    93  		if err != nil {
    94  			return err
    95  		}
    96  	}
    97  
    98  	for _, sub := range d.subDir {
    99  		err = makeDir(currentDir, sub)
   100  		if err != nil {
   101  			return err
   102  		}
   103  	}
   104  	return nil
   105  }
   106  
   107  /* func TestTarWithoutRootDir(t *testing.T) {
   108  	digest, _, err := TarCanonicalDigest("/Users/eric/Workspace/src/sealer/empty")
   109  	if err != nil {
   110  		t.Error(err)
   111  	}
   112  	fmt.Println(digest)
   113  }
   114  */
   115  
   116  func TestTarWithRootDir(t *testing.T) {
   117  	reader, err := TarWithRootDir("./attr.go")
   118  	if err != nil {
   119  		t.Error(err)
   120  	}
   121  
   122  	tmp, err := os.CreateTemp("/tmp", "tar")
   123  	_, err = io.Copy(tmp, reader)
   124  	if err != nil {
   125  		t.Error(err)
   126  	}
   127  }
   128  
   129  /*func TestName(t *testing.T) {
   130  	//err := os.Mkdir("abc", 0755)
   131  	//if err != nil {
   132  	//	t.Error(err)
   133  	//}
   134  	err := unix.Setxattr("abc", "trusted.overlay.opaque", []byte{'y'}, 0)
   135  	if err != nil {
   136  		t.Error(err)
   137  	}
   138  	//fmt.Println(fm.String())
   139  }
   140  */