|
| 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 | +} |
0 commit comments