include unit tests for uploader ip

This commit is contained in:
Karol Wypchlo 2022-04-22 15:34:28 +02:00
parent 39f9b4d196
commit f88dcf3eee
No known key found for this signature in database
GPG Key ID: B515DE9EEBE241E1
3 changed files with 68 additions and 25 deletions

View File

@ -235,7 +235,12 @@ location /skynet/skyfile {
local skynet_tracker = require("skynet.tracker") local skynet_tracker = require("skynet.tracker")
if skynet_modules.is_enabled("a") then if skynet_modules.is_enabled("a") then
skynet_tracker.track_upload(ngx.header["Skynet-Skylink"], ngx.status, skynet_account.get_auth_headers()) skynet_tracker.track_upload(
ngx.header["Skynet-Skylink"],
ngx.status,
skynet_account.get_auth_headers(),
ngx.var.remote_addr
)
end end
if skynet_modules.is_enabled("s") then if skynet_modules.is_enabled("s") then
@ -315,7 +320,12 @@ location /skynet/tus {
local skynet_tracker = require("skynet.tracker") local skynet_tracker = require("skynet.tracker")
if skynet_modules.is_enabled("a") then if skynet_modules.is_enabled("a") then
skynet_tracker.track_upload(ngx.header["Skynet-Skylink"], ngx.status, skynet_account.get_auth_headers()) skynet_tracker.track_upload(
ngx.header["Skynet-Skylink"],
ngx.status,
skynet_account.get_auth_headers(),
ngx.var.remote_addr
)
end end
if skynet_modules.is_enabled("s") then if skynet_modules.is_enabled("s") then
@ -346,7 +356,12 @@ location /skynet/pin {
local skynet_tracker = require("skynet.tracker") local skynet_tracker = require("skynet.tracker")
if skynet_modules.is_enabled("a") then if skynet_modules.is_enabled("a") then
skynet_tracker.track_upload(ngx.header["Skynet-Skylink"], ngx.status, skynet_account.get_auth_headers()) skynet_tracker.track_upload(
ngx.header["Skynet-Skylink"],
ngx.status,
skynet_account.get_auth_headers(),
ngx.var.remote_addr
)
end end
if skynet_modules.is_enabled("s") then if skynet_modules.is_enabled("s") then

View File

@ -56,9 +56,8 @@ function _M.track_upload_timer(premature, skylink, auth_headers, uploader_ip)
end end
end end
function _M.track_upload(skylink, status_code, auth_headers) function _M.track_upload(skylink, status_code, auth_headers, uploader_ip)
local status_success = status_code >= 200 and status_code <= 299 local status_success = status_code >= 200 and status_code <= 299
local uploader_ip = ngx.var.remote_addr
if skylink and status_success then if skylink and status_success then
local ok, err = ngx.timer.at(0, _M.track_upload_timer, skylink, auth_headers, uploader_ip) local ok, err = ngx.timer.at(0, _M.track_upload_timer, skylink, auth_headers, uploader_ip)

View File

@ -5,6 +5,7 @@ local skynet_tracker = require("skynet.tracker")
local valid_skylink = "AQBG8n_sgEM_nlEp3G0w3vLjmdvSZ46ln8ZXHn-eObZNjA" local valid_skylink = "AQBG8n_sgEM_nlEp3G0w3vLjmdvSZ46ln8ZXHn-eObZNjA"
local valid_status_code = 200 local valid_status_code = 200
local valid_auth_headers = { ["Skynet-Api-Key"] = "foo" } local valid_auth_headers = { ["Skynet-Api-Key"] = "foo" }
local valid_ip = "12.34.56.78"
describe("track_download", function() describe("track_download", function()
local valid_body_bytes_sent = 12345 local valid_body_bytes_sent = 12345
@ -200,20 +201,21 @@ describe("track_upload", function()
it("should schedule a timer when conditions are met", function() it("should schedule a timer when conditions are met", function()
ngx.timer.at.invokes(function() return true, nil end) ngx.timer.at.invokes(function() return true, nil end)
skynet_tracker.track_upload(valid_skylink, valid_status_code, valid_auth_headers) skynet_tracker.track_upload(valid_skylink, valid_status_code, valid_auth_headers, valid_ip)
assert.stub(ngx.timer.at).was_called_with( assert.stub(ngx.timer.at).was_called_with(
0, 0,
skynet_tracker.track_upload_timer, skynet_tracker.track_upload_timer,
valid_skylink, valid_skylink,
valid_auth_headers valid_auth_headers,
valid_ip
) )
end) end)
it("should not schedule a timer if skylink is empty", function() it("should not schedule a timer if skylink is empty", function()
ngx.timer.at.invokes(function() return true, nil end) ngx.timer.at.invokes(function() return true, nil end)
skynet_tracker.track_upload(nil, valid_status_code, valid_auth_headers) skynet_tracker.track_upload(nil, valid_status_code, valid_auth_headers, valid_ip)
assert.stub(ngx.timer.at).was_not_called() assert.stub(ngx.timer.at).was_not_called()
end) end)
@ -222,11 +224,11 @@ describe("track_upload", function()
ngx.timer.at.invokes(function() return true, nil end) ngx.timer.at.invokes(function() return true, nil end)
-- couple of example of 4XX and 5XX codes -- couple of example of 4XX and 5XX codes
skynet_tracker.track_upload(valid_skylink, 401, valid_auth_headers) skynet_tracker.track_upload(valid_skylink, 401, valid_auth_headers, valid_ip)
skynet_tracker.track_upload(valid_skylink, 403, valid_auth_headers) skynet_tracker.track_upload(valid_skylink, 403, valid_auth_headers, valid_ip)
skynet_tracker.track_upload(valid_skylink, 490, valid_auth_headers) skynet_tracker.track_upload(valid_skylink, 490, valid_auth_headers, valid_ip)
skynet_tracker.track_upload(valid_skylink, 500, valid_auth_headers) skynet_tracker.track_upload(valid_skylink, 500, valid_auth_headers, valid_ip)
skynet_tracker.track_upload(valid_skylink, 502, valid_auth_headers) skynet_tracker.track_upload(valid_skylink, 502, valid_auth_headers, valid_ip)
assert.stub(ngx.timer.at).was_not_called() assert.stub(ngx.timer.at).was_not_called()
end) end)
@ -234,13 +236,14 @@ describe("track_upload", function()
it("should schedule a timer if auth headers are empty", function() it("should schedule a timer if auth headers are empty", function()
ngx.timer.at.invokes(function() return true, nil end) ngx.timer.at.invokes(function() return true, nil end)
skynet_tracker.track_upload(valid_skylink, valid_status_code, {}) skynet_tracker.track_upload(valid_skylink, valid_status_code, {}, valid_ip)
assert.stub(ngx.timer.at).was_called_with( assert.stub(ngx.timer.at).was_called_with(
0, 0,
skynet_tracker.track_upload_timer, skynet_tracker.track_upload_timer,
valid_skylink, valid_skylink,
{} {},
valid_ip
) )
end) end)
@ -248,13 +251,14 @@ describe("track_upload", function()
stub(ngx, "log") stub(ngx, "log")
ngx.timer.at.invokes(function() return false, "such a failure" end) ngx.timer.at.invokes(function() return false, "such a failure" end)
skynet_tracker.track_upload(valid_skylink, valid_status_code, valid_auth_headers) skynet_tracker.track_upload(valid_skylink, valid_status_code, valid_auth_headers, valid_ip)
assert.stub(ngx.timer.at).was_called_with( assert.stub(ngx.timer.at).was_called_with(
0, 0,
skynet_tracker.track_upload_timer, skynet_tracker.track_upload_timer,
valid_skylink, valid_skylink,
valid_auth_headers valid_auth_headers,
valid_ip
) )
assert.stub(ngx.log).was_called_with(ngx.ERR, "Failed to create timer: ", "such a failure") assert.stub(ngx.log).was_called_with(ngx.ERR, "Failed to create timer: ", "such a failure")
@ -284,7 +288,8 @@ describe("track_upload", function()
skynet_tracker.track_upload_timer( skynet_tracker.track_upload_timer(
true, true,
valid_skylink, valid_skylink,
valid_auth_headers valid_auth_headers,
valid_ip
) )
assert.stub(request_uri).was_not_called() assert.stub(request_uri).was_not_called()
@ -302,11 +307,19 @@ describe("track_upload", function()
skynet_tracker.track_upload_timer( skynet_tracker.track_upload_timer(
false, false,
valid_skylink, valid_skylink,
valid_auth_headers valid_auth_headers,
valid_ip
) )
local uri = "http://10.10.10.70:3000/track/upload/" .. valid_skylink local uri = "http://10.10.10.70:3000/track/upload/" .. valid_skylink
assert.stub(request_uri).was_called_with(httpc, uri, { method = "POST", headers = valid_auth_headers }) assert.stub(request_uri).was_called_with(httpc, uri, {
method = "POST",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
["Skynet-Api-Key"] = "foo",
},
body = "ip=" .. valid_ip
})
assert.stub(ngx.log).was_not_called() assert.stub(ngx.log).was_not_called()
end) end)
@ -321,11 +334,19 @@ describe("track_upload", function()
skynet_tracker.track_upload_timer( skynet_tracker.track_upload_timer(
false, false,
valid_skylink, valid_skylink,
valid_auth_headers valid_auth_headers,
valid_ip
) )
local uri = "http://10.10.10.70:3000/track/upload/" .. valid_skylink local uri = "http://10.10.10.70:3000/track/upload/" .. valid_skylink
assert.stub(request_uri).was_called_with(httpc, uri, { method = "POST", headers = valid_auth_headers }) assert.stub(request_uri).was_called_with(httpc, uri, {
method = "POST",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
["Skynet-Api-Key"] = "foo",
},
body = "ip=" .. valid_ip
})
assert.stub(ngx.log).was_called_with( assert.stub(ngx.log).was_called_with(
ngx.ERR, ngx.ERR,
"Failed accounts service request /track/upload/" .. valid_skylink .. ": ", "Failed accounts service request /track/upload/" .. valid_skylink .. ": ",
@ -344,11 +365,19 @@ describe("track_upload", function()
skynet_tracker.track_upload_timer( skynet_tracker.track_upload_timer(
false, false,
valid_skylink, valid_skylink,
valid_auth_headers valid_auth_headers,
valid_ip
) )
local uri = "http://10.10.10.70:3000/track/upload/" .. valid_skylink local uri = "http://10.10.10.70:3000/track/upload/" .. valid_skylink
assert.stub(request_uri).was_called_with(httpc, uri, { method = "POST", headers = valid_auth_headers }) assert.stub(request_uri).was_called_with(httpc, uri, {
method = "POST",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
["Skynet-Api-Key"] = "foo",
},
body = "ip=" .. valid_ip
})
assert.stub(ngx.log).was_called_with( assert.stub(ngx.log).was_called_with(
ngx.ERR, ngx.ERR,
"Failed accounts service request /track/upload/" .. valid_skylink .. ": ", "Failed accounts service request /track/upload/" .. valid_skylink .. ": ",