diff --git a/spec/api/v_sphere_api_mocker.rb b/spec/api/v_sphere_api_mocker.rb index e202c9d7..1fc59341 100644 --- a/spec/api/v_sphere_api_mocker.rb +++ b/spec/api/v_sphere_api_mocker.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/controllers/requests_controller_spec.rb b/spec/controllers/requests_controller_spec.rb index f58ea32b..280b9812 100644 --- a/spec/controllers/requests_controller_spec.rb +++ b/spec/controllers/requests_controller_spec.rb @@ -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', @@ -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: [''] @@ -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