1 Star 0 Fork 0

wl4g-collect / mysqld_exporter

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
mysqld_exporter_test.go 7.51 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright 2018 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"reflect"
"runtime"
"strings"
"syscall"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestParseMycnf(t *testing.T) {
const (
tcpConfig = `
[client]
user = root
password = abc123
`
tcpConfig2 = `
[client]
user = root
password = abc123
port = 3308
`
clientAuthConfig = `
[client]
user = root
port = 3308
ssl-ca = ca.crt
ssl-cert = tls.crt
ssl-key = tls.key
`
socketConfig = `
[client]
user = user
password = pass
socket = /var/lib/mysql/mysql.sock
`
socketConfig2 = `
[client]
user = dude
password = nopassword
# host and port will not be used because of socket presence
host = 1.2.3.4
port = 3307
socket = /var/lib/mysql/mysql.sock
`
remoteConfig = `
[client]
user = dude
password = nopassword
host = 1.2.3.4
port = 3307
`
ignoreBooleanKeys = `
[client]
user = root
password = abc123
[mysql]
skip-auto-rehash
`
badConfig = `
[client]
user = root
`
badConfig2 = `
[client]
password = abc123
socket = /var/lib/mysql/mysql.sock
`
badConfig3 = `
[hello]
world = ismine
`
badConfig4 = `[hello`
)
convey.Convey("Various .my.cnf configurations", t, func() {
convey.Convey("Local tcp connection", func() {
dsn, _ := parseMycnf([]byte(tcpConfig))
convey.So(dsn, convey.ShouldEqual, "root:abc123@tcp(localhost:3306)/")
})
convey.Convey("Local tcp connection on non-default port", func() {
dsn, _ := parseMycnf([]byte(tcpConfig2))
convey.So(dsn, convey.ShouldEqual, "root:abc123@tcp(localhost:3308)/")
})
convey.Convey("Authentication with client certificate and no password", func() {
dsn, _ := parseMycnf([]byte(clientAuthConfig))
convey.So(dsn, convey.ShouldEqual, "root@tcp(localhost:3308)/")
})
convey.Convey("Socket connection", func() {
dsn, _ := parseMycnf([]byte(socketConfig))
convey.So(dsn, convey.ShouldEqual, "user:pass@unix(/var/lib/mysql/mysql.sock)/")
})
convey.Convey("Socket connection ignoring defined host", func() {
dsn, _ := parseMycnf([]byte(socketConfig2))
convey.So(dsn, convey.ShouldEqual, "dude:nopassword@unix(/var/lib/mysql/mysql.sock)/")
})
convey.Convey("Remote connection", func() {
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.ShouldBeError, fmt.Errorf("password or ssl-key should be specified under [client] in %s", badConfig))
})
convey.Convey("Missed password", func() {
_, err := parseMycnf([]byte(badConfig2))
convey.So(err, convey.ShouldBeError, fmt.Errorf("no user specified under [client] in %s", badConfig2))
})
convey.Convey("No [client] section", func() {
_, err := parseMycnf([]byte(badConfig3))
convey.So(err, convey.ShouldBeError, fmt.Errorf("no user specified under [client] in %s", badConfig3))
})
convey.Convey("Invalid config", func() {
_, err := parseMycnf([]byte(badConfig4))
convey.So(err, convey.ShouldBeError, fmt.Errorf("failed reading ini file: unclosed section: %s", badConfig4))
})
})
}
// bin stores information about path of executable and attached port
type bin struct {
path string
port int
}
// TestBin builds, runs and tests binary.
func TestBin(t *testing.T) {
var err error
binName := "mysqld_exporter"
binDir, err := ioutil.TempDir("/tmp", binName+"-test-bindir-")
if err != nil {
t.Fatal(err)
}
defer func() {
err := os.RemoveAll(binDir)
if err != nil {
t.Fatal(err)
}
}()
importpath := "github.com/prometheus/mysqld_exporter/vendor/github.com/prometheus/common"
path := binDir + "/" + binName
xVariables := map[string]string{
importpath + "/version.Version": "gotest-version",
importpath + "/version.Branch": "gotest-branch",
importpath + "/version.Revision": "gotest-revision",
}
var ldflags []string
for x, value := range xVariables {
ldflags = append(ldflags, fmt.Sprintf("-X %s=%s", x, value))
}
cmd := exec.Command(
"go",
"build",
"-o",
path,
"-ldflags",
strings.Join(ldflags, " "),
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
t.Fatalf("Failed to build: %s", err)
}
tests := []func(*testing.T, bin){
testLandingPage,
}
portStart := 56000
t.Run(binName, func(t *testing.T) {
for _, f := range tests {
f := f // capture range variable
fName := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
portStart++
data := bin{
path: path,
port: portStart,
}
t.Run(fName, func(t *testing.T) {
t.Parallel()
f(t, data)
})
}
})
}
func testLandingPage(t *testing.T, data bin) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Run exporter.
cmd := exec.CommandContext(
ctx,
data.path,
"--web.listen-address", fmt.Sprintf(":%d", data.port),
)
cmd.Env = append(os.Environ(), "DATA_SOURCE_NAME=127.0.0.1:3306")
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
// Get the main page.
urlToGet := fmt.Sprintf("http://127.0.0.1:%d", data.port)
body, err := waitForBody(urlToGet)
if err != nil {
t.Fatal(err)
}
got := string(body)
expected := `<html>
<head><title>MySQLd exporter</title></head>
<body>
<h1>MySQLd exporter</h1>
<p><a href='/metrics'>Metrics</a></p>
</body>
</html>
`
if got != expected {
t.Fatalf("got '%s' but expected '%s'", got, expected)
}
}
// waitForBody is a helper function which makes http calls until http server is up
// and then returns body of the successful call.
func waitForBody(urlToGet string) (body []byte, err error) {
tries := 60
// Get data, but we need to wait a bit for http server.
for i := 0; i <= tries; i++ {
// Try to get web page.
body, err = getBody(urlToGet)
if err == nil {
return body, err
}
// If there is a syscall.ECONNREFUSED error (web server not available) then retry.
if urlError, ok := err.(*url.Error); ok {
if opError, ok := urlError.Err.(*net.OpError); ok {
if osSyscallError, ok := opError.Err.(*os.SyscallError); ok {
if osSyscallError.Err == syscall.ECONNREFUSED {
time.Sleep(1 * time.Second)
continue
}
}
}
}
// There was an error, and it wasn't syscall.ECONNREFUSED.
return nil, err
}
return nil, fmt.Errorf("failed to GET %s after %d tries: %s", urlToGet, tries, err)
}
// getBody is a helper function which retrieves http body from given address.
func getBody(urlToGet string) ([]byte, error) {
resp, err := http.Get(urlToGet)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/wl4g-collect/mysqld_exporter.git
git@gitee.com:wl4g-collect/mysqld_exporter.git
wl4g-collect
mysqld_exporter
mysqld_exporter
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891