func (s *preallocSuite) TestPreallocFilesWriteErrors(c *gc.C) { dir := c.MkDir() prefix := filepath.Join(dir, "test.") err := ioutil.WriteFile(prefix+"0", nil, 0644) c.Assert(err, gc.IsNil) err = ioutil.WriteFile(prefix+"1", nil, 0644) c.Assert(err, gc.IsNil) var called int s.PatchValue(mongo.PreallocFile, func(filename string, size int) (bool, error) { var created bool var err error called++ if called == 2 { created = true err = fmt.Errorf("failed to zero test.1") } return created, err }) err = mongo.PreallocFiles(prefix, 4096, 8192) c.Assert(err, gc.ErrorMatches, "failed to zero test.1") // test.0 still exists because we said we didn't // create it (i.e. it already existed) _, err = os.Stat(prefix + "0") c.Assert(err, gc.IsNil) // test.1 no longer exists because we said we created // it, but then failed to write to it. _, err = os.Stat(prefix + "1") c.Assert(err, jc.Satisfies, os.IsNotExist) }
func (s *preallocSuite) TestPreallocFiles(c *gc.C) { dir := c.MkDir() prefix := filepath.Join(dir, "test.") err := mongo.PreallocFiles(prefix, 0, 4096, 8192) c.Assert(err, gc.IsNil) zeroes := [8192]byte{} for i := 0; i < 3; i++ { filename := fmt.Sprintf("%s%d", prefix, i) data, err := ioutil.ReadFile(filename) c.Check(err, gc.IsNil) c.Check(data, gc.DeepEquals, zeroes[:i*4096]) } _, err = os.Stat(prefix + "3") c.Assert(err, jc.Satisfies, os.IsNotExist) }
func (s *preallocSuite) TestPreallocFilesErrors(c *gc.C) { err := mongo.PreallocFiles("", 123) c.Assert(err, gc.ErrorMatches, `specified size 123 for file "0" is not a multiple of 4096`) }