github.com/grange74/docker@v1.6.0-rc3/pkg/chrootarchive/archive_test.go (about)

     1  package chrootarchive
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/docker/docker/pkg/archive"
    12  	"github.com/docker/docker/pkg/reexec"
    13  )
    14  
    15  func init() {
    16  	reexec.Init()
    17  }
    18  
    19  func TestChrootTarUntar(t *testing.T) {
    20  	tmpdir, err := ioutil.TempDir("", "docker-TestChrootTarUntar")
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	defer os.RemoveAll(tmpdir)
    25  	src := filepath.Join(tmpdir, "src")
    26  	if err := os.MkdirAll(src, 0700); err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	if err := ioutil.WriteFile(filepath.Join(src, "toto"), []byte("hello toto"), 0644); err != nil {
    30  		t.Fatal(err)
    31  	}
    32  	if err := ioutil.WriteFile(filepath.Join(src, "lolo"), []byte("hello lolo"), 0644); err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	stream, err := archive.Tar(src, archive.Uncompressed)
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	dest := filepath.Join(tmpdir, "src")
    40  	if err := os.MkdirAll(dest, 0700); err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	if err := Untar(stream, dest, &archive.TarOptions{ExcludePatterns: []string{"lolo"}}); err != nil {
    44  		t.Fatal(err)
    45  	}
    46  }
    47  
    48  type slowEmptyTarReader struct {
    49  	size      int
    50  	offset    int
    51  	chunkSize int
    52  }
    53  
    54  // Read is a slow reader of an empty tar (like the output of "tar c --files-from /dev/null")
    55  func (s *slowEmptyTarReader) Read(p []byte) (int, error) {
    56  	time.Sleep(100 * time.Millisecond)
    57  	count := s.chunkSize
    58  	if len(p) < s.chunkSize {
    59  		count = len(p)
    60  	}
    61  	for i := 0; i < count; i++ {
    62  		p[i] = 0
    63  	}
    64  	s.offset += count
    65  	if s.offset > s.size {
    66  		return count, io.EOF
    67  	}
    68  	return count, nil
    69  }
    70  
    71  func TestChrootUntarEmptyArchiveFromSlowReader(t *testing.T) {
    72  	tmpdir, err := ioutil.TempDir("", "docker-TestChrootUntarEmptyArchiveFromSlowReader")
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	defer os.RemoveAll(tmpdir)
    77  	dest := filepath.Join(tmpdir, "dest")
    78  	if err := os.MkdirAll(dest, 0700); err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	stream := &slowEmptyTarReader{size: 10240, chunkSize: 1024}
    82  	if err := Untar(stream, dest, nil); err != nil {
    83  		t.Fatal(err)
    84  	}
    85  }
    86  
    87  func TestChrootApplyEmptyArchiveFromSlowReader(t *testing.T) {
    88  	tmpdir, err := ioutil.TempDir("", "docker-TestChrootApplyEmptyArchiveFromSlowReader")
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  	defer os.RemoveAll(tmpdir)
    93  	dest := filepath.Join(tmpdir, "dest")
    94  	if err := os.MkdirAll(dest, 0700); err != nil {
    95  		t.Fatal(err)
    96  	}
    97  	stream := &slowEmptyTarReader{size: 10240, chunkSize: 1024}
    98  	if _, err := ApplyLayer(dest, stream); err != nil {
    99  		t.Fatal(err)
   100  	}
   101  }
   102  
   103  func TestChrootApplyDotDotFile(t *testing.T) {
   104  	tmpdir, err := ioutil.TempDir("", "docker-TestChrootApplyDotDotFile")
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	defer os.RemoveAll(tmpdir)
   109  	src := filepath.Join(tmpdir, "src")
   110  	if err := os.MkdirAll(src, 0700); err != nil {
   111  		t.Fatal(err)
   112  	}
   113  	if err := ioutil.WriteFile(filepath.Join(src, "..gitme"), []byte(""), 0644); err != nil {
   114  		t.Fatal(err)
   115  	}
   116  	stream, err := archive.Tar(src, archive.Uncompressed)
   117  	if err != nil {
   118  		t.Fatal(err)
   119  	}
   120  	dest := filepath.Join(tmpdir, "dest")
   121  	if err := os.MkdirAll(dest, 0700); err != nil {
   122  		t.Fatal(err)
   123  	}
   124  	if _, err := ApplyLayer(dest, stream); err != nil {
   125  		t.Fatal(err)
   126  	}
   127  }