From a65db7f594c6c5fde37d4c393e6438e1d0f71163 Mon Sep 17 00:00:00 2001 From: Kelsey Hightower Date: Tue, 8 Nov 2011 00:47:27 -0500 Subject: [PATCH] (#10570) Update `Fog::Compute::Vsphere` tests Update `Fog::Compute::Vsphere` tests to reflect the way the `Fog::Compute::Vsphere#convert_vm_mob_ref_to_attr_hash` method currently converts a `RbVmomi::VIM::ManagedObject` object to a hash. Without this patch, tests related to converting an instance of `RbVmomi::VIM::ManagedObject` to a hash will raise an exception: NoMethodError: undefined method `collect!' for Hash; causing the test to fail. This patch solves the problem by mocking `RbVmomi::VIM::ManagedObject` with a new `MockManagedObject` class, which provides a `collect!` method, and the `_ref` and `parent` attributes. This patch renames fake_vm to fake_vm_mob_ref in order to provide a more descriptive name for what's actually being tested. The `Fog::Compute::Vshpere::Mock` class has been updated to require the rbvmomi library, which prevents an `NameError` exception from being raised due to the `Fog::Compute::Vsphere::Shared::RbVmomi` constant not being initialized. The `Fog::Compute::Vshpere::Mock` class has been updated with a `get_folder_path` method, which prevents a `NoMethodError` exception from being raised due to the `get_folder_path` method being undefined. --- lib/fog/vsphere/compute.rb | 1 + .../requests/compute/list_virtual_machines.rb | 4 +++ tests/vsphere/compute_tests.rb | 28 ++++++++++++------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/fog/vsphere/compute.rb b/lib/fog/vsphere/compute.rb index 55acba26d..7292cb01e 100644 --- a/lib/fog/vsphere/compute.rb +++ b/lib/fog/vsphere/compute.rb @@ -84,6 +84,7 @@ module Fog include Shared def initialize(options={}) + require 'rbvmomi' @vsphere_username = options[:vsphere_username] @vsphere_password = 'REDACTED' @vsphere_server = options[:vsphere_server] diff --git a/lib/fog/vsphere/requests/compute/list_virtual_machines.rb b/lib/fog/vsphere/requests/compute/list_virtual_machines.rb index 1e68ddf0d..84a48cb40 100644 --- a/lib/fog/vsphere/requests/compute/list_virtual_machines.rb +++ b/lib/fog/vsphere/requests/compute/list_virtual_machines.rb @@ -86,6 +86,10 @@ module Fog class Mock + def get_folder_path(folder, root = nil) + nil + end + def list_virtual_machines(options = {}) case options['instance_uuid'] when nil diff --git a/tests/vsphere/compute_tests.rb b/tests/vsphere/compute_tests.rb index 13ff9c946..57a02f2e9 100644 --- a/tests/vsphere/compute_tests.rb +++ b/tests/vsphere/compute_tests.rb @@ -3,21 +3,28 @@ Shindo.tests('Fog::Compute[:vsphere]', ['vsphere']) do compute = Fog::Compute[:vsphere] tests("| convert_vm_mob_ref_to_attr_hash") do - require 'ostruct' + # Mock the RbVmomi::VIM::ManagedObject class + class MockManagedObject - fake_vm = OpenStruct.new({ - :_ref => 'vm-123', - :name => 'fakevm', - :summary => OpenStruct.new(:guest => OpenStruct.new), - :runtime => OpenStruct.new, - }) + attr_reader :parent, :_ref + + def initialize + @parent = @_ref = 'vm-123' + end + + def collect! *pathSet + { '_ref' => 'vm-123', 'name' => 'fakevm' } + end + end + + fake_vm_mob_ref = MockManagedObject.new tests("When converting an incomplete vm object") do test("it should return a Hash") do - compute.convert_vm_mob_ref_to_attr_hash(fake_vm).kind_of? Hash + compute.convert_vm_mob_ref_to_attr_hash(fake_vm_mob_ref).kind_of? Hash end tests("The converted Hash should") do - attr_hash = compute.convert_vm_mob_ref_to_attr_hash(fake_vm) + attr_hash = compute.convert_vm_mob_ref_to_attr_hash(fake_vm_mob_ref) test("have a name") { attr_hash['name'] == 'fakevm' } test("have a mo_ref") {attr_hash['mo_ref'] == 'vm-123' } test("have an id") { attr_hash['id'] == 'vm-123' } @@ -38,10 +45,11 @@ Shindo.tests('Fog::Compute[:vsphere]', ['vsphere']) do test("it should respond to #{attr}") { compute.respond_to? attr } end end + tests("Compute collections") do %w{ servers }.each do |collection| test("it should respond to #{collection}") { compute.respond_to? collection } end end - end +