| 
									
										
										
										
											2016-01-23 17:08:03 +01:00
										 |  |  | package local_test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2020-09-19 22:01:32 +02:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2016-01-23 17:08:03 +01:00
										 |  |  | 	"io/ioutil" | 
					
						
							| 
									
										
										
										
											2017-08-25 21:26:04 +02:00
										 |  |  | 	"os" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2017-05-01 22:46:51 +02:00
										 |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2016-01-23 17:08:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-23 14:21:03 +02:00
										 |  |  | 	"github.com/restic/restic/internal/backend/local" | 
					
						
							|  |  |  | 	"github.com/restic/restic/internal/backend/test" | 
					
						
							| 
									
										
										
										
											2017-07-24 17:42:25 +02:00
										 |  |  | 	"github.com/restic/restic/internal/restic" | 
					
						
							| 
									
										
										
										
											2017-10-02 15:06:39 +02:00
										 |  |  | 	rtest "github.com/restic/restic/internal/test" | 
					
						
							| 
									
										
										
										
											2016-01-23 17:08:03 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-13 21:45:41 +02:00
										 |  |  | func newTestSuite(t testing.TB) *test.Suite { | 
					
						
							|  |  |  | 	return &test.Suite{ | 
					
						
							| 
									
										
										
										
											2017-05-01 22:46:51 +02:00
										 |  |  | 		// NewConfig returns a config for a new temporary backend that will be used in tests. | 
					
						
							|  |  |  | 		NewConfig: func() (interface{}, error) { | 
					
						
							| 
									
										
										
										
											2017-10-02 15:06:39 +02:00
										 |  |  | 			dir, err := ioutil.TempDir(rtest.TestTempDir, "restic-test-local-") | 
					
						
							| 
									
										
										
										
											2017-05-01 22:46:51 +02:00
										 |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				t.Fatal(err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			t.Logf("create new backend at %v", dir) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			cfg := local.Config{ | 
					
						
							| 
									
										
										
										
											2021-08-07 19:50:00 +02:00
										 |  |  | 				Path:        dir, | 
					
						
							|  |  |  | 				Connections: 2, | 
					
						
							| 
									
										
										
										
											2017-05-01 22:46:51 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			return cfg, nil | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// CreateFn is a function that creates a temporary repository for the tests. | 
					
						
							|  |  |  | 		Create: func(config interface{}) (restic.Backend, error) { | 
					
						
							|  |  |  | 			cfg := config.(local.Config) | 
					
						
							| 
									
										
										
										
											2020-09-19 22:01:32 +02:00
										 |  |  | 			return local.Create(context.TODO(), cfg) | 
					
						
							| 
									
										
										
										
											2017-05-01 22:46:51 +02:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// OpenFn is a function that opens a previously created temporary repository. | 
					
						
							|  |  |  | 		Open: func(config interface{}) (restic.Backend, error) { | 
					
						
							|  |  |  | 			cfg := config.(local.Config) | 
					
						
							| 
									
										
										
										
											2020-09-19 22:01:32 +02:00
										 |  |  | 			return local.Open(context.TODO(), cfg) | 
					
						
							| 
									
										
										
										
											2017-05-01 22:46:51 +02:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// CleanupFn removes data created during the tests. | 
					
						
							|  |  |  | 		Cleanup: func(config interface{}) error { | 
					
						
							|  |  |  | 			cfg := config.(local.Config) | 
					
						
							| 
									
										
										
										
											2017-10-02 15:06:39 +02:00
										 |  |  | 			if !rtest.TestCleanupTempDirs { | 
					
						
							| 
									
										
										
										
											2017-05-01 22:46:51 +02:00
										 |  |  | 				t.Logf("leaving test backend dir at %v", cfg.Path) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-02 15:06:39 +02:00
										 |  |  | 			rtest.RemoveAll(t, cfg.Path) | 
					
						
							| 
									
										
										
										
											2016-01-23 17:08:03 +01:00
										 |  |  | 			return nil | 
					
						
							| 
									
										
										
										
											2017-05-01 22:46:51 +02:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-01-23 17:08:03 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-13 21:45:41 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestBackend(t *testing.T) { | 
					
						
							|  |  |  | 	newTestSuite(t).RunTests(t) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-05-01 22:46:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-13 21:45:41 +02:00
										 |  |  | func BenchmarkBackend(t *testing.B) { | 
					
						
							|  |  |  | 	newTestSuite(t).RunBenchmarks(t) | 
					
						
							| 
									
										
										
										
											2016-01-23 17:08:03 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-08-25 21:26:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | func readdirnames(t testing.TB, dir string) []string { | 
					
						
							|  |  |  | 	f, err := os.Open(dir) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	entries, err := f.Readdirnames(-1) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = f.Close() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return entries | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func empty(t testing.TB, dir string) { | 
					
						
							|  |  |  | 	entries := readdirnames(t, dir) | 
					
						
							|  |  |  | 	if len(entries) != 0 { | 
					
						
							|  |  |  | 		t.Fatalf("directory %v is not empty, contains: %v", dir, entries) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func openclose(t testing.TB, dir string) { | 
					
						
							|  |  |  | 	cfg := local.Config{Path: dir} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-19 22:01:32 +02:00
										 |  |  | 	be, err := local.Open(context.TODO(), cfg) | 
					
						
							| 
									
										
										
										
											2017-08-25 21:26:04 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Logf("Open returned error %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if be != nil { | 
					
						
							|  |  |  | 		err = be.Close() | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Logf("Close returned error %v", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-11 21:34:00 +02:00
										 |  |  | func mkdir(t testing.TB, dir string) { | 
					
						
							|  |  |  | 	err := os.Mkdir(dir, 0700) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func removeAll(t testing.TB, dir string) { | 
					
						
							|  |  |  | 	err := os.RemoveAll(dir) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-25 21:26:04 +02:00
										 |  |  | func TestOpenNotExistingDirectory(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-10-02 15:06:39 +02:00
										 |  |  | 	dir, cleanup := rtest.TempDir(t) | 
					
						
							| 
									
										
										
										
											2017-08-25 21:26:04 +02:00
										 |  |  | 	defer cleanup() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// local.Open must not create any files dirs in the repo | 
					
						
							|  |  |  | 	openclose(t, filepath.Join(dir, "repo")) | 
					
						
							|  |  |  | 	empty(t, dir) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	openclose(t, dir) | 
					
						
							|  |  |  | 	empty(t, dir) | 
					
						
							| 
									
										
										
										
											2017-09-11 21:34:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	mkdir(t, filepath.Join(dir, "data")) | 
					
						
							|  |  |  | 	openclose(t, dir) | 
					
						
							|  |  |  | 	removeAll(t, filepath.Join(dir, "data")) | 
					
						
							|  |  |  | 	empty(t, dir) | 
					
						
							| 
									
										
										
										
											2017-08-25 21:26:04 +02:00
										 |  |  | } |