github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/lib/bucket/bucket_test.go (about)

     1  package bucket
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestSplit(t *testing.T) {
    12  	for _, test := range []struct {
    13  		in         string
    14  		wantBucket string
    15  		wantPath   string
    16  	}{
    17  		{in: "", wantBucket: "", wantPath: ""},
    18  		{in: "bucket", wantBucket: "bucket", wantPath: ""},
    19  		{in: "bucket/path", wantBucket: "bucket", wantPath: "path"},
    20  		{in: "bucket/path/subdir", wantBucket: "bucket", wantPath: "path/subdir"},
    21  	} {
    22  		gotBucket, gotPath := Split(test.in)
    23  		assert.Equal(t, test.wantBucket, gotBucket, test.in)
    24  		assert.Equal(t, test.wantPath, gotPath, test.in)
    25  	}
    26  }
    27  
    28  func TestJoin(t *testing.T) {
    29  	for _, test := range []struct {
    30  		in1, in2 string
    31  		want     string
    32  	}{
    33  		{in1: "", in2: "", want: ""},
    34  		{in1: "in1", in2: "", want: "in1"},
    35  		{in1: "", in2: "in2", want: "in2"},
    36  		{in1: "in1", in2: "in2", want: "in1/in2"},
    37  		{in1: "in1/", in2: "in2", want: "in1/in2"},
    38  		{in1: "in1", in2: "/in2", want: "in1/in2"},
    39  		{in1: "in1", in2: "in2/", want: "in1/in2/"},
    40  		{in1: "/in1", in2: "/in2", want: "/in1/in2"},
    41  		{in1: "/in1", in2: "../in2", want: "/in1/../in2"},
    42  	} {
    43  		got := Join(test.in1, test.in2)
    44  		assert.Equal(t, test.want, got, fmt.Sprintf("in1=%q, in2=%q", test.in1, test.in2))
    45  	}
    46  }
    47  
    48  func TestCache(t *testing.T) {
    49  	c := NewCache()
    50  	errBoom := errors.New("boom")
    51  
    52  	assert.Equal(t, 0, len(c.status))
    53  
    54  	// IsDeleted before creation
    55  	assert.False(t, c.IsDeleted("bucket"))
    56  
    57  	// MarkOK
    58  
    59  	c.MarkOK("")
    60  	assert.Equal(t, 0, len(c.status))
    61  
    62  	// MarkOK again
    63  
    64  	c.MarkOK("bucket")
    65  	assert.Equal(t, map[string]bool{"bucket": true}, c.status)
    66  
    67  	// MarkDeleted
    68  
    69  	c.MarkDeleted("bucket")
    70  	assert.Equal(t, map[string]bool{"bucket": false}, c.status)
    71  
    72  	// MarkOK again
    73  
    74  	c.MarkOK("bucket")
    75  	assert.Equal(t, map[string]bool{"bucket": true}, c.status)
    76  
    77  	// IsDeleted after creation
    78  	assert.False(t, c.IsDeleted("bucket"))
    79  
    80  	// Create from root
    81  
    82  	err := c.Create("", nil, nil)
    83  	assert.NoError(t, err)
    84  	assert.Equal(t, map[string]bool{"bucket": true}, c.status)
    85  
    86  	// Create bucket that is already OK
    87  
    88  	err = c.Create("bucket", nil, nil)
    89  	assert.NoError(t, err)
    90  	assert.Equal(t, map[string]bool{"bucket": true}, c.status)
    91  
    92  	// Create new bucket
    93  
    94  	err = c.Create("bucket2", func() error {
    95  		return nil
    96  	}, func() (bool, error) {
    97  		return true, nil
    98  	})
    99  	assert.NoError(t, err)
   100  	assert.Equal(t, map[string]bool{"bucket": true, "bucket2": true}, c.status)
   101  
   102  	// Create bucket that has been deleted with error
   103  
   104  	c.status["bucket2"] = false // mark bucket deleted
   105  	err = c.Create("bucket2", nil, func() (bool, error) {
   106  		return false, errBoom
   107  	})
   108  	assert.Equal(t, errBoom, err)
   109  	assert.Equal(t, map[string]bool{"bucket": true, "bucket2": false}, c.status)
   110  
   111  	// Create bucket that has been deleted with no error
   112  
   113  	err = c.Create("bucket2", nil, func() (bool, error) {
   114  		return true, nil
   115  	})
   116  	assert.NoError(t, err)
   117  	assert.Equal(t, map[string]bool{"bucket": true, "bucket2": true}, c.status)
   118  
   119  	// Create a new bucket with no exists function
   120  
   121  	err = c.Create("bucket3", func() error {
   122  		return nil
   123  	}, nil)
   124  	assert.NoError(t, err)
   125  	assert.Equal(t, map[string]bool{"bucket": true, "bucket2": true, "bucket3": true}, c.status)
   126  
   127  	// Create a new bucket with no exists function with an error
   128  
   129  	err = c.Create("bucket4", func() error {
   130  		return errBoom
   131  	}, nil)
   132  	assert.Equal(t, errBoom, err)
   133  	assert.Equal(t, map[string]bool{"bucket": true, "bucket2": true, "bucket3": true}, c.status)
   134  
   135  	// Remove root
   136  
   137  	err = c.Remove("", func() error {
   138  		return nil
   139  	})
   140  	assert.NoError(t, err)
   141  	assert.Equal(t, map[string]bool{"bucket": true, "bucket2": true, "bucket3": true}, c.status)
   142  
   143  	// Remove existing bucket
   144  
   145  	err = c.Remove("bucket3", func() error {
   146  		return nil
   147  	})
   148  	assert.NoError(t, err)
   149  	assert.Equal(t, map[string]bool{"bucket": true, "bucket2": true, "bucket3": false}, c.status)
   150  
   151  	// IsDeleted after removal
   152  	assert.True(t, c.IsDeleted("bucket3"))
   153  
   154  	// Remove it again
   155  
   156  	err = c.Remove("bucket3", func() error {
   157  		return errBoom
   158  	})
   159  	assert.Equal(t, ErrAlreadyDeleted, err)
   160  	assert.Equal(t, map[string]bool{"bucket": true, "bucket2": true, "bucket3": false}, c.status)
   161  
   162  	// Remove bucket with error
   163  
   164  	err = c.Remove("bucket2", func() error {
   165  		return errBoom
   166  	})
   167  	assert.Equal(t, errBoom, err)
   168  	assert.Equal(t, map[string]bool{"bucket": true, "bucket2": true, "bucket3": false}, c.status)
   169  }