Skip to content

Commit

Permalink
Merge pull request #182 from glennsarti/ticket/master/POOLER-73-add-u…
Browse files Browse the repository at this point in the history
…nit-tests

(POOLER-73) Update unit tests prior to refactoring
  • Loading branch information
Rob Braden authored Mar 2, 2017
2 parents dbd16eb + e783e93 commit 0754f86
Show file tree
Hide file tree
Showing 12 changed files with 2,585 additions and 457 deletions.
41 changes: 32 additions & 9 deletions lib/vmpooler/pool_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,20 @@ def _revert_vm_snapshot(vm, snapshot_name, vsphere)
end
end

def check_disk_queue
def check_disk_queue(maxloop = 0, loop_delay = 5)
$logger.log('d', "[*] [disk_manager] starting worker thread")

$vsphere['disk_manager'] ||= Vmpooler::VsphereHelper.new $config, $metrics

$threads['disk_manager'] = Thread.new do
loop_count = 1
loop do
_check_disk_queue $vsphere['disk_manager']
sleep(5)
sleep(loop_delay)

unless maxloop.zero?
break if loop_count >= maxloop
loop_count = loop_count + 1
end
end
end
end
Expand All @@ -438,15 +443,21 @@ def _check_disk_queue(vsphere)
end
end

def check_snapshot_queue
def check_snapshot_queue(maxloop = 0, loop_delay = 5)
$logger.log('d', "[*] [snapshot_manager] starting worker thread")

$vsphere['snapshot_manager'] ||= Vmpooler::VsphereHelper.new $config, $metrics

$threads['snapshot_manager'] = Thread.new do
loop_count = 1
loop do
_check_snapshot_queue $vsphere['snapshot_manager']
sleep(5)
sleep(loop_delay)

unless maxloop.zero?
break if loop_count >= maxloop
loop_count = loop_count + 1
end
end
end
end
Expand Down Expand Up @@ -546,15 +557,21 @@ def migrate_vm_and_record_timing(vm_object, vm_name, pool, host, source_host_nam
finish
end

def check_pool(pool)
def check_pool(pool,maxloop = 0, loop_delay = 5)
$logger.log('d', "[*] [#{pool['name']}] starting worker thread")

$vsphere[pool['name']] ||= Vmpooler::VsphereHelper.new $config, $metrics

$threads[pool['name']] = Thread.new do
loop_count = 1
loop do
_check_pool(pool, $vsphere[pool['name']])
sleep(5)
sleep(loop_delay)

unless maxloop.zero?
break if loop_count >= maxloop
loop_count = loop_count + 1
end
end
end
end
Expand Down Expand Up @@ -714,14 +731,15 @@ def _check_pool(pool, vsphere)
raise
end

def execute!
def execute!(maxloop = 0, loop_delay = 1)
$logger.log('d', 'starting vmpooler')

# Clear out the tasks manager, as we don't know about any tasks at this point
$redis.set('vmpooler__tasks__clone', 0)
# Clear out vmpooler__migrations since stale entries may be left after a restart
$redis.del('vmpooler__migration')

loop_count = 1
loop do
if ! $threads['disk_manager']
check_disk_queue
Expand All @@ -746,7 +764,12 @@ def execute!
end
end

sleep(1)
sleep(loop_delay)

unless maxloop.zero?
break if loop_count >= maxloop
loop_count = loop_count + 1
end
end
end
end
Expand Down
43 changes: 43 additions & 0 deletions spec/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@ def redis
@redis
end

# Mock an object which is result from Vmpooler::VsphereHelper.find_folder(foldername)
class MockFindFolder
attr_reader :childEntity

def initialize(vmlist = [])
# Generate an array of hashes
@childEntity = vmlist.map do |vm|
vm_object = {}
vm_object['name'] = vm

vm_object
end
end
end

# Mock an object which represents a Logger. This stops the proliferation
# of allow(logger).to .... expectations in tests.
class MockLogger
def log(_level, string)
end
end

def expect_json(ok = true, http = 200)
expect(last_response.header['Content-Type']).to eq('application/json')

Expand Down Expand Up @@ -56,6 +78,18 @@ def create_vm(name, token = nil, redis_handle = nil)
redis_db.hset("vmpooler__vm__#{name}", 'token:token', token) if token
end

def create_completed_vm(name, pool, active = false, redis_handle = nil)
redis_db = redis_handle ? redis_handle : redis
redis_db.sadd("vmpooler__completed__#{pool}", name)
redis_db.hset("vmpooler__vm__#{name}", 'checkout', Time.now)
redis_db.hset("vmpooler__active__#{pool}", name, Time.now) if active
end

def create_discovered_vm(name, pool, redis_handle = nil)
redis_db = redis_handle ? redis_handle : redis
redis_db.sadd("vmpooler__discovered__#{pool}", name)
end

def create_migrating_vm(name, pool, redis_handle = nil)
redis_db = redis_handle ? redis_handle : redis
redis_db.hset("vmpooler__vm__#{name}", 'checkout', Time.now)
Expand All @@ -71,11 +105,20 @@ def fetch_vm(vm)
redis.hgetall("vmpooler__vm__#{vm}")
end

def snapshot_revert_vm(vm, snapshot = '12345678901234567890123456789012')
redis.sadd('vmpooler__tasks__snapshot-revert', "#{vm}:#{snapshot}")
redis.hset("vmpooler__vm__#{vm}", "snapshot:#{snapshot}", "1")
end

def snapshot_vm(vm, snapshot = '12345678901234567890123456789012')
redis.sadd('vmpooler__tasks__snapshot', "#{vm}:#{snapshot}")
redis.hset("vmpooler__vm__#{vm}", "snapshot:#{snapshot}", "1")
end

def disk_task_vm(vm, disk_size = '10')
redis.sadd('vmpooler__tasks__disk', "#{vm}:#{disk_size}")
end

def has_vm_snapshot?(vm)
redis.smembers('vmpooler__tasks__snapshot').any? do |snapshot|
instance, sha = snapshot.split(':')
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 0754f86

Please sign in to comment.