require 'spec_helper' describe "ESX host" do include ESXTestHelpers before do @test_host = ESX::Host.connect(esx_host, esx_user, esx_password) end after do @test_host.virtual_machines.each do |vm| vm.destroy end end it "connects to and ESX with a valid user/pass" do host = nil lambda do host = ESX::Host.connect(esx_host, esx_user, esx_password) end.should_not raise_error end it "retrives the host name" do @test_host.name.should_not be_nil end it "has no virtual machines" do @test_host.virtual_machines.should be_empty end it "creates one one virtual machine with a 1GB empty disk, 512MB RAB, 1NIC" do name = 'test1GB' disk_size = 1024 datastore = 'datastore1' guest_id = 'otherGuest' memory = 512 nics = [{ :mac_address => nil, :network => nil }] vm = @test_host.create_vm :vm_name => name, :datastore => datastore, :disk_type => :flat, :memory => memory, :disk_size => disk_size, :guest_id => guest_id, :nics => nics @test_host.virtual_machines.size.should eql(1) vm = @test_host.virtual_machines.find { |vm| vm.name == 'test1GB' } vm.should_not be_nil vm.memory_size.should eql(512 * 1024 * 1024) vm.ethernet_cards_number.should eql(1) vm.virtual_disks_number.should eql(1) end it "should be able to create a VM with 2 nics" do name = 'test1GB' disk_size = 1024 datastore = 'datastore1' guest_id = 'otherGuest' memory = 512 nics = [{ :mac_address => nil, :network => nil }, { :mac_address => nil, :network => nil }] vm = @test_host.create_vm :vm_name => name, :datastore => datastore, :disk_type => :flat, :memory => memory, :disk_size => disk_size, :guest_id => guest_id, :nics => nics vm.ethernet_cards_number.should eql(2) end it "should create a VM with 0 NICs if no NIC config is specified" do name = 'test1GB' disk_size = 1024 datastore = 'datastore1' guest_id = 'otherGuest' memory = 512 vm = @test_host.create_vm :vm_name => name, :datastore => datastore, :disk_type => :flat, :memory => memory, :disk_size => disk_size, :guest_id => guest_id vm.ethernet_cards_number.should eql(0) end it "should create a VM with 1 NICs and an autogenerated MAC address" do name = 'test1GB' disk_size = 1024 datastore = 'datastore1' guest_id = 'otherGuest' memory = 512 nics = [{}] vm = @test_host.create_vm :vm_name => name, :datastore => datastore, :disk_type => :flat, :memory => memory, :disk_size => disk_size, :guest_id => guest_id, :nics => nics vm.ethernet_cards_number.should eql(1) vm.nics.first.mac.should eql("") end it "has an array of datastores with one datastore named datastore1" do @test_host.datastores.is_a?(Array).should be_true @test_host.datastores.first.should be_a ESX::Datastore @test_host.datastores.first.name.should eql('datastore1') end it "should be able to create and destroy a VM" do name = 'test1GB' disk_size = 1024 datastore = 'datastore1' guest_id = 'otherGuest' memory = 512 nics = [{ :mac_address => nil, :network => nil }] vm = @test_host.create_vm :vm_name => name, :datastore => datastore, :disk_type => :flat, :memory => memory, :disk_size => disk_size, :guest_id => guest_id, :nics => nics @test_host.virtual_machines.size.should eql(1) vm = @test_host.virtual_machines.find { |vm| vm.name == 'test1GB' } vm.destroy @test_host.virtual_machines.size.should eql(0) end it "should be able to create a VM with a custom MAC address" do name = 'test1GB' disk_size = 1024 datastore = 'datastore1' guest_id = 'otherGuest' memory = 512 nics = [{ :mac_address => '00:01:02:03:04:05', :network => nil }] vm = @test_host.create_vm :vm_name => name, :datastore => datastore, :disk_type => :flat, :memory => memory, :disk_size => disk_size, :guest_id => guest_id, :nics => nics vm = @test_host.virtual_machines.find { |vm| vm.name == name } vm.nics.first.mac.should eql('00:01:02:03:04:05') end it "should have valid property types" do @test_host.memory_size.should be_a Fixnum @test_host.memory_size.should be > 0 @test_host.memory_usage.should be_a Fixnum @test_host.memory_usage.should be > 0 end it "should have default templates path" do @test_host.templates_dir.should == "/vmfs/volumes/datastore1/esx-gem/templates" end it "should not have tc.vmdk template" do @test_host.has_template?("tc.vmdk").should == false @test_host.list_templates.size.should == 0 end it "should not have -flat.vmdk postfix in template names" do @test_host.list_templates.each do |t| t.should_not match /-flat\.vmdk$/ end end it "should import tc.vmdk template" do @test_host.import_template File.join(test_data_dir, "tc.vmdk") @test_host.has_template?("tc.vmdk").should == true end it "should copy from template" do @test_host.has_template?("tc.vmdk").should == true @test_host.remote_command("mkdir -p /vmfs/volumes/datastore1/foovm/") @test_host.copy_from_template "tc.vmdk", "/vmfs/volumes/datastore1/foovm/foovm.vmdk" @test_host.remote_command("ls /vmfs/volumes/datastore1/foovm/foovm.vmdk").strip.chomp.should == "/vmfs/volumes/datastore1/foovm/foovm.vmdk" @test_host.remote_command("rm -rf /vmfs/volumes/datastore1/foovm") end it "should list one template" do @test_host.list_templates.is_a?(Array).should be_true @test_host.list_templates.size.should == 1 end it "should import duplicated tc.vmdk template" do @test_host.import_template File.join(test_data_dir, "tc.vmdk") @test_host.has_template?("tc.vmdk").should == true @test_host.list_templates.size.should == 1 end it "should delete tc.vmdk and tc-flat.vmdk template" do @test_host.delete_template File.join(test_data_dir, "tc.vmdk") @test_host.has_template?("tc.vmdk").should == false @test_host.list_templates.size.should == 0 end it "should import disk tc.vmdk into desired dir" do @test_host.remote_command("mkdir /vmfs/volumes/datastore1/foobar/") @test_host.import_disk(File.join(test_data_dir, "tc.vmdk"), "/vmfs/volumes/datastore1/foobar/foobar.vmdk") @test_host.remote_command("ls /vmfs/volumes/datastore1/foobar/foobar.vmdk").strip.chomp.should == "/vmfs/volumes/datastore1/foobar/foobar.vmdk" @test_host.remote_command "rm -rf /vmfs/volumes/datastore1/foobar/" end end