mirror of https://github.com/grafana/grafana
Merge pull request #15051 from ellisvlad/13711_parse_database_config_ipv6_host
Parse database host correctly when using IPv6pull/14711/head^2
commit
ed6cca61c9
@ -0,0 +1,101 @@ |
|||||||
|
package sqlstore |
||||||
|
|
||||||
|
import ( |
||||||
|
"testing" |
||||||
|
|
||||||
|
. "github.com/smartystreets/goconvey/convey" |
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/setting" |
||||||
|
) |
||||||
|
|
||||||
|
type sqlStoreTest struct { |
||||||
|
name string |
||||||
|
dbType string |
||||||
|
dbHost string |
||||||
|
connStrValues []string |
||||||
|
} |
||||||
|
|
||||||
|
var sqlStoreTestCases = []sqlStoreTest{ |
||||||
|
{ |
||||||
|
name: "MySQL IPv4", |
||||||
|
dbType: "mysql", |
||||||
|
dbHost: "1.2.3.4:5678", |
||||||
|
connStrValues: []string{"tcp(1.2.3.4:5678)"}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
name: "Postgres IPv4", |
||||||
|
dbType: "postgres", |
||||||
|
dbHost: "1.2.3.4:5678", |
||||||
|
connStrValues: []string{"host=1.2.3.4", "port=5678"}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
name: "Postgres IPv4 (Default Port)", |
||||||
|
dbType: "postgres", |
||||||
|
dbHost: "1.2.3.4", |
||||||
|
connStrValues: []string{"host=1.2.3.4", "port=5432"}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
name: "MySQL IPv4 (Default Port)", |
||||||
|
dbType: "mysql", |
||||||
|
dbHost: "1.2.3.4", |
||||||
|
connStrValues: []string{"tcp(1.2.3.4)"}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
name: "MySQL IPv6", |
||||||
|
dbType: "mysql", |
||||||
|
dbHost: "[fe80::24e8:31b2:91df:b177]:1234", |
||||||
|
connStrValues: []string{"tcp([fe80::24e8:31b2:91df:b177]:1234)"}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
name: "Postgres IPv6", |
||||||
|
dbType: "postgres", |
||||||
|
dbHost: "[fe80::24e8:31b2:91df:b177]:1234", |
||||||
|
connStrValues: []string{"host=fe80::24e8:31b2:91df:b177", "port=1234"}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
name: "MySQL IPv6 (Default Port)", |
||||||
|
dbType: "mysql", |
||||||
|
dbHost: "::1", |
||||||
|
connStrValues: []string{"tcp(::1)"}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
name: "Postgres IPv6 (Default Port)", |
||||||
|
dbType: "postgres", |
||||||
|
dbHost: "::1", |
||||||
|
connStrValues: []string{"host=::1", "port=5432"}, |
||||||
|
}, |
||||||
|
} |
||||||
|
|
||||||
|
func TestSqlConnectionString(t *testing.T) { |
||||||
|
Convey("Testing SQL Connection Strings", t, func() { |
||||||
|
t.Helper() |
||||||
|
|
||||||
|
for _, testCase := range sqlStoreTestCases { |
||||||
|
Convey(testCase.name, func() { |
||||||
|
sqlstore := &SqlStore{} |
||||||
|
sqlstore.Cfg = makeSqlStoreTestConfig(testCase.dbType, testCase.dbHost) |
||||||
|
sqlstore.readConfig() |
||||||
|
|
||||||
|
connStr, err := sqlstore.buildConnectionString() |
||||||
|
|
||||||
|
So(err, ShouldBeNil) |
||||||
|
for _, connSubStr := range testCase.connStrValues { |
||||||
|
So(connStr, ShouldContainSubstring, connSubStr) |
||||||
|
} |
||||||
|
}) |
||||||
|
} |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
func makeSqlStoreTestConfig(dbType string, host string) *setting.Cfg { |
||||||
|
cfg := setting.NewCfg() |
||||||
|
|
||||||
|
sec, _ := cfg.Raw.NewSection("database") |
||||||
|
sec.NewKey("type", dbType) |
||||||
|
sec.NewKey("host", host) |
||||||
|
sec.NewKey("user", "user") |
||||||
|
sec.NewKey("name", "test_db") |
||||||
|
sec.NewKey("password", "pass") |
||||||
|
|
||||||
|
return cfg |
||||||
|
} |
@ -0,0 +1,24 @@ |
|||||||
|
package util |
||||||
|
|
||||||
|
import ( |
||||||
|
"net" |
||||||
|
) |
||||||
|
|
||||||
|
func SplitIpPort(ipStr string, portDefault string) (ip string, port string, err error) { |
||||||
|
ipAddr := net.ParseIP(ipStr) |
||||||
|
|
||||||
|
if ipAddr == nil { |
||||||
|
// Port was included
|
||||||
|
ip, port, err = net.SplitHostPort(ipStr) |
||||||
|
|
||||||
|
if err != nil { |
||||||
|
return "", "", err |
||||||
|
} |
||||||
|
} else { |
||||||
|
// No port was included
|
||||||
|
ip = ipAddr.String() |
||||||
|
port = portDefault |
||||||
|
} |
||||||
|
|
||||||
|
return ip, port, nil |
||||||
|
} |
@ -0,0 +1,43 @@ |
|||||||
|
package util |
||||||
|
|
||||||
|
import ( |
||||||
|
"testing" |
||||||
|
|
||||||
|
. "github.com/smartystreets/goconvey/convey" |
||||||
|
) |
||||||
|
|
||||||
|
func TestSplitIpPort(t *testing.T) { |
||||||
|
|
||||||
|
Convey("When parsing an IPv4 without explicit port", t, func() { |
||||||
|
ip, port, err := SplitIpPort("1.2.3.4", "5678") |
||||||
|
|
||||||
|
So(err, ShouldEqual, nil) |
||||||
|
So(ip, ShouldEqual, "1.2.3.4") |
||||||
|
So(port, ShouldEqual, "5678") |
||||||
|
}) |
||||||
|
|
||||||
|
Convey("When parsing an IPv6 without explicit port", t, func() { |
||||||
|
ip, port, err := SplitIpPort("::1", "5678") |
||||||
|
|
||||||
|
So(err, ShouldEqual, nil) |
||||||
|
So(ip, ShouldEqual, "::1") |
||||||
|
So(port, ShouldEqual, "5678") |
||||||
|
}) |
||||||
|
|
||||||
|
Convey("When parsing an IPv4 with explicit port", t, func() { |
||||||
|
ip, port, err := SplitIpPort("1.2.3.4:56", "78") |
||||||
|
|
||||||
|
So(err, ShouldEqual, nil) |
||||||
|
So(ip, ShouldEqual, "1.2.3.4") |
||||||
|
So(port, ShouldEqual, "56") |
||||||
|
}) |
||||||
|
|
||||||
|
Convey("When parsing an IPv6 with explicit port", t, func() { |
||||||
|
ip, port, err := SplitIpPort("[::1]:56", "78") |
||||||
|
|
||||||
|
So(err, ShouldEqual, nil) |
||||||
|
So(ip, ShouldEqual, "::1") |
||||||
|
So(port, ShouldEqual, "56") |
||||||
|
}) |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue