mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 12:43:09 +00:00
add mongo test case: auth
This commit is contained in:
11
examples/config.mongodb
Normal file
11
examples/config.mongodb
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
root = "./"
|
||||||
|
thread = 8
|
||||||
|
logger = nil
|
||||||
|
harbor = 0
|
||||||
|
start = "main_mongodb" -- main script
|
||||||
|
bootstrap = "snlua bootstrap" -- The service for bootstrap
|
||||||
|
luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua"
|
||||||
|
lualoader = "lualib/loader.lua"
|
||||||
|
snax = root.."examples/?.lua;"..root.."test/?.lua"
|
||||||
|
cpath = root.."cservice/?.so"
|
||||||
|
-- daemon = "./skynet.pid"
|
||||||
12
examples/main_mongodb.lua
Normal file
12
examples/main_mongodb.lua
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
print("Main Server start")
|
||||||
|
local console = skynet.newservice(
|
||||||
|
"testmongodb", "127.0.0.1", 27017, "testdb", "test", "test"
|
||||||
|
)
|
||||||
|
|
||||||
|
print("Main Server exit")
|
||||||
|
skynet.exit()
|
||||||
|
end)
|
||||||
@@ -309,7 +309,7 @@ end
|
|||||||
|
|
||||||
function mongo_db:auth(user, pass)
|
function mongo_db:auth(user, pass)
|
||||||
local authmod = rawget(self.connection, "authmod") or "scram_sha1"
|
local authmod = rawget(self.connection, "authmod") or "scram_sha1"
|
||||||
local auth_func = auth_method[authmod]
|
local auth_func = auth_method["auth_" .. authmod]
|
||||||
assert(auth_func , "Invalid authmod")
|
assert(auth_func , "Invalid authmod")
|
||||||
return auth_func(self, user, pass)
|
return auth_func(self, user, pass)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,6 +3,11 @@ local mongo = require "skynet.db.mongo"
|
|||||||
local bson = require "bson"
|
local bson = require "bson"
|
||||||
|
|
||||||
local host, port, db_name, username, password = ...
|
local host, port, db_name, username, password = ...
|
||||||
|
if port then
|
||||||
|
port = math.tointeger(port)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- print(host, port, db_name, username, password)
|
||||||
|
|
||||||
local function _create_client()
|
local function _create_client()
|
||||||
return mongo.client(
|
return mongo.client(
|
||||||
@@ -14,6 +19,25 @@ local function _create_client()
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function test_auth()
|
||||||
|
local c = mongo.client(
|
||||||
|
{
|
||||||
|
host = host, port = port,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
db = c[db_name]
|
||||||
|
db:auth(username, password)
|
||||||
|
|
||||||
|
db.testdb:dropIndex("*")
|
||||||
|
db.testdb:drop()
|
||||||
|
|
||||||
|
local ok, err, ret = db.testdb:safe_insert({test_key = 1});
|
||||||
|
assert(ok and ret and ret.n == 1, err)
|
||||||
|
|
||||||
|
local ok, err, ret = db.testdb:safe_insert({test_key = 1});
|
||||||
|
assert(ok and ret and ret.n == 1, err)
|
||||||
|
end
|
||||||
|
|
||||||
function test_insert_without_index()
|
function test_insert_without_index()
|
||||||
local db = _create_client()
|
local db = _create_client()
|
||||||
db[db_name].testdb:dropIndex("*")
|
db[db_name].testdb:dropIndex("*")
|
||||||
@@ -105,6 +129,10 @@ function test_expire_index()
|
|||||||
end
|
end
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
|
if username then
|
||||||
|
print("Test auth")
|
||||||
|
test_auth()
|
||||||
|
end
|
||||||
print("Test insert without index")
|
print("Test insert without index")
|
||||||
test_insert_without_index()
|
test_insert_without_index()
|
||||||
print("Test insert index")
|
print("Test insert index")
|
||||||
|
|||||||
Reference in New Issue
Block a user