github.com/mckael/restic@v0.8.3/internal/backend/sftp/sftp_test.go (about)

     1  package sftp_test
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/restic/restic/internal/backend/sftp"
    12  	"github.com/restic/restic/internal/backend/test"
    13  	"github.com/restic/restic/internal/errors"
    14  	"github.com/restic/restic/internal/restic"
    15  	rtest "github.com/restic/restic/internal/test"
    16  )
    17  
    18  func findSFTPServerBinary() string {
    19  	for _, dir := range strings.Split(rtest.TestSFTPPath, ":") {
    20  		testpath := filepath.Join(dir, "sftp-server")
    21  		_, err := os.Stat(testpath)
    22  		if !os.IsNotExist(errors.Cause(err)) {
    23  			return testpath
    24  		}
    25  	}
    26  
    27  	return ""
    28  }
    29  
    30  var sftpServer = findSFTPServerBinary()
    31  
    32  func newTestSuite(t testing.TB) *test.Suite {
    33  	return &test.Suite{
    34  		// NewConfig returns a config for a new temporary backend that will be used in tests.
    35  		NewConfig: func() (interface{}, error) {
    36  			dir, err := ioutil.TempDir(rtest.TestTempDir, "restic-test-sftp-")
    37  			if err != nil {
    38  				t.Fatal(err)
    39  			}
    40  
    41  			t.Logf("create new backend at %v", dir)
    42  
    43  			cfg := sftp.Config{
    44  				Path:    dir,
    45  				Command: fmt.Sprintf("%q -e", sftpServer),
    46  			}
    47  			return cfg, nil
    48  		},
    49  
    50  		// CreateFn is a function that creates a temporary repository for the tests.
    51  		Create: func(config interface{}) (restic.Backend, error) {
    52  			cfg := config.(sftp.Config)
    53  			return sftp.Create(cfg)
    54  		},
    55  
    56  		// OpenFn is a function that opens a previously created temporary repository.
    57  		Open: func(config interface{}) (restic.Backend, error) {
    58  			cfg := config.(sftp.Config)
    59  			return sftp.Open(cfg)
    60  		},
    61  
    62  		// CleanupFn removes data created during the tests.
    63  		Cleanup: func(config interface{}) error {
    64  			cfg := config.(sftp.Config)
    65  			if !rtest.TestCleanupTempDirs {
    66  				t.Logf("leaving test backend dir at %v", cfg.Path)
    67  			}
    68  
    69  			rtest.RemoveAll(t, cfg.Path)
    70  			return nil
    71  		},
    72  	}
    73  }
    74  
    75  func TestBackendSFTP(t *testing.T) {
    76  	defer func() {
    77  		if t.Skipped() {
    78  			rtest.SkipDisallowed(t, "restic/backend/sftp.TestBackendSFTP")
    79  		}
    80  	}()
    81  
    82  	if sftpServer == "" {
    83  		t.Skip("sftp server binary not found")
    84  	}
    85  
    86  	newTestSuite(t).RunTests(t)
    87  }
    88  
    89  func BenchmarkBackendSFTP(t *testing.B) {
    90  	if sftpServer == "" {
    91  		t.Skip("sftp server binary not found")
    92  	}
    93  
    94  	newTestSuite(t).RunBenchmarks(t)
    95  }