(POOLER-73) Restructure tests to unit and integration directories

Previously all of the spec tests for VM Pooler were all together in the specs
directory.  However some tests require a working local Redis server to operate
and other instead mock all external dependencies.  This commit splits the test
files between unit and integration, where integration tests require a working
Redis instance, and unit tests do not.  This commit also removes the root
`vmpooler` directory as it is not required.  The tests rake test still operates
correctly.

This commit also adds the mock_redis library for testing for the pool_manager.
This commit is contained in:
Glenn Sarti 2017-02-08 13:43:26 -08:00
parent eb67ccad5a
commit 8bcf74872a
9 changed files with 1 additions and 0 deletions

View file

@ -0,0 +1,171 @@
require 'spec_helper'
# A class for testing purposes that includes the Helpers.
# this is impersonating V1's `helpers do include Helpers end`
#
# This is the subject used throughout the test file.
#
class TestHelpers
include Vmpooler::API::Helpers
end
describe Vmpooler::API::Helpers do
subject { TestHelpers.new }
describe '#hostname_shorten' do
[
['example.com', 'not-example.com', 'example.com'],
['example.com', 'example.com', 'example.com'],
['sub.example.com', 'example.com', 'sub'],
['example.com', nil, 'example.com']
].each do |hostname, domain, expected|
it { expect(subject.hostname_shorten(hostname, domain)).to eq expected }
end
end
describe '#validate_date_str' do
[
['2015-01-01', true],
[nil, false],
[false, false],
[true, false],
['01-01-2015', false],
['1/1/2015', false]
].each do |date, expected|
it { expect(subject.validate_date_str(date)).to eq expected }
end
end
describe '#mean' do
[
[[1, 2, 3, 4], 2.5],
[[1], 1],
[[nil], 0],
[[], 0]
].each do |list, expected|
it "returns #{expected.inspect} for #{list.inspect}" do
expect(subject.mean(list)).to eq expected
end
end
end
describe '#get_task_times' do
let(:redis) { double('redis') }
[
['task1', '2014-01-01', [1, 2, 3, 4], [1.0, 2.0, 3.0, 4.0]],
['task1', 'some date', [], []],
['task1', 'date', [2.2], [2.2]],
['task1', 'date', [2.2, 3, 3.0], [2.2, 3.0, 3.0]]
].each do |task, date, time_list, expected|
it "returns #{expected.inspect} for task #{task.inspect}@#{date.inspect}" do
allow(redis).to receive(:hvals).and_return time_list
expect(subject.get_task_times(redis, task, date)).to eq expected
end
end
end
describe '#get_capacity_metrics' do
let(:redis) { double('redis') }
it 'adds up pools correctly' do
pools = [
{'name' => 'p1', 'size' => 5},
{'name' => 'p2', 'size' => 5}
]
allow(redis).to receive(:scard).with('vmpooler__ready__p1').and_return 1
allow(redis).to receive(:scard).with('vmpooler__ready__p2').and_return 1
expect(subject.get_capacity_metrics(pools, redis)).to eq({current: 2, total: 10, percent: 20.0})
end
it 'handles 0 from redis' do
pools = [
{'name' => 'p1', 'size' => 5},
{'name' => 'p2', 'size' => 5}
]
allow(redis).to receive(:scard).with('vmpooler__ready__p1').and_return 1
allow(redis).to receive(:scard).with('vmpooler__ready__p2').and_return 0
expect(subject.get_capacity_metrics(pools, redis)).to eq({current: 1, total: 10, percent: 10.0})
end
it 'handles 0 size' do
pools = [
{'name' => 'p1', 'size' => 5},
{'name' => 'p2', 'size' => 0}
]
allow(redis).to receive(:scard).with('vmpooler__ready__p1').and_return 1
allow(redis).to receive(:scard).with('vmpooler__ready__p2').and_return 0
expect(subject.get_capacity_metrics(pools, redis)).to eq({current: 1, total: 5, percent: 20.0})
end
it 'handles empty pool array' do
expect(subject.get_capacity_metrics([], redis)).to eq({current: 0, total: 0, percent: 0})
end
end
describe '#get_queue_metrics' do
let(:redis) { double('redis') }
it 'handles empty pool array' do
allow(redis).to receive(:scard).and_return 0
allow(redis).to receive(:get).and_return 0
expect(subject.get_queue_metrics([], redis)).to eq({pending: 0, cloning: 0, booting: 0, ready: 0, running: 0, completed: 0, total: 0})
end
it 'adds pool queues correctly' do
pools = [
{'name' => 'p1'},
{'name' => 'p2'}
]
pools.each do |p|
%w(pending ready running completed).each do |action|
allow(redis).to receive(:scard).with('vmpooler__' + action + '__' + p['name']).and_return 1
end
end
allow(redis).to receive(:get).and_return 1
expect(subject.get_queue_metrics(pools, redis)).to eq({pending: 2, cloning: 1, booting: 1, ready: 2, running: 2, completed: 2, total: 8})
end
it 'sets booting to 0 when negative calculation' do
pools = [
{'name' => 'p1'},
{'name' => 'p2'}
]
pools.each do |p|
%w(pending ready running completed).each do |action|
allow(redis).to receive(:scard).with('vmpooler__' + action + '__' + p['name']).and_return 1
end
end
allow(redis).to receive(:get).and_return 5
expect(subject.get_queue_metrics(pools, redis)).to eq({pending: 2, cloning: 5, booting: 0, ready: 2, running: 2, completed: 2, total: 8})
end
end
describe '#get_tag_metrics' do
let(:redis) { double('redis') }
it 'returns basic tag metrics' do
allow(redis).to receive(:hgetall).with('vmpooler__tag__2015-01-01').and_return({"abcdefghijklmno:tag" => "value"})
expect(subject.get_tag_metrics(redis, '2015-01-01')).to eq({"tag" => {"value"=>1, "total"=>1}})
end
it 'calculates tag totals' do
allow(redis).to receive(:hgetall).with('vmpooler__tag__2015-01-01').and_return({"abcdefghijklmno:tag" => "value", "pqrstuvwxyz12345:tag" => "another_value"})
expect(subject.get_tag_metrics(redis, '2015-01-01')).to eq({"tag"=>{"value"=>1, "total"=>2, "another_value"=>1}})
end
end
end

