From d4c0b52aa7fbc642f58036d24f26b6d1752b2f99 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Mon, 21 Jan 2019 17:47:21 +0100 Subject: [PATCH 01/25] Associate requests with responsible users --- app/controllers/requests_controller.rb | 5 +++-- app/models/request.rb | 2 ++ app/models/user.rb | 1 + app/views/errors/timeout.html.erb | 3 ++- app/views/requests/_form.html.erb | 14 +++++++++++-- app/views/requests/show.html.erb | 11 ++++++++++ ...e_join_table_requests_responsible_users.rb | 7 +++++++ db/schema.rb | 8 +++++++- spec/controllers/requests_controller_spec.rb | 9 ++++++++- spec/factories/requests.rb | 1 + spec/features/dashboard/dashboard_spec.rb | 20 ------------------- spec/views/requests/edit.html.erb_spec.rb | 3 ++- spec/views/requests/index.html.erb_spec.rb | 6 ++++-- spec/views/requests/show.html.erb_spec.rb | 3 ++- 14 files changed, 62 insertions(+), 31 deletions(-) create mode 100644 db/migrate/20190121160434_create_join_table_requests_responsible_users.rb diff --git a/app/controllers/requests_controller.rb b/app/controllers/requests_controller.rb index cb9fe271..95937040 100644 --- a/app/controllers/requests_controller.rb +++ b/app/controllers/requests_controller.rb @@ -41,7 +41,7 @@ def notify_users(title, message) # POST /requests # POST /requests.json def create - params[:request][:name] = replace_whitespaces(params[:request][:name]) + params[:request][:name] = replace_whitespaces(params[:request][:name]) if params[:request] && params[:request][:name] @request = Request.new(request_params.merge(user: current_user)) respond_to do |format| @@ -124,6 +124,7 @@ def successful_save(format) end def unsuccessful_action(format, method) + @request_templates = RequestTemplate.all format.html { render method } format.json { render json: @request.errors, status: :unprocessable_entity } end @@ -132,6 +133,6 @@ def unsuccessful_action(format, method) def request_params params.require(:request).permit(:name, :cpu_cores, :ram_mb, :storage_mb, :operating_system, :port, :application_name, :description, :comment, - :rejection_information, :status, user_ids: [], sudo_user_ids: []) + :rejection_information, :status, responsible_user_ids: [], user_ids: [], sudo_user_ids: []) end end diff --git a/app/models/request.rb b/app/models/request.rb index 50731cd4..1a61365f 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -4,6 +4,7 @@ class Request < ApplicationRecord has_many :users_assigned_to_requests has_many :users, through: :users_assigned_to_requests belongs_to :user + has_and_belongs_to_many :responsible_users, class_name: 'User', join_table: 'requests_responsible_users' attr_accessor :sudo_user_ids @@ -21,6 +22,7 @@ class Request < ApplicationRecord validates :cpu_cores, numericality: { greater_than: 0, less_than: MAX_CPU_CORES } validates :ram_mb, numericality: { greater_than: 0, less_than_or_equal_to: MAX_RAM_MB } validates :storage_mb, numericality: { greater_than: 0, less_than_or_equal_to: MAX_STORAGE_MB } + validates :responsible_users, presence: true def description_text(host_name) description = "- VM Name: #{name}\n" diff --git a/app/models/user.rb b/app/models/user.rb index 25db8bf8..774115a9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -17,6 +17,7 @@ class User < ApplicationRecord has_many :users_assigned_to_requests has_many :requests, through: :users_assigned_to_requests + has_and_belongs_to_many :request_responsibilities, class_name: 'Request', join_table: 'requests_responsible_users' validates :first_name, presence: true validates :last_name, presence: true validate :valid_ssh_key diff --git a/app/views/errors/timeout.html.erb b/app/views/errors/timeout.html.erb index 07103adc..2daf657c 100644 --- a/app/views/errors/timeout.html.erb +++ b/app/views/errors/timeout.html.erb @@ -1,5 +1,6 @@

Sorry!

