Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Merge "remove duplicated spec and make backup test items independent"…
Browse files Browse the repository at this point in the history
… into services-r9
  • Loading branch information
cliffwang authored and Gerrit Code Review committed Mar 2, 2012
2 parents 0b2a22a + 4cd055d commit ed0e99b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 191 deletions.
115 changes: 66 additions & 49 deletions mongodb/spec/mongodb_backup_spec.rb
Expand Up @@ -5,29 +5,48 @@
describe "mongodb backup/restore" do

before :all do
EM.run do
@app_id = "myapp"
@opts = get_node_config()
@logger = @opts[:logger]
@app_id = "myapp"
@opts = get_node_config()
@logger = @opts[:logger]

BINARY_DIR = File.dirname(@opts[:mongod_path])
@node = Node.new(@opts)
@resp = @node.provision("free")
BINARY_DIR = File.dirname(@opts[:mongod_path])

@config_template = ERB.new(File.read(TEMPLATE_FILE))
config = @config_template.result(binding)
FileUtils.rm_f(CONFIG_FILE)
File.open(CONFIG_FILE, "w") {|f| f.write(config)}
@config_template = ERB.new(File.read(TEMPLATE_FILE))
config = @config_template.result(binding)
FileUtils.rm_f(CONFIG_FILE)
File.open(CONFIG_FILE, "w") {|f| f.write(config)}
FileUtils.rm_rf(BACKUP_DIR)

FileUtils.rm_rf(BACKUP_DIR)
EM.run do
@node = Node.new(@opts)
EM.add_timer(3) { EM.stop }
end
end

EM.add_timer(1) do
@bind_resp = @node.bind(@resp['name'], 'rw')
EM.add_timer(1) do
EM.stop
end
end
before :each do
@resp = @node.provision("free")
sleep 1
@bind_resp = @node.bind(@resp['name'], 'rw')
# Write some data in database
conn = Mongo::Connection.new(@bind_resp['hostname'], @bind_resp['port']).db(@resp['db'])
conn.authenticate(@bind_resp['username'], @bind_resp['password'])
coll = conn.collection(TEST_COLL)
row = { TEST_KEY => TEST_VAL }
coll.insert(row)
end

after :each do
@node.unbind(@bind_resp).should be_true
EM.run do
@node.unprovision(@resp['name'], [])
EM.add_timer(3) { EM.stop }
end
e = nil
begin
conn = Mongo::Connection.new('localhost', @resp['port']).db('db')
rescue => e
end
e.should_not be_nil
end

after :all do
Expand All @@ -36,22 +55,23 @@
end

it "should be able to backup the database" do
# Write some data in database
conn = Mongo::Connection.new(@bind_resp['hostname'], @bind_resp['port']).db(@resp['db'])
conn.authenticate(@bind_resp['username'], @bind_resp['password'])
coll = conn.collection(TEST_COLL)
row = { TEST_KEY => TEST_VAL }
coll.insert(row)
# Run mongodb_backup
res = Kernel.system("../bin/mongodb_backup -c #{CONFIG_FILE} -t > /dev/null")
res.should == true
end

it "should be able to restore the database" do
# Run mongodb_backup
res = Kernel.system("../bin/mongodb_backup -c #{CONFIG_FILE} -t > /dev/null")
raise 'mongodb_backup failed' unless res

# Change value after backup
conn = Mongo::Connection.new(@bind_resp['hostname'], @bind_resp['port']).db(@resp['db'])
conn.authenticate(@bind_resp['username'], @bind_resp['password'])
coll = conn.collection(TEST_COLL)
row = { TEST_KEY => TEST_VAL }
coll.update(row, { TEST_KEY => TEST_VAL_2 })
end

it "should be able to restore the database" do
# Get backup file location
dir = get_backup_dir(BACKUP_DIR)

Expand All @@ -62,38 +82,35 @@
sleep 10

# Should be the same like what it was before backup
conn = Mongo::Connection.new(@bind_resp['hostname'], @bind_resp['port']).db(@resp['db'])
conn.authenticate(@bind_resp['username'], @bind_resp['password'])
coll = conn.collection(TEST_COLL)
doc = coll.find_one()
doc[TEST_KEY].should == TEST_VAL
end

# unbind here
it "should be able to unbind it" do
EM.run do
resp = @node.unbind(@bind_resp)
resp.should be_true
EM.add_timer(1) do
EM.stop
end
end
end
it "should be able to recover the backup instance" do
# Run mongodb_backup
res = Kernel.system("../bin/mongodb_backup -c #{CONFIG_FILE} -t > /dev/null")
raise 'mongodb_backup failed' unless res

# Get backup file location
dir = get_backup_dir(BACKUP_DIR)

# unprovision here
it "should be able to unprovision an existing instance" do
@node.unbind(@bind_resp)
EM.run do
@node.unprovision(@resp['name'], [])

e = nil
begin
conn = Mongo::Connection.new('localhost', @resp['port']).db('db')
rescue => e
end
e.should_not be_nil
EM.stop
EM.add_timer(5) { EM.stop }
end
@node.provision('free', @resp)
@node.restore(@resp['name'], dir)
@node.bind(@resp['name'], 'rw', @bind_resp)

# Should be the same like what it was before backup
conn = Mongo::Connection.new(@bind_resp['hostname'], @bind_resp['port']).db(@resp['db'])
conn.authenticate(@bind_resp['username'], @bind_resp['password'])
coll = conn.collection(TEST_COLL)
doc = coll.find_one()
doc[TEST_KEY].should == TEST_VAL
end

end


142 changes: 0 additions & 142 deletions mongodb/spec/mongodb_recovery_spec.rb

This file was deleted.

0 comments on commit ed0e99b

Please sign in to comment.