Skip to content

Commit 9024357

Browse files
Merge pull request #47 from lighttiger2505/add-validation-config
Add validation config
2 parents 9aa58ad + deb8478 commit 9024357

14 files changed

+426
-0
lines changed

internal/config/config.go

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"errors"
5+
"fmt"
56
"io/ioutil"
67
"os"
78
"path/filepath"
@@ -24,6 +25,14 @@ type Config struct {
2425
Connections []*database.DBConfig `json:"connections" yaml:"connections"`
2526
}
2627

28+
func (c *Config) Validate() error {
29+
fmt.Println("Validate")
30+
if len(c.Connections) > 0 {
31+
return c.Connections[0].Validate()
32+
}
33+
return nil
34+
}
35+
2736
func NewConfig() *Config {
2837
cfg := &Config{}
2938
cfg.LowercaseKeywords = false
@@ -69,6 +78,10 @@ func (c *Config) Load(fp string) error {
6978
if err = yaml.Unmarshal(b, c); err != nil {
7079
return xerrors.Errorf("failed unmarshal yaml, %+v", err, string(b))
7180
}
81+
82+
if err := c.Validate(); err != nil {
83+
return xerrors.Errorf("failed validation, %+v", err)
84+
}
7285
return nil
7386
}
7487

internal/config/config_test.go

+185
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/google/go-cmp/cmp"
9+
"github.com/lighttiger2505/sqls/internal/database"
10+
)
11+
12+
func TestGetConfig(t *testing.T) {
13+
type args struct {
14+
fp string
15+
}
16+
tests := []struct {
17+
name string
18+
args args
19+
want *Config
20+
wantErr bool
21+
errMsg string
22+
}{
23+
{
24+
name: "basic",
25+
args: args{
26+
fp: "basic.yml",
27+
},
28+
want: &Config{
29+
LowercaseKeywords: true,
30+
Connections: []*database.DBConfig{
31+
{
32+
Alias: "sqls_mysql",
33+
Driver: "mysql",
34+
Proto: "tcp",
35+
User: "root",
36+
Passwd: "root",
37+
Host: "127.0.0.1",
38+
Port: 13306,
39+
DBName: "world",
40+
Params: map[string]string{"autocommit": "true", "tls": "skip-verify"},
41+
},
42+
{
43+
Alias: "sqls_sqlite3",
44+
Driver: "sqlite3",
45+
DataSourceName: "file:/home/lighttiger2505/chinook.db",
46+
},
47+
{
48+
Alias: "sqls_postgresql",
49+
Driver: "postgresql",
50+
Proto: "tcp",
51+
User: "postgres",
52+
Passwd: "mysecretpassword1234",
53+
Host: "127.0.0.1",
54+
Port: 15432,
55+
DBName: "dvdrental",
56+
Params: map[string]string{"sslmode": "disable"},
57+
},
58+
{
59+
Alias: "mysql_with_bastion",
60+
Driver: "mysql",
61+
Proto: "tcp",
62+
User: "admin",
63+
Passwd: "Q+ACgv12ABx/",
64+
Host: "192.168.121.163",
65+
Port: 3306,
66+
DBName: "world",
67+
SSHCfg: &database.SSHConfig{
68+
Host: "192.168.121.168",
69+
Port: 22,
70+
User: "vagrant",
71+
PassPhrase: "passphrase1234",
72+
PrivateKey: "/home/lighttiger2505/.ssh/id_rsa",
73+
},
74+
},
75+
},
76+
},
77+
wantErr: false,
78+
},
79+
{
80+
name: "no driver",
81+
args: args{
82+
fp: "no_driver.yml",
83+
},
84+
want: nil,
85+
wantErr: true,
86+
errMsg: "failed validation, required: connections[].driver",
87+
},
88+
{
89+
name: "no connection",
90+
args: args{
91+
fp: "no_connection.yml",
92+
},
93+
want: nil,
94+
wantErr: true,
95+
errMsg: "failed validation, required: connections[].dataSourceName or connections[].proto",
96+
},
97+
{
98+
name: "no user",
99+
args: args{
100+
fp: "no_user.yml",
101+
},
102+
want: nil,
103+
wantErr: true,
104+
errMsg: "failed validation, required: connections[].user",
105+
},
106+
{
107+
name: "invalid proto",
108+
args: args{
109+
fp: "invalid_proto.yml",
110+
},
111+
want: nil,
112+
wantErr: true,
113+
errMsg: "failed validation, invalid: connections[].proto",
114+
},
115+
{
116+
name: "no path",
117+
args: args{
118+
fp: "no_path.yml",
119+
},
120+
want: nil,
121+
wantErr: true,
122+
errMsg: "failed validation, required: connections[].path",
123+
},
124+
{
125+
name: "no dsn",
126+
args: args{
127+
fp: "no_dsn.yml",
128+
},
129+
want: nil,
130+
wantErr: true,
131+
errMsg: "failed validation, required: connections[].dataSourceName",
132+
},
133+
{
134+
name: "no ssh host",
135+
args: args{
136+
fp: "no_ssh_host.yml",
137+
},
138+
want: nil,
139+
wantErr: true,
140+
errMsg: "failed validation, required: connections[]sshConfig.host",
141+
},
142+
{
143+
name: "no ssh user",
144+
args: args{
145+
fp: "no_ssh_user.yml",
146+
},
147+
want: nil,
148+
wantErr: true,
149+
errMsg: "failed validation, required: connections[].sshConfig.user",
150+
},
151+
{
152+
name: "no ssh private key",
153+
args: args{
154+
fp: "no_ssh_private_key.yml",
155+
},
156+
want: nil,
157+
wantErr: true,
158+
errMsg: "failed validation, required: connections[].sshConfig.privateKey",
159+
},
160+
}
161+
for _, tt := range tests {
162+
packageDir, err := os.Getwd()
163+
if err != nil {
164+
t.Fatalf("cannot get package path, Err=%v", err)
165+
}
166+
testFile := filepath.Join(packageDir, "testdata", tt.args.fp)
167+
168+
t.Run(tt.name, func(t *testing.T) {
169+
got, err := GetConfig(testFile)
170+
if err != nil {
171+
if tt.wantErr {
172+
if err.Error() != tt.errMsg {
173+
t.Errorf("unmatch error message, want:%q got:%q", tt.errMsg, err.Error())
174+
}
175+
} else {
176+
t.Errorf("GetConfig() error = %v, wantErr %v", err, tt.wantErr)
177+
return
178+
}
179+
}
180+
if diff := cmp.Diff(tt.want, got); diff != "" {
181+
t.Errorf("unmatch (- want, + got):\n%s", diff)
182+
}
183+
})
184+
}
185+
}

internal/config/testdata/basic.yml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
lowercaseKeywords: true
2+
connections:
3+
- alias: sqls_mysql
4+
driver: mysql
5+
dataSourceName: ""
6+
proto: tcp
7+
user: root
8+
passwd: root
9+
host: 127.0.0.1
10+
port: 13306
11+
path: ""
12+
dbName: world
13+
params:
14+
autocommit: "true"
15+
tls: skip-verify
16+
- alias: sqls_sqlite3
17+
driver: sqlite3
18+
dataSourceName: "file:/home/lighttiger2505/chinook.db"
19+
- alias: sqls_postgresql
20+
driver: postgresql
21+
dataSourceName: ""
22+
proto: tcp
23+
user: postgres
24+
passwd: mysecretpassword1234
25+
host: 127.0.0.1
26+
port: 15432
27+
path: ""
28+
dbName: dvdrental
29+
params:
30+
sslmode: disable
31+
- alias: mysql_with_bastion
32+
driver: mysql
33+
dataSourceName: ""
34+
proto: tcp
35+
user: admin
36+
passwd: Q+ACgv12ABx/
37+
host: 192.168.121.163
38+
port: 3306
39+
dbName: world
40+
sshConfig:
41+
host: 192.168.121.168
42+
port: 22
43+
user: vagrant
44+
passPhrase: passphrase1234
45+
privateKey: /home/lighttiger2505/.ssh/id_rsa
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
connections:
2+
- alias: sqls_mysql
3+
driver: mysql
4+
dataSourceName: ""
5+
proto: invalid
6+
user: root
7+
passwd: root
8+
host: 127.0.0.1
9+
port: 13306
10+
path: ""
11+
dbName: world
12+
params:
13+
autocommit: "true"
14+
tls: skip-verify
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
connections:
2+
- alias: sqls_mysql
3+
driver: mysql
4+
dataSourceName: ""
5+
proto: ""
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
connections:
2+
- alias: sqls_mysql
3+
driver: ""
4+
dataSourceName: ""
5+
proto: tcp
6+
user: root
7+
passwd: root
8+
host: 127.0.0.1
9+
port: 13306
10+
path: ""
11+
dbName: world
12+
params:
13+
autocommit: "true"
14+
tls: skip-verify

internal/config/testdata/no_dsn.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
connections:
2+
- alias: sqls_sqlite3
3+
driver: sqlite3
4+
dataSourceName: ""

internal/config/testdata/no_host.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
connections:
2+
- alias: sqls_mysql
3+
driver: mysql
4+
dataSourceName: ""
5+
proto: unix
6+
user: root
7+
passwd: root
8+
host: ""
9+
port: 0
10+
path: ""
11+
dbName: world
12+
params:
13+
autocommit: "true"
14+
tls: skip-verify

internal/config/testdata/no_path.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
connections:
2+
- alias: sqls_mysql
3+
driver: mysql
4+
dataSourceName: ""
5+
proto: unix
6+
user: root
7+
passwd: root
8+
host: 127.0.0.1
9+
port: 13306
10+
path: ""
11+
dbName: world
12+
params:
13+
autocommit: "true"
14+
tls: skip-verify
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
connections:
2+
- alias: mysql_with_bastion
3+
driver: mysql
4+
dataSourceName: ""
5+
proto: tcp
6+
user: admin
7+
passwd: Q+ACgv12ABx/
8+
host: 192.168.121.163
9+
port: 3306
10+
dbName: world
11+
sshConfig:
12+
host: ""
13+
port: 22
14+
user: vagrant
15+
passPhrase: passphrase1234
16+
privateKey: /home/lighttiger2505/.ssh/id_rsa
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
connections:
2+
- alias: mysql_with_bastion
3+
driver: mysql
4+
dataSourceName: ""
5+
proto: tcp
6+
user: admin
7+
passwd: Q+ACgv12ABx/
8+
host: 192.168.121.163
9+
port: 3306
10+
dbName: world
11+
sshConfig:
12+
host: 192.168.121.168
13+
port: 22
14+
user: vagrant
15+
passPhrase: passphrase1234
16+
privateKey: ""
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
connections:
2+
- alias: mysql_with_bastion
3+
driver: mysql
4+
dataSourceName: ""
5+
proto: tcp
6+
user: admin
7+
passwd: Q+ACgv12ABx/
8+
host: 192.168.121.163
9+
port: 3306
10+
dbName: world
11+
sshConfig:
12+
host: 192.168.121.168
13+
port: 22
14+
user: ""
15+
passPhrase: passphrase1234
16+
privateKey: /home/lighttiger2505/.ssh/id_rsa

0 commit comments

Comments
 (0)