Software /
code /
prosody
Annotate
spec/core_storagemanager_spec.lua @ 9466:b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 10 Oct 2018 22:00:37 +0100 |
parent | 9452:9d892b2415bf |
child | 9467:2098794ac866 |
rev | line source |
---|---|
9389
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local server = require "net.server_select"; |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 package.loaded["net.server"] = server; |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
9466
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
4 local st = require "util.stanza"; |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
5 |
9389
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local function mock_prosody() |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 _G.prosody = { |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 core_post_stanza = function () end; |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 events = require "util.events".new(); |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 hosts = {}; |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 paths = { |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 data = "./data"; |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 }; |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 }; |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 end |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 local configs = { |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 internal = { |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 storage = "internal"; |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 }; |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 sqlite = { |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 storage = "sql"; |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 sql = { driver = "SQLite3", database = "prosody-tests.sqlite" }; |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 }; |
9452
9d892b2415bf
Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents:
9389
diff
changeset
|
25 mysql = { |
9d892b2415bf
Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents:
9389
diff
changeset
|
26 storage = "sql"; |
9d892b2415bf
Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents:
9389
diff
changeset
|
27 sql = { driver = "MySQL", database = "prosody", username = "prosody", password = "secret", host = "localhost" }; |
9d892b2415bf
Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents:
9389
diff
changeset
|
28 }; |
9d892b2415bf
Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents:
9389
diff
changeset
|
29 postgres = { |
9d892b2415bf
Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents:
9389
diff
changeset
|
30 storage = "sql"; |
9d892b2415bf
Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents:
9389
diff
changeset
|
31 sql = { driver = "PostgreSQL", database = "prosody", username = "prosody", password = "secret", host = "localhost" }; |
9d892b2415bf
Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents:
9389
diff
changeset
|
32 }; |
9389
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 }; |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 local test_host = "storage-unit-tests.invalid"; |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 describe("storagemanager", function () |
9452
9d892b2415bf
Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents:
9389
diff
changeset
|
38 for backend, backend_config in pairs(configs) do |
9389
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 local tagged_name = "#"..backend; |
9452
9d892b2415bf
Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents:
9389
diff
changeset
|
40 if backend ~= backend_config.storage then |
9d892b2415bf
Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents:
9389
diff
changeset
|
41 tagged_name = tagged_name.." #"..backend_config.storage; |
9389
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 end |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 insulate(tagged_name.." #storage backend", function () |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 mock_prosody(); |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 local config = require "core.configmanager"; |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 local sm = require "core.storagemanager"; |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 local hm = require "core.hostmanager"; |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 local mm = require "core.modulemanager"; |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 -- Simple check to ensure insulation is working correctly |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 assert.is_nil(config.get(test_host, "storage")); |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 for k, v in pairs(backend_config) do |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 config.set(test_host, k, v); |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 end |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 assert(hm.activate(test_host, {})); |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 sm.initialize_host(test_host); |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 assert(mm.load(test_host, "storage_"..backend_config.storage)); |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 |
9466
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
61 describe("key-value stores", function () |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
62 -- These tests rely on being executed in order, disable any order |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
63 -- randomization for this block |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
64 randomize(false); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
65 |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
66 local store; |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
67 it("may be opened", function () |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
68 store = assert(sm.open(test_host, "test")); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
69 end); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
70 |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
71 local simple_data = { foo = "bar" }; |
9389
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 |
9466
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
73 it("may set data for a user", function () |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
74 assert(store:set("user9999", simple_data)); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
75 end); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
76 |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
77 it("may get data for a user", function () |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
78 assert.same(simple_data, assert(store:get("user9999"))); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
79 end); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
80 |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
81 it("may remove data for a user", function () |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
82 assert(store:set("user9999", nil)); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
83 local ret, err = store:get("user9999"); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
84 assert.is_nil(ret); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
85 assert.is_nil(err); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
86 end); |
9389
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 end); |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 |
9466
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
89 describe("archive stores", function () |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
90 randomize(false); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
91 |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
92 local archive; |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
93 it("can be opened", function () |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
94 archive = assert(sm.open(test_host, "test-archive", "archive")); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
95 end); |
9389
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 |
9466
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
97 local test_stanza = st.stanza("test", { xmlns = "urn:example:foo" }) |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
98 :tag("foo"):up() |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
99 :tag("foo"):up(); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
100 local test_time = 1539204123; |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
101 it("can be added to", function () |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
102 local ok = archive:append("user", nil, test_stanza, 1539204123, "contact@example.com"); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
103 assert.truthy(ok); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
104 end); |
9389
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 |
9466
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
106 it("can be queried", function () |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
107 local data, err = archive:find("user", { |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
108 with = "contact@example.com"; |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
109 }); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
110 assert.truthy(data); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
111 for id, item, when in data do |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
112 assert.truthy(id); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
113 assert(st.is_stanza(item)); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
114 assert.equal("test", item.name); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
115 assert.equal("urn:example:foo", item.attr.xmlns); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
116 assert.equal(2, #item.tags); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
117 assert.equal(test_time, when); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
118 end |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
119 end); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
120 it("can be purged", function () |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
121 local ok, err = archive:delete("user"); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
122 assert.truthy(ok); |
b70ce39d366f
storagemanager tests: Add initial basic tests for archive stores
Matthew Wild <mwild1@gmail.com>
parents:
9452
diff
changeset
|
123 end); |
9389
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 end); |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 end); |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 end |
9ae575efbb1f
Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 end); |