github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/cmd/serve/restic/restic_test.go (about) 1 // Serve restic tests set up a server and run the integration tests 2 // for restic against it. 3 4 package restic 5 6 import ( 7 "context" 8 "os" 9 "os/exec" 10 "testing" 11 12 _ "github.com/rclone/rclone/backend/all" 13 "github.com/rclone/rclone/cmd/serve/httplib" 14 "github.com/rclone/rclone/fstest" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 const ( 19 testBindAddress = "localhost:0" 20 resticSource = "../../../../../restic/restic" 21 ) 22 23 // TestRestic runs the restic server then runs the unit tests for the 24 // restic remote against it. 25 func TestRestic(t *testing.T) { 26 _, err := os.Stat(resticSource) 27 if err != nil { 28 t.Skipf("Skipping test as restic source not found: %v", err) 29 } 30 31 opt := httplib.DefaultOpt 32 opt.ListenAddr = testBindAddress 33 34 fstest.Initialise() 35 36 fremote, _, clean, err := fstest.RandomRemote() 37 assert.NoError(t, err) 38 defer clean() 39 40 err = fremote.Mkdir(context.Background(), "") 41 assert.NoError(t, err) 42 43 // Start the server 44 w := newServer(fremote, &opt) 45 assert.NoError(t, w.Serve()) 46 defer func() { 47 w.Close() 48 w.Wait() 49 }() 50 51 // Change directory to run the tests 52 err = os.Chdir(resticSource) 53 assert.NoError(t, err, "failed to cd to restic source code") 54 55 // Run the restic tests 56 runTests := func(path string) { 57 args := []string{"test", "./internal/backend/rest", "-run", "TestBackendRESTExternalServer", "-count=1"} 58 if testing.Verbose() { 59 args = append(args, "-v") 60 } 61 cmd := exec.Command("go", args...) 62 cmd.Env = append(os.Environ(), 63 "RESTIC_TEST_REST_REPOSITORY=rest:"+w.Server.URL()+path, 64 "GO111MODULE=on", 65 ) 66 out, err := cmd.CombinedOutput() 67 if len(out) != 0 { 68 t.Logf("\n----------\n%s----------\n", string(out)) 69 } 70 assert.NoError(t, err, "Running restic integration tests") 71 } 72 73 // Run the tests with no path 74 runTests("") 75 //... and again with a path 76 runTests("potato/sausage/") 77 78 } 79 80 func TestMakeRemote(t *testing.T) { 81 for _, test := range []struct { 82 in, want string 83 }{ 84 {"", ""}, 85 {"/", ""}, 86 {"/data", "data"}, 87 {"/data/", "data"}, 88 {"/data/1", "data/1"}, 89 {"/data/12", "data/12/12"}, 90 {"/data/123", "data/12/123"}, 91 {"/data/123/", "data/12/123"}, 92 {"/keys", "keys"}, 93 {"/keys/1", "keys/1"}, 94 {"/keys/12", "keys/12"}, 95 {"/keys/123", "keys/123"}, 96 } { 97 got := makeRemote(test.in) 98 assert.Equal(t, test.want, got, test.in) 99 } 100 }