github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/backend/swift/swift_test.go (about)

     1  // Test Swift filesystem interface
     2  package swift
     3  
     4  import (
     5  	"bytes"
     6  	"context"
     7  	"io"
     8  	"testing"
     9  
    10  	"github.com/rclone/rclone/fs"
    11  	"github.com/rclone/rclone/fs/hash"
    12  	"github.com/rclone/rclone/fs/object"
    13  	"github.com/rclone/rclone/fstest"
    14  	"github.com/rclone/rclone/fstest/fstests"
    15  	"github.com/rclone/rclone/lib/random"
    16  	"github.com/stretchr/testify/assert"
    17  	"github.com/stretchr/testify/require"
    18  )
    19  
    20  // TestIntegration runs integration tests against the remote
    21  func TestIntegration(t *testing.T) {
    22  	fstests.Run(t, &fstests.Opt{
    23  		RemoteName: "TestSwiftAIO:",
    24  		NilObject:  (*Object)(nil),
    25  	})
    26  }
    27  
    28  func (f *Fs) SetUploadChunkSize(cs fs.SizeSuffix) (fs.SizeSuffix, error) {
    29  	return f.setUploadChunkSize(cs)
    30  }
    31  
    32  var _ fstests.SetUploadChunkSizer = (*Fs)(nil)
    33  
    34  // Check that PutStream works with NoChunk as it is the major code
    35  // deviation
    36  func (f *Fs) testNoChunk(t *testing.T) {
    37  	ctx := context.Background()
    38  	f.opt.NoChunk = true
    39  	defer func() {
    40  		f.opt.NoChunk = false
    41  	}()
    42  
    43  	file := fstest.Item{
    44  		ModTime: fstest.Time("2001-02-03T04:05:06.499999999Z"),
    45  		Path:    "piped data no chunk.txt",
    46  		Size:    -1, // use unknown size during upload
    47  	}
    48  
    49  	const contentSize = 100
    50  
    51  	contents := random.String(contentSize)
    52  	buf := bytes.NewBufferString(contents)
    53  	uploadHash := hash.NewMultiHasher()
    54  	in := io.TeeReader(buf, uploadHash)
    55  
    56  	file.Size = -1
    57  	obji := object.NewStaticObjectInfo(file.Path, file.ModTime, file.Size, true, nil, nil)
    58  	obj, err := f.Features().PutStream(ctx, in, obji)
    59  	require.NoError(t, err)
    60  
    61  	file.Hashes = uploadHash.Sums()
    62  	file.Size = int64(contentSize) // use correct size when checking
    63  	file.Check(t, obj, f.Precision())
    64  
    65  	// Re-read the object and check again
    66  	obj, err = f.NewObject(ctx, file.Path)
    67  	require.NoError(t, err)
    68  	file.Check(t, obj, f.Precision())
    69  
    70  	// Delete the object
    71  	assert.NoError(t, obj.Remove(ctx))
    72  }
    73  
    74  // Additional tests that aren't in the framework
    75  func (f *Fs) InternalTest(t *testing.T) {
    76  	t.Run("NoChunk", f.testNoChunk)
    77  }
    78  
    79  var _ fstests.InternalTester = (*Fs)(nil)