@@ -22,6 +22,7 @@ import (
22
22
"testing"
23
23
24
24
sqlmock "github.com/DATA-DOG/go-sqlmock"
25
+ "github.com/google/go-cmp/cmp"
25
26
_ "github.com/lib/pq"
26
27
27
28
api_pb "github.com/kubeflow/katib/pkg/apis/manager/v1beta1"
@@ -129,11 +130,60 @@ func TestDeleteObservationLog(t *testing.T) {
129
130
}
130
131
131
132
func TestGetDbName (t * testing.T ) {
132
- // dbName := "root:@tcp(katib-mysql:3306)/katib?timeout=5s"
133
- dbName := "host=katib-postgres port=5432 user=katib password= dbname=katib sslmode=disable"
134
-
135
- if getDbName () != dbName {
136
- t .Errorf ("getDbName returns wrong value %v" , getDbName ())
133
+ cases := map [string ]struct {
134
+ updateEnvs map [string ]string
135
+ wantName string
136
+ }{
137
+ "All parameters are default" : {
138
+ wantName : "host=katib-postgres port=5432 user=katib password= dbname=katib sslmode=disable" ,
139
+ },
140
+ "Set DB_USER" : {
141
+ updateEnvs : map [string ]string {
142
+ common .DBUserEnvName : "testUser" ,
143
+ },
144
+ wantName : "host=katib-postgres port=5432 user=testUser password= dbname=katib sslmode=disable" ,
145
+ },
146
+ "Set KATIB_POSTGRESQL_DB_HOST" : {
147
+ updateEnvs : map [string ]string {
148
+ common .PostgreSQLDBHostEnvName : "testHost" ,
149
+ },
150
+ wantName : "host=testHost port=5432 user=katib password= dbname=katib sslmode=disable" ,
151
+ },
152
+ "Set KATIB_POSTGRESQL_DB_PORT" : {
153
+ updateEnvs : map [string ]string {
154
+ common .PostgreSQLDBPortEnvName : "1234" ,
155
+ },
156
+ wantName : "host=katib-postgres port=1234 user=katib password= dbname=katib sslmode=disable" ,
157
+ },
158
+ "Set KATIB_POSTGRESQL_DB_DATABASE" : {
159
+ updateEnvs : map [string ]string {
160
+ common .PostgreSQLDatabase : "testDB" ,
161
+ },
162
+ wantName : "host=katib-postgres port=5432 user=katib password= dbname=testDB sslmode=disable" ,
163
+ },
164
+ "Set DB_PASSWORD" : {
165
+ updateEnvs : map [string ]string {
166
+ common .DBPasswordEnvName : "testPassword" ,
167
+ },
168
+ wantName : "host=katib-postgres port=5432 user=katib password=testPassword dbname=katib sslmode=disable" ,
169
+ },
170
+ "Set KATIB_POSTGRESQL_SSL_MODE" : {
171
+ updateEnvs : map [string ]string {
172
+ common .PostgreSSLMode : "require" ,
173
+ },
174
+ wantName : "host=katib-postgres port=5432 user=katib password= dbname=katib sslmode=require" ,
175
+ },
137
176
}
138
177
178
+ for name , tc := range cases {
179
+ t .Run (name , func (t * testing.T ) {
180
+ for k , v := range tc .updateEnvs {
181
+ t .Setenv (k , v )
182
+ }
183
+ gotName := getDbName ()
184
+ if diff := cmp .Diff (tc .wantName , gotName ); len (diff ) != 0 {
185
+ t .Errorf ("Unexpected DBName (-want,+got):\n %s" , diff )
186
+ }
187
+ })
188
+ }
139
189
}
0 commit comments