| 
									
										
										
										
											2017-05-01 22:55:09 +02:00
										 |  |  | package sftp_test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2020-09-19 22:01:32 +02:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2017-05-01 22:55:09 +02:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-23 14:21:03 +02:00
										 |  |  | 	"github.com/restic/restic/internal/backend/sftp" | 
					
						
							|  |  |  | 	"github.com/restic/restic/internal/backend/test" | 
					
						
							|  |  |  | 	"github.com/restic/restic/internal/errors" | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2017-05-01 22:55:09 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func findSFTPServerBinary() string { | 
					
						
							| 
									
										
										
										
											2017-10-02 15:06:39 +02:00
										 |  |  | 	for _, dir := range strings.Split(rtest.TestSFTPPath, ":") { | 
					
						
							| 
									
										
										
										
											2017-05-01 22:55:09 +02:00
										 |  |  | 		testpath := filepath.Join(dir, "sftp-server") | 
					
						
							|  |  |  | 		_, err := os.Stat(testpath) | 
					
						
							| 
									
										
										
										
											2022-06-13 20:35:37 +02:00
										 |  |  | 		if !errors.Is(err, os.ErrNotExist) { | 
					
						
							| 
									
										
										
										
											2017-05-01 22:55:09 +02:00
										 |  |  | 			return testpath | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return "" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var sftpServer = findSFTPServerBinary() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-13 21:59:00 +02:00
										 |  |  | func newTestSuite(t testing.TB) *test.Suite { | 
					
						
							|  |  |  | 	return &test.Suite{ | 
					
						
							| 
									
										
										
										
											2017-05-01 22:55:09 +02:00
										 |  |  | 		// NewConfig returns a config for a new temporary backend that will be used in tests. | 
					
						
							|  |  |  | 		NewConfig: func() (interface{}, error) { | 
					
						
							| 
									
										
										
										
											2022-12-02 19:36:43 +01:00
										 |  |  | 			dir, err := os.MkdirTemp(rtest.TestTempDir, "restic-test-sftp-") | 
					
						
							| 
									
										
										
										
											2017-05-01 22:55:09 +02:00
										 |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				t.Fatal(err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			t.Logf("create new backend at %v", dir) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			cfg := sftp.Config{ | 
					
						
							| 
									
										
										
										
											2021-08-07 19:56:59 +02:00
										 |  |  | 				Path:        dir, | 
					
						
							|  |  |  | 				Command:     fmt.Sprintf("%q -e", sftpServer), | 
					
						
							|  |  |  | 				Connections: 5, | 
					
						
							| 
									
										
										
										
											2017-05-01 22:55:09 +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.(sftp.Config) | 
					
						
							| 
									
										
										
										
											2020-09-19 22:01:32 +02:00
										 |  |  | 			return sftp.Create(context.TODO(), cfg) | 
					
						
							| 
									
										
										
										
											2017-05-01 22:55:09 +02:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// OpenFn is a function that opens a previously created temporary repository. | 
					
						
							|  |  |  | 		Open: func(config interface{}) (restic.Backend, error) { | 
					
						
							|  |  |  | 			cfg := config.(sftp.Config) | 
					
						
							| 
									
										
										
										
											2020-09-19 22:01:32 +02:00
										 |  |  | 			return sftp.Open(context.TODO(), cfg) | 
					
						
							| 
									
										
										
										
											2017-05-01 22:55:09 +02:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// CleanupFn removes data created during the tests. | 
					
						
							|  |  |  | 		Cleanup: func(config interface{}) error { | 
					
						
							|  |  |  | 			cfg := config.(sftp.Config) | 
					
						
							| 
									
										
										
										
											2017-10-02 15:06:39 +02:00
										 |  |  | 			if !rtest.TestCleanupTempDirs { | 
					
						
							| 
									
										
										
										
											2017-05-01 22:55:09 +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) | 
					
						
							| 
									
										
										
										
											2017-05-01 22:55:09 +02:00
										 |  |  | 			return nil | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-13 21:59:00 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestBackendSFTP(t *testing.T) { | 
					
						
							|  |  |  | 	defer func() { | 
					
						
							|  |  |  | 		if t.Skipped() { | 
					
						
							| 
									
										
										
										
											2017-10-02 15:06:39 +02:00
										 |  |  | 			rtest.SkipDisallowed(t, "restic/backend/sftp.TestBackendSFTP") | 
					
						
							| 
									
										
										
										
											2017-05-13 21:59:00 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if sftpServer == "" { | 
					
						
							|  |  |  | 		t.Skip("sftp server binary not found") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	newTestSuite(t).RunTests(t) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func BenchmarkBackendSFTP(t *testing.B) { | 
					
						
							|  |  |  | 	if sftpServer == "" { | 
					
						
							|  |  |  | 		t.Skip("sftp server binary not found") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-01 22:55:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-13 21:59:00 +02:00
										 |  |  | 	newTestSuite(t).RunBenchmarks(t) | 
					
						
							| 
									
										
										
										
											2017-05-01 22:55:09 +02:00
										 |  |  | } |