-
Notifications
You must be signed in to change notification settings - Fork 84
/
dbtest.rs
296 lines (280 loc) · 10.1 KB
/
dbtest.rs
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Created on Wed Nov 29 2023
*
* This file is a part of Skytable
* Skytable (formerly known as TerrabaseDB or Skybase) is a free and open-source
* NoSQL database written by Sayan Nandan ("the Author") with the
* vision to provide flexibility in data modelling without compromising
* on performance, queryability or scalability.
*
* Copyright (c) 2023, Sayan Nandan <ohsayan@outlook.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
use {
crate::util::{self, AttributeKind},
proc_macro::TokenStream,
quote::quote,
std::collections::HashMap,
syn::{parse_macro_input, AttributeArgs, ItemFn},
};
/*
host setup
*/
#[derive(Debug)]
enum DbTestClient {
Skyhash,
Tcp,
}
struct DbConfig {
client: DbTestClient,
port: u16,
host: String,
}
impl Default for DbConfig {
fn default() -> Self {
Self {
client: DbTestClient::Skyhash,
port: libsky::test_utils::DEFAULT_PORT,
host: libsky::test_utils::DEFAULT_HOST.into(),
}
}
}
/*
client setup
*/
#[derive(Debug)]
struct ClientConfig {
username: String,
password: String,
}
impl Default for ClientConfig {
fn default() -> Self {
Self {
username: libsky::test_utils::DEFAULT_USER_NAME.into(),
password: libsky::test_utils::DEFAULT_USER_PASS.into(),
}
}
}
/*
test setup
*/
#[derive(Debug)]
enum TestStrategy {
Standard,
Relogin { username: String, password: String },
}
impl TestStrategy {
fn is_relogin(&self) -> bool {
matches!(self, TestStrategy::Relogin { .. })
}
}
struct TestSetup {
client: ClientConfig,
db: DbConfig,
strategy: TestStrategy,
}
fn parse_attrs(attrs: AttributeArgs) -> TestSetup {
let mut db_config = DbConfig::default();
let mut client_config = ClientConfig::default();
let mut collected_attrs = HashMap::new();
let mut strategy = TestStrategy::Standard;
for attr in attrs {
match util::extract_attribute(&attr) {
AttributeKind::Pair(k, v) => {
assert!(
collected_attrs.insert(k.to_string(), v).is_none(),
"duplicate key: {}",
k.to_string()
);
continue;
}
AttributeKind::NestedAttrs { name, attrs } => match name.to_string().as_str() {
"switch_user" => {
if strategy.is_relogin() {
panic!("already set `switch_user` strategy");
}
let mut username = None;
let mut password = None;
for (key, data) in attrs.into_iter().map(AttributeKind::into_pair) {
match key.to_string().as_str() {
"username" => {
assert!(username.is_none(), "username already set");
username = Some(util::extract_str_from_lit(&data).unwrap());
}
"password" => {
assert!(password.is_none(), "password already set");
password = Some(util::extract_str_from_lit(&data).unwrap());
}
unknown_subattr => panic!(
"unknown sub-attribute for `switch_user`: `{unknown_subattr}`"
),
}
}
assert!(username.is_some(), "username must be set");
strategy = TestStrategy::Relogin {
username: username.unwrap(),
password: password.unwrap_or(libsky::test_utils::DEFAULT_USER_PASS.into()),
};
}
unknown => panic!("unknown nested attribute `{unknown}`"),
},
AttributeKind::Path(_) | AttributeKind::Lit(_) => {
panic!("unexpected tokens")
}
}
}
for (attr_name, attr_val) in collected_attrs {
match attr_name.as_str() {
"client" => match util::extract_str_from_lit(&attr_val).unwrap().as_str() {
"skyhash" => db_config.client = DbTestClient::Skyhash,
"tcp" => db_config.client = DbTestClient::Tcp,
unknown_client => panic!("unknown client mode {unknown_client}"),
},
"port" => db_config.port = util::extract_int_from_lit(&attr_val).unwrap(),
"host" => db_config.host = util::extract_str_from_lit(&attr_val).unwrap(),
"username" => {
assert!(
!strategy.is_relogin(),
"`username` makes no sense when used with strategy `switch_user`. instead, set dbtest(switch_user(username = ...))"
);
client_config.username = util::extract_str_from_lit(&attr_val).unwrap()
}
"password" => {
assert!(
!strategy.is_relogin(),
"`password` makes no sense when used with strategy `switch_user`. instead, set dbtest(switch_user(password = ...))"
);
client_config.password = util::extract_str_from_lit(&attr_val).unwrap();
}
unknown_attr => panic!("unknown dbtest attribute `{unknown_attr}`"),
}
}
TestSetup {
client: client_config,
db: db_config,
strategy,
}
}
pub fn dbtest(attrs: TokenStream, item: TokenStream) -> TokenStream {
let attr_args = parse_macro_input!(attrs as AttributeArgs);
let input_fn = parse_macro_input!(item as ItemFn);
let TestSetup {
client:
ClientConfig {
username: login_username,
password: login_password,
},
db: DbConfig { client, port, host },
strategy,
} = parse_attrs(attr_args);
let function_attrs = &input_fn.attrs;
let function_vis = &input_fn.vis;
let function_sig = &input_fn.sig;
let function_block = &input_fn.block;
let retfn = quote!(
#(#function_attrs)* #function_vis #function_sig
);
let mut block = quote! {
const __DBTEST_HOST: &str = #host;
const __DBTEST_PORT: u16 = #port;
};
match strategy {
TestStrategy::Standard => {
block = quote! {
#block
/// username set by [`sky_macros::dbtest`]
const __DBTEST_USER: &str = #login_username;
/// password set by [`sky_macros::dbtest`]
const __DBTEST_PASS: &str = #login_password;
};
}
TestStrategy::Relogin {
username: ref new_username,
password: ref new_password,
} => {
// we need to create an user, log in and log out
block = quote! {
#block
/// username set by [`sky_macros::dbtest`] (relogin)
const __DBTEST_USER: &str = #new_username;
/// password set by [`sky_macros::dbtest`] (relogin)
const __DBTEST_PASS: &str = #new_password;
{
let query_assembled = format!("sysctl create user {} with {{ password: ? }}", #new_username);
let mut db = skytable::Config::new(#host, #port, #login_username, #login_password).connect().unwrap();
db.query_parse::<()>(&skytable::query!(query_assembled, #new_password)).unwrap();
}
};
}
}
match client {
DbTestClient::Skyhash => {
block = quote! {
#block
/// Get a Skyhash connection the database (defined by [`sky_macros::dbtest`])
#[allow(unused_macros)]
macro_rules! db_connect {
() => {{
skytable::Config::new(__DBTEST_HOST, __DBTEST_PORT, __DBTEST_USER, __DBTEST_PASS).connect()
}}
}
/// Get a Skyhash connection the database (defined by [`sky_macros::dbtest`])
#[allow(unused_macros)]
macro_rules! db {
() => {{
skytable::Config::new(__DBTEST_HOST, __DBTEST_PORT, __DBTEST_USER, __DBTEST_PASS).connect().unwrap()
}}
}
};
}
DbTestClient::Tcp => {
block = quote! {
#block
/// Get a TCP connection the database (defined by [`sky_macros::dbtest`])
macro_rules! tcp {
() => {{
std::net::TcpStream::connect((__DBTEST_HOST, __DBTEST_PORT)).unwrap()
}}
}
};
}
}
let mut ret_block = quote! {
#block
#function_block
};
match strategy {
TestStrategy::Relogin { ref username, .. } => {
let new_username = username;
ret_block = quote! {
#ret_block
{
let query_assembled = format!("sysctl drop user {}", #new_username);
let mut db = skytable::Config::new(#host, #port, #login_username, #login_password).connect().unwrap();
db.query_parse::<()>(&skytable::query!(query_assembled)).unwrap();
}
};
}
TestStrategy::Standard => {}
}
let ret = quote! {
#[core::prelude::v1::test]
#retfn {
#ret_block
}
};
ret.into()
}