mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-26 01:58:41 -05:00
(POOLER-73) Add spec tests for clone_vm
Add spec tests for clone_vm
This commit is contained in:
parent
9d8b792c4a
commit
434ab546a2
1 changed files with 265 additions and 0 deletions
|
|
@ -517,6 +517,271 @@ EOT
|
|||
end
|
||||
end
|
||||
|
||||
describe '#clone_vm' do
|
||||
before do
|
||||
expect(subject).not_to be_nil
|
||||
end
|
||||
|
||||
before(:each) do
|
||||
expect(Thread).to receive(:new).and_yield
|
||||
end
|
||||
|
||||
let(:config) {
|
||||
YAML.load(<<-EOT
|
||||
---
|
||||
:config:
|
||||
prefix: "prefix"
|
||||
:vsphere:
|
||||
username: "vcenter_user"
|
||||
EOT
|
||||
)
|
||||
}
|
||||
|
||||
let (:folder) { 'vmfolder' }
|
||||
let (:folder_object) { double('folder_object') }
|
||||
let (:template_name) { pool }
|
||||
let (:template) { "template/#{template_name}" }
|
||||
let (:datastore) { 'datastore' }
|
||||
let (:target) { 'clone_target' }
|
||||
let (:vsphere) { double('vsphere') }
|
||||
let (:template_folder_object) { double('template_folder_object') }
|
||||
let (:template_vm_object) { double('template_vm_object') }
|
||||
let (:clone_task) { double('clone_task') }
|
||||
|
||||
context 'no template specified' do
|
||||
it 'should raise an error' do
|
||||
|
||||
expect{subject.clone_vm(nil,folder,datastore,target,vsphere)}.to raise_error(/Please provide a full path to the template/)
|
||||
end
|
||||
|
||||
it 'should log a message' do
|
||||
expect(logger).to receive(:log).with('s', "[!] [] '' failed while preparing to clone with an error: Please provide a full path to the template")
|
||||
|
||||
expect{subject.clone_vm(nil,folder,datastore,target,vsphere)}.to raise_error(RuntimeError)
|
||||
end
|
||||
end
|
||||
|
||||
context 'a template with no forward slash in the string' do
|
||||
it 'should raise an error' do
|
||||
|
||||
expect{subject.clone_vm('vm1',folder,datastore,target,vsphere)}.to raise_error(/Please provide a full path to the template/)
|
||||
end
|
||||
|
||||
it 'should log a message' do
|
||||
expect(logger).to receive(:log).with('s', "[!] [] '' failed while preparing to clone with an error: Please provide a full path to the template")
|
||||
|
||||
expect{subject.clone_vm('vm1',folder,datastore,target,vsphere)}.to raise_error(RuntimeError)
|
||||
end
|
||||
end
|
||||
|
||||
# Note - It is impossible to get into the following code branch
|
||||
# ...
|
||||
# if vm['template'].length == 0
|
||||
# fail "Unable to find template '#{vm['template']}'!"
|
||||
# end
|
||||
# ...
|
||||
|
||||
context "Template name does not match pool name (Implementation Bug)" do
|
||||
let (:template_name) { 'template_vm' }
|
||||
|
||||
# The implementaion of clone_vm incorrectly uses the VM Template name instead of the pool name. The VM Template represents the
|
||||
# name of the VM to clone in vSphere whereas pool is the name of the pool in Pooler. The tests below document the behaviour of
|
||||
# clone_vm if the Template and Pool name differ. It is expected that these test will fail once this bug is removed.
|
||||
|
||||
context 'a valid template' do
|
||||
before(:each) do
|
||||
expect(template_folder_object).to receive(:find).with(template_name).and_return(template_vm_object)
|
||||
expect(vsphere).to receive(:find_folder).with('template').and_return(template_folder_object)
|
||||
end
|
||||
|
||||
context 'with no errors during cloning' do
|
||||
before(:each) do
|
||||
expect(vsphere).to receive(:find_least_used_host).with(target).and_return('least_used_host')
|
||||
expect(vsphere).to receive(:find_datastore).with(datastore).and_return('datastore')
|
||||
expect(vsphere).to receive(:find_folder).with('vmfolder').and_return(folder_object)
|
||||
expect(template_vm_object).to receive(:CloneVM_Task).and_return(clone_task)
|
||||
expect(clone_task).to receive(:wait_for_completion)
|
||||
expect(metrics).to receive(:timing).with(/clone\./,/0/)
|
||||
end
|
||||
|
||||
it 'should create a cloning VM' do
|
||||
expect(logger).to receive(:log).at_least(:once)
|
||||
expect(redis.scard("vmpooler__pending__#{pool}")).to eq(0)
|
||||
|
||||
subject.clone_vm(template,folder,datastore,target,vsphere)
|
||||
|
||||
expect(redis.scard("vmpooler__pending__#{template_name}")).to eq(1)
|
||||
# Get the new VM Name from the pending pool queue as it should be the only entry
|
||||
vm_name = redis.smembers("vmpooler__pending__#{template_name}")[0]
|
||||
expect(redis.hget("vmpooler__vm__#{vm_name}", 'clone')).to_not be_nil
|
||||
expect(redis.hget("vmpooler__vm__#{vm_name}", 'template')).to eq(template_name)
|
||||
expect(redis.hget("vmpooler__clone__#{Date.today.to_s}", "#{template_name}:#{vm_name}")).to_not be_nil
|
||||
expect(redis.hget("vmpooler__vm__#{vm_name}", 'clone_time')).to_not be_nil
|
||||
end
|
||||
|
||||
it 'should log a message that is being cloned from a template' do
|
||||
expect(logger).to receive(:log).with('d',/\[ \] \[#{template_name}\] '(.+)' is being cloned from '#{template_name}'/)
|
||||
allow(logger).to receive(:log)
|
||||
|
||||
subject.clone_vm(template,folder,datastore,target,vsphere)
|
||||
end
|
||||
|
||||
it 'should log a message that it completed being cloned' do
|
||||
expect(logger).to receive(:log).with('s',/\[\+\] \[#{template_name}\] '(.+)' cloned from '#{template_name}' in [0-9.]+ seconds/)
|
||||
allow(logger).to receive(:log)
|
||||
|
||||
subject.clone_vm(template,folder,datastore,target,vsphere)
|
||||
end
|
||||
end
|
||||
|
||||
# An error can be cause by the following configuration errors:
|
||||
# - Missing or invalid datastore
|
||||
# - Missing or invalid clone target
|
||||
# also any runtime errors during the cloning process
|
||||
# https://www.vmware.com/support/developer/converter-sdk/conv50_apireference/vim.VirtualMachine.html#clone
|
||||
context 'with an error during cloning' do
|
||||
before(:each) do
|
||||
expect(vsphere).to receive(:find_least_used_host).with(target).and_return('least_used_host')
|
||||
expect(vsphere).to receive(:find_datastore).with(datastore).and_return(nil)
|
||||
expect(vsphere).to receive(:find_folder).with('vmfolder').and_return(folder_object)
|
||||
expect(template_vm_object).to receive(:CloneVM_Task).and_return(clone_task)
|
||||
expect(clone_task).to receive(:wait_for_completion).and_raise(RuntimeError,'SomeError')
|
||||
expect(metrics).to receive(:timing).with(/clone\./,/0/).exactly(0).times
|
||||
|
||||
end
|
||||
|
||||
it 'should raise an error within the Thread' do
|
||||
expect(logger).to receive(:log).at_least(:once)
|
||||
expect{subject.clone_vm(template,folder,datastore,target,vsphere)}.to raise_error(/SomeError/)
|
||||
end
|
||||
|
||||
it 'should log a message that is being cloned from a template' do
|
||||
expect(logger).to receive(:log).with('d',/\[ \] \[#{template_name}\] '(.+)' is being cloned from '#{template_name}'/)
|
||||
allow(logger).to receive(:log)
|
||||
|
||||
# Swallow the error
|
||||
begin
|
||||
subject.clone_vm(template,folder,datastore,target,vsphere)
|
||||
rescue
|
||||
end
|
||||
end
|
||||
|
||||
it 'should log messages that the clone failed' do
|
||||
expect(logger).to receive(:log).with('s', /\[!\] \[#{template_name}\] '(.+)' clone failed with an error: SomeError/)
|
||||
expect(logger).to receive(:log).with('s', /\[!\] \[#{template_name}\] '(.+)' failed while preparing to clone with an error: SomeError/)
|
||||
allow(logger).to receive(:log)
|
||||
|
||||
# Swallow the error
|
||||
begin
|
||||
subject.clone_vm(template,folder,datastore,target,vsphere)
|
||||
rescue
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'a valid template' do
|
||||
before(:each) do
|
||||
expect(template_folder_object).to receive(:find).with(template_name).and_return(template_vm_object)
|
||||
expect(vsphere).to receive(:find_folder).with('template').and_return(template_folder_object)
|
||||
end
|
||||
|
||||
context 'with no errors during cloning' do
|
||||
before(:each) do
|
||||
expect(vsphere).to receive(:find_least_used_host).with(target).and_return('least_used_host')
|
||||
expect(vsphere).to receive(:find_datastore).with(datastore).and_return('datastore')
|
||||
expect(vsphere).to receive(:find_folder).with('vmfolder').and_return(folder_object)
|
||||
expect(template_vm_object).to receive(:CloneVM_Task).and_return(clone_task)
|
||||
expect(clone_task).to receive(:wait_for_completion)
|
||||
expect(metrics).to receive(:timing).with(/clone\./,/0/)
|
||||
end
|
||||
|
||||
it 'should create a cloning VM' do
|
||||
expect(logger).to receive(:log).at_least(:once)
|
||||
expect(redis.scard("vmpooler__pending__#{pool}")).to eq(0)
|
||||
|
||||
subject.clone_vm(template,folder,datastore,target,vsphere)
|
||||
|
||||
expect(redis.scard("vmpooler__pending__#{pool}")).to eq(1)
|
||||
# Get the new VM Name from the pending pool queue as it should be the only entry
|
||||
vm_name = redis.smembers("vmpooler__pending__#{pool}")[0]
|
||||
expect(redis.hget("vmpooler__vm__#{vm_name}", 'clone')).to_not be_nil
|
||||
expect(redis.hget("vmpooler__vm__#{vm_name}", 'template')).to eq(template_name)
|
||||
expect(redis.hget("vmpooler__clone__#{Date.today.to_s}", "#{pool}:#{vm_name}")).to_not be_nil
|
||||
expect(redis.hget("vmpooler__vm__#{vm_name}", 'clone_time')).to_not be_nil
|
||||
end
|
||||
|
||||
it 'should decrement the clone tasks counter' do
|
||||
redis.incr('vmpooler__tasks__clone')
|
||||
redis.incr('vmpooler__tasks__clone')
|
||||
expect(redis.get('vmpooler__tasks__clone')).to eq('2')
|
||||
subject.clone_vm(template,folder,datastore,target,vsphere)
|
||||
expect(redis.get('vmpooler__tasks__clone')).to eq('1')
|
||||
end
|
||||
|
||||
it 'should log a message that is being cloned from a template' do
|
||||
expect(logger).to receive(:log).with('d',/\[ \] \[#{pool}\] '(.+)' is being cloned from '#{template_name}'/)
|
||||
allow(logger).to receive(:log)
|
||||
|
||||
subject.clone_vm(template,folder,datastore,target,vsphere)
|
||||
end
|
||||
|
||||
it 'should log a message that it completed being cloned' do
|
||||
expect(logger).to receive(:log).with('s',/\[\+\] \[#{pool}\] '(.+)' cloned from '#{template_name}' in [0-9.]+ seconds/)
|
||||
allow(logger).to receive(:log)
|
||||
|
||||
subject.clone_vm(template,folder,datastore,target,vsphere)
|
||||
end
|
||||
end
|
||||
|
||||
# An error can be cause by the following configuration errors:
|
||||
# - Missing or invalid datastore
|
||||
# - Missing or invalid clone target
|
||||
# also any runtime errors during the cloning process
|
||||
# https://www.vmware.com/support/developer/converter-sdk/conv50_apireference/vim.VirtualMachine.html#clone
|
||||
context 'with an error during cloning' do
|
||||
before(:each) do
|
||||
expect(vsphere).to receive(:find_least_used_host).with(target).and_return('least_used_host')
|
||||
expect(vsphere).to receive(:find_datastore).with(datastore).and_return(nil)
|
||||
expect(vsphere).to receive(:find_folder).with('vmfolder').and_return(folder_object)
|
||||
expect(template_vm_object).to receive(:CloneVM_Task).and_return(clone_task)
|
||||
expect(clone_task).to receive(:wait_for_completion).and_raise(RuntimeError,'SomeError')
|
||||
expect(metrics).to receive(:timing).with(/clone\./,/0/).exactly(0).times
|
||||
|
||||
end
|
||||
|
||||
it 'should raise an error within the Thread' do
|
||||
expect(logger).to receive(:log).at_least(:once)
|
||||
expect{subject.clone_vm(template,folder,datastore,target,vsphere)}.to raise_error(/SomeError/)
|
||||
end
|
||||
|
||||
it 'should log a message that is being cloned from a template' do
|
||||
expect(logger).to receive(:log).with('d',/\[ \] \[#{pool}\] '(.+)' is being cloned from '#{template_name}'/)
|
||||
allow(logger).to receive(:log)
|
||||
|
||||
# Swallow the error
|
||||
begin
|
||||
subject.clone_vm(template,folder,datastore,target,vsphere)
|
||||
rescue
|
||||
end
|
||||
end
|
||||
|
||||
it 'should log messages that the clone failed' do
|
||||
expect(logger).to receive(:log).with('s', /\[!\] \[#{pool}\] '(.+)' clone failed with an error: SomeError/)
|
||||
expect(logger).to receive(:log).with('s', /\[!\] \[#{pool}\] '(.+)' failed while preparing to clone with an error: SomeError/)
|
||||
allow(logger).to receive(:log)
|
||||
|
||||
# Swallow the error
|
||||
begin
|
||||
subject.clone_vm(template,folder,datastore,target,vsphere)
|
||||
rescue
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#_check_pool' do
|
||||
let(:pool_helper) { double('pool') }
|
||||
let(:vsphere) { {pool => pool_helper} }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue