spec/core_storagemanager_spec.lua
author Matthew Wild <mwild1@gmail.com>
Mon, 01 Oct 2018 20:21:26 +0100
changeset 9455 9d892b2415bf
parent 9392 spec/core_storagemanager.lua@9ae575efbb1f
child 9469 b70ce39d366f
permissions -rw-r--r--
Fix storage tests so they run, but not by default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9392
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
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
local function mock_prosody()
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
	_G.prosody = {
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
		core_post_stanza = function () end;
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
		events = require "util.events".new();
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
		hosts = {};
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
		paths = {
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
			data = "./data";
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
		};
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
	};
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
end
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
local configs = {
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
	internal = {
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
		storage = "internal";
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
	};
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
	sqlite = {
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
		storage = "sql";
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
		sql = { driver = "SQLite3", database = "prosody-tests.sqlite" };
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
	};
9455
9d892b2415bf Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents: 9392
diff changeset
    23
	mysql = {
9d892b2415bf Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents: 9392
diff changeset
    24
		storage = "sql";
9d892b2415bf Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents: 9392
diff changeset
    25
		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: 9392
diff changeset
    26
	};
9d892b2415bf Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents: 9392
diff changeset
    27
	postgres = {
9d892b2415bf Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents: 9392
diff changeset
    28
		storage = "sql";
9d892b2415bf Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents: 9392
diff changeset
    29
		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: 9392
diff changeset
    30
	};
9392
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
};
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
local test_host = "storage-unit-tests.invalid";
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
describe("storagemanager", function ()
9455
9d892b2415bf Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents: 9392
diff changeset
    36
	for backend, backend_config in pairs(configs) do
9392
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
		local tagged_name = "#"..backend;
9455
9d892b2415bf Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents: 9392
diff changeset
    38
		if backend ~= backend_config.storage then
9d892b2415bf Fix storage tests so they run, but not by default
Matthew Wild <mwild1@gmail.com>
parents: 9392
diff changeset
    39
			tagged_name = tagged_name.." #"..backend_config.storage;
9392
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
		end
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
		insulate(tagged_name.." #storage backend", function ()
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
			mock_prosody();
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
			local config = require "core.configmanager";
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
			local sm = require "core.storagemanager";
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
			local hm = require "core.hostmanager";
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
			local mm = require "core.modulemanager";
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
			-- 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
    50
			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
    51
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
			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
    53
				config.set(test_host, k, v);
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
			end
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
			assert(hm.activate(test_host, {}));
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
			sm.initialize_host(test_host);
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
			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
    58
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
			-- These tests rely on being executed in order, disable any order
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
			-- randomization for this block
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
			randomize(false);
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
			local store;
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
			it("may open a store", function ()
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
				store = assert(sm.open(test_host, "test"));
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
			end);
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
			local simple_data = { foo = "bar" };
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
			it("may set data for a user", function ()
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
				assert(store:set("user9999", simple_data));
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
			end);
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
			it("may get data for a user", function ()
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
				assert.same(simple_data, assert(store:get("user9999")));
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
			end);
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
			it("may remove data for a user", function ()
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
				assert(store:set("user9999", nil));
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
				local ret, err = store:get("user9999");
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
				assert.is_nil(ret);
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
				assert.is_nil(err);
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
			end);
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
		end);
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
	end
9ae575efbb1f Add storage tests (currently only internal and sqlite)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
end);