Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonMatthes committed Mar 13, 2019
1 parent 24011c3 commit 3edc49a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
32 changes: 20 additions & 12 deletions spec/api/v_sphere_api_mocker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ def v_sphere_vm_mock(name, power_state: 'poweredOn', vm_ware_tools: 'toolsNotIns
boot_time: boot_time)
end

def vim_host_summary_mock
summary = double
allow(summary).to receive_message_chain(:runtime, :powerState)
allow(summary).to receive_message_chain(:config, :product, :osType).and_return('someOS')
allow(summary).to receive_message_chain(:config, :product, :fullName).and_return(['someProduct'])
allow(summary).to receive_message_chain(:hardware, :cpuModel).and_return('someModel')
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(:quickStats, :overallMemoryUsage).and_return(0)
allow(summary).to receive_message_chain(:quickStats, :overallCpuUsage).and_return(0)
summary
end

def vim_host_mock(name)
host = double
allow(host).to receive(:name).and_return name
Expand All @@ -149,18 +164,8 @@ def vim_host_mock(name)
power_state: 'poweredOn',
vm_ware_tools: 'toolsInstalled')])

summary = double
allow(summary).to receive_message_chain(:runtime, :powerState)
allow(summary).to receive_message_chain(:config, :product, :osType).and_return('someOS')
allow(summary).to receive_message_chain(:config, :product, :fullName).and_return(['someProduct'])
allow(summary).to receive_message_chain(:hardware, :cpuModel).and_return('someModel')
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(:quickStats, :overallMemoryUsage).and_return(0)
allow(summary).to receive_message_chain(:quickStats, :overallCpuUsage).and_return(0)
allow(host).to receive(:summary).and_return summary

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
Expand All @@ -183,6 +188,9 @@ def vim_cluster_mock(name, hosts)
allow(cluster).to receive(:host).and_return hosts
allow(cluster).to receive(:name).and_return name
allow(cluster).to receive(:resourcePool).and_return nil
network = double
allow(network).to receive(:name).and_return 'MyNetwork'
allow(cluster).to receive(:network).and_return [network]
cluster
end
# rubocop:enable Metrics/AbcSize
Expand Down
43 changes: 29 additions & 14 deletions spec/controllers/requests_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
end

describe 'PATCH #update' do
context 'with valid params' do
context 'accepts the request' do
let(:new_attributes) do
{
name: 'mynewvm',
Expand All @@ -230,7 +230,6 @@
storage_gb: 3,
operating_system: 'MyNewOS',
comment: 'newComment',
status: 'pending',
user: user,
sudo_user_ids: ['', sudo_user.id.to_s, user.id.to_s],
user_ids: ['']
Expand All @@ -245,25 +244,41 @@
request
end

before do
it 'redirects to the index page if no cluster is available' do
allow(VSphere::Cluster).to receive(:all).and_return []
patch :update, params: { id: the_request.to_param, request: new_attributes }
the_request.reload
expect(response).to redirect_to(requests_path)
end

it 'updates the request' do
expect(the_request.name).to eq('mynewvm')
it 'redirects to the index page if the cluster does not have a network' do
cluster = double
allow(cluster).to receive(:networks).and_return []
allow(VSphere::Cluster).to receive(:all).and_return [cluster]
patch :update, params: { id: the_request.to_param, request: new_attributes }
expect(response).to redirect_to(requests_path)
end

it 'redirects to the new VMS config' do
expect(response).to redirect_to(edit_config_path(the_request.name))
end
context 'update already performed' do
before do
patch :update, params: { id: the_request.to_param, request: new_attributes }
the_request.reload
end

it 'accepts the request' do
expect(the_request).to be_accepted
end
it 'updates the request' do
expect(the_request.name).to eq('mynewvm')
end

it 'redirects to the new VMS config' do
expect(response).to redirect_to(edit_config_path(the_request.name))
end

it 'accepts the request' do
expect(the_request).to be_accepted
end

it 'correctly updates the sudo users' do
expect(the_request.sudo_users).to match_array([sudo_user, user])
it 'correctly updates the sudo users' do
expect(the_request.sudo_users).to match_array([sudo_user, user])
end
end
end

Expand Down

0 comments on commit 3edc49a

Please sign in to comment.