View file

@ -0,0 +1,87 @@
require 'spec_helper'
require 'mock_redis'
require 'time'
describe 'Pool Manager' do
let(:logger) { double('logger') }
let(:redis) { MockRedis.new }
let(:metrics) { Vmpooler::DummyStatsd.new }
let(:config) {
{
config: {
'site_name' => 'test pooler',
'migration_limit' => 2,
vsphere: {
'server' => 'vsphere.puppet.com',
'username' => 'vmpooler@vsphere.local',
'password' => '',
'insecure' => true
},
pools: [ {'name' => 'pool1', 'size' => 5, 'folder' => 'pool1_folder'} ],
statsd: { 'prefix' => 'stats_prefix'},
pool_names: [ 'pool1' ]
}
}
}
let(:pool) { config[:config][:pools][0]['name'] }
let(:vm) {
{
'name' => 'vm1',
'host' => 'host1',
'template' => pool,
}
}
describe '#_migrate_vm' do
let(:vsphere) { double(pool) }
let(:pooler) { Vmpooler::PoolManager.new(config, logger, redis, metrics) }
context 'evaluates VM for migration and logs host' do
before do
create_migrating_vm vm['name'], pool, redis
allow(vsphere).to receive(:find_vm).and_return(vm)
allow(pooler).to receive(:get_vm_host_info).and_return([{'name' => 'host1'}, 'host1'])
end
it 'logs VM host when migration is disabled' do
config[:config]['migration_limit'] = nil
expect(redis.sismember("vmpooler__migrating__#{pool}", vm['name'])).to be true
expect(logger).to receive(:log).with('s', "[ ] [#{pool}] '#{vm['name']}' is running on #{vm['host']}")
pooler._migrate_vm(vm['name'], pool, vsphere)
expect(redis.sismember("vmpooler__migrating__#{pool}", vm['name'])).to be false
end
it 'verifies that migration_limit greater than or equal to migrations in progress and logs host' do
add_vm_to_migration_set vm['name'], redis
add_vm_to_migration_set 'vm2', redis
expect(logger).to receive(:log).with('s', "[ ] [#{pool}] '#{vm['name']}' is running on #{vm['host']}. No migration will be evaluated since the migration_limit has been reached")
pooler._migrate_vm(vm['name'], pool, vsphere)
end
it 'verifies that migration_limit is less than migrations in progress and logs old host, new host and migration time' do
allow(vsphere).to receive(:find_least_used_compatible_host).and_return([{'name' => 'host2'}, 'host2'])
allow(vsphere).to receive(:migrate_vm_host)
expect(redis.hget("vmpooler__vm__#{vm['name']}", 'migration_time'))
expect(redis.hget("vmpooler__vm__#{vm['name']}", 'checkout_to_migration'))
expect(logger).to receive(:log).with('s', "[>] [#{pool}] '#{vm['name']}' migrated from #{vm['host']} to host2 in 0.00 seconds")
pooler._migrate_vm(vm['name'], pool, vsphere)
end
it 'fails when no suitable host can be found' do
error = 'ArgumentError: No target host found'
allow(vsphere).to receive(:find_least_used_compatible_host)
allow(vsphere).to receive(:migrate_vm_host).and_raise(error)
expect(logger).to receive(:log).with('s', "[x] [#{pool}] '#{vm['name']}' migration failed with an error: #{error}")
pooler._migrate_vm(vm['name'], pool, vsphere)
end
end
end
end

