github.com/tiagovtristao/plz@v13.4.0+incompatible/tools/jarcat/tar/tar_test.go (about)

     1  package tar
     2  
     3  import (
     4  	"archive/tar"
     5  	"bytes"
     6  	"compress/gzip"
     7  	"io"
     8  	"os"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  	"github.com/ulikunitz/xz"
    16  )
    17  
    18  var testInputs = []string{"tools/jarcat/tar/test_data/dir1", "tools/jarcat/tar/test_data/dir2"}
    19  
    20  func TestNoCompression(t *testing.T) {
    21  	filename := "test_no_compression.tar"
    22  	err := Write(filename, testInputs, "", false, false)
    23  	require.NoError(t, err)
    24  
    25  	m := ReadTar(t, filename, false, false)
    26  	assert.EqualValues(t, map[string]string{
    27  		"dir1/file1.txt": "test file 1",
    28  		"dir2/file2.txt": "test file 2",
    29  	}, toFilenameMap(m))
    30  
    31  	// All the timestamps should be fixated and there should be no user/group id.
    32  	var zeroTime time.Time
    33  	for h := range m {
    34  		assert.EqualValues(t, mtime, h.ModTime.In(time.UTC))
    35  		// These two seem to always be zero regardless of what we send in.
    36  		// We don't really care as long as they're always the same.
    37  		assert.EqualValues(t, zeroTime, h.AccessTime)
    38  		assert.EqualValues(t, zeroTime, h.ChangeTime)
    39  		assert.EqualValues(t, nobody, h.Uid)
    40  		assert.EqualValues(t, nobody, h.Gid)
    41  		assert.EqualValues(t, "nobody", h.Uname)
    42  		assert.EqualValues(t, "nobody", h.Gname)
    43  	}
    44  }
    45  
    46  func TestCompression(t *testing.T) {
    47  	filename := "test_compression.tar.gz"
    48  	err := Write(filename, testInputs, "", true, false)
    49  	require.NoError(t, err)
    50  
    51  	m := ReadTar(t, filename, true, false)
    52  	assert.EqualValues(t, map[string]string{
    53  		"dir1/file1.txt": "test file 1",
    54  		"dir2/file2.txt": "test file 2",
    55  	}, toFilenameMap(m))
    56  }
    57  
    58  func TestXzipCompression(t *testing.T) {
    59  	filename := "test_compression.tar.xz"
    60  	err := Write(filename, testInputs, "", false, true)
    61  	require.NoError(t, err)
    62  
    63  	m := ReadTar(t, filename, false, true)
    64  	assert.EqualValues(t, map[string]string{
    65  		"dir1/file1.txt": "test file 1",
    66  		"dir2/file2.txt": "test file 2",
    67  	}, toFilenameMap(m))
    68  }
    69  
    70  func TestWithPrefix(t *testing.T) {
    71  	filename := "test_prefix.tar"
    72  	err := Write(filename, testInputs, "/", false, false)
    73  	require.NoError(t, err)
    74  
    75  	m := ReadTar(t, filename, false, false)
    76  	assert.EqualValues(t, map[string]string{
    77  		"/dir1/file1.txt": "test file 1",
    78  		"/dir2/file2.txt": "test file 2",
    79  	}, toFilenameMap(m))
    80  }
    81  
    82  // ReadTar is a test utility that reads all the files from a tarball and returns a map of
    83  // their headers -> their contents.
    84  func ReadTar(t *testing.T, filename string, gzcompress, xzcompress bool) map[*tar.Header]string {
    85  	f, err := os.Open(filename)
    86  	require.NoError(t, err)
    87  	if xzcompress {
    88  		r, err := xz.NewReader(f)
    89  		require.NoError(t, err)
    90  		return readTar(t, r)
    91  	} else if gzcompress {
    92  		r, err := gzip.NewReader(f)
    93  		require.NoError(t, err)
    94  		return readTar(t, r)
    95  	}
    96  	return readTar(t, f)
    97  }
    98  
    99  // readTar is a test utility that reads all the files from a tarball and returns a map of
   100  // their headers -> their contents.
   101  func readTar(t *testing.T, r io.Reader) map[*tar.Header]string {
   102  	tr := tar.NewReader(r)
   103  	m := map[*tar.Header]string{}
   104  	for {
   105  		hdr, err := tr.Next()
   106  		if err == io.EOF {
   107  			break
   108  		}
   109  		require.NoError(t, err)
   110  		var buf bytes.Buffer
   111  		_, err = io.Copy(&buf, tr)
   112  		require.NoError(t, err)
   113  		m[hdr] = strings.TrimSpace(buf.String()) // Don't worry about newline, they're just test files...
   114  	}
   115  	return m
   116  }
   117  
   118  // toFilenameMap converts one of the maps returned by above to a map of filenames to contents.
   119  func toFilenameMap(m map[*tar.Header]string) map[string]string {
   120  	r := map[string]string{}
   121  	for hdr, contents := range m {
   122  		r[hdr.Name] = contents
   123  	}
   124  	return r
   125  }