@@ -17,7 +17,6 @@ import (
17
17
"time"
18
18
19
19
"github.com/docker/docker/api/types/container"
20
- "github.com/docker/go-units"
21
20
"github.com/grafana/grafana-plugin-sdk-go/backend"
22
21
"github.com/grafana/grafana-plugin-sdk-go/data"
23
22
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
@@ -57,35 +56,29 @@ func TestMain(m *testing.M) {
57
56
}
58
57
questDbName := GetEnv ("QUESTDB_NAME" , "questdb/questdb" )
59
58
questDbVersion := GetEnv ("QUESTDB_VERSION" , "latest" )
59
+ questDbTlsEnabled := GetEnv ("QUESTDB_TLS_ENABLED" , "false" )
60
60
fmt .Printf ("Using Docker for tests with QuestDB %s:%s\n " , questDbName , questDbVersion )
61
+
61
62
cwd , err := os .Getwd ()
62
63
if err != nil {
63
64
panic (err )
64
65
}
65
66
66
- keysPath := "../../config/keys"
67
- serverConfPath := "../../config/server.conf"
67
+ keysPath := "../../keys"
68
68
69
69
req := testcontainers.ContainerRequest {
70
70
Env : map [string ]string {
71
- "TZ" : "UTC" ,
71
+ "TZ" : "UTC" ,
72
+ "QDB_PG_TLS_ENABLED" : questDbTlsEnabled ,
73
+ "QDB_PG_TLS_CERT_PATH" : "/var/lib/questdb/conf/keys/server.crt" ,
74
+ "QDB_PG_TLS_PRIVATE_KEY_PATH" : "/var/lib/questdb/conf/keys/server.key" ,
72
75
},
73
76
ExposedPorts : []string {"9000/tcp" , "8812/tcp" },
74
77
HostConfigModifier : func (config * container.HostConfig ) {
75
78
config .Mounts = append (config .Mounts ,
76
- mount.Mount {Source : path .Join (cwd , serverConfPath ), Target : "/var/lib/questdb/conf/server.conf" , ReadOnly : true , Type : mount .TypeBind },
77
79
mount.Mount {Source : path .Join (cwd , keysPath ), Target : "/var/lib/questdb/conf/keys" , ReadOnly : true , Type : mount .TypeBind })
78
80
},
79
- Image : fmt .Sprintf ("%s:%s" , questDbName , questDbVersion ),
80
- Resources : container.Resources {
81
- Ulimits : []* units.Ulimit {
82
- {
83
- Name : "nofile" ,
84
- Hard : 262144 ,
85
- Soft : 262144 ,
86
- },
87
- },
88
- },
81
+ Image : fmt .Sprintf ("%s:%s" , questDbName , questDbVersion ),
89
82
WaitingFor : wait .ForLog ("A server-main enjoy" ),
90
83
}
91
84
questdbContainer , err := testcontainers .GenericContainer (ctx , testcontainers.GenericContainerRequest {
@@ -115,45 +108,40 @@ func TestConnect(t *testing.T) {
115
108
host := getEnv ("QUESTDB_HOST" , "localhost" )
116
109
username := getEnv ("QUESTDB_USERNAME" , "admin" )
117
110
password := getEnv ("QUESTDB_PASSWORD" , "quest" )
118
- tlsMode := getEnv ("QUESTDB_SSL" , "disable" )
119
- queryTimeoutNumber := 3600
120
- queryTimeoutString := "3600"
121
- questdb := plugin.QuestDB {}
122
- t .Run ("should not error when valid settings passed" , func (t * testing.T ) {
123
- secure := map [string ]string {}
124
- secure ["password" ] = password
125
- settings := backend.DataSourceInstanceSettings {JSONData : []byte (fmt .Sprintf (`{ "server": "%s", "port": %s, "username": "%s", "queryTimeout": "%s", "tlsMode": "%s"}` ,
126
- host , port , username , queryTimeoutString , tlsMode )), DecryptedSecureJSONData : secure }
127
- _ , err := questdb .Connect (settings , json.RawMessage {})
128
- assert .Equal (t , nil , err )
129
- })
130
- t .Run ("should not error when valid settings passed - with query timeout as number" , func (t * testing.T ) {
131
- secure := map [string ]string {}
132
- secure ["password" ] = password
133
- settings := backend.DataSourceInstanceSettings {JSONData : []byte (fmt .Sprintf (`{ "server": "%s", "port": %s, "username": "%s", "queryTimeout": %d, "tlsMode": "%s"}` ,
134
- host , port , username , queryTimeoutNumber , tlsMode )), DecryptedSecureJSONData : secure }
135
- _ , err := questdb .Connect (settings , json.RawMessage {})
136
- assert .Equal (t , nil , err )
137
- })
138
- }
111
+ tlsEnabled := getEnv ("QUESTDB_TLS_ENABLED" , "false" )
112
+ queryTimeout := 3600
113
+ connectTimeout := 1000
114
+ maxOpenConns := 10
115
+ maxIdleConns := 5
116
+ maxConnLife := 14400
139
117
140
- func TestPgWireConnect (t * testing.T ) {
141
- port := getEnv ("QUESTDB_PORT" , "8812" )
142
- host := getEnv ("QUESTDB_HOST" , "localhost" )
143
- username := getEnv ("QUESTDB_USERNAME" , "admin" )
144
- password := getEnv ("QUESTDB_PASSWORD" , "quest" )
145
- tlsMode := getEnv ("QUESTDB_SSL" , "disable" )
146
118
questdb := plugin.QuestDB {}
147
- t .Run ("should not error when valid settings passed" , func (t * testing.T ) {
148
- secure := map [string ]string {}
149
- secure ["password" ] = password
150
- settings := backend.DataSourceInstanceSettings {JSONData : []byte (fmt .Sprintf (`{ "server": "%s", "port": %s, "username": "%s", "password": "%s", "tlsMode": "%s"}` , host , port , username , password , tlsMode )), DecryptedSecureJSONData : secure }
151
- _ , err := questdb .Connect (settings , json.RawMessage {})
152
- assert .Equal (t , nil , err )
153
- })
119
+
120
+ var tlsModes []string
121
+ if tlsEnabled == "true" {
122
+ tlsModes = []string {"require" , "verify-ca" , "verify-full" }
123
+ } else {
124
+ tlsModes = []string {"disable" }
125
+ }
126
+
127
+ for _ , tlsMode := range tlsModes {
128
+ t .Run ("should not error when valid settings passed, tlsMode: " + tlsMode , func (t * testing.T ) {
129
+ secure := map [string ]string {}
130
+ secure ["password" ] = password
131
+ settings := backend.DataSourceInstanceSettings {JSONData : []byte (fmt .Sprintf (
132
+ `{ "server": "%s", "port": %s, "username": "%s", "tlsMode": "%s", "queryTimeout": "%d", "timeout": "%d", "maxOpenConnections": "%d", "maxIdleConnections": "%d", "maxConnectionLifetime": "%d" }` ,
133
+ host , port , username , tlsMode , queryTimeout , connectTimeout , maxOpenConns , maxIdleConns , maxConnLife )), DecryptedSecureJSONData : secure }
134
+
135
+ db , err := questdb .Connect (settings , json.RawMessage {})
136
+ assert .Equal (t , nil , err )
137
+
138
+ err = db .Ping ()
139
+ assert .Equal (t , nil , err )
140
+ })
141
+ }
154
142
}
155
143
156
- func setupConnection (t * testing.T , settings * plugin. Settings ) * sql.DB {
144
+ func setupConnection (t * testing.T ) * sql.DB {
157
145
port , err := strconv .ParseInt (getEnv ("QUESTDB_PORT" , "8812" ), 10 , 64 )
158
146
if err != nil {
159
147
panic (err )
@@ -162,7 +150,7 @@ func setupConnection(t *testing.T, settings *plugin.Settings) *sql.DB {
162
150
host := getEnv ("QUESTDB_HOST" , "localhost" )
163
151
username := getEnv ("QUESTDB_USERNAME" , "admin" )
164
152
password := getEnv ("QUESTDB_PASSWORD" , "quest" )
165
- tlsMode := getEnv ("QUESTDB_SSL " , "disable " )
153
+ tlsEnabled := getEnv ("QUESTDB_TLS_ENABLED " , "false " )
166
154
tlsConfigurationMethod := getEnv ("QUESTDB_METHOD" , "file-content" )
167
155
tlsCaCert := getEnv ("QUESTDB_CA_CERT" , `
168
156
-----BEGIN CERTIFICATE-----
@@ -189,7 +177,13 @@ TIFr7kfJsOwa+y1x3aTs/7VSwNjfS4FqbvXy3S7OAOs=
189
177
pool := x509 .NewCertPool ()
190
178
pool .AppendCertsFromPEM ([]byte (tlsCaCert ))
191
179
192
- // we create a direct connection since we need specific settings for insert
180
+ var tlsMode string
181
+ if tlsEnabled == "true" {
182
+ tlsMode = "verify-full"
183
+ } else {
184
+ tlsMode = "disable"
185
+ }
186
+
193
187
cnnstr , err := plugin .GenerateConnectionString (plugin.Settings {
194
188
Server : host ,
195
189
Port : port ,
@@ -209,7 +203,7 @@ TIFr7kfJsOwa+y1x3aTs/7VSwNjfS4FqbvXy3S7OAOs=
209
203
}
210
204
211
205
func TestInsertAndQueryData (t * testing.T ) {
212
- conn := setupConnection (t , nil )
206
+ conn := setupConnection (t )
213
207
214
208
_ , err := conn .Exec ("DROP TABLE IF EXISTS all_types" )
215
209
require .NoError (t , err )
0 commit comments