Skip to content

Commit

Permalink
Merge pull request #229 from OSLL/7167_libvirt_uuid_by_domain_name
Browse files Browse the repository at this point in the history
7167 libvirt uuid by domain name
  • Loading branch information
zmm committed Jul 5, 2016
2 parents 55f77db + d7f28f5 commit 8d4cfde
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
15 changes: 12 additions & 3 deletions core/clone.rb
@@ -1,11 +1,19 @@
require 'open3'

require_relative 'out'
require_relative 'helper'

UUID_FOR_DOMAIN_NOT_FOUND_ERROR = 'uuid for domain is not found'
CONFIG_DIRECTORY_NOT_FOUND_ERROR = 'config directory is not found'
NODE_NOT_FOUND_ERROR = 'node is not found'

def get_libvirt_uuid_by_domain_name(domain_name)
list_output = execute_bash('virsh -q list --all | awk \'{print $2}\'', true).to_s.split "\n"
list_uuid_output = execute_bash('virsh -q list --uuid --all', true).to_s.split "\n"
list_output.zip(list_uuid_output).each do |domain, uuid|
return uuid if domain == domain_name
end
raise "#{domain_name}: #{UUID_FOR_DOMAIN_NOT_FOUND_ERROR}"
end

# method returns new image name (which will be used later as a box for template)
def create_docker_node_clone(path_to_nodes, node_name, path_to_new_config_directory)
raise "#{path_to_nodes} #{CONFIG_DIRECTORY_NOT_FOUND_ERROR}" unless Dir.exist? path_to_nodes
Expand All @@ -14,4 +22,5 @@ def create_docker_node_clone(path_to_nodes, node_name, path_to_new_config_direct
new_docker_image_name = "#{path_to_new_config_directory}_#{node_name}_#{Time.now.to_i}"
execute_bash "docker commit -p #{old_docker_image_name} #{new_docker_image_name}"
return new_docker_image_name
end
end

@@ -0,0 +1,7 @@
{
"cookbook_path": "../recipes/cookbooks/",
"node0": {
"hostname": "node0",
"box": "ubuntu_trusty_libvirt"
}
}
26 changes: 26 additions & 0 deletions spec/unit/7167_libvirt_uuid_by_domain_name.rb
@@ -0,0 +1,26 @@
require_relative '../../core/clone'
require_relative '../../core/helper'

describe nil do

before :all do
$out = Out.new
$session = Session.new
end

it 'node exists and method will not throws error' do
lambda {get_libvirt_uuid_by_domain_name("#{ENV['config_name']}_#{ENV['node_name']}")}.should_not raise_error
end

it 'node does not exist and method will throws error' do
lambda {get_libvirt_uuid_by_domain_name('WRONG')}
.should raise_error 'WRONG: uuid for domain is not found'
end

it 'method returns existing uuid' do
uuid = get_libvirt_uuid_by_domain_name("#{ENV['config_name']}_#{ENV['node_name']}")
output = execute_bash('virsh -q list --uuid --all', true)
output.should include uuid
end

end

0 comments on commit 8d4cfde

Please sign in to comment.