-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from lighttiger2505/add-validation-config
Add validation config
- Loading branch information
Showing
14 changed files
with
426 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/lighttiger2505/sqls/internal/database" | ||
) | ||
|
||
func TestGetConfig(t *testing.T) { | ||
type args struct { | ||
fp string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *Config | ||
wantErr bool | ||
errMsg string | ||
}{ | ||
{ | ||
name: "basic", | ||
args: args{ | ||
fp: "basic.yml", | ||
}, | ||
want: &Config{ | ||
LowercaseKeywords: true, | ||
Connections: []*database.DBConfig{ | ||
{ | ||
Alias: "sqls_mysql", | ||
Driver: "mysql", | ||
Proto: "tcp", | ||
User: "root", | ||
Passwd: "root", | ||
Host: "127.0.0.1", | ||
Port: 13306, | ||
DBName: "world", | ||
Params: map[string]string{"autocommit": "true", "tls": "skip-verify"}, | ||
}, | ||
{ | ||
Alias: "sqls_sqlite3", | ||
Driver: "sqlite3", | ||
DataSourceName: "file:/home/lighttiger2505/chinook.db", | ||
}, | ||
{ | ||
Alias: "sqls_postgresql", | ||
Driver: "postgresql", | ||
Proto: "tcp", | ||
User: "postgres", | ||
Passwd: "mysecretpassword1234", | ||
Host: "127.0.0.1", | ||
Port: 15432, | ||
DBName: "dvdrental", | ||
Params: map[string]string{"sslmode": "disable"}, | ||
}, | ||
{ | ||
Alias: "mysql_with_bastion", | ||
Driver: "mysql", | ||
Proto: "tcp", | ||
User: "admin", | ||
Passwd: "Q+ACgv12ABx/", | ||
Host: "192.168.121.163", | ||
Port: 3306, | ||
DBName: "world", | ||
SSHCfg: &database.SSHConfig{ | ||
Host: "192.168.121.168", | ||
Port: 22, | ||
User: "vagrant", | ||
PassPhrase: "passphrase1234", | ||
PrivateKey: "/home/lighttiger2505/.ssh/id_rsa", | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "no driver", | ||
args: args{ | ||
fp: "no_driver.yml", | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
errMsg: "failed validation, required: connections[].driver", | ||
}, | ||
{ | ||
name: "no connection", | ||
args: args{ | ||
fp: "no_connection.yml", | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
errMsg: "failed validation, required: connections[].dataSourceName or connections[].proto", | ||
}, | ||
{ | ||
name: "no user", | ||
args: args{ | ||
fp: "no_user.yml", | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
errMsg: "failed validation, required: connections[].user", | ||
}, | ||
{ | ||
name: "invalid proto", | ||
args: args{ | ||
fp: "invalid_proto.yml", | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
errMsg: "failed validation, invalid: connections[].proto", | ||
}, | ||
{ | ||
name: "no path", | ||
args: args{ | ||
fp: "no_path.yml", | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
errMsg: "failed validation, required: connections[].path", | ||
}, | ||
{ | ||
name: "no dsn", | ||
args: args{ | ||
fp: "no_dsn.yml", | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
errMsg: "failed validation, required: connections[].dataSourceName", | ||
}, | ||
{ | ||
name: "no ssh host", | ||
args: args{ | ||
fp: "no_ssh_host.yml", | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
errMsg: "failed validation, required: connections[]sshConfig.host", | ||
}, | ||
{ | ||
name: "no ssh user", | ||
args: args{ | ||
fp: "no_ssh_user.yml", | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
errMsg: "failed validation, required: connections[].sshConfig.user", | ||
}, | ||
{ | ||
name: "no ssh private key", | ||
args: args{ | ||
fp: "no_ssh_private_key.yml", | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
errMsg: "failed validation, required: connections[].sshConfig.privateKey", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
packageDir, err := os.Getwd() | ||
if err != nil { | ||
t.Fatalf("cannot get package path, Err=%v", err) | ||
} | ||
testFile := filepath.Join(packageDir, "testdata", tt.args.fp) | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := GetConfig(testFile) | ||
if err != nil { | ||
if tt.wantErr { | ||
if err.Error() != tt.errMsg { | ||
t.Errorf("unmatch error message, want:%q got:%q", tt.errMsg, err.Error()) | ||
} | ||
} else { | ||
t.Errorf("GetConfig() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
} | ||
if diff := cmp.Diff(tt.want, got); diff != "" { | ||
t.Errorf("unmatch (- want, + got):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
lowercaseKeywords: true | ||
connections: | ||
- alias: sqls_mysql | ||
driver: mysql | ||
dataSourceName: "" | ||
proto: tcp | ||
user: root | ||
passwd: root | ||
host: 127.0.0.1 | ||
port: 13306 | ||
path: "" | ||
dbName: world | ||
params: | ||
autocommit: "true" | ||
tls: skip-verify | ||
- alias: sqls_sqlite3 | ||
driver: sqlite3 | ||
dataSourceName: "file:/home/lighttiger2505/chinook.db" | ||
- alias: sqls_postgresql | ||
driver: postgresql | ||
dataSourceName: "" | ||
proto: tcp | ||
user: postgres | ||
passwd: mysecretpassword1234 | ||
host: 127.0.0.1 | ||
port: 15432 | ||
path: "" | ||
dbName: dvdrental | ||
params: | ||
sslmode: disable | ||
- alias: mysql_with_bastion | ||
driver: mysql | ||
dataSourceName: "" | ||
proto: tcp | ||
user: admin | ||
passwd: Q+ACgv12ABx/ | ||
host: 192.168.121.163 | ||
port: 3306 | ||
dbName: world | ||
sshConfig: | ||
host: 192.168.121.168 | ||
port: 22 | ||
user: vagrant | ||
passPhrase: passphrase1234 | ||
privateKey: /home/lighttiger2505/.ssh/id_rsa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
connections: | ||
- alias: sqls_mysql | ||
driver: mysql | ||
dataSourceName: "" | ||
proto: invalid | ||
user: root | ||
passwd: root | ||
host: 127.0.0.1 | ||
port: 13306 | ||
path: "" | ||
dbName: world | ||
params: | ||
autocommit: "true" | ||
tls: skip-verify |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
connections: | ||
- alias: sqls_mysql | ||
driver: mysql | ||
dataSourceName: "" | ||
proto: "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
connections: | ||
- alias: sqls_mysql | ||
driver: "" | ||
dataSourceName: "" | ||
proto: tcp | ||
user: root | ||
passwd: root | ||
host: 127.0.0.1 | ||
port: 13306 | ||
path: "" | ||
dbName: world | ||
params: | ||
autocommit: "true" | ||
tls: skip-verify |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
connections: | ||
- alias: sqls_sqlite3 | ||
driver: sqlite3 | ||
dataSourceName: "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
connections: | ||
- alias: sqls_mysql | ||
driver: mysql | ||
dataSourceName: "" | ||
proto: unix | ||
user: root | ||
passwd: root | ||
host: "" | ||
port: 0 | ||
path: "" | ||
dbName: world | ||
params: | ||
autocommit: "true" | ||
tls: skip-verify |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
connections: | ||
- alias: sqls_mysql | ||
driver: mysql | ||
dataSourceName: "" | ||
proto: unix | ||
user: root | ||
passwd: root | ||
host: 127.0.0.1 | ||
port: 13306 | ||
path: "" | ||
dbName: world | ||
params: | ||
autocommit: "true" | ||
tls: skip-verify |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
connections: | ||
- alias: mysql_with_bastion | ||
driver: mysql | ||
dataSourceName: "" | ||
proto: tcp | ||
user: admin | ||
passwd: Q+ACgv12ABx/ | ||
host: 192.168.121.163 | ||
port: 3306 | ||
dbName: world | ||
sshConfig: | ||
host: "" | ||
port: 22 | ||
user: vagrant | ||
passPhrase: passphrase1234 | ||
privateKey: /home/lighttiger2505/.ssh/id_rsa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
connections: | ||
- alias: mysql_with_bastion | ||
driver: mysql | ||
dataSourceName: "" | ||
proto: tcp | ||
user: admin | ||
passwd: Q+ACgv12ABx/ | ||
host: 192.168.121.163 | ||
port: 3306 | ||
dbName: world | ||
sshConfig: | ||
host: 192.168.121.168 | ||
port: 22 | ||
user: vagrant | ||
passPhrase: passphrase1234 | ||
privateKey: "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
connections: | ||
- alias: mysql_with_bastion | ||
driver: mysql | ||
dataSourceName: "" | ||
proto: tcp | ||
user: admin | ||
passwd: Q+ACgv12ABx/ | ||
host: 192.168.121.163 | ||
port: 3306 | ||
dbName: world | ||
sshConfig: | ||
host: 192.168.121.168 | ||
port: 22 | ||
user: "" | ||
passPhrase: passphrase1234 | ||
privateKey: /home/lighttiger2505/.ssh/id_rsa |
Oops, something went wrong.