| 1 |
/* |
| 2 |
Copyright (c) 2019, Percona LLC. |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without |
| 6 |
modification, are permitted provided that the following conditions are met: |
| 7 |
|
| 8 |
* Redistributions of source code must retain the above copyright notice, this |
| 9 |
list of conditions and the following disclaimer. |
| 10 |
|
| 11 |
* Redistributions in binary form must reproduce the above copyright notice, |
| 12 |
this list of conditions and the following disclaimer in the documentation |
| 13 |
and/or other materials provided with the distribution. |
| 14 |
|
| 15 |
* Neither the name of the copyright holder nor the names of its |
| 16 |
contributors may be used to endorse or promote products derived from |
| 17 |
this software without specific prior written permission. |
| 18 |
|
| 19 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 23 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 25 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 26 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 27 |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
|
| 31 |
package dsn |
| 32 |
|
| 33 |
import ( |
| 34 |
"context" |
| 35 |
"os" |
| 36 |
"path" |
| 37 |
"testing" |
| 38 |
"time" |
| 39 |
|
| 40 |
"github.com/stretchr/testify/assert" |
| 41 |
|
| 42 |
"github.com/percona/go-mysql/test" |
| 43 |
) |
| 44 |
|
| 45 |
func TestParseDefaults(t *testing.T) { |
| 46 |
input := "--user=root\n--password=rootpwd\n--port=3306\n--host=localhost\n--socket=/var/run/mysqld/mysqld.sock\n" |
| 47 |
dsn := ParseMySQLDefaults(input) |
| 48 |
assert.Equal(t, "root", dsn.Username) |
| 49 |
assert.Equal(t, "rootpwd", dsn.Password) |
| 50 |
assert.Equal(t, "", dsn.Hostname) |
| 51 |
assert.Equal(t, "", dsn.Port) |
| 52 |
assert.Equal(t, "/var/run/mysqld/mysqld.sock", dsn.Socket) |
| 53 |
|
| 54 |
input = "--user=root\n--password=rootpwd\n--port=3306\n--host=localhost" |
| 55 |
dsn = ParseMySQLDefaults(input) |
| 56 |
assert.Equal(t, "root", dsn.Username) |
| 57 |
assert.Equal(t, "rootpwd", dsn.Password) |
| 58 |
assert.Equal(t, "localhost", dsn.Hostname) |
| 59 |
assert.Equal(t, "3306", dsn.Port) |
| 60 |
assert.Equal(t, "", dsn.Socket) |
| 61 |
} |
| 62 |
|
| 63 |
func TestDefaults(t *testing.T) { |
| •64 |
t.Skip("Requires active MySQL instance.") |
| 65 |
originalPath := os.Getenv("PATH") |
| 66 |
|
| 67 |
// Since we cannot install different versions of my_print_defaults, we are going |
| 68 |
// to use 2 shell scripts to mock the behavior of the original programs. |
| 69 |
// os.Exec (used in the Defaults func) search in the PATH for the program to run so, |
| 70 |
// let's change the path to point to our mock scripts. |
| 71 |
os.Setenv("PATH", path.Join(test.RootDir(), "test/scripts/my_print_defaults/5.5")) |
| 72 |
dsn, err := Defaults("a_fake_filename") |
| 73 |
assert.NoError(t, err) |
| 74 |
assert.Equal(t, "root5.5", dsn.Username) |
| 75 |
assert.Equal(t, "rootpwd", dsn.Password) |
| 76 |
assert.Equal(t, "", dsn.Hostname) |
| 77 |
assert.Equal(t, "", dsn.Port) |
| 78 |
assert.Equal(t, "/var/run/mysqld/mysqld.sock", dsn.Socket) |
| 79 |
|
| 80 |
os.Setenv("PATH", path.Join(test.RootDir(), "test/scripts/my_print_defaults/5.6")) |
| 81 |
dsn, err = Defaults("a_fake_filename") |
| 82 |
assert.NoError(t, err) |
| 83 |
assert.Equal(t, "root5.6", dsn.Username) |
| 84 |
assert.Equal(t, "rootpwd", dsn.Password) |
| 85 |
assert.Equal(t, "", dsn.Hostname) |
| 86 |
assert.Equal(t, "", dsn.Port) |
| 87 |
assert.Equal(t, "/var/run/mysqld/mysqld.sock", dsn.Socket) |
| 88 |
|
| 89 |
os.Setenv("PATH", originalPath) |
| 90 |
} |
| 91 |
|
| 92 |
func TestGetSocketFromProcessLists(t *testing.T) { |
| •93 |
t.Skip("Requires active MySQL instance.") |
| 94 |
var err error |
| 95 |
var socket string |
| 96 |
dsn := DSN{} |
| 97 |
ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 98 |
defer cancel() |
| 99 |
socket, err = GetSocket(ctx, dsn.String()) |
| 100 |
assert.NoError(t, err) |
| 101 |
assert.NotEmpty(t, socket) |
| 102 |
|
| 103 |
// Each of below command may fail as result depends on OS. |
| 104 |
socket, err = GetSocketFromTCPConnection(context.TODO(), dsn.String()) |
| 105 |
if err == nil { |
| 106 |
assert.NotEmpty(t, socket) |
| 107 |
} |
| 108 |
socket, err = GetSocketFromNetstat(context.TODO()) |
| 109 |
if err == nil { |
| 110 |
assert.NotEmpty(t, socket) |
| 111 |
} |
| 112 |
socket, err = GetSocketFromProcessList(context.TODO()) |
| 113 |
if err == nil { |
| 114 |
assert.NotEmpty(t, socket) |
| 115 |
} |
| 116 |
} |