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

end