github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/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 ) 65 out, err := cmd.CombinedOutput() 66 if len(out) != 0 { 67 t.Logf("\n----------\n%s----------\n", string(out)) 68 } 69 assert.NoError(t, err, "Running restic integration tests") 70 } 71 72 // Run the tests with no path 73 runTests("") 74 //... and again with a path 75 runTests("potato/sausage/") 76 77 } 78 79 func TestMakeRemote(t *testing.T) { 80 for _, test := range []struct { 81 in, want string 82 }{ 83 {"", ""}, 84 {"/", ""}, 85 {"/data", "data"}, 86 {"/data/", "data"}, 87 {"/data/1", "data/1"}, 88 {"/data/12", "data/12/12"}, 89 {"/data/123", "data/12/123"}, 90 {"/data/123/", "data/12/123"}, 91 {"/keys", "keys"}, 92 {"/keys/1", "keys/1"}, 93 {"/keys/12", "keys/12"}, 94 {"/keys/123", "keys/123"}, 95 } { 96 got := makeRemote(test.in) 97 assert.Equal(t, test.want, got, test.in) 98 } 99 }