2015-05-18 18:54:11 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2015-12-22 07:06:39 +00:00
|
|
|
"database/sql"
|
2015-07-07 01:18:31 +00:00
|
|
|
"fmt"
|
2015-05-18 18:54:11 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2015-05-18 18:54:11 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMysqlDefaultsToLocal(t *testing.T) {
|
2015-08-04 20:52:44 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
|
|
|
|
2015-07-06 04:46:43 +00:00
|
|
|
m := &Mysql{
|
2015-07-07 01:18:31 +00:00
|
|
|
Servers: []string{fmt.Sprintf("root@tcp(%s:3306)/", testutil.GetLocalHost())},
|
2015-07-06 04:46:43 +00:00
|
|
|
}
|
2015-05-18 18:54:11 +00:00
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := m.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-01-07 00:37:56 +00:00
|
|
|
assert.True(t, acc.HasMeasurement("mysql"))
|
2015-05-18 18:54:11 +00:00
|
|
|
}
|
2015-10-22 23:11:51 +00:00
|
|
|
|
2016-10-12 11:43:51 +00:00
|
|
|
func TestMysqlGetDSNTag(t *testing.T) {
|
2015-10-22 23:11:51 +00:00
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
output string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"",
|
|
|
|
"127.0.0.1:3306",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"localhost",
|
|
|
|
"127.0.0.1:3306",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"127.0.0.1",
|
|
|
|
"127.0.0.1:3306",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tcp(192.168.1.1:3306)/",
|
|
|
|
"192.168.1.1:3306",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tcp(localhost)/",
|
|
|
|
"localhost",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"root:passwd@tcp(192.168.1.1:3306)/?tls=false",
|
|
|
|
"192.168.1.1:3306",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"root@tcp(127.0.0.1:3306)/?tls=false",
|
|
|
|
"127.0.0.1:3306",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"root:passwd@tcp(localhost:3036)/dbname?allowOldPasswords=1",
|
|
|
|
"localhost:3036",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"root:foo@bar@tcp(192.1.1.1:3306)/?tls=false",
|
|
|
|
"192.1.1.1:3306",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"root:f00@b4r@tcp(192.1.1.1:3306)/?tls=false",
|
|
|
|
"192.1.1.1:3306",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"root:fl!p11@tcp(192.1.1.1:3306)/?tls=false",
|
|
|
|
"192.1.1.1:3306",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2016-10-12 11:43:51 +00:00
|
|
|
output := getDSNTag(test.input)
|
2015-10-22 23:11:51 +00:00
|
|
|
if output != test.output {
|
2016-10-12 11:43:51 +00:00
|
|
|
t.Errorf("Input: %s Expected %s, got %s\n", test.input, test.output, output)
|
2015-10-22 23:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-29 16:52:58 +00:00
|
|
|
|
|
|
|
func TestMysqlDNSAddTimeout(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
output string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"",
|
2016-10-12 11:43:51 +00:00
|
|
|
"tcp(127.0.0.1:3306)/?timeout=5s",
|
2016-02-29 16:52:58 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"tcp(192.168.1.1:3306)/",
|
|
|
|
"tcp(192.168.1.1:3306)/?timeout=5s",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"root:passwd@tcp(192.168.1.1:3306)/?tls=false",
|
|
|
|
"root:passwd@tcp(192.168.1.1:3306)/?timeout=5s&tls=false",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"root:passwd@tcp(192.168.1.1:3306)/?tls=false&timeout=10s",
|
2016-10-12 11:43:51 +00:00
|
|
|
"root:passwd@tcp(192.168.1.1:3306)/?timeout=10s&tls=false",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tcp(10.150.1.123:3306)/",
|
|
|
|
"tcp(10.150.1.123:3306)/?timeout=5s",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"root:@!~(*&$#%(&@#(@&#Password@tcp(10.150.1.123:3306)/",
|
|
|
|
"root:@!~(*&$#%(&@#(@&#Password@tcp(10.150.1.123:3306)/?timeout=5s",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"root:Test3a#@!@tcp(10.150.1.123:3306)/",
|
|
|
|
"root:Test3a#@!@tcp(10.150.1.123:3306)/?timeout=5s",
|
2016-02-29 16:52:58 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2016-02-29 18:04:56 +00:00
|
|
|
output, _ := dsnAddTimeout(test.input)
|
2016-02-29 16:52:58 +00:00
|
|
|
if output != test.output {
|
|
|
|
t.Errorf("Expected %s, got %s\n", test.output, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-22 07:06:39 +00:00
|
|
|
func TestParseValue(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
rawByte sql.RawBytes
|
2017-12-12 21:22:11 +00:00
|
|
|
output interface{}
|
2015-12-22 07:06:39 +00:00
|
|
|
boolValue bool
|
|
|
|
}{
|
2017-12-12 21:22:11 +00:00
|
|
|
{sql.RawBytes("123"), int64(123), true},
|
|
|
|
{sql.RawBytes("abc"), "abc", true},
|
|
|
|
{sql.RawBytes("10.1"), 10.1, true},
|
2015-12-22 07:06:39 +00:00
|
|
|
{sql.RawBytes("ON"), 1, true},
|
2017-12-12 21:22:11 +00:00
|
|
|
{sql.RawBytes("OFF"), 0, true},
|
|
|
|
{sql.RawBytes("NO"), 0, true},
|
|
|
|
{sql.RawBytes("YES"), 1, true},
|
|
|
|
{sql.RawBytes("No"), 0, true},
|
|
|
|
{sql.RawBytes("Yes"), 1, true},
|
|
|
|
{sql.RawBytes(""), nil, false},
|
2015-12-22 07:06:39 +00:00
|
|
|
}
|
|
|
|
for _, cases := range testCases {
|
2017-12-12 21:22:11 +00:00
|
|
|
if got, ok := parseValue(cases.rawByte); got != cases.output && ok != cases.boolValue {
|
|
|
|
t.Errorf("for %s wanted %t, got %t", string(cases.rawByte), cases.output, got)
|
2015-12-22 07:06:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestNewNamespace(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
words []string
|
|
|
|
namespace string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]string{"thread", "info_scheme", "query update"},
|
|
|
|
"thread_info_scheme_query_update",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"thread", "info_scheme", "query_update"},
|
|
|
|
"thread_info_scheme_query_update",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"thread", "info", "scheme", "query", "update"},
|
|
|
|
"thread_info_scheme_query_update",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, cases := range testCases {
|
|
|
|
if got := newNamespace(cases.words...); got != cases.namespace {
|
|
|
|
t.Errorf("want %s, got %s", cases.namespace, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|