Skip to content

Commit

Permalink
480/Cleanup tests (#484)
Browse files Browse the repository at this point in the history
* Remove unnecessary tests and reanable skipped test

* Update the end to end testing

* Continue update tests

* Remove test, which tested a user - VM responsibility via the requests

* Fix mocking of VM API

* Fix selection for powering on a VM

* Fix(end to end spec): Adapt to changed VM index HTML.
Make matchers more robust.

* Finally fix the power on test

* Fix formatting

* Add mocking and first validation test

* Add test for valid app_settings

* Finalize tests for app settings, fix regex for vSphere root folder validation
  • Loading branch information
bdaase committed Mar 23, 2019
1 parent 6b1d318 commit b87e6dd
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 168 deletions.
2 changes: 1 addition & 1 deletion app/api/v_sphere/virtual_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def project

# Information about the vm
def boot_time
@vm.runtime.bootTime
@vm.summary.runtime.bootTime
end

def summary
Expand Down
2 changes: 1 addition & 1 deletion app/models/app_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AppSetting < ApplicationRecord
format: { with: %r{\A\/?(?:[0-9a-zA-Z_-]+\/?)*\z}, message: 'has to be a valid UNIX file path' }
validates :vsphere_root_folder,
length: { maximum: VSphere::Folder::VSPHERE_FOLDER_NAME_CHARACTER_LIMIT },
format: { without: %r{/%\/}, message: 'The vSphere root folder may not contain "/", "\" or "%"' }
format: { without: %r{.*[%\/\\].*}, message: 'The vSphere root folder may not contain "/", "\" or "%"' }

after_commit :apply_settings, on: :update

Expand Down
2 changes: 1 addition & 1 deletion spec/api/v_sphere/virtual_machine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
it 'does not throw an error when reading users from invalid node file' do
vm = v_sphere_vm_mock('myVM')
allow(Puppetscript).to receive(:read_node_file).and_raise('Unsupported Format')
expect { vm.users }.not_to raise_error(RuntimeError)
expect {vm.users}.not_to raise_error(RuntimeError)
end
end
29 changes: 14 additions & 15 deletions spec/api/v_sphere_api_mocker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
def extract_vim_objects(collection)
collection.map do |each|
if [VSphere::Cluster, VSphere::Folder, VSphere::VirtualMachine].include? each.class
each.instance_exec { managed_folder_entry }
each.instance_exec {managed_folder_entry}
else
each
end
Expand Down Expand Up @@ -84,12 +84,13 @@ def vim_vm_summary_mock(power_state: 'poweredOn')
allow(summary_double).to receive_message_chain(:config, :numCpu).and_return(2)
allow(summary_double).to receive_message_chain(:runtime, :powerState).and_return(power_state)
allow(summary_double).to receive_message_chain(:runtime, :host, :name).and_return 'aHost'
allow(summary_double).to receive_message_chain(:runtime, :bootTime).and_return Time.now
summary_double
end

def vim_disks_mock
disk = double
allow(disk).to receive(:capacityInKB).and_return(100 * (1024**2))
allow(disk).to receive(:capacityInKB).and_return(100 * (1024 ** 2))
[disk]
end

Expand All @@ -102,33 +103,31 @@ def vim_guest_mock(tools_status: 'toolsNotInstalled')

def vim_vm_mock(
name,
power_state: 'poweredOn',
power_state: 'poweredOff',
vm_ware_tools: 'toolsNotInstalled',
boot_time: 'Yesterday',
guest_heartbeat_status: 'green',
macs: [['Network Adapter 1', 'My Mac address']]
)
)
vm = double
allow(vm).to receive(:name).and_return(name)
allow(vm).to receive(:guestHeartbeatStatus).and_return(guest_heartbeat_status)
allow(vm).to receive(:is_a?).and_return false
allow(vm).to receive(:is_a?).with(RbVmomi::VIM::VirtualMachine).and_return true
allow(vm).to receive_message_chain(:runtime, :powerState).and_return power_state
allow(vm).to receive_message_chain(:runtime, :bootTime).and_return boot_time
allow(vm).to receive(:guest).and_return vim_guest_mock(tools_status: vm_ware_tools)
allow(vm).to receive(:macs).and_return macs
allow(vm).to receive(:boot_time). and_return 0
allow(vm).to receive(:summary).and_return vim_vm_summary_mock(power_state: power_state)
allow(vm).to receive(:disks).and_return vim_disks_mock
allow(vm).to receive_message_chain(:PowerOnVM_Task, :wait_for_completion) do
allow(vm).to receive(:summary).and_return vim_vm_summary_mock(power_state: 'poweredOn')
end

