github.com/mckael/restic@v0.8.3/internal/backend/location/location_test.go (about)

     1  package location
     2  
     3  import (
     4  	"net/url"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/restic/restic/internal/backend/b2"
     9  	"github.com/restic/restic/internal/backend/local"
    10  	"github.com/restic/restic/internal/backend/rest"
    11  	"github.com/restic/restic/internal/backend/s3"
    12  	"github.com/restic/restic/internal/backend/sftp"
    13  	"github.com/restic/restic/internal/backend/swift"
    14  )
    15  
    16  func parseURL(s string) *url.URL {
    17  	u, err := url.Parse(s)
    18  	if err != nil {
    19  		panic(err)
    20  	}
    21  
    22  	return u
    23  }
    24  
    25  var parseTests = []struct {
    26  	s string
    27  	u Location
    28  }{
    29  	{
    30  		"local:/srv/repo",
    31  		Location{Scheme: "local",
    32  			Config: local.Config{
    33  				Path: "/srv/repo",
    34  			},
    35  		},
    36  	},
    37  	{
    38  		"local:dir1/dir2",
    39  		Location{Scheme: "local",
    40  			Config: local.Config{
    41  				Path: "dir1/dir2",
    42  			},
    43  		},
    44  	},
    45  	{
    46  		"local:dir1/dir2",
    47  		Location{Scheme: "local",
    48  			Config: local.Config{
    49  				Path: "dir1/dir2",
    50  			},
    51  		},
    52  	},
    53  	{
    54  		"dir1/dir2",
    55  		Location{Scheme: "local",
    56  			Config: local.Config{
    57  				Path: "dir1/dir2",
    58  			},
    59  		},
    60  	},
    61  	{
    62  		"/dir1/dir2",
    63  		Location{Scheme: "local",
    64  			Config: local.Config{
    65  				Path: "/dir1/dir2",
    66  			},
    67  		},
    68  	},
    69  	{
    70  		"local:../dir1/dir2",
    71  		Location{Scheme: "local",
    72  			Config: local.Config{
    73  				Path: "../dir1/dir2",
    74  			},
    75  		},
    76  	},
    77  	{
    78  		"/dir1/dir2",
    79  		Location{Scheme: "local",
    80  			Config: local.Config{
    81  				Path: "/dir1/dir2",
    82  			},
    83  		},
    84  	},
    85  	{
    86  		"/dir1:foobar/dir2",
    87  		Location{Scheme: "local",
    88  			Config: local.Config{
    89  				Path: "/dir1:foobar/dir2",
    90  			},
    91  		},
    92  	},
    93  	{
    94  		`\dir1\foobar\dir2`,
    95  		Location{Scheme: "local",
    96  			Config: local.Config{
    97  				Path: `\dir1\foobar\dir2`,
    98  			},
    99  		},
   100  	},
   101  	{
   102  		`c:\dir1\foobar\dir2`,
   103  		Location{Scheme: "local",
   104  			Config: local.Config{
   105  				Path: `c:\dir1\foobar\dir2`,
   106  			},
   107  		},
   108  	},
   109  	{
   110  		`C:\Users\appveyor\AppData\Local\Temp\1\restic-test-879453535\repo`,
   111  		Location{Scheme: "local",
   112  			Config: local.Config{
   113  				Path: `C:\Users\appveyor\AppData\Local\Temp\1\restic-test-879453535\repo`,
   114  			},
   115  		},
   116  	},
   117  	{
   118  		`c:/dir1/foobar/dir2`,
   119  		Location{Scheme: "local",
   120  			Config: local.Config{
   121  				Path: `c:/dir1/foobar/dir2`,
   122  			},
   123  		},
   124  	},
   125  	{
   126  		"sftp:user@host:/srv/repo",
   127  		Location{Scheme: "sftp",
   128  			Config: sftp.Config{
   129  				User: "user",
   130  				Host: "host",
   131  				Path: "/srv/repo",
   132  			},
   133  		},
   134  	},
   135  	{
   136  		"sftp:host:/srv/repo",
   137  		Location{Scheme: "sftp",
   138  			Config: sftp.Config{
   139  				User: "",
   140  				Host: "host",
   141  				Path: "/srv/repo",
   142  			},
   143  		},
   144  	},
   145  	{
   146  		"sftp://user@host/srv/repo",
   147  		Location{Scheme: "sftp",
   148  			Config: sftp.Config{
   149  				User: "user",
   150  				Host: "host",
   151  				Path: "srv/repo",
   152  			},
   153  		},
   154  	},
   155  	{
   156  		"sftp://user@host//srv/repo",
   157  		Location{Scheme: "sftp",
   158  			Config: sftp.Config{
   159  				User: "user",
   160  				Host: "host",
   161  				Path: "/srv/repo",
   162  			},
   163  		},
   164  	},
   165  
   166  	{
   167  		"s3://eu-central-1/bucketname",
   168  		Location{Scheme: "s3",
   169  			Config: s3.Config{
   170  				Endpoint:    "eu-central-1",
   171  				Bucket:      "bucketname",
   172  				Prefix:      "",
   173  				Connections: 5,
   174  			},
   175  		},
   176  	},
   177  	{
   178  		"s3://hostname.foo/bucketname",
   179  		Location{Scheme: "s3",
   180  			Config: s3.Config{
   181  				Endpoint:    "hostname.foo",
   182  				Bucket:      "bucketname",
   183  				Prefix:      "",
   184  				Connections: 5,
   185  			},
   186  		},
   187  	},
   188  	{
   189  		"s3://hostname.foo/bucketname/prefix/directory",
   190  		Location{Scheme: "s3",
   191  			Config: s3.Config{
   192  				Endpoint:    "hostname.foo",
   193  				Bucket:      "bucketname",
   194  				Prefix:      "prefix/directory",
   195  				Connections: 5,
   196  			},
   197  		},
   198  	},
   199  	{
   200  		"s3:eu-central-1/repo",
   201  		Location{Scheme: "s3",
   202  			Config: s3.Config{
   203  				Endpoint:    "eu-central-1",
   204  				Bucket:      "repo",
   205  				Prefix:      "",
   206  				Connections: 5,
   207  			},
   208  		},
   209  	},
   210  	{
   211  		"s3:eu-central-1/repo/prefix/directory",
   212  		Location{Scheme: "s3",
   213  			Config: s3.Config{
   214  				Endpoint:    "eu-central-1",
   215  				Bucket:      "repo",
   216  				Prefix:      "prefix/directory",
   217  				Connections: 5,
   218  			},
   219  		},
   220  	},
   221  	{
   222  		"s3:https://hostname.foo/repo",
   223  		Location{Scheme: "s3",
   224  			Config: s3.Config{
   225  				Endpoint:    "hostname.foo",
   226  				Bucket:      "repo",
   227  				Prefix:      "",
   228  				Connections: 5,
   229  			},
   230  		},
   231  	},
   232  	{
   233  		"s3:https://hostname.foo/repo/prefix/directory",
   234  		Location{Scheme: "s3",
   235  			Config: s3.Config{
   236  				Endpoint:    "hostname.foo",
   237  				Bucket:      "repo",
   238  				Prefix:      "prefix/directory",
   239  				Connections: 5,
   240  			},
   241  		},
   242  	},
   243  	{
   244  		"s3:http://hostname.foo/repo",
   245  		Location{Scheme: "s3",
   246  			Config: s3.Config{
   247  				Endpoint:    "hostname.foo",
   248  				Bucket:      "repo",
   249  				Prefix:      "",
   250  				UseHTTP:     true,
   251  				Connections: 5,
   252  			},
   253  		},
   254  	},
   255  	{
   256  		"swift:container17:/",
   257  		Location{Scheme: "swift",
   258  			Config: swift.Config{
   259  				Container:   "container17",
   260  				Prefix:      "",
   261  				Connections: 5,
   262  			},
   263  		},
   264  	},
   265  	{
   266  		"swift:container17:/prefix97",
   267  		Location{Scheme: "swift",
   268  			Config: swift.Config{
   269  				Container:   "container17",
   270  				Prefix:      "prefix97",
   271  				Connections: 5,
   272  			},
   273  		},
   274  	},
   275  	{
   276  		"rest:http://hostname.foo:1234/",
   277  		Location{Scheme: "rest",
   278  			Config: rest.Config{
   279  				URL:         parseURL("http://hostname.foo:1234/"),
   280  				Connections: 5,
   281  			},
   282  		},
   283  	},
   284  	{
   285  		"b2:bucketname:/prefix", Location{Scheme: "b2",
   286  			Config: b2.Config{
   287  				Bucket:      "bucketname",
   288  				Prefix:      "prefix",
   289  				Connections: 5,
   290  			},
   291  		},
   292  	},
   293  	{
   294  		"b2:bucketname", Location{Scheme: "b2",
   295  			Config: b2.Config{
   296  				Bucket:      "bucketname",
   297  				Prefix:      "",
   298  				Connections: 5,
   299  			},
   300  		},
   301  	},
   302  }
   303  
   304  func TestParse(t *testing.T) {
   305  	for i, test := range parseTests {
   306  		t.Run(test.s, func(t *testing.T) {
   307  			u, err := Parse(test.s)
   308  			if err != nil {
   309  				t.Fatalf("unexpected error: %v", err)
   310  			}
   311  
   312  			if test.u.Scheme != u.Scheme {
   313  				t.Errorf("test %d: scheme does not match, want %q, got %q",
   314  					i, test.u.Scheme, u.Scheme)
   315  			}
   316  
   317  			if !reflect.DeepEqual(test.u.Config, u.Config) {
   318  				t.Errorf("test %d: cfg map does not match, want:\n  %#v\ngot: \n  %#v",
   319  					i, test.u.Config, u.Config)
   320  			}
   321  		})
   322  	}
   323  }
   324  
   325  func TestInvalidScheme(t *testing.T) {
   326  	var invalidSchemes = []string{
   327  		"foobar:xxx",
   328  		"foobar:/dir/dir2",
   329  	}
   330  
   331  	for _, s := range invalidSchemes {
   332  		t.Run(s, func(t *testing.T) {
   333  			_, err := Parse(s)
   334  			if err == nil {
   335  				t.Fatalf("error for invalid location %q not found", s)
   336  			}
   337  		})
   338  	}
   339  }