github.com/ncw/rclone@v1.48.1-0.20190724201158-a35aa1360e3e/fs/rc/config_test.go (about) 1 package rc 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/pkg/errors" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func clearOptionBlock() { 14 optionBlock = map[string]interface{}{} 15 } 16 17 var testOptions = struct { 18 String string 19 Int int 20 }{ 21 String: "hello", 22 Int: 42, 23 } 24 25 func TestAddOption(t *testing.T) { 26 defer clearOptionBlock() 27 assert.Equal(t, len(optionBlock), 0) 28 AddOption("potato", &testOptions) 29 assert.Equal(t, len(optionBlock), 1) 30 assert.Equal(t, len(optionReload), 0) 31 assert.Equal(t, &testOptions, optionBlock["potato"]) 32 } 33 34 func TestAddOptionReload(t *testing.T) { 35 defer clearOptionBlock() 36 assert.Equal(t, len(optionBlock), 0) 37 reload := func() error { return nil } 38 AddOptionReload("potato", &testOptions, reload) 39 assert.Equal(t, len(optionBlock), 1) 40 assert.Equal(t, len(optionReload), 1) 41 assert.Equal(t, &testOptions, optionBlock["potato"]) 42 assert.Equal(t, fmt.Sprintf("%p", reload), fmt.Sprintf("%p", optionReload["potato"])) 43 } 44 45 func TestOptionsBlocks(t *testing.T) { 46 defer clearOptionBlock() 47 AddOption("potato", &testOptions) 48 call := Calls.Get("options/blocks") 49 require.NotNil(t, call) 50 in := Params{} 51 out, err := call.Fn(context.Background(), in) 52 require.NoError(t, err) 53 require.NotNil(t, out) 54 assert.Equal(t, Params{"options": []string{"potato"}}, out) 55 } 56 57 func TestOptionsGet(t *testing.T) { 58 defer clearOptionBlock() 59 AddOption("potato", &testOptions) 60 call := Calls.Get("options/get") 61 require.NotNil(t, call) 62 in := Params{} 63 out, err := call.Fn(context.Background(), in) 64 require.NoError(t, err) 65 require.NotNil(t, out) 66 assert.Equal(t, Params{"potato": &testOptions}, out) 67 } 68 69 func TestOptionsSet(t *testing.T) { 70 defer clearOptionBlock() 71 var reloaded int 72 AddOptionReload("potato", &testOptions, func() error { 73 if reloaded > 0 { 74 return errors.New("error while reloading") 75 } 76 reloaded++ 77 return nil 78 }) 79 call := Calls.Get("options/set") 80 require.NotNil(t, call) 81 82 in := Params{ 83 "potato": Params{ 84 "Int": 50, 85 }, 86 } 87 out, err := call.Fn(context.Background(), in) 88 require.NoError(t, err) 89 require.Nil(t, out) 90 assert.Equal(t, 50, testOptions.Int) 91 assert.Equal(t, "hello", testOptions.String) 92 assert.Equal(t, 1, reloaded) 93 94 // error from reload 95 _, err = call.Fn(context.Background(), in) 96 require.Error(t, err) 97 assert.Contains(t, err.Error(), "error while reloading") 98 99 // unknown option block 100 in = Params{ 101 "sausage": Params{ 102 "Int": 50, 103 }, 104 } 105 _, err = call.Fn(context.Background(), in) 106 require.Error(t, err) 107 assert.Contains(t, err.Error(), "unknown option block") 108 109 // bad shape 110 in = Params{ 111 "potato": []string{"a", "b"}, 112 } 113 _, err = call.Fn(context.Background(), in) 114 require.Error(t, err) 115 assert.Contains(t, err.Error(), "failed to write options") 116 117 }