Commit 2d82ab3c 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 e97ac850
......@@ -598,6 +598,73 @@ 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}
i := NEOUrlInfo{}
// Most simple valid URI
testParseURL(t, "neo://127.0.0.1/test", &i, o, o)
// With 2 masters
i.masterAddr = "127.0.0.1,127.0.0.2"
testParseURL(t, "neo://127.0.0.1,127.0.0.2/test", &i, o, o)
// With ssl
i.netcfg = neonet.Config{CA: "ca", Cert: "cert", Key: "key"}
testParseURL(t, "neos://ca=ca;cert=cert;key=key@127.0.0.1/test", &i, o, o)
// With query parameters
testParseURL(t, "neo://127.0.0.1/test?read-only=true&compress=true&logfile=n.log&cache-size=256", &i, zodb.DriverOptions{}, o)
}
// testParseURL tests one zurl for correctness by comparing its parsed
// data with a user provided NEOUrlInfo. It also checks whether parameters
// are propagated from a URL to DriverOptions.
//
// Hint: testParseURL automatically sets default to undefined fields of the
// user provided NEOUrlInfo and also resets all fields to their null
// value after the test is finished.
func testParseURL(t *testing.T, zurl string, urlinfoOk *NEOUrlInfo, opt zodb.DriverOptions, optOk zodb.DriverOptions) {
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 NEOUrlInfo
if urlinfo.masterAddr != urlinfoOk.masterAddr {
t.Fatalf("got %q, wanted %q", urlinfo.masterAddr, urlinfoOk.masterAddr)
}
if urlinfo.name != urlinfoOk.name {
t.Fatalf("got %q, wanted %q", urlinfo.name, urlinfoOk.name)
}
if urlinfo.netcfg != urlinfoOk.netcfg {
t.Fatalf("got %q, wanted %q", urlinfo.netcfg, urlinfoOk.netcfg)
}
// Test DriverOptions
if opt != optOk {
t.Fatalf("got %v, wanted %v", opt, optOk)
}
urlinfoOk.reset()
}
// setDefault sets default test values for a NEOUrlInfo
func (urlinfo *NEOUrlInfo) setDefault() {
if urlinfo.masterAddr == "" {
urlinfo.masterAddr = "127.0.0.1"
}
if urlinfo.name == "" {
urlinfo.name = "test"
}
}
// reset resets null values to all fields of a NEOUrlInfo
func (urlinfo *NEOUrlInfo) reset() {
urlinfo.masterAddr = ""
urlinfo.name = ""
urlinfo.netcfg = neonet.Config{}
}
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