Commit 33b5df20 authored by Kamil Dziedzic's avatar Kamil Dziedzic Committed by Ben Kochie

Ignore boolean keys in my.cnf. (#283)

Below is valid MySQL config however mysqld_exporter fails to read it,
because of the boolean keys.
Config:
```
[client]
user = root
password = abc123

[mysql]
skip-auto-rehash
```
Signed-off-by: 's avatarKamil Dziedzic <arvenil@klecza.pl>
parent d560e62e
......@@ -65,7 +65,11 @@ var scrapers = map[collector.Scraper]bool{
func parseMycnf(config interface{}) (string, error) {
var dsn string
cfg, err := ini.Load(config)
opts := ini.LoadOptions{
// MySQL ini file can have boolean keys.
AllowBooleanKeys: true,
}
cfg, err := ini.LoadSources(opts, config)
if err != nil {
return dsn, fmt.Errorf("failed reading ini file: %s", err)
}
......
......@@ -54,6 +54,14 @@ func TestParseMycnf(t *testing.T) {
host = 1.2.3.4
port = 3307
`
ignoreBooleanKeys = `
[client]
user = root
password = abc123
[mysql]
skip-auto-rehash
`
badConfig = `
[client]
user = root
......@@ -67,10 +75,7 @@ func TestParseMycnf(t *testing.T) {
[hello]
world = ismine
`
badConfig4 = `
[hello]
world
`
badConfig4 = `[hello`
)
convey.Convey("Various .my.cnf configurations", t, func() {
convey.Convey("Local tcp connection", func() {
......@@ -93,21 +98,25 @@ func TestParseMycnf(t *testing.T) {
dsn, _ := parseMycnf([]byte(remoteConfig))
convey.So(dsn, convey.ShouldEqual, "dude:nopassword@tcp(1.2.3.4:3307)/")
})
convey.Convey("Ignore boolean keys", func() {
dsn, _ := parseMycnf([]byte(ignoreBooleanKeys))
convey.So(dsn, convey.ShouldEqual, "root:abc123@tcp(localhost:3306)/")
})
convey.Convey("Missed user", func() {
_, err := parseMycnf([]byte(badConfig))
convey.So(err, convey.ShouldNotBeNil)
convey.So(err, convey.ShouldBeError, fmt.Errorf("no user or password specified under [client] in %s", badConfig))
})
convey.Convey("Missed password", func() {
_, err := parseMycnf([]byte(badConfig2))
convey.So(err, convey.ShouldNotBeNil)
convey.So(err, convey.ShouldBeError, fmt.Errorf("no user or password specified under [client] in %s", badConfig2))
})
convey.Convey("No [client] section", func() {
_, err := parseMycnf([]byte(badConfig3))
convey.So(err, convey.ShouldNotBeNil)
convey.So(err, convey.ShouldBeError, fmt.Errorf("no user or password specified under [client] in %s", badConfig3))
})
convey.Convey("Invalid config", func() {
_, err := parseMycnf([]byte(badConfig4))
convey.So(err, convey.ShouldNotBeNil)
convey.So(err, convey.ShouldBeError, fmt.Errorf("failed reading ini file: unclosed section: %s", badConfig4))
})
})
}
......
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