| 
									
										
										
										
											2015-04-26 14:46:15 +02:00
										 |  |  | package test_helper | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"flag" | 
					
						
							|  |  |  | 	"io/ioutil" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/restic/restic" | 
					
						
							|  |  |  | 	"github.com/restic/restic/backend" | 
					
						
							|  |  |  | 	"github.com/restic/restic/backend/local" | 
					
						
							|  |  |  | 	"github.com/restic/restic/server" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-03 16:36:52 +02:00
										 |  |  | var TestPassword = flag.String("test.password", "", `use this password for repositories created during tests (default: "geheim")`) | 
					
						
							| 
									
										
										
										
											2015-04-26 14:46:15 +02:00
										 |  |  | var TestCleanup = flag.Bool("test.cleanup", true, "clean up after running tests (remove local backend directory with all content)") | 
					
						
							|  |  |  | var TestTempDir = flag.String("test.tempdir", "", "use this directory for temporary storage (default: system temp dir)") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func SetupBackend(t testing.TB) *server.Server { | 
					
						
							|  |  |  | 	tempdir, err := ioutil.TempDir(*TestTempDir, "restic-test-") | 
					
						
							|  |  |  | 	OK(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create repository below temp dir | 
					
						
							|  |  |  | 	b, err := local.Create(filepath.Join(tempdir, "repo")) | 
					
						
							|  |  |  | 	OK(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-03 16:36:52 +02:00
										 |  |  | 	// set cache dir below temp dir | 
					
						
							| 
									
										
										
										
											2015-04-26 14:46:15 +02:00
										 |  |  | 	err = os.Setenv("RESTIC_CACHE", filepath.Join(tempdir, "cache")) | 
					
						
							|  |  |  | 	OK(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-03 16:36:52 +02:00
										 |  |  | 	s := server.NewServer(b) | 
					
						
							| 
									
										
										
										
											2015-05-04 20:40:17 +02:00
										 |  |  | 	OK(t, s.Init(*TestPassword)) | 
					
						
							| 
									
										
										
										
											2015-05-03 16:36:52 +02:00
										 |  |  | 	return s | 
					
						
							| 
									
										
										
										
											2015-04-26 14:46:15 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TeardownBackend(t testing.TB, s *server.Server) { | 
					
						
							|  |  |  | 	if !*TestCleanup { | 
					
						
							|  |  |  | 		l := s.Backend().(*local.Local) | 
					
						
							|  |  |  | 		t.Logf("leaving local backend at %s\n", l.Location()) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	OK(t, s.Delete()) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func SnapshotDir(t testing.TB, server *server.Server, path string, parent backend.ID) *restic.Snapshot { | 
					
						
							| 
									
										
										
										
											2015-04-29 21:41:51 -04:00
										 |  |  | 	arch := restic.NewArchiver(server) | 
					
						
							| 
									
										
										
										
											2015-04-26 14:46:15 +02:00
										 |  |  | 	sn, _, err := arch.Snapshot(nil, []string{path}, parent) | 
					
						
							|  |  |  | 	OK(t, err) | 
					
						
							|  |  |  | 	return sn | 
					
						
							|  |  |  | } |