1 local unpack
= table.unpack
or unpack
; -- luacheck: ignore 113
2 local server
= require
"net.server_select";
3 package
.loaded
["net.server"] = server
;
5 local st
= require
"util.stanza";
7 local function mock_prosody()
9 core_post_stanza
= function () end;
10 events
= require
"util.events".new();
27 sql
= { driver
= "SQLite3", database
= "prosody-tests.sqlite" };
31 sql
= { driver
= "MySQL", database
= "prosody", username
= "prosody", password
= "secret", host
= "localhost" };
35 sql
= { driver
= "PostgreSQL", database
= "prosody", username
= "prosody", password
= "secret", host
= "localhost" };
39 local test_host
= "storage-unit-tests.invalid";
41 describe("storagemanager", function ()
42 for backend
, backend_config
in pairs(configs
) do
43 local tagged_name
= "#"..backend
;
44 if backend
~= backend_config
.storage
then
45 tagged_name
= tagged_name
.." #"..backend_config
.storage
;
47 insulate(tagged_name
.." #storage backend", function ()
50 local config
= require
"core.configmanager";
51 local sm
= require
"core.storagemanager";
52 local hm
= require
"core.hostmanager";
53 local mm
= require
"core.modulemanager";
55 -- Simple check to ensure insulation is working correctly
56 assert.is_nil(config
.get(test_host
, "storage"));
58 for k
, v
in pairs(backend_config
) do
59 config
.set(test_host
, k
, v
);
61 assert(hm
.activate(test_host
, {}));
62 sm
.initialize_host(test_host
);
63 assert(mm
.load(test_host
, "storage_"..backend_config
.storage
));
65 describe("key-value stores", function ()
66 -- These tests rely on being executed in order, disable any order
67 -- randomization for this block
71 it("may be opened", function ()
72 store
= assert(sm
.open(test_host
, "test"));
75 local simple_data
= { foo
= "bar" };
77 it("may set data for a user", function ()
78 assert(store
:set("user9999", simple_data
));
81 it("may get data for a user", function ()
82 assert.same(simple_data
, assert(store
:get("user9999")));
85 it("may remove data for a user", function ()
86 assert(store
:set("user9999", nil));
87 local ret
, err
= store
:get("user9999");
93 describe("archive stores", function ()
97 it("can be opened", function ()
98 archive
= assert(sm
.open(test_host
, "test-archive", "archive"));
101 local test_stanza
= st
.stanza("test", { xmlns
= "urn:example:foo" })
104 local test_time
= 1539204123;
107 { nil, test_stanza
, test_time
, "contact@example.com" };
108 { nil, test_stanza
, test_time
+1, "contact2@example.com" };
109 { nil, test_stanza
, test_time
+2, "contact2@example.com" };
110 { nil, test_stanza
, test_time
-1, "contact2@example.com" };
113 it("can be added to", function ()
114 for _
, data_item
in ipairs(test_data
) do
115 local ok
= archive
:append("user", unpack(data_item
, 1, 4));
120 describe("can be queried", function ()
121 it("for all items", function ()
122 local data
, err
= archive
:find("user", {});
125 for id
, item
, when
in data
do
128 assert(st
.is_stanza(item
));
129 assert.equal("test", item
.name
);
130 assert.equal("urn:example:foo", item
.attr
.xmlns
);
131 assert.equal(2, #item
.tags
);
132 assert.equal(test_data
[count
][3], when
);
134 assert.equal(#test_data
, count
);
137 it("by JID", function ()
138 local data
, err
= archive
:find("user", {
139 with
= "contact@example.com";
143 for id
, item
, when
in data
do
146 assert(st
.is_stanza(item
));
147 assert.equal("test", item
.name
);
148 assert.equal("urn:example:foo", item
.attr
.xmlns
);
149 assert.equal(2, #item
.tags
);
150 assert.equal(test_time
, when
);
152 assert.equal(1, count
);
155 it("by time (end)", function ()
156 local data
, err
= archive
:find("user", {
161 for id
, item
, when
in data
do
164 assert(st
.is_stanza(item
));
165 assert.equal("test", item
.name
);
166 assert.equal("urn:example:foo", item
.attr
.xmlns
);
167 assert.equal(2, #item
.tags
);
168 assert(test_time
>= when
);
170 assert.equal(2, count
);
173 it("by time (start)", function ()
174 local data
, err
= archive
:find("user", {
175 ["start"] = test_time
;
179 for id
, item
, when
in data
do
182 assert(st
.is_stanza(item
));
183 assert.equal("test", item
.name
);
184 assert.equal("urn:example:foo", item
.attr
.xmlns
);
185 assert.equal(2, #item
.tags
);
186 assert(test_time
<= when
);
188 assert.equal(#test_data
-1, count
);
191 it("by time (start+end)", function ()
192 local data
, err
= archive
:find("user", {
193 ["start"] = test_time
;
194 ["end"] = test_time
+1;
198 for id
, item
, when
in data
do
201 assert(st
.is_stanza(item
));
202 assert.equal("test", item
.name
);
203 assert.equal("urn:example:foo", item
.attr
.xmlns
);
204 assert.equal(2, #item
.tags
);
205 assert(when
>= test_time
, ("%d >= %d"):format(when
, test_time
));
206 assert(when
<= test_time
+1, ("%d <= %d"):format(when
, test_time
+1));
208 assert.equal(2, count
);
212 it("can selectively delete items", function ()
215 local data
= assert(archive
:find("user", {}));
217 for id
, item
, when
in data
do --luacheck: ignore 213/item 213/when
224 assert.equal(#test_data
, count
);
227 assert(archive
:delete("user", { key
= delete_id
}));
230 local data
= assert(archive
:find("user", {}));
232 for id
, item
, when
in data
do --luacheck: ignore 213/item 213/when
235 assert.not_equal(delete_id
, id
);
237 assert.equal(#test_data
-1, count
);
241 it("can be purged", function ()
242 local ok
, err
= archive
:delete("user");
244 local data
, err
= archive
:find("user", {
245 with
= "contact@example.com";
249 for id
, item
, when
in data
do -- luacheck: ignore id item when
252 assert.equal(0, count
);
255 it("can truncate the oldest items", function ()
256 local username
= "user-truncate";
258 assert(archive
:append(username
, nil, test_stanza
, i
, "contact@example.com"));
260 assert(archive
:delete(username
, { truncate
= 3 }));
263 local data
= assert(archive
:find(username
, {}));
265 for id
, item
, when
in data
do --luacheck: ignore 213/when
268 assert(st
.is_stanza(item
));
269 assert(when
> 7, ("%d > 7"):format(when
));
271 assert.equal(3, count
);
275 it("overwrites existing keys with new data", function ()
276 local prefix
= ("a"):rep(50);
277 local username
= "user-overwrite";
278 assert(archive
:append(username
, prefix
.."-1", test_stanza
, test_time
, "contact@example.com"));
279 assert(archive
:append(username
, prefix
.."-2", test_stanza
, test_time
, "contact@example.com"));
282 local data
= assert(archive
:find(username
, {}));
284 for id
, item
, when
in data
do --luacheck: ignore 213/when
287 assert.equals(("%s-%d"):format(prefix
, count
), id
);
288 assert(st
.is_stanza(item
));
290 assert.equal(2, count
);
293 local new_stanza
= st
.clone(test_stanza
);
294 new_stanza
.attr
.foo
= "bar";
295 assert(archive
:append(username
, prefix
.."-2", new_stanza
, test_time
+1, "contact2@example.com"));
298 local data
= assert(archive
:find(username
, {}));
300 for id
, item
, when
in data
do
303 assert.equals(("%s-%d"):format(prefix
, count
), id
);
304 assert(st
.is_stanza(item
));
306 assert.equals(test_time
+1, when
);
307 assert.equals("bar", item
.attr
.foo
);
310 assert.equal(2, count
);
314 it("can contain multiple long unique keys #issue1073", function ()
315 local prefix
= ("a"):rep(50);
316 assert(archive
:append("user-issue1073", prefix
.."-1", test_stanza
, test_time
, "contact@example.com"));
317 assert(archive
:append("user-issue1073", prefix
.."-2", test_stanza
, test_time
, "contact@example.com"));
319 local data
= assert(archive
:find("user-issue1073", {}));
321 for id
, item
, when
in data
do --luacheck: ignore 213/when
324 assert(st
.is_stanza(item
));
326 assert.equal(2, count
);
327 assert(archive
:delete("user-issue1073"));