github.com/sashka/siva@v1.6.0/readwriter_test.go (about)

     1  package siva_test
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"gopkg.in/src-d/go-siva.v1"
    11  
    12  	. "gopkg.in/check.v1"
    13  )
    14  
    15  type ReadWriterSuite struct {
    16  	tmpDir string
    17  }
    18  
    19  var _ = Suite(&ReadWriterSuite{})
    20  
    21  func (s *ReadWriterSuite) SetUpSuite(c *C) {
    22  	s.tmpDir = c.MkDir()
    23  }
    24  
    25  func (s *ReadWriterSuite) TestWriteRead(c *C) {
    26  	path := filepath.Join(s.tmpDir, c.TestName())
    27  	tmpFile, err := os.Create(path)
    28  	c.Assert(err, IsNil)
    29  	c.Assert(tmpFile, NotNil)
    30  	s.testWriteRead(c, tmpFile, 0)
    31  	c.Assert(tmpFile.Close(), IsNil)
    32  
    33  	tmpFile, err = os.OpenFile(path, os.O_RDWR, 0)
    34  	c.Assert(err, IsNil)
    35  	c.Assert(tmpFile, NotNil)
    36  	s.testWriteRead(c, tmpFile, 1)
    37  	c.Assert(tmpFile.Close(), IsNil)
    38  
    39  	tmpFile, err = os.OpenFile(path, os.O_RDWR, 0)
    40  	c.Assert(err, IsNil)
    41  	c.Assert(tmpFile, NotNil)
    42  	s.testWriteRead(c, tmpFile, 2)
    43  	c.Assert(tmpFile.Close(), IsNil)
    44  }
    45  
    46  func (s *ReadWriterSuite) testWriteRead(c *C, f *os.File, iter int) {
    47  	rw, err := siva.NewReaderWriter(f)
    48  	c.Assert(err, IsNil)
    49  	c.Assert(rw, NotNil)
    50  
    51  	iters := 100
    52  	for i := 0; i < iters; i++ {
    53  		curName := fmt.Sprintf("foo-%d", i)
    54  		content := strings.Repeat("#", i)
    55  
    56  		err := rw.WriteHeader(&siva.Header{
    57  			Name: curName,
    58  		})
    59  		c.Assert(err, IsNil)
    60  
    61  		written, err := rw.Write([]byte(content))
    62  		c.Assert(err, IsNil)
    63  		c.Assert(written, Equals, i)
    64  
    65  		err = rw.Flush()
    66  		c.Assert(err, IsNil)
    67  
    68  		index, err := rw.Index()
    69  		c.Assert(err, IsNil)
    70  
    71  		// index after the first iteration will contain the total amount
    72  		// of files
    73  		num := i + 1
    74  		if iter > 0 {
    75  			num = iters
    76  		}
    77  
    78  		c.Assert(len(index), Equals, num)
    79  
    80  		e := index.Find(curName)
    81  		c.Assert(e, NotNil)
    82  
    83  		sr, err := rw.Get(e)
    84  		c.Assert(err, IsNil)
    85  		c.Assert(sr, NotNil)
    86  
    87  		read, err := ioutil.ReadAll(sr)
    88  		c.Assert(err, IsNil)
    89  		c.Assert(string(read), Equals, content)
    90  	}
    91  
    92  	c.Assert(rw.Close(), IsNil)
    93  }
    94  
    95  func (s *ReadWriterSuite) TestReadExisting(c *C) {
    96  	f, err := os.OpenFile("fixtures/basic.siva", os.O_RDONLY, os.ModePerm)
    97  	c.Assert(err, IsNil)
    98  	c.Assert(f, NotNil)
    99  
   100  	rw, err := siva.NewReaderWriter(f)
   101  	c.Assert(err, IsNil)
   102  	c.Assert(rw, NotNil)
   103  
   104  	index, err := rw.Index()
   105  	c.Assert(err, IsNil)
   106  	c.Assert(len(index), Equals, 3)
   107  
   108  	c.Assert(rw.Close(), IsNil)
   109  }
   110  
   111  func (s *ReadWriterSuite) TestOverwriteExisting(c *C) {
   112  	tmpFile, err := os.Create(filepath.Join(s.tmpDir, c.TestName()))
   113  	c.Assert(err, IsNil)
   114  	c.Assert(tmpFile, NotNil)
   115  
   116  	rw, err := siva.NewReaderWriter(tmpFile)
   117  	c.Assert(err, IsNil)
   118  	c.Assert(rw, NotNil)
   119  
   120  	err = rw.WriteHeader(&siva.Header{
   121  		Name: "foo",
   122  	})
   123  	c.Assert(err, IsNil)
   124  	_, err = rw.Write([]byte("foo"))
   125  	c.Assert(err, IsNil)
   126  	c.Assert(rw.Flush(), IsNil)
   127  
   128  	index, err := rw.Index()
   129  	c.Assert(err, IsNil)
   130  	index = index.Filter()
   131  
   132  	e := index.Find("foo")
   133  	c.Assert(e, NotNil)
   134  
   135  	sr, err := rw.Get(e)
   136  	c.Assert(err, IsNil)
   137  	written, err := ioutil.ReadAll(sr)
   138  	c.Assert(err, IsNil)
   139  	c.Assert(string(written), Equals, "foo")
   140  
   141  	err = rw.WriteHeader(&siva.Header{
   142  		Name: "foo",
   143  	})
   144  	c.Assert(err, IsNil)
   145  	_, err = rw.Write([]byte("bar"))
   146  	c.Assert(err, IsNil)
   147  	c.Assert(rw.Flush(), IsNil)
   148  
   149  	index, err = rw.Index()
   150  	c.Assert(err, IsNil)
   151  
   152  	e = index.Filter().Find("foo")
   153  	c.Assert(e, NotNil)
   154  
   155  	sr, err = rw.Get(e)
   156  	c.Assert(err, IsNil)
   157  	written, err = ioutil.ReadAll(sr)
   158  	c.Assert(err, IsNil)
   159  	c.Assert(string(written), Equals, "bar")
   160  	c.Assert(rw.Close(), IsNil)
   161  }
   162  
   163  func (s *ReadWriterSuite) TestFailIfNotReadAt(c *C) {
   164  	rw, err := siva.NewReaderWriter(&dummyReadWriterSeeker{})
   165  	c.Assert(err, Equals, siva.ErrInvalidReaderAt)
   166  	c.Assert(rw, IsNil)
   167  }
   168  
   169  type dummyReadWriterSeeker struct {
   170  }
   171  
   172  func (_ dummyReadWriterSeeker) Read(p []byte) (n int, err error) {
   173  	return
   174  }
   175  
   176  func (_ dummyReadWriterSeeker) Write(p []byte) (n int, err error) {
   177  	return
   178  }
   179  
   180  func (_ dummyReadWriterSeeker) Seek(offset int64, whence int) (n int64, err error) {
   181  	return
   182  }
   183  
   184  func (s *ReadWriterSuite) TestDelete(c *C) {
   185  	data := "data"
   186  
   187  	path := filepath.Join(s.tmpDir, c.TestName())
   188  	tmpFile, err := os.Create(path)
   189  	c.Assert(err, IsNil)
   190  	c.Assert(tmpFile, NotNil)
   191  
   192  	rw, err := siva.NewReaderWriter(tmpFile)
   193  	c.Assert(err, IsNil)
   194  
   195  	testSteps := []struct {
   196  		name  string
   197  		del   bool
   198  		files []string
   199  	}{
   200  		{"one", false, []string{"one"}},
   201  		{"two", false, []string{"one", "two"}},
   202  		{"three", false, []string{"one", "three", "two"}},
   203  		{"two", true, []string{"one", "three"}},
   204  		{"two", false, []string{"one", "three", "two"}},
   205  		{"four", true, []string{"one", "three", "two"}},
   206  		{"three", true, []string{"one", "two"}},
   207  	}
   208  
   209  	for _, t := range testSteps {
   210  		var flags siva.Flag
   211  		if t.del {
   212  			flags = siva.FlagDeleted
   213  		}
   214  
   215  		err := rw.WriteHeader(&siva.Header{
   216  			Name:  t.name,
   217  			Flags: flags,
   218  		})
   219  		c.Assert(err, IsNil)
   220  
   221  		written, err := rw.Write([]byte(data))
   222  		c.Assert(err, IsNil)
   223  		c.Assert(written, Equals, len(data))
   224  
   225  		err = rw.Flush()
   226  		c.Assert(err, IsNil)
   227  
   228  		index, err := rw.Index()
   229  		c.Assert(err, IsNil)
   230  
   231  		c.Assert(len(index), Equals, len(t.files))
   232  		for i, name := range t.files {
   233  			c.Assert(index[i].Name, Equals, name)
   234  		}
   235  	}
   236  }