-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
services: Implement GSP-90: Re-support Initialization Via Connection …
…String (#589) * parse pairs * remove debug log * fix service pair internal name, add tests * update - PairMap: reflect.Type -> string - move from package pairs to services - only expose NewServicerFromMap, hide parseMap * - generate pairMap variable, remove PairMap type - merge global pairs into service pairMaps - remove New*FromMap, add New*FromString * RegisterServicePairMap -> RegisterServiceSchema * implement config string * - rename config string -> connection string - mark `RegisterServiceSchema` should not call by users - remove parseListMode * update test * rename config to connStr * ServiceSchema -> Schema * - allow both <name> and <work_dir> missing - test pairs order * style updates - variable work_dir -> workDir - err1 -> parseErr - merge parse_pairs.go into new.go - parseString ->parseConnectionString * defensive programming
- Loading branch information
Showing
7 changed files
with
314 additions
and
14 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package tests | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/beyondstorage/go-storage/v4/pairs" | ||
"github.com/beyondstorage/go-storage/v4/services" | ||
. "github.com/beyondstorage/go-storage/v4/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFromString(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
connStr string | ||
pairs []Pair | ||
err error | ||
}{ | ||
{ | ||
"empty", | ||
"", | ||
nil, | ||
services.ErrConnectionStringInvalid, | ||
}, | ||
{ | ||
"simplest", | ||
"tests://", | ||
nil, | ||
nil, | ||
}, | ||
{ | ||
"only options", | ||
"tests://?size=200", | ||
[]Pair{ | ||
pairs.WithSize(200), | ||
}, | ||
nil, | ||
}, | ||
{ | ||
"only root dir", | ||
"tests:///", | ||
[]Pair{ | ||
pairs.WithWorkDir("/"), | ||
}, | ||
nil, | ||
}, | ||
{ | ||
"end with ?", | ||
"tests:///?", | ||
[]Pair{ | ||
pairs.WithWorkDir("/"), | ||
}, | ||
nil, | ||
}, | ||
{ | ||
"stupid, but valid (ignored)", | ||
"tests:///?&??&&&", | ||
[]Pair{ | ||
pairs.WithWorkDir("/"), | ||
}, | ||
nil, | ||
}, | ||
{ | ||
"value can contain all characters except &", | ||
"tests:///?string_pair=a=b:/c?d&size=200", | ||
[]Pair{ | ||
pairs.WithWorkDir("/"), | ||
WithStringPair("a=b:/c?d"), | ||
pairs.WithSize(200), | ||
}, | ||
nil, | ||
}, | ||
{ | ||
"full format", | ||
"tests://abc/tmp/tmp1?size=200&expire=100&storage_class=sc", | ||
[]Pair{ | ||
pairs.WithName("abc"), | ||
pairs.WithWorkDir("/tmp/tmp1"), | ||
pairs.WithSize(200), | ||
pairs.WithExpire(100), | ||
WithStorageClass("sc"), | ||
}, | ||
nil, | ||
}, | ||
{ | ||
"duplicate key, appear in order (finally, first will be picked)", | ||
"tests://abc/tmp/tmp1?size=200&name=def&size=300", | ||
[]Pair{ | ||
pairs.WithName("abc"), | ||
pairs.WithWorkDir("/tmp/tmp1"), | ||
pairs.WithSize(200), | ||
pairs.WithName("def"), | ||
pairs.WithSize(300), | ||
}, | ||
nil, | ||
}, | ||
{ | ||
"not registered pair", | ||
"tests://abc/tmp?not_a_pair=a", | ||
nil, | ||
services.ErrConnectionStringInvalid, | ||
}, | ||
{ | ||
"key without value is ignored (even not registered pair)", | ||
"tests://abc/tmp?not_a_pair&&", | ||
[]Pair{ | ||
pairs.WithName("abc"), | ||
pairs.WithWorkDir("/tmp"), | ||
}, | ||
nil, | ||
}, | ||
{ | ||
"not parseable pair", | ||
"tests://abc/tmp?io_call_back=a", | ||
nil, | ||
services.ErrConnectionStringInvalid, | ||
}, | ||
} | ||
|
||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
servicer, err := services.NewServicerFromString(tt.connStr) | ||
service, ok := servicer.(*Service) | ||
|
||
if tt.err == nil { | ||
assert.Nil(t, err) | ||
assert.True(t, ok) | ||
} else { | ||
assert.True(t, errors.Is(err, tt.err)) | ||
return | ||
} | ||
|
||
assert.Equal(t, service.Pairs, tt.pairs) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters