Created
October 23, 2012 01:27
-
-
Save kiwanami/3936125 to your computer and use it in GitHub Desktop.
gridfs-proxyのrspecによるテスト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
require 'mongo' | |
require 'stringio' | |
MONGO_PORT = 28017 | |
HTTP_PORT = 5001 | |
$mongodb = nil | |
def prepare_test_mongodb | |
if $mongodb then | |
clear_all_mongo_files | |
return | |
end | |
abort "Another mongodb is running!!" if `pgrep mongod` != '' | |
unless File.directory?("./testdb") then | |
Dir.mkdir("./testdb") | |
end | |
mongo_proc = IO.popen("mongod --port #{MONGO_PORT} --dbpath ./testdb", "r") | |
print "Waiting for mongod starting up." | |
$stdout.flush | |
loop do | |
line = mongo_proc.gets | |
if line =~ /waiting for connections/ then | |
puts "OK" | |
t = Thread.new do | |
loop do | |
mongo_proc.gets | |
end | |
end | |
break | |
end | |
print "." | |
$stdout.flush | |
end | |
at_exit do | |
Process.kill(2,mongo_proc.pid) | |
Process.waitpid(mongo_proc.pid) | |
end | |
$mongodb = true | |
clear_all_mongo_files | |
end | |
def clear_mongo_files(conn, dbname) | |
db = conn.db(dbname) | |
gfs = Mongo::Grid.new(db) | |
files = gfs.instance_variable_get :@files | |
list = files.find.to_a | |
return list if list.size == 0 | |
print "clearing #{list.size} files from #{dbname} ... " | |
list.each do |i| | |
gfs.delete(i["_id"]) | |
end | |
puts " ok." | |
end | |
def clear_all_mongo_files | |
conn = Mongo::Connection.new("localhost",MONGO_PORT) | |
begin | |
clear_mongo_files(conn,"test") | |
ensure | |
conn.close | |
end | |
end | |
def _add_mongo(content_type, data, oid = nil) | |
conn = Mongo::Connection.new("localhost",MONGO_PORT) | |
begin | |
db = conn.db("test") | |
fs = Mongo::Grid.new(db) | |
opt = {:content_type => content_type, } | |
opt[:_id] = oid if oid | |
oid = fs.put(data, opt) | |
return oid | |
ensure | |
conn.close | |
end | |
end | |
$test_process = nil | |
def prepare_process(cmd,wait_regexp) | |
if $test_process then | |
return | |
end | |
cmd_proc = IO.popen(cmd, "r") | |
logfile = File.open("./cmd.log","w") | |
print "Waiting for command starting up." | |
$stdout.flush | |
loop do | |
line = cmd_proc.gets | |
if line =~ wait_regexp then | |
puts "OK" | |
t = Thread.new do | |
loop do | |
a = cmd_proc.gets | |
break unless a | |
logfile.puts(a) | |
end | |
end | |
break | |
end | |
print line | |
$stdout.flush | |
end | |
at_exit do | |
Process.kill(2,cmd_proc.pid) | |
Process.waitpid(cmd_proc.pid) | |
logfile.close | |
end | |
$test_process = true | |
end | |
THEADER = '.header_dump' | |
TBODY = '.loaded_file' | |
def curl_get(url,opts) | |
begin | |
#puts url | |
`curl -s -D #{THEADER} -o #{TBODY} #{opts} http://localhost:#{HTTP_PORT}/#{url}` | |
raise "Header File is not found" unless File.exist?(THEADER) | |
raise "Download File is not found" unless File.exist?(TBODY) | |
yield THEADER,TBODY | |
ensure | |
File.delete(THEADER) if File.exist?(THEADER) | |
File.delete(TBODY) if File.exist?(TBODY) | |
end | |
end | |
def md5sum(file) | |
`md5sum #{file}`[/^[^ ]*/] | |
end | |
################################################### | |
prepare_test_mongodb | |
describe "node-proxy.js" do | |
before (:all) do | |
prepare_process('node node-proxy.js ./test-config.js',/OK/) | |
end | |
context "small data" do | |
oid = nil | |
before(:all) do | |
data = StringIO.new('1234567890',"r") | |
oid = _add_mongo('text/plain',data) | |
end | |
context "200 whole data" do | |
it "should return the response." do | |
curl_get("/test/"+oid.to_s,"") do |h,b| | |
hstr = IO.read(h) | |
hstr.should match /200/ | |
hstr.should match /Content-Type: text\/plain/ | |
bstr = IO.read(b) | |
bstr.size.should == 10 | |
bstr.should == "1234567890" | |
end | |
end | |
end | |
context "206 head" do | |
it "should return the response." do | |
curl_get("/test/"+oid.to_s,"-r 0-4") do |h,b| | |
hstr = IO.read(h) | |
hstr.should match /206/ | |
hstr.should match /Content-Range: bytes 0-4\/10/ | |
bstr = IO.read(b) | |
bstr.size.should == 5 | |
bstr.should == "12345" | |
end | |
end | |
end | |
context "206 tail" do | |
it "should return the response." do | |
curl_get("/test/"+oid.to_s,"-r 5-9") do |h,b| | |
hstr = IO.read(h) | |
hstr.should match /206/ | |
hstr.should match /Content-Range: bytes 5-9\/10/ | |
bstr = IO.read(b) | |
bstr.size.should == 5 | |
bstr.should == "67890" | |
end | |
end | |
end | |
context "206 0-" do | |
it "should return the response." do | |
curl_get("/test/"+oid.to_s,"-r 0-") do |h,b| | |
hstr = IO.read(h) | |
hstr.should match /206/ | |
hstr.should match /Content-Range: bytes 0-9\/10/ | |
bstr = IO.read(b) | |
bstr.size.should == 10 | |
bstr.should == "1234567890" | |
end | |
end | |
end | |
end | |
context "large data" do | |
oid = nil | |
large_file = 'large_data' | |
total_size = nil | |
md5 = nil | |
before(:all) do | |
data = StringIO.new('1234567890'*256*1024,"r") | |
open(large_file,'w') do |f| | |
f.write(data.read) | |
end | |
total_size = data.size | |
data.rewind | |
md5 = md5sum(large_file) | |
oid = _add_mongo('image/jpeg',data) | |
end | |
after(:all) do | |
File.delete(large_file) if File.exist?(large_file) | |
end | |
context "200 whole data" do | |
it "should return the response." do | |
curl_get("/test/"+oid.to_s,"") do |h,b| | |
hstr = IO.read(h) | |
hstr.should match /200/ | |
hstr.should match /Content-Type: image\/jpeg/ | |
bstr = IO.read(b) | |
bstr.size.should == total_size | |
md5sum(b).should == md5 | |
end | |
end | |
end | |
context "206 head" do | |
it "should return the response." do | |
pos = 256*1024+100 | |
curl_get("/test/"+oid.to_s,"-r 0-#{pos}") do |h,b| | |
hstr = IO.read(h) | |
hstr.should match /206/ | |
hstr.should match /Content-Range: bytes 0-#{pos}\/#{total_size}/o | |
File.size(b).should == pos+1 | |
`head -c #{pos+1} #{large_file} | md5sum | cut -d' ' -f1`.strip.should == md5sum(b) | |
end | |
end | |
end | |
context "206 tail" do | |
it "should return the response." do | |
pos = 256*1024+100 | |
size = total_size - pos | |
curl_get("/test/"+oid.to_s,"-r #{pos}-#{total_size-1}") do |h,b| | |
hstr = IO.read(h) | |
hstr.should match /206/ | |
hstr.should match /Content-Range: bytes #{pos}-#{total_size-1}\/#{total_size}/o | |
File.size(b).should == size | |
`tail -c #{size} #{large_file} | md5sum | cut -d' ' -f1`.strip.should == md5sum(b) | |
end | |
end | |
end | |
context "206 0-" do | |
it "should return the response." do | |
curl_get("/test/"+oid.to_s,"-r 0-") do |h,b| | |
hstr = IO.read(h) | |
hstr.should match /206/ | |
hstr.should match /Content-Range: bytes 0-#{total_size-1}\/#{total_size}/o | |
File.size(b).should == total_size | |
md5sum(large_file).should == md5sum(b) | |
end | |
end | |
end | |
context "206 mid" do | |
it "should return the response." do | |
head_pos = (total_size * 0.1).to_i | |
tail_pos = (total_size * 0.9).to_i | |
size = tail_pos-head_pos | |
curl_get("/test/"+oid.to_s,"-r #{head_pos}-#{tail_pos-1}") do |h,b| | |
hstr = IO.read(h) | |
hstr.should match /206/ | |
hstr.should match /Content-Range: bytes #{head_pos}-#{tail_pos-1}\/#{total_size}/o | |
File.size(b).should == size | |
`head -c #{head_pos+size} #{large_file}| tail -c #{size}| md5sum | cut -d' ' -f1`.strip.should == md5sum(b) | |
end | |
end | |
end | |
end | |
context "404 file not found" do | |
it "should return the 404 response." do | |
curl_get("/test/12346789012","") do |h,b| | |
hstr = IO.read(h) | |
hstr.should match /Content-Type: text\/plain/ | |
hstr.should match /404/ | |
bstr = IO.read(b) | |
bstr.should match /File not found/ | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment