func testPut(t *testing.T, file *fstest.Item) { again: buf := bytes.NewBufferString(fstest.RandomString(100)) hash := fs.NewMultiHasher() in := io.TeeReader(buf, hash) tries := 1 const maxTries = 10 file.Size = int64(buf.Len()) obji := fs.NewStaticObjectInfo(file.Path, file.ModTime, file.Size, true, nil, nil) obj, err := remote.Put(in, obji) if err != nil { // Retry if err returned a retry error if fs.IsRetryError(err) && tries < maxTries { t.Logf("Put error: %v - low level retry %d/%d", err, tries, maxTries) time.Sleep(2 * time.Second) tries++ goto again } require.NoError(t, err, "Put error") } file.Hashes = hash.Sums() file.Check(t, obj, remote.Precision()) // Re-read the object and check again obj = findObject(t, file.Path) file.Check(t, obj, remote.Precision()) }
// WriteObjectTo writes an object to the fs, remote passed in func (r *Run) WriteObjectTo(f fs.Fs, remote, content string, modTime time.Time, useUnchecked bool) fstest.Item { put := f.Put if useUnchecked { if fPutUnchecked, ok := f.(fs.PutUncheckeder); ok { put = fPutUnchecked.PutUnchecked } else { r.Fatalf("Fs doesn't support PutUnchecked") } } const maxTries = 5 if !r.mkdir[f.String()] { err := f.Mkdir() if err != nil { r.Fatalf("Failed to mkdir %q: %v", f, err) } r.mkdir[f.String()] = true } for tries := 1; ; tries++ { in := bytes.NewBufferString(content) objinfo := fs.NewStaticObjectInfo(remote, modTime, int64(len(content)), true, nil, nil) _, err := put(in, objinfo) if err == nil { break } // Retry if err returned a retry error if fs.IsRetryError(err) && tries < maxTries { r.Logf("Retry Put of %q to %v: %d/%d (%v)", remote, f, tries, maxTries, err) continue } r.Fatalf("Failed to put %q to %q: %v", remote, f, err) } return fstest.NewItem(remote, content, modTime) }
// WriteObjectTo writes an object to the fs, remote passed in func (r *Run) WriteObjectTo(f fs.Fs, remote, content string, modTime time.Time, useUnchecked bool) fstest.Item { put := f.Put if useUnchecked { put = f.Features().PutUnchecked if put == nil { r.Fatalf("Fs doesn't support PutUnchecked") } } r.Mkdir(f) const maxTries = 10 for tries := 1; ; tries++ { in := bytes.NewBufferString(content) objinfo := fs.NewStaticObjectInfo(remote, modTime, int64(len(content)), true, nil, nil) _, err := put(in, objinfo) if err == nil { break } // Retry if err returned a retry error if fs.IsRetryError(err) && tries < maxTries { r.Logf("Retry Put of %q to %v: %d/%d (%v)", remote, f, tries, maxTries, err) time.Sleep(2 * time.Second) continue } r.Fatalf("Failed to put %q to %q: %v", remote, f, err) } return fstest.NewItem(remote, content, modTime) }
// TestObjectUpdate tests that Update works func TestObjectUpdate(t *testing.T) { skipIfNotOk(t) contents := fstest.RandomString(200) buf := bytes.NewBufferString(contents) hash := fs.NewMultiHasher() in := io.TeeReader(buf, hash) file1.Size = int64(buf.Len()) obj := findObject(t, file1.Path) obji := fs.NewStaticObjectInfo(file1.Path, file1.ModTime, int64(len(contents)), true, nil, obj.Fs()) err := obj.Update(in, obji) require.NoError(t, err) file1.Hashes = hash.Sums() // check the object has been updated file1.Check(t, obj, remote.Precision()) // Re-read the object and check again obj = findObject(t, file1.Path) file1.Check(t, obj, remote.Precision()) // check contents correct assert.Equal(t, contents, readObject(t, obj), "contents of updated file1 differ") file1Contents = contents }
// TestObjectUpdate tests that Update works func TestObjectUpdate(t *testing.T) { skipIfNotOk(t) buf := bytes.NewBufferString(fstest.RandomString(200)) hash := fs.NewMultiHasher() in := io.TeeReader(buf, hash) file1.Size = int64(buf.Len()) obj := findObject(t, file1.Path) obji := fs.NewStaticObjectInfo("", file1.ModTime, file1.Size, true, nil, obj.Fs()) err := obj.Update(in, obji) require.NoError(t, err) file1.Hashes = hash.Sums() file1.Check(t, obj, remote.Precision()) // Re-read the object and check again obj = findObject(t, file1.Path) file1.Check(t, obj, remote.Precision()) }
// TestFsPutError tests uploading a file where there is an error // // It makes sure that aborting a file half way through does not create // a file on the remote. func TestFsPutError(t *testing.T) { skipIfNotOk(t) // Read 50 bytes then produce an error contents := fstest.RandomString(50) buf := bytes.NewBufferString(contents) er := &errorReader{errors.New("potato")} in := io.MultiReader(buf, er) obji := fs.NewStaticObjectInfo(file2.Path, file2.ModTime, 100, true, nil, nil) obj, err := remote.Put(in, obji) // assert.Nil(t, obj) - FIXME some remotes return the object even on nil assert.NotNil(t, err) obj, err = remote.NewObject(file2.Path) assert.Nil(t, obj) assert.Equal(t, fs.ErrorObjectNotFound, err) }