vm
end

def v_sphere_vm_mock(name, power_state: 'poweredOn', vm_ware_tools: 'toolsNotInstalled', boot_time: Time.now - 60 * 60 * 24)
def v_sphere_vm_mock(name, power_state: 'poweredOn', vm_ware_tools: 'toolsNotInstalled')
VSphere::VirtualMachine.new vim_vm_mock(name,
power_state: power_state,
vm_ware_tools: vm_ware_tools,
boot_time: boot_time)
vm_ware_tools: vm_ware_tools)
end

def vim_host_summary_mock
Expand All @@ -140,7 +139,7 @@ def vim_host_summary_mock
allow(summary).to receive_message_chain(:hardware, :numCpuCores).and_return(4)
allow(summary).to receive_message_chain(:hardware, :numCpuThreads).and_return(4)
allow(summary).to receive_message_chain(:hardware, :cpuMhz).and_return(1000)
allow(summary).to receive_message_chain(:hardware, :memorySize).and_return(1000 * 1024**3) # in bytes
allow(summary).to receive_message_chain(:hardware, :memorySize).and_return(1000 * 1024 ** 3) # in bytes
allow(summary).to receive_message_chain(:quickStats, :overallMemoryUsage).and_return(0)
allow(summary).to receive_message_chain(:quickStats, :overallCpuUsage).and_return(0)
summary
Expand All @@ -167,8 +166,8 @@ def vim_host_mock(name)
allow(host).to receive(:summary).and_return vim_host_summary_mock

datastore = double
allow(datastore).to receive_message_chain(:summary, :capacity).and_return(1000 * 1024**3) # in bytes
allow(datastore).to receive_message_chain(:summary, :freeSpace).and_return(1000 * 1024**3) # in bytes
allow(datastore).to receive_message_chain(:summary, :capacity).and_return(1000 * 1024 ** 3) # in bytes
allow(datastore).to receive_message_chain(:summary, :freeSpace).and_return(1000 * 1024 ** 3) # in bytes
allow(host).to receive(:datastore).and_return([datastore])

host
Expand Down Expand Up @@ -202,7 +201,7 @@ def v_sphere_connection_mock(
pending_archivation_vms: [],
pending_revivings_vms: [],
clusters: []
)
)
archived_vms_folder = v_sphere_folder_mock 'Archived VMs', vms: archived_vms
pending_archivation_vms_folder = v_sphere_folder_mock 'Pending archivings', vms: pending_archivation_vms
pending_reviving_vms_folder = v_sphere_folder_mock 'Pending revivings', vms: pending_revivings_vms
Expand Down
8 changes: 0 additions & 8 deletions spec/api/v_sphere_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,6 @@
expect(VSphere::VirtualMachine.find_by_name('Archived VM2').users).to be_empty
end

it 'has users if a fitting request exists' do
skip 'git will not be tested, should have users when a fitting puppet script exists'
request = FactoryBot.create :accepted_request
request.users << FactoryBot.create(:user)
vm = VSphere::VirtualMachine.new(vim_vm_mock(request.name))
expect(vm.users).to match_array(request.users)
end

it 'does not have responsible users if there is no fitting config' do
expect(VSphere::VirtualMachine.find_by_name('Archived VM2').responsible_users).to be_empty
end
Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/dashboard_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
require 'rails_helper'

RSpec.describe DashboardController, type: :controller do
let(:user) { FactoryBot.create :user }
let(:user) {FactoryBot.create :user}

let(:vm1) do
vm = v_sphere_vm_mock 'my-insanely-cool-vm', power_state: 'poweredOn', boot_time: 'Thursday', vm_ware_tools: 'toolsInstalled'
vm = v_sphere_vm_mock 'my-insanely-cool-vm', power_state: 'poweredOn', vm_ware_tools: 'toolsInstalled'
associate_users_with_vms(users: [user], vms: [vm])
vm
end

let(:vm2) do
v_sphere_vm_mock 'not-my-vm', power_state: 'poweredOn', boot_time: 'Thursday', vm_ware_tools: 'toolsInstalled'
v_sphere_vm_mock 'not-my-vm', power_state: 'poweredOn', vm_ware_tools: 'toolsInstalled'
end

before do
Expand All @@ -37,7 +37,7 @@
end

context 'when logged in as an admin' do
let(:user) { FactoryBot.create :admin }
let(:user) {FactoryBot.create :admin}

before do
sign_in(user)
Expand Down
Loading

0 comments on commit b87e6dd

Please sign in to comment.