github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/utils/zip/package_test.go (about)

     1  // Copyright 2011-2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package zip_test
     5  
     6  import (
     7  	"archive/zip"
     8  	"fmt"
     9  	"io/ioutil"
    10  	"os"
    11  	"os/exec"
    12  	"path/filepath"
    13  	"testing"
    14  
    15  	gc "launchpad.net/gocheck"
    16  
    17  	"launchpad.net/juju-core/testing/testbase"
    18      "launchpad.net/juju-core/utils"
    19  )
    20  
    21  func TestPackage(t *testing.T) {
    22  	gc.TestingT(t)
    23  }
    24  
    25  type BaseSuite struct {
    26  	testbase.LoggingSuite
    27  }
    28  
    29  func (s *BaseSuite) makeZip(c *gc.C, creators ...creator) *zip.Reader {
    30  	basePath := c.MkDir()
    31  	for _, creator := range creators {
    32  		creator.create(c, basePath)
    33  	}
    34  	defer os.RemoveAll(basePath)
    35  
    36  	outPath := join(c.MkDir(), "test.zip")
    37  	cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("cd %q; zip --fifo --symlinks -r %q .", basePath, outPath))
    38  	output, err := cmd.CombinedOutput()
    39  	c.Assert(err, gc.IsNil, gc.Commentf("Command output: %s", output))
    40  
    41  	file, err := os.Open(outPath)
    42  	c.Assert(err, gc.IsNil)
    43  	s.AddCleanup(func(c *gc.C) {
    44  		err := file.Close()
    45  		c.Assert(err, gc.IsNil)
    46  	})
    47  	fileInfo, err := file.Stat()
    48  	c.Assert(err, gc.IsNil)
    49  	reader, err := zip.NewReader(file, fileInfo.Size())
    50  	c.Assert(err, gc.IsNil)
    51  	return reader
    52  }
    53  
    54  type creator interface {
    55  	create(c *gc.C, basePath string)
    56  	check(c *gc.C, basePath string)
    57  }
    58  
    59  func join(basePath, path string) string {
    60  	return filepath.Join(basePath, filepath.FromSlash(path))
    61  }
    62  
    63  type dir struct {
    64  	path string
    65  	perm os.FileMode
    66  }
    67  
    68  func (d dir) create(c *gc.C, basePath string) {
    69  	err := os.MkdirAll(join(basePath, d.path), d.perm)
    70  	c.Assert(err, gc.IsNil)
    71  }
    72  
    73  func (d dir) check(c *gc.C, basePath string) {
    74  	fileInfo, err := os.Lstat(join(basePath, d.path))
    75  	c.Check(err, gc.IsNil)
    76  	c.Check(fileInfo.Mode()&os.ModePerm, gc.Equals, d.perm)
    77  }
    78  
    79  type file struct {
    80  	path string
    81  	data string
    82  	perm os.FileMode
    83  }
    84  
    85  func (f file) create(c *gc.C, basePath string) {
    86  	err := ioutil.WriteFile(join(basePath, f.path), []byte(f.data), f.perm)
    87  	c.Assert(err, gc.IsNil)
    88  }
    89  
    90  func (f file) check(c *gc.C, basePath string) {
    91  	path := join(basePath, f.path)
    92  	fileInfo, err := os.Lstat(path)
    93  	if !c.Check(err, gc.IsNil) {
    94  		return
    95  	}
    96  	mode := fileInfo.Mode()
    97  	c.Check(mode&os.ModeType, gc.Equals, os.FileMode(0))
    98  	c.Check(mode&os.ModePerm, gc.Equals, f.perm)
    99  	data, err := ioutil.ReadFile(path)
   100  	c.Check(err, gc.IsNil)
   101  	c.Check(string(data), gc.Equals, f.data)
   102  }
   103  
   104  type symlink struct {
   105  	path string
   106  	data string
   107  }
   108  
   109  func (s symlink) create(c *gc.C, basePath string) {
   110  	err := utils.Symlink(s.data, join(basePath, s.path))
   111  	c.Assert(err, gc.IsNil)
   112  }
   113  
   114  func (s symlink) check(c *gc.C, basePath string) {
   115  	data, err := os.Readlink(join(basePath, s.path))
   116  	c.Check(err, gc.IsNil)
   117  	c.Check(data, gc.Equals, s.data)
   118  }