| 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 dsnscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/ |
| 32 |
|
| 33 |
import ( |
| 34 |
"contextscip-go gomod github.com/golang/go/src go1.25.4 context/" |
| 35 |
"osscip-go gomod github.com/golang/go/src go1.25.4 os/" |
| 36 |
"pathscip-go gomod github.com/golang/go/src go1.25.4 path/" |
| 37 |
"testingscip-go gomod github.com/golang/go/src go1.25.4 testing/" |
| 38 |
"timescip-go gomod github.com/golang/go/src go1.25.4 time/" |
| 39 |
|
| 40 |
"github.com/stretchr/testify/assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/" |
| 41 |
|
| 42 |
"github.com/percona/go-mysql/testscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/test`/" |
| 43 |
) |
| 44 |
|
| 45 |
func TestParseDefaultsscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/TestParseDefaults().(t *testingscip-go gomod github.com/golang/go/src go1.25.4 testing/.Tscip-go gomod github.com/golang/go/src go1.25.4 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 := ParseMySQLDefaultsscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/ParseMySQLDefaults().(input) |
| 48 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "root", dsn.Usernamescip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Username.) |
| 49 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "rootpwd", dsn.Passwordscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Password.) |
| 50 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "", dsn.Hostnamescip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Hostname.) |
| 51 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "", dsn.Portscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Port.) |
| 52 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "/var/run/mysqld/mysqld.sock", dsn.Socketscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Socket.) |
| 53 |
|
| 54 |
input = "--user=root\n--password=rootpwd\n--port=3306\n--host=localhost" |
| 55 |
dsn = ParseMySQLDefaultsscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/ParseMySQLDefaults().(input) |
| 56 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "root", dsn.Usernamescip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Username.) |
| 57 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "rootpwd", dsn.Passwordscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Password.) |
| 58 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "localhost", dsn.Hostnamescip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Hostname.) |
| 59 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "3306", dsn.Portscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Port.) |
| 60 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "", dsn.Socketscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Socket.) |
| 61 |
} |
| 62 |
|
| 63 |
func TestDefaultsscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/TestDefaults().(t *testingscip-go gomod github.com/golang/go/src go1.25.4 testing/.Tscip-go gomod github.com/golang/go/src go1.25.4 testing/T#) { |
| •64 |
t.Skipscip-go gomod github.com/golang/go/src go1.25.4 testing/common#Skip().("Requires active MySQL instance.") |
| 65 |
originalPath := osscip-go gomod github.com/golang/go/src go1.25.4 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.4 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 |
osscip-go gomod github.com/golang/go/src go1.25.4 os/.Setenvscip-go gomod github.com/golang/go/src go1.25.4 os/Setenv().("PATH", pathscip-go gomod github.com/golang/go/src go1.25.4 path/.Joinscip-go gomod github.com/golang/go/src go1.25.4 path/Join().(testscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/test`/.RootDirscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/test`/RootDir().(), "test/scripts/my_print_defaults/5.5")) |
| 72 |
dsn, err := Defaultsscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/Defaults().("a_fake_filename") |
| 73 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.NoErrorscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/NoError().(t, err) |
| 74 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "root5.5", dsn.Usernamescip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Username.) |
| 75 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "rootpwd", dsn.Passwordscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Password.) |
| 76 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "", dsn.Hostnamescip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Hostname.) |
| 77 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "", dsn.Portscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Port.) |
| 78 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "/var/run/mysqld/mysqld.sock", dsn.Socketscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Socket.) |
| 79 |
|
| 80 |
osscip-go gomod github.com/golang/go/src go1.25.4 os/.Setenvscip-go gomod github.com/golang/go/src go1.25.4 os/Setenv().("PATH", pathscip-go gomod github.com/golang/go/src go1.25.4 path/.Joinscip-go gomod github.com/golang/go/src go1.25.4 path/Join().(testscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/test`/.RootDirscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/test`/RootDir().(), "test/scripts/my_print_defaults/5.6")) |
| 81 |
dsn, err = Defaultsscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/Defaults().("a_fake_filename") |
| 82 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.NoErrorscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/NoError().(t, err) |
| 83 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "root5.6", dsn.Usernamescip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Username.) |
| 84 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "rootpwd", dsn.Passwordscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Password.) |
| 85 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "", dsn.Hostnamescip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Hostname.) |
| 86 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "", dsn.Portscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Port.) |
| 87 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/Equal().(t, "/var/run/mysqld/mysqld.sock", dsn.Socketscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#Socket.) |
| 88 |
|
| 89 |
osscip-go gomod github.com/golang/go/src go1.25.4 os/.Setenvscip-go gomod github.com/golang/go/src go1.25.4 os/Setenv().("PATH", originalPath) |
| 90 |
} |
| 91 |
|
| 92 |
func TestGetSocketFromProcessListsscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/TestGetSocketFromProcessLists().(t *testingscip-go gomod github.com/golang/go/src go1.25.4 testing/.Tscip-go gomod github.com/golang/go/src go1.25.4 testing/T#) { |
| •93 |
t.Skipscip-go gomod github.com/golang/go/src go1.25.4 testing/common#Skip().("Requires active MySQL instance.") |
| 94 |
var err error |
| 95 |
var socket string |
| 96 |
dsn := DSNscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#{} |
| 97 |
ctx, cancel := contextscip-go gomod github.com/golang/go/src go1.25.4 context/.WithTimeoutscip-go gomod github.com/golang/go/src go1.25.4 context/WithTimeout().(contextscip-go gomod github.com/golang/go/src go1.25.4 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.4 context/Background().(), timescip-go gomod github.com/golang/go/src go1.25.4 time/.Secondscip-go gomod github.com/golang/go/src go1.25.4 time/Second.) |
| 98 |
defer cancel() |
| 99 |
socket, err = GetSocketscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/GetSocket().(ctx, dsn.Stringscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#String().()) |
| 100 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.NoErrorscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/NoError().(t, err) |
| 101 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.NotEmptyscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/NotEmpty().(t, socket) |
| 102 |
|
| 103 |
// Each of below command may fail as result depends on OS. |
| 104 |
socket, err = GetSocketFromTCPConnectionscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/GetSocketFromTCPConnection().(contextscip-go gomod github.com/golang/go/src go1.25.4 context/.TODOscip-go gomod github.com/golang/go/src go1.25.4 context/TODO().(), dsn.Stringscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/DSN#String().()) |
| 105 |
if err == nil { |
| 106 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.NotEmptyscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/NotEmpty().(t, socket) |
| 107 |
} |
| 108 |
socket, err = GetSocketFromNetstatscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/GetSocketFromNetstat().(contextscip-go gomod github.com/golang/go/src go1.25.4 context/.TODOscip-go gomod github.com/golang/go/src go1.25.4 context/TODO().()) |
| 109 |
if err == nil { |
| 110 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.NotEmptyscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/NotEmpty().(t, socket) |
| 111 |
} |
| 112 |
socket, err = GetSocketFromProcessListscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/dsn`/GetSocketFromProcessList().(contextscip-go gomod github.com/golang/go/src go1.25.4 context/.TODOscip-go gomod github.com/golang/go/src go1.25.4 context/TODO().()) |
| 113 |
if err == nil { |
| 114 |
assertscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/.NotEmptyscip-go gomod github.com/stretchr/testify v1.10.0 `github.com/stretchr/testify/assert`/NotEmpty().(t, socket) |
| 115 |
} |
| 116 |
} |