View file

@ -0,0 +1,362 @@
require 'spec_helper'
require 'time'
require 'mock_redis'
describe 'Pool Manager' do
let(:logger) { double('logger') }
let(:redis) { double('redis') }
let(:metrics) { Vmpooler::DummyStatsd.new }
let(:config) { {} }
let(:pool) { 'pool1' }
let(:vm) { 'vm1' }
let(:timeout) { 5 }
let(:host) { double('host') }
subject { Vmpooler::PoolManager.new(config, logger, redis, metrics) }
describe '#_check_pending_vm' do
let(:pool_helper) { double('pool') }
let(:vsphere) { {pool => pool_helper} }
before do
expect(subject).not_to be_nil
$vsphere = vsphere
end
context 'host not in pool' do
it 'calls fail_pending_vm' do
allow(vsphere).to receive(:find_vm).and_return(nil)
allow(redis).to receive(:hget)
subject._check_pending_vm(vm, pool, timeout, vsphere)
end
end
context 'host is in pool' do
let(:vm_finder) { double('vm_finder') }
let(:tcpsocket) { double('TCPSocket') }
it 'calls move_pending_vm_to_ready' do
allow(subject).to receive(:open_socket).and_return(true)
allow(vsphere).to receive(:find_vm).and_return(vm_finder)
allow(vm_finder).to receive(:summary).and_return(nil)
expect(vm_finder).to receive(:summary).once
expect(redis).not_to receive(:hget).with(String, 'clone')
subject._check_pending_vm(vm, pool, timeout, vsphere)
end
end
end
describe '#move_vm_to_ready' do
before do
expect(subject).not_to be_nil
end
context 'a host without correct summary' do
it 'does nothing when summary is nil' do
allow(host).to receive(:summary).and_return nil
subject.move_pending_vm_to_ready(vm, pool, host)
end
it 'does nothing when guest is nil' do
allow(host).to receive(:summary).and_return true
allow(host).to receive_message_chain(:summary, :guest).and_return nil
subject.move_pending_vm_to_ready(vm, pool, host)
end
it 'does nothing when hostName is nil' do
allow(host).to receive(:summary).and_return true
allow(host).to receive_message_chain(:summary, :guest).and_return true
allow(host).to receive_message_chain(:summary, :guest, :hostName).and_return nil
subject.move_pending_vm_to_ready(vm, pool, host)
end
it 'does nothing when hostName does not match vm' do
allow(host).to receive(:summary).and_return true
allow(host).to receive_message_chain(:summary, :guest).and_return true
allow(host).to receive_message_chain(:summary, :guest, :hostName).and_return 'adifferentvm'
subject.move_pending_vm_to_ready(vm, pool, host)
end
end
context 'a host with proper summary' do
before do
allow(host).to receive(:summary).and_return true
allow(host).to receive_message_chain(:summary, :guest).and_return true
allow(host).to receive_message_chain(:summary, :guest, :hostName).and_return vm
allow(redis).to receive(:hget)
allow(redis).to receive(:smove)
allow(redis).to receive(:hset)
allow(logger).to receive(:log)
end
it 'moves vm to ready' do
allow(redis).to receive(:hget).with(String, 'clone').and_return Time.now.to_s
expect(redis).to receive(:smove).with(String, String, vm)
expect(redis).to receive(:hset).with(String, String, String)
expect(logger).to receive(:log).with('s', String)
subject.move_pending_vm_to_ready(vm, pool, host)
end
it 'sets finish to nil when clone_time is nil' do
expect(redis).to receive(:smove).with(String, String, vm)
expect(redis).to receive(:hset).with(String, String, nil)
expect(logger).to receive(:log).with('s', String)
subject.move_pending_vm_to_ready(vm, pool, host)
end
end
end
describe '#fail_pending_vm' do
before do
expect(subject).not_to be_nil
end
context 'does not have a clone stamp' do
it 'has no side effects' do
allow(redis).to receive(:hget)
subject.fail_pending_vm(vm, pool, timeout)
end
end
context 'has valid clone stamp' do
it 'does nothing when less than timeout' do
allow(redis).to receive(:hget).with(String, 'clone').and_return Time.now.to_s
subject.fail_pending_vm(vm, pool, timeout)
end
it 'moves vm to completed when over timeout' do
allow(redis).to receive(:hget).with(String, 'clone').and_return '2005-01-1'
allow(redis).to receive(:smove).with(String, String, String)
allow(logger).to receive(:log).with(String, String)
expect(redis).to receive(:smove).with(String, String, vm)
expect(logger).to receive(:log).with('d', String)
subject.fail_pending_vm(vm, pool, timeout)
end
end
end
describe '#_check_running_vm' do
let(:pool_helper) { double('pool') }
let(:vsphere) { {pool => pool_helper} }
before do
expect(subject).not_to be_nil
$vsphere = vsphere
end
it 'does nothing with nil host' do
allow(vsphere).to receive(:find_vm).and_return(nil)
expect(redis).not_to receive(:smove)
subject._check_running_vm(vm, pool, timeout, vsphere)
end
context 'valid host' do
let(:vm_host) { double('vmhost') }
it 'does not move vm when not poweredOn' do
allow(vsphere).to receive(:find_vm).and_return vm_host
allow(vm_host).to receive(:runtime).and_return true
allow(vm_host).to receive_message_chain(:runtime, :powerState).and_return 'poweredOff'
expect(redis).to receive(:hget)
expect(redis).not_to receive(:smove)
expect(logger).not_to receive(:log).with('d', "[!] [#{pool}] '#{vm}' appears to be powered off or dead")
subject._check_running_vm(vm, pool, timeout, vsphere)
end
it 'moves vm when poweredOn, but past TTL' do
allow(vsphere).to receive(:find_vm).and_return vm_host
allow(vm_host).to receive(:runtime).and_return true
allow(vm_host).to receive_message_chain(:runtime, :powerState).and_return 'poweredOn'
expect(redis).to receive(:hget).with('vmpooler__active__pool1', 'vm1').and_return((Time.now - timeout*60*60).to_s)
expect(redis).to receive(:smove)
expect(logger).to receive(:log).with('d', "[!] [#{pool}] '#{vm}' reached end of TTL after #{timeout} hours")
subject._check_running_vm(vm, pool, timeout, vsphere)
end
end
end
describe '#move_running_to_completed' do
before do
expect(subject).not_to be_nil
end
it 'uses the pool in smove' do
allow(redis).to receive(:smove).with(String, String, String)
allow(logger).to receive(:log)
expect(redis).to receive(:smove).with('vmpooler__running__p1', 'vmpooler__completed__p1', 'vm1')
subject.move_vm_queue('p1', 'vm1', 'running', 'completed', 'msg')
end
it 'logs msg' do
allow(redis).to receive(:smove)
allow(logger).to receive(:log)
expect(logger).to receive(:log).with('d', "[!] [p1] 'vm1' a msg here")
subject.move_vm_queue('p1', 'vm1', 'running', 'completed', 'a msg here')
end
end
describe '#_check_pool' do
let(:pool_helper) { double('pool') }
let(:vsphere) { {pool => pool_helper} }
let(:config) { {
config: { task_limit: 10 },
pools: [ {'name' => 'pool1', 'size' => 5} ]
} }
before do
expect(subject).not_to be_nil
$vsphere = vsphere
allow(logger).to receive(:log)
allow(pool_helper).to receive(:find_folder)
allow(redis).to receive(:smembers).with('vmpooler__pending__pool1').and_return([])
allow(redis).to receive(:smembers).with('vmpooler__ready__pool1').and_return([])
allow(redis).to receive(:smembers).with('vmpooler__running__pool1').and_return([])
allow(redis).to receive(:smembers).with('vmpooler__completed__pool1').and_return([])
allow(redis).to receive(:smembers).with('vmpooler__discovered__pool1').and_return([])
allow(redis).to receive(:smembers).with('vmpooler__migrating__pool1').and_return([])
allow(redis).to receive(:set)
allow(redis).to receive(:get).with('vmpooler__tasks__clone').and_return(0)
allow(redis).to receive(:get).with('vmpooler__empty__pool1').and_return(nil)
end
context 'logging' do
it 'logs empty pool' do
allow(redis).to receive(:scard).with('vmpooler__pending__pool1').and_return(0)
allow(redis).to receive(:scard).with('vmpooler__ready__pool1').and_return(0)
allow(redis).to receive(:scard).with('vmpooler__running__pool1').and_return(0)
expect(logger).to receive(:log).with('s', "[!] [pool1] is empty")
subject._check_pool(config[:pools][0], vsphere)
end
end
end
describe '#_stats_running_ready' do
let(:pool_helper) { double('pool') }
let(:vsphere) { {pool => pool_helper} }
let(:metrics) { Vmpooler::DummyStatsd.new }
let(:config) { {
config: { task_limit: 10 },
pools: [ {'name' => 'pool1', 'size' => 5} ],
graphite: { 'prefix' => 'vmpooler' }
} }
before do
expect(subject).not_to be_nil
$vsphere = vsphere
allow(logger).to receive(:log)
allow(pool_helper).to receive(:find_folder)
allow(redis).to receive(:smembers).and_return([])
allow(redis).to receive(:set)
allow(redis).to receive(:get).with('vmpooler__tasks__clone').and_return(0)
allow(redis).to receive(:get).with('vmpooler__empty__pool1').and_return(nil)
end
context 'metrics' do
subject { Vmpooler::PoolManager.new(config, logger, redis, metrics) }
it 'increments metrics' do
allow(redis).to receive(:scard).with('vmpooler__ready__pool1').and_return(1)
allow(redis).to receive(:scard).with('vmpooler__cloning__pool1').and_return(0)
allow(redis).to receive(:scard).with('vmpooler__pending__pool1').and_return(0)
allow(redis).to receive(:scard).with('vmpooler__running__pool1').and_return(5)
expect(metrics).to receive(:gauge).with('ready.pool1', 1)
expect(metrics).to receive(:gauge).with('running.pool1', 5)
subject._check_pool(config[:pools][0], vsphere)
end
it 'increments metrics when ready with 0 when pool empty' do
allow(redis).to receive(:scard).with('vmpooler__ready__pool1').and_return(0)
allow(redis).to receive(:scard).with('vmpooler__cloning__pool1').and_return(0)
allow(redis).to receive(:scard).with('vmpooler__pending__pool1').and_return(0)
allow(redis).to receive(:scard).with('vmpooler__running__pool1').and_return(5)
expect(metrics).to receive(:gauge).with('ready.pool1', 0)
expect(metrics).to receive(:gauge).with('running.pool1', 5)
subject._check_pool(config[:pools][0], vsphere)
end
end
end
describe '#_create_vm_snapshot' do
let(:snapshot_manager) { 'snapshot_manager' }
let(:pool_helper) { double('snapshot_manager') }
let(:vsphere) { {snapshot_manager => pool_helper} }
before do
expect(subject).not_to be_nil
$vsphere = vsphere
end
context '(valid host)' do
let(:vm_host) { double('vmhost') }
it 'creates a snapshot' do
expect(vsphere).to receive(:find_vm).and_return vm_host
expect(logger).to receive(:log)
expect(vm_host).to receive_message_chain(:CreateSnapshot_Task, :wait_for_completion)
expect(redis).to receive(:hset).with('vmpooler__vm__testvm', 'snapshot:testsnapshot', Time.now.to_s)
expect(logger).to receive(:log)
subject._create_vm_snapshot('testvm', 'testsnapshot', vsphere)
end
end
end
describe '#_revert_vm_snapshot' do
let(:snapshot_manager) { 'snapshot_manager' }
let(:pool_helper) { double('snapshot_manager') }
let(:vsphere) { {snapshot_manager => pool_helper} }
before do
expect(subject).not_to be_nil
$vsphere = vsphere
end
context '(valid host)' do
let(:vm_host) { double('vmhost') }
let(:vm_snapshot) { double('vmsnapshot') }
it 'reverts a snapshot' do
expect(vsphere).to receive(:find_vm).and_return vm_host
expect(vsphere).to receive(:find_snapshot).and_return vm_snapshot
expect(logger).to receive(:log)
expect(vm_snapshot).to receive_message_chain(:RevertToSnapshot_Task, :wait_for_completion)
expect(logger).to receive(:log)
subject._revert_vm_snapshot('testvm', 'testsnapshot', vsphere)
end
end
end
describe '#_check_snapshot_queue' do
let(:pool_helper) { double('pool') }
let(:vsphere) { {pool => pool_helper} }
before do
expect(subject).not_to be_nil
$vsphere = vsphere
end
it 'checks appropriate redis queues' do
expect(redis).to receive(:spop).with('vmpooler__tasks__snapshot')
expect(redis).to receive(:spop).with('vmpooler__tasks__snapshot-revert')
subject._check_snapshot_queue(vsphere)
end
end
end