-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_test.go
228 lines (203 loc) · 5.81 KB
/
init_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright 2022-2024 Thorsten A. Knieling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*/
package flynn
import (
"errors"
"fmt"
"os"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tknie/flynn/common"
"github.com/tknie/flynn/postgres"
)
const postPortNotSet = "Postgres Port not set"
const postgresHostNotSet = "Postgres Host not set"
func postgresTarget(t *testing.T) (string, error) {
postgresHost := os.Getenv("POSTGRES_HOST")
postgresPort := os.Getenv("POSTGRES_PORT")
postgresPassword := os.Getenv("POSTGRES_PWD")
if !assert.NotEmpty(t, postgresHost) {
return "", errors.New(postgresHostNotSet)
}
assert.NotEmpty(t, postgresPort)
port := 5432
var err error
if postgresPort != "" {
port, err = strconv.Atoi(postgresPort)
}
if !assert.NoError(t, err) {
return "", errors.New(postPortNotSet)
}
pg := fmt.Sprintf("postgres://%s:%s@%s:%d/%s", "admin", postgresPassword, postgresHost, port, "bitgarten")
return pg, nil
}
func postgresTargetInstance(t *testing.T) (*common.Reference, string, error) {
postgresHost := os.Getenv("POSTGRES_HOST")
postgresPort := os.Getenv("POSTGRES_PORT")
postgresPassword := os.Getenv("POSTGRES_PWD")
if !assert.NotEmpty(t, postgresHost) {
return nil, "", errors.New(postgresHostNotSet)
}
assert.NotEmpty(t, postgresPort)
port := 5432
if postgresPort == "" {
var err error
port, err = strconv.Atoi(postgresPort)
if !assert.NoError(t, err) {
return nil, "", errors.New(postPortNotSet)
}
}
pgInstance := &common.Reference{Driver: common.PostgresType,
User: "admin", Host: postgresHost,
Port: port, Database: "bitgarten"}
return pgInstance, postgresPassword, nil
}
func postgresUserTarget(t *testing.T) (string, error) {
postgresHost := os.Getenv("POSTGRES_HOST")
postgresPort := os.Getenv("POSTGRES_PORT")
if !assert.NotEmpty(t, postgresHost) {
return "", errors.New(postgresHostNotSet)
}
assert.NotEmpty(t, postgresPort)
port, err := strconv.Atoi(postgresPort)
if !assert.NoError(t, err) {
return "", errors.New(postPortNotSet)
}
pg := fmt.Sprintf("postgres://<user>:<password>@%s:%d/%s",
postgresHost, port, "bitgarten")
return pg, nil
}
func mysqlTarget(t *testing.T) (string, error) {
mysqlHost := os.Getenv("MYSQL_HOST")
mysqlPort := os.Getenv("MYSQL_PORT")
mysqlPassword := os.Getenv("MYSQL_PWD")
if !assert.NotEmpty(t, mysqlHost) {
return "", fmt.Errorf("MySQL Host not set")
}
assert.NotEmpty(t, mysqlPort)
port, err := strconv.Atoi(mysqlPort)
if !assert.NoError(t, err) {
return "", fmt.Errorf("MYSQL Port not set")
}
mysql := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", "admin",
mysqlPassword, mysqlHost, port, "Bitgarten")
return mysql, nil
}
func mysqlUserTarget(t *testing.T) (string, error) {
mysqlHost := os.Getenv("MYSQL_HOST")
mysqlPort := os.Getenv("MYSQL_PORT")
if !assert.NotEmpty(t, mysqlHost) {
return "", fmt.Errorf("MySQL Host not set")
}
assert.NotEmpty(t, mysqlPort)
port, err := strconv.Atoi(mysqlPort)
if !assert.NoError(t, err) {
return "", fmt.Errorf("MYSQL Port not set")
}
pg := fmt.Sprintf("<user>:<password>@tcp(%s:%d)/%s", mysqlHost, port, "Bitgarten")
return pg, nil
}
func adabasTarget(t *testing.T) (string, error) {
adabasHost := os.Getenv("ADABAS_HOST")
adabasPort := os.Getenv("ADABAS_PORT")
// adabasPassword := os.Getenv("ADABAS_PWD")
if !assert.NotEmpty(t, adabasHost) {
return "", fmt.Errorf("Adabas Host not set")
}
assert.NotEmpty(t, adabasPort)
port, err := strconv.Atoi(adabasPort)
if !assert.NoError(t, err) {
return "", fmt.Errorf("Adabaas Port not set")
}
ada := fmt.Sprintf("adatcp://%s:%d", adabasHost, port)
return ada, nil
}
func TestInitDatabases(t *testing.T) {
pg, err := postgresTarget(t)
if !assert.NoError(t, err) {
return
}
x, err := Handle("postgres", pg)
assert.NoError(t, err)
assert.True(t, x > 0)
assert.Len(t, common.Databases, 1)
err = x.FreeHandler()
if !assert.NoError(t, err) {
return
}
x, err = Handle("postgres", pg)
assert.NoError(t, err)
assert.True(t, x > 0)
pg2, err := postgresTarget(t)
if !assert.NoError(t, err) {
return
}
x2, err := Handle("postgres", pg2)
assert.NoError(t, err)
assert.True(t, x2 > 0)
assert.Len(t, common.Databases, 2)
err = x.FreeHandler()
assert.NoError(t, err)
assert.Len(t, common.Databases, 1)
err = x2.FreeHandler()
assert.NoError(t, err)
assert.Len(t, common.Databases, 0)
x2, err = Handle(pg2)
assert.NoError(t, err)
assert.True(t, x2 > 0)
if assert.Len(t, common.Databases, 1) {
db := common.Databases[0].(*postgres.PostGres)
assert.Equal(t, "postgres", db.CommonDatabase.Driver)
}
err = x2.FreeHandler()
assert.NoError(t, err)
assert.Len(t, common.Databases, 0)
mst, err := mysqlTarget(t)
if !assert.NoError(t, err) {
return
}
x2, err = Handle(mst)
assert.NoError(t, err)
assert.True(t, x2 > 0)
assert.Len(t, common.Databases, 1)
err = x2.FreeHandler()
assert.NoError(t, err)
assert.Len(t, common.Databases, 0)
adat, err := adabasTarget(t)
if !assert.NoError(t, err) {
return
}
x2, err = Handle(adat)
assert.NoError(t, err)
assert.True(t, x2 > 0)
assert.Len(t, common.Databases, 1)
err = x2.FreeHandler()
assert.NoError(t, err)
assert.Len(t, common.Databases, 0)
}
func TestInitWrongDatabases(t *testing.T) {
postgresPort := os.Getenv("POSTGRES_PORT")
assert.NotEmpty(t, postgresPort)
port, err := strconv.Atoi(postgresPort)
if !assert.NoError(t, err) {
return
}
assert.Len(t, common.Databases, 0)
pg := fmt.Sprintf("postgres://%s:%s@%s:%d/%s", "admin", "Test123", "abs", port, "bitgarten")
x, err := Handle("postgres", pg)
defer x.FreeHandler()
assert.NoError(t, err)
assert.NotEqual(t, common.RegDbID(0), x)
err = x.Ping()
assert.Error(t, err)
assert.Len(t, common.Databases, 1)
}