- It seems you have lost connection to the HPI network :( + We could not reach vSphere :(
+ Please make sure you are connected to the HPI network.

\ No newline at end of file diff --git a/app/views/requests/_form.html.erb b/app/views/requests/_form.html.erb index c84b12c3..e04ca26f 100644 --- a/app/views/requests/_form.html.erb +++ b/app/views/requests/_form.html.erb @@ -35,6 +35,16 @@ <%= form.text_field :name, class: 'form-control' %> +
+

Responsible Users

+ <%= form.select 'responsible_user_ids', + options_from_collection_for_select(User.all, + :id, + :email), + {}, + { multiple: true, class: "selecttwo", data: { placeholder: 'Who is responsible for this VM?' }, style: 'width: 50%' } %> +
+
<%= form.label :cpu_cores, "CPU-Cores" %> <%= form.number_field :cpu_cores, min: 0, class: 'form-control', id: 'cpu' %> @@ -51,7 +61,7 @@
-

Users with sudo rights:

+

Users with sudo rights

<%= form.select 'sudo_user_ids', options_from_collection_for_select(User.all, :id, @@ -61,7 +71,7 @@
-

Assigned Users:

+

Assigned Users

<%= form.select 'user_ids', options_from_collection_for_select(User.all, :id, diff --git a/app/views/requests/show.html.erb b/app/views/requests/show.html.erb index 122e8c83..aff7d9e8 100644 --- a/app/views/requests/show.html.erb +++ b/app/views/requests/show.html.erb @@ -54,6 +54,17 @@ +

+ Responsible users: +

+

+

Users with sudo rights:

@@ -63,7 +64,7 @@
- <%= form.label :operating_system, "Operating System" %>
+ <%= form.label :operating_system, "Operating System" %> <%= form.select :operating_system, operating_system_options, {}, id: 'operating_system', style: 'width: 100%' %>
From f256541f07a7772935c0c87c4e6aaf2ed42a9875 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 10:30:41 +0100 Subject: [PATCH 07/25] Add responsible users to VMConfigs --- app/models/virtual_machine_config.rb | 1 + ...ate_join_table_virtual_machine_config_users.rb | 10 ++++++++++ db/schema.rb | 15 +++++++++++---- spec/factories/virtual_machine_configs.rb | 1 + spec/models/virtual_machine_config_spec.rb | 10 +++++++++- 5 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20190201091109_create_join_table_virtual_machine_config_users.rb diff --git a/app/models/virtual_machine_config.rb b/app/models/virtual_machine_config.rb index c10b111a..f0cd2684 100644 --- a/app/models/virtual_machine_config.rb +++ b/app/models/virtual_machine_config.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true class VirtualMachineConfig < ApplicationRecord + has_and_belongs_to_many :responsible_users, class_name: 'User' end diff --git a/db/migrate/20190201091109_create_join_table_virtual_machine_config_users.rb b/db/migrate/20190201091109_create_join_table_virtual_machine_config_users.rb new file mode 100644 index 00000000..bb93a51f --- /dev/null +++ b/db/migrate/20190201091109_create_join_table_virtual_machine_config_users.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class CreateJoinTableVirtualMachineConfigUsers < ActiveRecord::Migration[5.2] + def change + create_join_table :virtual_machine_configs, :users do |t| + t.index %i[virtual_machine_config_id user_id], name: 'index_virtual_machine_configs_responsible_users' + t.index %i[user_id virtual_machine_config_id], name: 'index_responsible_users_virtual_machine_configs' + end + end +end diff --git a/db/schema.rb b/db/schema.rb index e4f35b6a..6b3365f5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_01_29_184200) do +ActiveRecord::Schema.define(version: 2019_02_01_091109) do create_table "archivation_requests", force: :cascade do |t| t.string "name", null: false @@ -129,12 +129,12 @@ t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.integer "role" - t.string "provider" - t.string "uid" - t.string "ssh_key" t.string "first_name" t.string "last_name" + t.string "provider" + t.string "uid" t.integer "user_id" + t.string "ssh_key" t.boolean "email_notifications", default: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true @@ -150,6 +150,13 @@ t.index ["user_id"], name: "index_users_assigned_to_requests_on_user_id" end + create_table "users_virtual_machine_configs", id: false, force: :cascade do |t| + t.integer "virtual_machine_config_id", null: false + t.integer "user_id", null: false + t.index ["user_id", "virtual_machine_config_id"], name: "index_responsible_users_virtual_machine_configs" + t.index ["virtual_machine_config_id", "user_id"], name: "index_virtual_machine_configs_responsible_users" + end + create_table "virtual_machine_configs", force: :cascade do |t| t.string "name" t.string "ip" diff --git a/spec/factories/virtual_machine_configs.rb b/spec/factories/virtual_machine_configs.rb index 39697131..70af3f36 100644 --- a/spec/factories/virtual_machine_configs.rb +++ b/spec/factories/virtual_machine_configs.rb @@ -5,5 +5,6 @@ name { 'My VM' } ip { '127.0.0.1' } dns { 'my-vm.epic-hpi.de' } + responsible_users { [FactoryBot.create(:user)] } end end diff --git a/spec/models/virtual_machine_config_spec.rb b/spec/models/virtual_machine_config_spec.rb index 3e8fb15e..908bfd30 100644 --- a/spec/models/virtual_machine_config_spec.rb +++ b/spec/models/virtual_machine_config_spec.rb @@ -3,5 +3,13 @@ require 'rails_helper' RSpec.describe VirtualMachineConfig, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + + let(:config) { FactoryBot.create(:virtual_machine_config) } + + it 'can save responsible users' do + responsible_users = [FactoryBot.create(:admin)] + config.responsible_users = responsible_users + config.save! + expect(config.responsible_users).to match_array(responsible_users) + end end From 2ed7955004a5989f8bc37d068afcd10e2274220d Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 10:31:46 +0100 Subject: [PATCH 08/25] More tests --- spec/models/virtual_machine_config_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/models/virtual_machine_config_spec.rb b/spec/models/virtual_machine_config_spec.rb index 908bfd30..23a3aeab 100644 --- a/spec/models/virtual_machine_config_spec.rb +++ b/spec/models/virtual_machine_config_spec.rb @@ -12,4 +12,9 @@ config.save! expect(config.responsible_users).to match_array(responsible_users) end + + it 'can save without responsible users' do + config.responsible_users = [] + expect(config).to be_valid + end end From a259c00643cf2778643b2ae6ce58d88954d4f76e Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 11:40:51 +0100 Subject: [PATCH 09/25] Fix potential issue, if a vm is called requests or configs --- config/routes.rb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index c8695c9a..5abd037c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,19 +20,19 @@ get '/hosts/:id' => 'hosts#show', constraints: { id: /.*/ } - get '/vms/configs/:id' => 'vms#edit_config', constraints: { id: /.*/ } - patch '/vms/configs/:id' => 'vms#update_config', constraints: { id: /.*/ } - - post '/vms/:id/change_power_state' => 'vms#change_power_state', constraints: { id: /.*/ } - post '/vms/:id/suspend_vm' => 'vms#suspend_vm', constraints: { id: /.*/ } - post '/vms/:id/shutdown_guest_os' => 'vms#shutdown_guest_os', constraints: { id: /.*/ } - post '/vms/:id/reboot_guest_os' => 'vms#reboot_guest_os', constraints: { id: /.*/ } - post '/vms/:id/reset_vm' => 'vms#reset_vm', constraints: { id: /.*/ } - post '/vms/:id/request_vm_archivation' => 'vms#request_vm_archivation', constraints: { id: /.*/ } - post '/vms/:id/archive_vm' => 'vms#archive_vm', constraints: { id: /.*/ } - post '/vms/:id/request_vm_revive' => 'vms#request_vm_revive', constraints: { id: /.*/ } - post '/vms/:id/revive_vm' => 'vms#revive_vm', constraints: { id: /.*/ } - post '/vms/:id/stop_archiving' => 'vms#stop_archiving', constraints: { id: /.*/ } + get '/vms/configs/:id' => 'vms#edit_config', constraints: { id: /.*/ }, as: :edit_config + patch '/vms/configs/:id' => 'vms#update_config', constraints: { id: /.*/ }, as: :update_config + + post '/vms/vm/:id/change_power_state' => 'vms#change_power_state', constraints: { id: /.*/ } + post '/vms/vm/:id/suspend_vm' => 'vms#suspend_vm', constraints: { id: /.*/ } + post '/vms/vm/:id/shutdown_guest_os' => 'vms#shutdown_guest_os', constraints: { id: /.*/ } + post '/vms/vm/:id/reboot_guest_os' => 'vms#reboot_guest_os', constraints: { id: /.*/ } + post '/vms/vm/:id/reset_vm' => 'vms#reset_vm', constraints: { id: /.*/ } + post '/vms/vm/:id/request_vm_archivation' => 'vms#request_vm_archivation', constraints: { id: /.*/ } + post '/vms/vm/:id/archive_vm' => 'vms#archive_vm', constraints: { id: /.*/ } + post '/vms/vm/:id/request_vm_revive' => 'vms#request_vm_revive', constraints: { id: /.*/ } + post '/vms/vm/:id/revive_vm' => 'vms#revive_vm', constraints: { id: /.*/ } + post '/vms/vm/:id/stop_archiving' => 'vms#stop_archiving', constraints: { id: /.*/ } get 'slack/new' => 'slack#new', as: :new_slack get 'slack/auth' => 'slack#update', as: :update_slack From da09d61a2b8d12535dcc57139387010118407a84 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 11:53:04 +0100 Subject: [PATCH 10/25] Add tests for vm creation --- spec/api/v_sphere_api_mocker.rb | 20 ++++++++++++++----- spec/api/v_sphere_api_spec.rb | 4 ++-- spec/controllers/requests_controller_spec.rb | 14 ++++++------- spec/controllers/vms_controller_spec.rb | 2 +- .../requests/accept_reject_request_spec.rb | 4 ++-- spec/models/request_spec.rb | 10 ++++++++++ spec/spec_helper.rb | 3 ++- spec/views/vms/show.html.erb_spec.rb | 2 +- 8 files changed, 39 insertions(+), 20 deletions(-) diff --git a/spec/api/v_sphere_api_mocker.rb b/spec/api/v_sphere_api_mocker.rb index 4fbf20d0..350c2288 100644 --- a/spec/api/v_sphere_api_mocker.rb +++ b/spec/api/v_sphere_api_mocker.rb @@ -43,6 +43,15 @@ def vim_folder_mock(name, subfolders, vms, clusters) # rubocop:disable Metrics/A allow(folder).to receive(:children).and_return(children) allow(folder).to receive(:is_a?).and_return false allow(folder).to receive(:is_a?).with(RbVmomi::VIM::Folder).and_return true + allow(folder).to receive(:CreateVM_Task) do |*args| + task = double + allow(task).to receive(:wait_for_completion) do + vm = vim_vm_mock(args.first[:config][:name]) + children << vm + vm + end + task + end folder end @@ -118,6 +127,7 @@ def vim_cluster_mock(name, hosts) allow(cluster).to receive(:is_a?).with(RbVmomi::VIM::ComputeResource).and_return true allow(cluster).to receive(:host).and_return hosts allow(cluster).to receive(:name).and_return name + allow(cluster).to receive(:resourcePool).and_return nil cluster end # rubocop:enable Metrics/AbcSize @@ -127,11 +137,11 @@ def v_sphere_cluster_mock(name, hosts) end def v_sphere_connection_mock( - normal_vms, - archived_vms, - pending_archivation_vms, - pending_revivings_vms, - clusters + normal_vms: [], + archived_vms: [], + 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 diff --git a/spec/api/v_sphere_api_spec.rb b/spec/api/v_sphere_api_spec.rb index ceeb9d4f..50d662da 100644 --- a/spec/api/v_sphere_api_spec.rb +++ b/spec/api/v_sphere_api_spec.rb @@ -80,7 +80,7 @@ describe VSphere::Cluster do before do - allow(VSphere::Connection).to receive(:instance).and_return v_sphere_connection_mock([], [], [], [], clusters_mock) + allow(VSphere::Connection).to receive(:instance).and_return v_sphere_connection_mock(clusters: clusters_mock) end it 'Cluster.all finds all clusters' do @@ -90,7 +90,7 @@ describe VSphere::Host do before do - allow(VSphere::Connection).to receive(:instance).and_return v_sphere_connection_mock([], [], [], [], clusters_mock) + allow(VSphere::Connection).to receive(:instance).and_return v_sphere_connection_mock(clusters: clusters_mock) end it 'Host.all finds all hosts' do diff --git a/spec/controllers/requests_controller_spec.rb b/spec/controllers/requests_controller_spec.rb index d70693ab..2759a9bc 100644 --- a/spec/controllers/requests_controller_spec.rb +++ b/spec/controllers/requests_controller_spec.rb @@ -199,26 +199,24 @@ request end - it 'updates the request' do + before do patch :update, params: { id: the_request.to_param, request: new_attributes } the_request.reload + end + + it 'updates the request' do expect(the_request.name).to eq('mynewvm') end - it 'redirects to the requests index page, as there is no cluster available' do - patch :update, params: { id: the_request.to_param, request: valid_attributes } - expect(response).to redirect_to(requests_path) + 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 - patch :update, params: { id: the_request.to_param, request: valid_attributes } - the_request.reload expect(the_request).to be_accepted end it 'correctly updates the sudo users' do - patch :update, params: { id: the_request.to_param, request: new_attributes } - the_request.reload expect(the_request.sudo_users).to match_array([sudo_user]) end end diff --git a/spec/controllers/vms_controller_spec.rb b/spec/controllers/vms_controller_spec.rb index f8ac3648..a70fe96f 100644 --- a/spec/controllers/vms_controller_spec.rb +++ b/spec/controllers/vms_controller_spec.rb @@ -28,7 +28,7 @@ end before do - allow(VSphere::Connection).to receive(:instance).and_return v_sphere_connection_mock([vm1, vm2], [], [], [], []) + allow(VSphere::Connection).to receive(:instance).and_return v_sphere_connection_mock(normal_vms: [vm1, vm2]) end describe 'GET #index' do diff --git a/spec/features/requests/accept_reject_request_spec.rb b/spec/features/requests/accept_reject_request_spec.rb index 1e94b21d..1d2f384e 100644 --- a/spec/features/requests/accept_reject_request_spec.rb +++ b/spec/features/requests/accept_reject_request_spec.rb @@ -22,8 +22,8 @@ expect(request.status).to eq('accepted') end - it 'redirects to the requests page, as the vm cannot be created' do - expect(page).to have_current_path(requests_path) + it 'redirects to VMs config page' do + expect(page).to have_current_path(edit_config_path(request.name)) end end diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb index 7bc95df2..1b5b7e0f 100644 --- a/spec/models/request_spec.rb +++ b/spec/models/request_spec.rb @@ -204,6 +204,16 @@ end end + describe 'create_vm' do + let(:request) {FactoryBot.create(:request)} + + it 'saves the responsible users in the VM' do + request.responsible_users = [FactoryBot.create(:admin)] + vm = request.create_vm + expect(vm.responsible_users).to match_array(request.responsible_users) + end + end + describe 'puppet script helper methods' do let(:request) { FactoryBot.create(:request_with_users) } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 759bd585..5fa05231 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -49,7 +49,8 @@ RSpec.configure do |config| config.before do - allow(VSphere::Connection).to receive(:instance).and_return(v_sphere_connection_mock([], [], [], [], [])) + cluster_mock = vim_cluster_mock('MockCluster', []) + allow(VSphere::Connection).to receive(:instance).and_return(v_sphere_connection_mock(clusters: [cluster_mock])) @git_stub = create_git_stub end diff --git a/spec/views/vms/show.html.erb_spec.rb b/spec/views/vms/show.html.erb_spec.rb index 97bf075e..2baca4e1 100644 --- a/spec/views/vms/show.html.erb_spec.rb +++ b/spec/views/vms/show.html.erb_spec.rb @@ -24,7 +24,7 @@ before do sign_in current_user assign(:vm, vm_on) - connection = v_sphere_connection_mock [vm_on, vm_on_without_tools, vm_off], [], [], [], [] + connection = v_sphere_connection_mock normal_vms: [vm_on, vm_on_without_tools, vm_off] allow(VSphere::Connection).to receive(:instance).and_return connection render end From 1d480c68e1b9e86374d56ff50aa541f18f450a22 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 11:55:26 +0100 Subject: [PATCH 11/25] Responsible users are now also stored in the VM config --- app/api/v_sphere/virtual_machine.rb | 27 ++++++++++++++------------- spec/models/request_spec.rb | 2 +- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/app/api/v_sphere/virtual_machine.rb b/app/api/v_sphere/virtual_machine.rb index ae5469ce..8612139f 100644 --- a/app/api/v_sphere/virtual_machine.rb +++ b/app/api/v_sphere/virtual_machine.rb @@ -197,14 +197,27 @@ def boot_time @vm.runtime.bootTime end + # Users def responsible_users - request&.responsible_users || [] + config&.responsible_users || [] end def users request&.users || [] end + def sudo_users + if request + request.sudo_users + else + [] + end + end + + def belongs_to(user) + users.include? user + end + def summary @vm.summary end @@ -237,18 +250,6 @@ def status end end - def sudo_users - if request - request.sudo_users - else - [] - end - end - - def belongs_to(user) - users.include? user - end - # We cannot use Object identity to check if to Virtual Machine objects are equal # because they are created on demand and to Virtual Machine objects can wrap the same vSphere VM. # Therefore we must use another method of comparing equality. diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb index 1b5b7e0f..cc793148 100644 --- a/spec/models/request_spec.rb +++ b/spec/models/request_spec.rb @@ -205,7 +205,7 @@ end describe 'create_vm' do - let(:request) {FactoryBot.create(:request)} + let(:request) { FactoryBot.create(:request) } it 'saves the responsible users in the VM' do request.responsible_users = [FactoryBot.create(:admin)] From 5ab9ebe21ebf4b74cf4880f8f101b4325963a42f Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 12:03:24 +0100 Subject: [PATCH 12/25] responsible users are now assigned to virtual machines --- app/models/request.rb | 4 +++- spec/api/v_sphere_api_spec.rb | 12 ++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/models/request.rb b/app/models/request.rb index 47ca0772..1344debd 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -70,7 +70,9 @@ def non_sudo_user_assignments def create_vm folder = VSphere::Connection.instance.root_folder clusters = VSphere::Cluster.all - folder.create_vm(cpu_cores, ram_mb, storage_mb, name, clusters.first) if clusters.first + vm = clusters.first ? folder.create_vm(cpu_cores, ram_mb, storage_mb, name, clusters.first) : nil + vm.ensure_config.responsible_users = responsible_users if vm + vm end def push_to_git diff --git a/spec/api/v_sphere_api_spec.rb b/spec/api/v_sphere_api_spec.rb index 50d662da..a204f4f9 100644 --- a/spec/api/v_sphere_api_spec.rb +++ b/spec/api/v_sphere_api_spec.rb @@ -143,15 +143,15 @@ expect(vm.users).to match_array(request.users) end - it 'does not have responsible users if there is no fitting request' do + 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 - it 'has responsible users if a fitting request exists' do - request = FactoryBot.create :accepted_request - request.responsible_users << FactoryBot.create(:user) - vm = v_sphere_vm_mock request.name - expect(vm.responsible_users).to match_array(request.responsible_users) + it 'has responsible users if a fitting config exists' do + config = FactoryBot.create :virtual_machine_config + config.responsible_users << FactoryBot.create(:user) + vm = v_sphere_vm_mock config.name + expect(vm.responsible_users).to match_array(config.responsible_users) end it 'does not have an IP if the fitting config does not exist' do From 9f1288c70a63f0a372c0e951690934032ba88291 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 12:14:33 +0100 Subject: [PATCH 13/25] Add responsible to notificaiton --- app/models/request.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/request.rb b/app/models/request.rb index 1344debd..f9965e23 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -29,7 +29,7 @@ class Request < ApplicationRecord def description_text(host_name) description = "- VM Name: #{name}\n" - description += "- Responsible: TBD\n" + description += "- Responsible: #{responsible_users.first.name}\n" description += comment.empty? ? '' : "- Comment: #{comment}\n" description += url(host_name) + "\n" description From dcb34cbfba1cc8b6fd26dbaf471c663dea55210c Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 14:16:51 +0100 Subject: [PATCH 14/25] Work on moving into correct subfolder --- app/api/v_sphere/folder.rb | 12 ++++++++++-- app/api/v_sphere/virtual_machine.rb | 29 ++++++++++++++++++++++++++--- app/models/user.rb | 4 ++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/app/api/v_sphere/folder.rb b/app/api/v_sphere/folder.rb index e852a9f9..e8cfc851 100644 --- a/app/api/v_sphere/folder.rb +++ b/app/api/v_sphere/folder.rb @@ -48,8 +48,16 @@ def name # Ensure that a subfolder exists and return it # folder_name is a string with the name of the subfolder def ensure_subfolder(folder_name) - folder = subfolders.find { |each| each.name == folder_name } - folder || VSphere::Folder.new(@folder.CreateFolder(name: folder_name)) + subfolder = subfolders.find { |each| each.name == folder_name } + subfolder || VSphere::Folder.new(@folder.CreateFolder(name: folder_name)) + end + + # Ensure that the path relative to this folder is a valid folder and return it + # The path is an array of strings + def ensure_subfolder_by_path(path) + return self if path.empty? + + ensure_subfolder(path.first).ensure_subfolder_by_path(path[1..-1]) end def move_here(folder_entry) diff --git a/app/api/v_sphere/virtual_machine.rb b/app/api/v_sphere/virtual_machine.rb index 8612139f..f0c4dad7 100644 --- a/app/api/v_sphere/virtual_machine.rb +++ b/app/api/v_sphere/virtual_machine.rb @@ -188,13 +188,30 @@ def dns config&.dns || '' end - # Utilities + # Folder Utilities def move_into(folder) folder.move_here self end - def boot_time - @vm.runtime.bootTime + def parent + VSphere::Folder.new @vm.parent + end + + def target_subfolder + path = [] << case status + when :archived then archived_folder.name + when :pending_reviving then pending_archivation_folder.name + when :pending_archivation then pending_revivings_folder.name + else + 'Active VMs' + end + path << responsible_users.first.human_readable_identifier if responsible_users.first + VSphere::Connection.instance.root_folder.ensure_subfolder_by_path path + end + + def move_into_correct_subfolder + target = target_subfolder + move_into target unless target.vms(recursive: false).include? self end # Users @@ -202,6 +219,7 @@ def responsible_users config&.responsible_users || [] end + # this method should return all users, including the sudo users def users request&.users || [] end @@ -218,6 +236,11 @@ def belongs_to(user) users.include? user end + # Information about the vm + def boot_time + @vm.runtime.bootTime + end + def summary @vm.summary end diff --git a/app/models/user.rb b/app/models/user.rb index 3b33a53d..a11d8620 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -60,6 +60,10 @@ def name "#{first_name} #{last_name}" end + def human_readable_identifier + email.split(/@/).first + end + def valid_ssh_key errors.add(:danger, 'Invalid SSH-Key') unless valid_ssh_key? end From 4f4d5fa36072a56786990f387f4a33dbd4b9b9ed Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 15:20:57 +0100 Subject: [PATCH 15/25] VM now moves into the correct subfolder whenever its created, archived, revived or pending --- app/api/v_sphere/folder.rb | 4 +++- app/api/v_sphere/virtual_machine.rb | 28 ++++++++++++++++------------ spec/api/v_sphere_api_mocker.rb | 10 +++++++++- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/app/api/v_sphere/folder.rb b/app/api/v_sphere/folder.rb index e8cfc851..dccde504 100644 --- a/app/api/v_sphere/folder.rb +++ b/app/api/v_sphere/folder.rb @@ -68,7 +68,9 @@ def move_here(folder_entry) def create_vm(cpu, ram, capacity, name, cluster) vm_config = creation_config(cpu, ram, capacity, name) vm = @folder.CreateVM_Task(config: vm_config, pool: cluster.resource_pool).wait_for_completion - VSphere::VirtualMachine.new vm + vm = VSphere::VirtualMachine.new vm + vm.move_into_correct_subfolder + vm end private diff --git a/app/api/v_sphere/virtual_machine.rb b/app/api/v_sphere/virtual_machine.rb index f0c4dad7..ac74d8b9 100644 --- a/app/api/v_sphere/virtual_machine.rb +++ b/app/api/v_sphere/virtual_machine.rb @@ -134,6 +134,7 @@ def archived? def set_pending_archivation move_into pending_archivation_folder ArchivationRequest.new(name: name).save + move_into_correct_subfolder end def archivable? @@ -154,6 +155,7 @@ def set_archived move_into archived_folder archivation_request&.delete + move_into_correct_subfolder end # Reviving @@ -163,11 +165,13 @@ def pending_reviving? def set_pending_reviving move_into pending_revivings_folder + move_into_correct_subfolder end def set_revived move_into root_folder archivation_request&.delete + move_into_correct_subfolder end # Config methods @@ -197,18 +201,6 @@ def parent VSphere::Folder.new @vm.parent end - def target_subfolder - path = [] << case status - when :archived then archived_folder.name - when :pending_reviving then pending_archivation_folder.name - when :pending_archivation then pending_revivings_folder.name - else - 'Active VMs' - end - path << responsible_users.first.human_readable_identifier if responsible_users.first - VSphere::Connection.instance.root_folder.ensure_subfolder_by_path path - end - def move_into_correct_subfolder target = target_subfolder move_into target unless target.vms(recursive: false).include? self @@ -291,6 +283,18 @@ def macs private + def target_subfolder + path = [] << case status + when :archived then archived_folder.name + when :pending_reviving then pending_archivation_folder.name + when :pending_archivation then pending_revivings_folder.name + else + 'Active VMs' + end + path << responsible_users.first.human_readable_identifier if responsible_users.first + VSphere::Connection.instance.root_folder.ensure_subfolder_by_path path + end + def request Request.accepted.find { |each| name == each.name } end diff --git a/spec/api/v_sphere_api_mocker.rb b/spec/api/v_sphere_api_mocker.rb index 350c2288..1d26d306 100644 --- a/spec/api/v_sphere_api_mocker.rb +++ b/spec/api/v_sphere_api_mocker.rb @@ -43,6 +43,7 @@ def vim_folder_mock(name, subfolders, vms, clusters) # rubocop:disable Metrics/A allow(folder).to receive(:children).and_return(children) allow(folder).to receive(:is_a?).and_return false allow(folder).to receive(:is_a?).with(RbVmomi::VIM::Folder).and_return true + allow(folder).to receive_message_chain(:MoveIntoFolder_Task, :wait_for_completion) allow(folder).to receive(:CreateVM_Task) do |*args| task = double allow(task).to receive(:wait_for_completion) do @@ -52,6 +53,12 @@ def vim_folder_mock(name, subfolders, vms, clusters) # rubocop:disable Metrics/A end task end + + allow(folder).to receive(:CreateFolder) do |subfolder_name| + folder = vim_folder_mock(subfolder_name, [], [], []) + children << folder + folder + end folder end @@ -146,8 +153,9 @@ def v_sphere_connection_mock( 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 + active_vms_folder = v_sphere_folder_mock 'Active VMs', vms: normal_vms root_folder = v_sphere_folder_mock 'root', subfolders: [archived_vms_folder, pending_archivation_vms_folder, - pending_reviving_vms_folder], vms: normal_vms + pending_reviving_vms_folder, active_vms_folder] clusters_folder = v_sphere_folder_mock 'clusters', clusters: clusters double_connection = double allow(double_connection).to receive(:root_folder).and_return root_folder From 3e3cf594635a41836b57df6a6b641179195f2e74 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 15:23:41 +0100 Subject: [PATCH 16/25] Move responsibilty for VM creation from folder to request --- app/api/v_sphere/folder.rb | 4 +--- app/models/request.rb | 5 ++++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/api/v_sphere/folder.rb b/app/api/v_sphere/folder.rb index dccde504..e8cfc851 100644 --- a/app/api/v_sphere/folder.rb +++ b/app/api/v_sphere/folder.rb @@ -68,9 +68,7 @@ def move_here(folder_entry) def create_vm(cpu, ram, capacity, name, cluster) vm_config = creation_config(cpu, ram, capacity, name) vm = @folder.CreateVM_Task(config: vm_config, pool: cluster.resource_pool).wait_for_completion - vm = VSphere::VirtualMachine.new vm - vm.move_into_correct_subfolder - vm + VSphere::VirtualMachine.new vm end private diff --git a/app/models/request.rb b/app/models/request.rb index f9965e23..aa41848f 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -71,7 +71,10 @@ def create_vm folder = VSphere::Connection.instance.root_folder clusters = VSphere::Cluster.all vm = clusters.first ? folder.create_vm(cpu_cores, ram_mb, storage_mb, name, clusters.first) : nil - vm.ensure_config.responsible_users = responsible_users if vm + if vm + vm.ensure_config.responsible_users = responsible_users + vm.move_into_correct_subfolder + end vm end From b51c834f72946f31e254ad9a83071102d1f2e77f Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 15:26:53 +0100 Subject: [PATCH 17/25] Whoops --- app/api/v_sphere/virtual_machine.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/api/v_sphere/virtual_machine.rb b/app/api/v_sphere/virtual_machine.rb index ac74d8b9..8e03846e 100644 --- a/app/api/v_sphere/virtual_machine.rb +++ b/app/api/v_sphere/virtual_machine.rb @@ -286,8 +286,8 @@ def macs def target_subfolder path = [] << case status when :archived then archived_folder.name - when :pending_reviving then pending_archivation_folder.name - when :pending_archivation then pending_revivings_folder.name + when :pending_reviving then pending_revivings_folder.name + when :pending_archivation then pending_archivation_folder.name else 'Active VMs' end From 59228fb65a6cfaa61c541c94e57099baebebd331 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 15:43:11 +0100 Subject: [PATCH 18/25] Add test for move_into_correct_subfolder --- spec/api/v_sphere_api_spec.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/spec/api/v_sphere_api_spec.rb b/spec/api/v_sphere_api_spec.rb index a204f4f9..855fa325 100644 --- a/spec/api/v_sphere_api_spec.rb +++ b/spec/api/v_sphere_api_spec.rb @@ -47,8 +47,12 @@ end end + let(:user_folder_mock) { vim_folder_mock('user', [], [], []) } + + let(:active_vms_folder) { v_sphere_folder_mock('Active VMs', subfolders: [user_folder_mock]) } + let(:mock_folder) do - vim_folder_mock('vm', [mock_archived_vms_folder, mock_pending_archivings_folder, mock_pending_revivings_folder], mock_root_folder_vms, []) + vim_folder_mock('vm', [mock_archived_vms_folder, mock_pending_archivings_folder, mock_pending_revivings_folder, active_vms_folder], mock_root_folder_vms, []) end let(:root_folder) { VSphere::Folder.new(mock_folder) } @@ -132,6 +136,14 @@ expect(VSphere::VirtualMachine.find_by_name('Archived VM2')).not_to be_nil end + it 'can move into the responsible users subfolder' do + config = FactoryBot.create :virtual_machine_config + config.responsible_users = [FactoryBot.create(:user, email: 'user@user.de')] + config.save! + v_sphere_vm_mock(config.name).move_into_correct_subfolder + expect(user_folder_mock).to have_received(:MoveIntoFolder_Task) + end + it 'does not have users if there is no fitting request' do expect(VSphere::VirtualMachine.find_by_name('Archived VM2').users).to be_empty end From 11deaa079e43b4e1a92c4153d9e69b3a18d61c20 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 15:47:41 +0100 Subject: [PATCH 19/25] Migrate --- db/schema.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index 6b3365f5..9ec66a2c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -52,8 +52,8 @@ create_table "requests", force: :cascade do |t| t.string "name" t.integer "cpu_cores" - t.integer "ram_mb" - t.integer "storage_mb" + t.integer "ram_gb" + t.integer "storage_gb" t.string "operating_system" t.text "comment" t.text "rejection_information" @@ -129,12 +129,12 @@ t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.integer "role" - t.string "first_name" - t.string "last_name" t.string "provider" t.string "uid" - t.integer "user_id" t.string "ssh_key" + t.string "first_name" + t.string "last_name" + t.integer "user_id" t.boolean "email_notifications", default: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true From 075fad3fddf9157c6dabb448dd40b7ac673c0fa2 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 15:55:42 +0100 Subject: [PATCH 20/25] Fix end to end testing --- app/models/request.rb | 19 ++++++++++--------- spec/controllers/vms_controller_spec.rb | 2 +- spec/end_to_end/end_to_end_spec.rb | 2 ++ 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/app/models/request.rb b/app/models/request.rb index 784152d1..622c8876 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -69,16 +69,17 @@ def non_sudo_user_assignments end def create_vm - folder = VSphere::Connection.instance.root_folder clusters = VSphere::Cluster.all - if clusters.first - vm = folder.create_vm(cpu_cores, gibi_to_mibi(ram_gb), gibi_to_kibi(storage_gb), name, clusters.first) - vm.ensure_config.responsible_users = responsible_users - vm.move_into_correct_subfolder - vm - else - nil - end + return nil unless clusters.first + + create_vm_in_cluster(clusters.first) + end + + def create_vm_in_cluster(cluster) + vm = VSphere::Connection.instance.root_folder.create_vm(cpu_cores, gibi_to_mibi(ram_gb), gibi_to_kibi(storage_gb), name, cluster) + vm.ensure_config.responsible_users = responsible_users + vm.move_into_correct_subfolder + vm end def push_to_git diff --git a/spec/controllers/vms_controller_spec.rb b/spec/controllers/vms_controller_spec.rb index a70fe96f..0c12b061 100644 --- a/spec/controllers/vms_controller_spec.rb +++ b/spec/controllers/vms_controller_spec.rb @@ -9,7 +9,7 @@ let(:admin) { FactoryBot.create :admin } let(:vm1) do - vm1 = v_sphere_vm_mock 'My insanely cool vm', power_state: 'poweredOn', boot_time: 'Thursday', vm_ware_tools: 'toolsInstalled' + v_sphere_vm_mock 'My insanely cool vm', power_state: 'poweredOn', boot_time: 'Thursday', vm_ware_tools: 'toolsInstalled' end let(:vm2) do diff --git a/spec/end_to_end/end_to_end_spec.rb b/spec/end_to_end/end_to_end_spec.rb index 86b9bff0..c688cca1 100644 --- a/spec/end_to_end/end_to_end_spec.rb +++ b/spec/end_to_end/end_to_end_spec.rb @@ -27,6 +27,7 @@ fill_in('cpu', :with => 4) fill_in('ram', :with => 8) fill_in('storage', :with => 126) + select(@employee.email, from: 'request_responsible_user_ids') select(@employee.email, :from => 'request_sudo_user_ids') select(@user.email, :from => 'request_user_ids') select('none', :from => 'operating_system') @@ -46,6 +47,7 @@ fill_in('cpu', :with => 4) fill_in('ram', :with => 8) fill_in('storage', :with => 126) + select(@admin.email, from: 'request_responsible_user_ids') select(@admin.email, :from => 'request_sudo_user_ids') select(@user.email, :from => 'request_user_ids') select('none', :from => 'operating_system') From 6caf79dcb0106d7169ee993406b5ef6a378c311e Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 15:59:04 +0100 Subject: [PATCH 21/25] Fix some CodeFactor issues --- spec/end_to_end/end_to_end_spec.rb | 184 ++++++++++----------- spec/models/virtual_machine_config_spec.rb | 1 - 2 files changed, 91 insertions(+), 94 deletions(-) diff --git a/spec/end_to_end/end_to_end_spec.rb b/spec/end_to_end/end_to_end_spec.rb index c688cca1..acd8989e 100644 --- a/spec/end_to_end/end_to_end_spec.rb +++ b/spec/end_to_end/end_to_end_spec.rb @@ -1,102 +1,100 @@ +# frozen_string_literal: true require 'rails_helper' -RSpec.describe "End to End testing", type: :feature do +RSpec.describe 'End to End testing', type: :feature do + before do + @user = FactoryBot.create :user + @employee = FactoryBot.create :employee + @admin = FactoryBot.create :admin + @requestname = 'capybara-test-vm' + end - before :each do - @user = FactoryBot.create :user - @employee = FactoryBot.create :employee - @admin = FactoryBot.create :admin - @requestname = 'capybara-test-vm' - end + describe 'GET "/" - Landing Page' + it 'has a o-auth log-in button' do + visit '/' + expect(page).to have_current_path('/users/sign_in') + expect(page).to have_link('Sign in with HPI OpenID Connect') + end - describe 'GET "/" - Landing Page' - it 'has a o-auth log-in button' do - visit '/' - expect(current_path).to eq('/users/sign_in') - expect(page).to have_link('Sign in with HPI OpenID Connect') - end + describe 'Request Process' + it 'is possible to request a new VM' do + sign_in @employee + visit '/vms' + click_on 'New Request' + expect(page).to have_current_path('/vms/requests/new') + fill_in('VM Name', with: @requestname) + fill_in('cpu', with: 4) + fill_in('ram', with: 8) + fill_in('storage', with: 126) + select(@employee.email, from: 'request_responsible_user_ids') + select(@employee.email, from: 'request_sudo_user_ids') + select(@user.email, from: 'request_user_ids') + select('none', from: 'operating_system') + fill_in('Description', with: 'test') + click_on 'Create Request' + expect(page).to have_text('Request was successfully created.') + expect(page).to have_current_path('/vms/requests') + click_on @requestname + expect(page.body).to have_text('Request Information') + end - describe 'Request Process' - it 'is possible to request a new VM' do - sign_in @employee - visit '/vms' - click_on 'New Request' - expect(current_path).to eq('/vms/requests/new') - fill_in('VM Name', :with => @requestname) - fill_in('cpu', :with => 4) - fill_in('ram', :with => 8) - fill_in('storage', :with => 126) - select(@employee.email, from: 'request_responsible_user_ids') - select(@employee.email, :from => 'request_sudo_user_ids') - select(@user.email, :from => 'request_user_ids') - select('none', :from => 'operating_system') - fill_in('Description', :with => 'test') - click_on 'Create Request' - expect(page).to have_text('Request was successfully created.') - expect(current_path).to eq('/vms/requests') - click_on @requestname - expect(page.body).to have_text('Request Information') - end + it 'is possible to accept a VM request' do + sign_in @admin + visit '/vms' + click_on 'New Request' + fill_in('VM Name', with: @requestname) + fill_in('cpu', with: 4) + fill_in('ram', with: 8) + fill_in('storage', with: 126) + select(@admin.email, from: 'request_responsible_user_ids') + select(@admin.email, from: 'request_sudo_user_ids') + select(@user.email, from: 'request_user_ids') + select('none', from: 'operating_system') + fill_in('Description', with: 'test') + click_on 'Create Request' + click_on @requestname + expect(page).to have_button('acceptButton') + expect(page).to have_button('rejectButton') - it 'is possible to accept a VM request' do - sign_in @admin - visit '/vms' - click_on 'New Request' - fill_in('VM Name', :with => @requestname) - fill_in('cpu', :with => 4) - fill_in('ram', :with => 8) - fill_in('storage', :with => 126) - select(@admin.email, from: 'request_responsible_user_ids') - select(@admin.email, :from => 'request_sudo_user_ids') - select(@user.email, :from => 'request_user_ids') - select('none', :from => 'operating_system') - fill_in('Description', :with => 'test') - click_on 'Create Request' - click_on @requestname - expect(page).to have_button('acceptButton') - expect(page).to have_button('rejectButton') - - - # click_on 'acceptButton' - # expect(page).to have_text('Request was successfully updated and the vm was successfully created.') - # expect(page).to have_text('Editing configuration of VM') - # fill_in('virtual_machine_config_ip', :with => '123.0.0.23') - # fill_in('virtual_machine_config_dns', :with => 'www.example.com') - # click_on 'Update Configuration' - # expect(page).to have_text('Successfully updated configuration') - # expect(current_path).to eq('/vms/requests') - # click_on @requestname - # expect(page).to have_text('accepted') - # visit "/vms/#{@requestname}" - # expect(page).to have_text('offline') + # click_on 'acceptButton' + # expect(page).to have_text('Request was successfully updated and the vm was successfully created.') + # expect(page).to have_text('Editing configuration of VM') + # fill_in('virtual_machine_config_ip', :with => '123.0.0.23') + # fill_in('virtual_machine_config_dns', :with => 'www.example.com') + # click_on 'Update Configuration' + # expect(page).to have_text('Successfully updated configuration') + # expect(current_path).to eq('/vms/requests') + # click_on @requestname + # expect(page).to have_text('accepted') + # visit "/vms/#{@requestname}" + # expect(page).to have_text('offline') + end - end - - it 'is possible to turn on a VM' do - skip - sign_in @admin - visit '/vms' - click_on 'New Request' - fill_in('VM Name', :with => @requestname) - fill_in('cpu', :with => 4) - fill_in('ram', :with => 8) - fill_in('storage', :with => 126) - select(@admin.email, :from => 'request_sudo_user_ids') - select(@user.email, :from => 'request_user_ids') - select('none', :from => 'operating_system') - fill_in('Description', :with => 'test') - click_on 'Create Request' - click_on @requestname - click_on 'acceptButton' - page.reload - fill_in('virtual_machine_config_ip', :with => '123.0.0.23') - fill_in('virtual_machine_config_dns', :with => 'www.example.com') - click_on 'Update Configuration' - click_on @requestname - visit "/vms/#{@requestname}" - expect(page).to have_text('offline') - select('Power On', :from => 'navbarDropdown') - expect(page).to have_text('online') - end + it 'is possible to turn on a VM' do + skip + sign_in @admin + visit '/vms' + click_on 'New Request' + fill_in('VM Name', with: @requestname) + fill_in('cpu', with: 4) + fill_in('ram', with: 8) + fill_in('storage', with: 126) + select(@admin.email, from: 'request_sudo_user_ids') + select(@user.email, from: 'request_user_ids') + select('none', from: 'operating_system') + fill_in('Description', with: 'test') + click_on 'Create Request' + click_on @requestname + click_on 'acceptButton' + page.reload + fill_in('virtual_machine_config_ip', with: '123.0.0.23') + fill_in('virtual_machine_config_dns', with: 'www.example.com') + click_on 'Update Configuration' + click_on @requestname + visit "/vms/#{@requestname}" + expect(page).to have_text('offline') + select('Power On', from: 'navbarDropdown') + expect(page).to have_text('online') + end end diff --git a/spec/models/virtual_machine_config_spec.rb b/spec/models/virtual_machine_config_spec.rb index 23a3aeab..f75e1faf 100644 --- a/spec/models/virtual_machine_config_spec.rb +++ b/spec/models/virtual_machine_config_spec.rb @@ -3,7 +3,6 @@ require 'rails_helper' RSpec.describe VirtualMachineConfig, type: :model do - let(:config) { FactoryBot.create(:virtual_machine_config) } it 'can save responsible users' do From 71c8bd06a5cf0afceff814c81895fac628df25be Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 16:05:10 +0100 Subject: [PATCH 22/25] Remove unneccessary method --- app/api/v_sphere/virtual_machine.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/api/v_sphere/virtual_machine.rb b/app/api/v_sphere/virtual_machine.rb index 699a36d2..3b49be7c 100644 --- a/app/api/v_sphere/virtual_machine.rb +++ b/app/api/v_sphere/virtual_machine.rb @@ -197,10 +197,6 @@ def move_into(folder) folder.move_here self end - def parent - VSphere::Folder.new @vm.parent - end - def move_into_correct_subfolder target = target_subfolder move_into target unless target.vms(recursive: false).include? self From 45fd8aee2c33b6640876e3f568565bcc3c37ae68 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 16:16:02 +0100 Subject: [PATCH 23/25] Comment routes to be clearer why the change is neccessary --- config/routes.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/routes.rb b/config/routes.rb index 5abd037c..9ce9395a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -23,6 +23,7 @@ get '/vms/configs/:id' => 'vms#edit_config', constraints: { id: /.*/ }, as: :edit_config patch '/vms/configs/:id' => 'vms#update_config', constraints: { id: /.*/ }, as: :update_config + # move all vm actions under /vms/vm because a VM named requests might otherwise lead to issues! post '/vms/vm/:id/change_power_state' => 'vms#change_power_state', constraints: { id: /.*/ } post '/vms/vm/:id/suspend_vm' => 'vms#suspend_vm', constraints: { id: /.*/ } post '/vms/vm/:id/shutdown_guest_os' => 'vms#shutdown_guest_os', constraints: { id: /.*/ } From 5825d42a69196eb4ecde41db2b7299c625456b58 Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 16:19:14 +0100 Subject: [PATCH 24/25] Make CodeFactor a bit happy --- spec/api/v_sphere_api_spec.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spec/api/v_sphere_api_spec.rb b/spec/api/v_sphere_api_spec.rb index 855fa325..bdadd478 100644 --- a/spec/api/v_sphere_api_spec.rb +++ b/spec/api/v_sphere_api_spec.rb @@ -52,7 +52,10 @@ let(:active_vms_folder) { v_sphere_folder_mock('Active VMs', subfolders: [user_folder_mock]) } let(:mock_folder) do - vim_folder_mock('vm', [mock_archived_vms_folder, mock_pending_archivings_folder, mock_pending_revivings_folder, active_vms_folder], mock_root_folder_vms, []) + vim_folder_mock('vm', + [mock_archived_vms_folder, mock_pending_archivings_folder, mock_pending_revivings_folder, active_vms_folder], + mock_root_folder_vms, + []) end let(:root_folder) { VSphere::Folder.new(mock_folder) } From a0272abdf6010989ecb2c64c5d75a376fbe018bc Mon Sep 17 00:00:00 2001 From: Leon Matthes Date: Fri, 1 Feb 2019 16:28:53 +0100 Subject: [PATCH 25/25] Fix in skipped test --- spec/end_to_end/end_to_end_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/end_to_end/end_to_end_spec.rb b/spec/end_to_end/end_to_end_spec.rb index acd8989e..07b0a5a5 100644 --- a/spec/end_to_end/end_to_end_spec.rb +++ b/spec/end_to_end/end_to_end_spec.rb @@ -80,6 +80,7 @@ fill_in('cpu', with: 4) fill_in('ram', with: 8) fill_in('storage', with: 126) + select(@admin.email, from: 'request_responsible_user_ids') select(@admin.email, from: 'request_sudo_user_ids') select(@user.email, from: 'request_user_ids') select('none', from: 'operating_system')