Commit 4ec03295 authored by Levin Zimmermann's avatar Levin Zimmermann

client_test: Add tests for NEO URI parser

This test was missing so far. Particularly recent changes of the
NEO URI scheme [1], but also problems with valid old URI [2] stressed
out the necessity for comprehensive NEO URI parser tests.

[1] kirr/neo@4c9414ea
[2] 573514c6 (comment 184417)
parent c3d8bf21
......@@ -598,6 +598,68 @@ func TestWatch(t *testing.T) {
})
}
// TestParseURL ensures that parsing NEO URL works as expected (= following the
// scheme neo(s)://[credentials@]master1,master2,...,masterN/name?options)
func TestParseURL(t *testing.T) {
o := zodb.DriverOptions{ReadOnly: true}
// Most simple valid URI
testParseURL(t, "neo://127.0.0.1/test", urlInfo{}, o, o)
// With 2 masters
testParseURL(t, "neo://127.0.0.1,127.0.0.2/test", urlInfo{masterAddr: "127.0.0.1,127.0.0.2"}, o, o)
// With ssl
u := "neos://ca=ca;cert=cert;key=key@127.0.0.1/test"
testParseURL(t, u, urlInfo{netcfg: neonet.Config{CA: "ca", Cert: "cert", Key: "key"}}, o, o)
// With query parameters
u = "neo://127.0.0.1/test?read-only=true&compress=true&logfile=n.log&cache-size=256"
testParseURL(t, u, urlInfo{}, zodb.DriverOptions{}, o)
}
// testParseURL tests one zurl for correctness by comparing its parsed
// data with a user provided urlInfo. It also checks whether parameters
// are propagated from a URL to DriverOptions.
//
// Hint: testParseURL automatically sets default to undefined fields of the
// user provided urlInfo and also resets all fields to their null
// value after the test is finished.
func testParseURL(t *testing.T, zurl string, urlinfoOk urlInfo, opt zodb.DriverOptions, optOk zodb.DriverOptions) {
e := func (format string, a ...interface{}) {
t.Errorf(zurl + ": " + format, a...)
}
urlinfoOk.setDefault()
u, err := url.Parse(zurl)
if err != nil {
t.Fatal(err)
}
urlinfo, err := parseURL(context.Background(), u, &opt)
if err != nil {
t.Fatal(err)
}
// Test urlInfo
if urlinfo.masterAddr != urlinfoOk.masterAddr {
e("masterAddr: got %q, want %q", urlinfo.masterAddr, urlinfoOk.masterAddr)
}
if urlinfo.name != urlinfoOk.name {
e("name: got %q, want %q", urlinfo.name, urlinfoOk.name)
}
if urlinfo.netcfg != urlinfoOk.netcfg {
e("netcfg: got %q, want %q", urlinfo.netcfg, urlinfoOk.netcfg)
}
// Test DriverOptions
if opt != optOk {
e("opt: got %v, want %v", opt, optOk)
}
}
// setDefault sets default test values for a urlInfo
func (urlinfo *urlInfo) setDefault() {
if urlinfo.masterAddr == "" {
urlinfo.masterAddr = "127.0.0.1"
}
if urlinfo.name == "" {
urlinfo.name = "test"
}
}
func neoOpen(zurl string, opt *zodb.DriverOptions) (_ *Client, at0 zodb.Tid, err error) {
defer xerr.Contextf(&err, "openneo %s", zurl)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment