Skip to content

Commit

Permalink
Merge pull request #47 from lighttiger2505/add-validation-config
Browse files Browse the repository at this point in the history
Add validation config
  • Loading branch information
lighttiger2505 authored Feb 21, 2021
2 parents 9aa58ad + deb8478 commit 9024357
Show file tree
Hide file tree
Showing 14 changed files with 426 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -24,6 +25,14 @@ type Config struct {
Connections []*database.DBConfig `json:"connections" yaml:"connections"`
}

func (c *Config) Validate() error {
fmt.Println("Validate")
if len(c.Connections) > 0 {
return c.Connections[0].Validate()
}
return nil
}

func NewConfig() *Config {
cfg := &Config{}
cfg.LowercaseKeywords = false
Expand Down Expand Up @@ -69,6 +78,10 @@ func (c *Config) Load(fp string) error {
if err = yaml.Unmarshal(b, c); err != nil {
return xerrors.Errorf("failed unmarshal yaml, %+v", err, string(b))
}

if err := c.Validate(); err != nil {
return xerrors.Errorf("failed validation, %+v", err)
}
return nil
}

Expand Down
185 changes: 185 additions & 0 deletions internal/config/config_test.go
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)
}
})
}
}
45 changes: 45 additions & 0 deletions internal/config/testdata/basic.yml
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
14 changes: 14 additions & 0 deletions internal/config/testdata/invalid_proto.yml
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
5 changes: 5 additions & 0 deletions internal/config/testdata/no_connection.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
connections:
- alias: sqls_mysql
driver: mysql
dataSourceName: ""
proto: ""
14 changes: 14 additions & 0 deletions internal/config/testdata/no_driver.yml
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
4 changes: 4 additions & 0 deletions internal/config/testdata/no_dsn.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
connections:
- alias: sqls_sqlite3
driver: sqlite3
dataSourceName: ""
14 changes: 14 additions & 0 deletions internal/config/testdata/no_host.yml
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
14 changes: 14 additions & 0 deletions internal/config/testdata/no_path.yml
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
16 changes: 16 additions & 0 deletions internal/config/testdata/no_ssh_host.yml
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
16 changes: 16 additions & 0 deletions internal/config/testdata/no_ssh_private_key.yml
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: ""
16 changes: 16 additions & 0 deletions internal/config/testdata/no_ssh_user.yml
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
Loading

0 comments on commit 9024357

Please sign in to comment.