From 39ace926e0decf5a53ec2dbd654612e71bc22922 Mon Sep 17 00:00:00 2001 From: Aday Bujeda Date: Thu, 17 Aug 2023 11:25:02 +0100 Subject: [PATCH 1/3] Added implementation for overriding project location path --- .../app/controllers/projects_controller.rb | 8 +++--- apps/dashboard/app/models/project.rb | 3 ++- .../app/views/projects/_form.html.erb | 4 +++ apps/dashboard/config/locales/en.yml | 1 + apps/dashboard/test/models/projects_test.rb | 22 ++++++++++++++++ apps/dashboard/test/system/jobs_app_test.rb | 25 ++++++++++++++++--- 6 files changed, 54 insertions(+), 9 deletions(-) diff --git a/apps/dashboard/app/controllers/projects_controller.rb b/apps/dashboard/app/controllers/projects_controller.rb index 3818a901b4..ea81e36459 100644 --- a/apps/dashboard/app/controllers/projects_controller.rb +++ b/apps/dashboard/app/controllers/projects_controller.rb @@ -25,7 +25,7 @@ def new if name_or_icon_nil? @project = Project.new else - returned_params = { name: new_project_params[:name], icon: new_project_params[:icon] } + returned_params = { name: new_project_params[:name], icon: new_project_params[:icon], directory: new_project_params[:directory] } @project = Project.new(returned_params) end end @@ -65,7 +65,7 @@ def create else # TODO: loop through all errors and show them instead of this flash[:alert] = @project.errors[:name].last || @project.errors[:icon].last - redirect_to new_project_path(name: project_params[:name], icon: project_params[:icon]) + redirect_to new_project_path(name: project_params[:name], directory: project_params[:directory], icon: project_params[:icon]) end end @@ -102,7 +102,7 @@ def name_or_icon_nil? def project_params params .require(:project) - .permit(:name, :description, :icon, :id) + .permit(:name, :directory, :description, :icon, :id) end def show_project_params @@ -110,6 +110,6 @@ def show_project_params end def new_project_params - params.permit(:template, :icon, :name) + params.permit(:template, :icon, :name, :directory) end end diff --git a/apps/dashboard/app/models/project.rb b/apps/dashboard/app/models/project.rb index 924c08ec97..cecec9dd11 100644 --- a/apps/dashboard/app/models/project.rb +++ b/apps/dashboard/app/models/project.rb @@ -78,7 +78,8 @@ def initialize(attributes = {}) @manifest = Manifest.new({}) else @id = attributes.delete(:id) - @directory = attributes.delete(:directory) || Project.dataroot.join(id.to_s) + @directory = attributes.delete(:directory) + @directory = Project.dataroot.join(id.to_s).to_s if @directory.blank? @manifest = Manifest.new(attributes).merge(Manifest.load(manifest_path)) end diff --git a/apps/dashboard/app/views/projects/_form.html.erb b/apps/dashboard/app/views/projects/_form.html.erb index 062b9bee46..446e79f841 100644 --- a/apps/dashboard/app/views/projects/_form.html.erb +++ b/apps/dashboard/app/views/projects/_form.html.erb @@ -10,6 +10,10 @@ <%= form.text_field :name, placeholder: "Project name", help: I18n.t('dashboard.jobs_project_name_validation') %> +
+ <%= form.text_field :directory, readonly: action_name == "edit", + help: I18n.t('dashboard.jobs_project_directory_help') %> +
<%= form.text_area :description, placeholder: "Project description" %>
diff --git a/apps/dashboard/config/locales/en.yml b/apps/dashboard/config/locales/en.yml index be004ff648..8e0594b125 100644 --- a/apps/dashboard/config/locales/en.yml +++ b/apps/dashboard/config/locales/en.yml @@ -211,6 +211,7 @@ en: jobs_project_delete_project_confirmation: "Delete all contents of project directory?" jobs_project_manifest_updated: "Project manifest updated!" jobs_project_name_validation: "Project name may only contain letters, digits, dashes, underscores, and spaces" + jobs_project_directory_help: "Override the default location for this project" jobs_create_blank_project: "Create a new project" jobs_create_template_project: "Create a new project from a template" diff --git a/apps/dashboard/test/models/projects_test.rb b/apps/dashboard/test/models/projects_test.rb index 98adcf9a65..92f37da644 100644 --- a/apps/dashboard/test/models/projects_test.rb +++ b/apps/dashboard/test/models/projects_test.rb @@ -51,6 +51,28 @@ class ProjectsTest < ActiveSupport::TestCase end end + test 'creates project with directory override' do + Dir.mktmpdir do |project_path| + OodAppkit.stubs(:dataroot).returns(Pathname.new('/tmp')) + attrs = { name: 'directory override', directory: project_path, icon: 'fas://arrow-right', id: 1 } + project = Project.new(attrs) + + assert_equal "#{project_path}", project.directory + assert project.save(attrs), project.errors.inspect + assert File.directory?(Pathname.new(project_path)) + assert File.file?(Pathname.new("#{project_path}/.ondemand/manifest.yml")) + end + end + + test 'creates with empty directory defaults to projects dataroot' do + project_path = Pathname.new('/tmp') + OodAppkit.stubs(:dataroot).returns(project_path) + attrs = { name: 'test-project', directory: ' ', id: 1 } + + project = Project.new(attrs) + assert_equal "#{project_path}/projects/1", project.directory + end + test 'deletes project' do Dir.mktmpdir do |tmp| projects_path = Pathname.new(tmp) diff --git a/apps/dashboard/test/system/jobs_app_test.rb b/apps/dashboard/test/system/jobs_app_test.rb index 5f5b8962e6..963ed98cee 100644 --- a/apps/dashboard/test/system/jobs_app_test.rb +++ b/apps/dashboard/test/system/jobs_app_test.rb @@ -11,18 +11,23 @@ def setup stub_scontrol end - def setup_project(dir) - OodAppkit.stubs(:dataroot).returns(Pathname.new(dir)) + def setup_project(dir, override_location = false) + data_root = override_location ? '/tmp' : dir + OodAppkit.stubs(:dataroot).returns(Pathname.new(data_root)) proj = 'test-project' + desc = 'test-description' icon = 'fas://arrow-right' visit projects_path click_on I18n.t('dashboard.jobs_create_blank_project') find('#project_name').set(proj) + find('#project_directory').set(dir) if override_location + find('#project_description').set(desc) find('#product_icon_select').set(icon) click_on 'Save' - `echo 'some_other_command' > #{dir}/projects/1/my_cool_script.sh` - `echo 'hostname' > #{dir}/projects/1/my_cooler_script.bash` + project_dir = override_location ? dir : "#{dir}/projects/1" + `echo 'some_other_command' > #{project_dir}/my_cool_script.sh` + `echo 'hostname' > #{project_dir}/my_cooler_script.bash` end def setup_script(project_id) @@ -73,6 +78,14 @@ def add_bc_num_hours(project_id) end end + test 'creates project overriding default project location' do + Dir.mktmpdir do |dir| + setup_project(dir, true) + + assert File.directory? File.join("#{dir}", '.ondemand') + end + end + test 'delete a project from the fs and ensure no table entry' do Dir.mktmpdir do |dir| setup_project(dir) @@ -107,6 +120,10 @@ def add_bc_num_hours(project_id) assert_selector '[href="/projects/1"]', text: 'My Test Project' click_on 'Edit' assert_selector 'h1', text: 'Editing: My Test Project' + assert_equal 'my-test-project', find('#project_name').value + assert_equal "#{dir}/projects/1", find('#project_directory').value + assert_equal 'test-description', find('#project_description').value + assert_equal 'fas://arrow-right', find('#product_icon_select').value assert_selector '.btn.btn-default', text: 'Back' end end From ecca8d83f176eaa572a9c57bb71dd33fb5bf8b3b Mon Sep 17 00:00:00 2001 From: Aday Bujeda Date: Thu, 17 Aug 2023 16:41:23 +0100 Subject: [PATCH 2/3] Updated OodAppkit.dataroot stub when overriding projects directory --- apps/dashboard/test/system/jobs_app_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/dashboard/test/system/jobs_app_test.rb b/apps/dashboard/test/system/jobs_app_test.rb index 963ed98cee..0044e5e968 100644 --- a/apps/dashboard/test/system/jobs_app_test.rb +++ b/apps/dashboard/test/system/jobs_app_test.rb @@ -12,8 +12,8 @@ def setup end def setup_project(dir, override_location = false) - data_root = override_location ? '/tmp' : dir - OodAppkit.stubs(:dataroot).returns(Pathname.new(data_root)) + OodAppkit.stubs(:dataroot).returns(Pathname.new(dir)) unless override_location + proj = 'test-project' desc = 'test-description' icon = 'fas://arrow-right' From 391750326fa2c70dabbe3ae59f900d92ab97eb41 Mon Sep 17 00:00:00 2001 From: Aday Bujeda Date: Thu, 17 Aug 2023 17:16:42 +0100 Subject: [PATCH 3/3] Fixing project_lookup side effects --- apps/dashboard/test/system/jobs_app_test.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/apps/dashboard/test/system/jobs_app_test.rb b/apps/dashboard/test/system/jobs_app_test.rb index 0044e5e968..cfc85f185c 100644 --- a/apps/dashboard/test/system/jobs_app_test.rb +++ b/apps/dashboard/test/system/jobs_app_test.rb @@ -83,6 +83,11 @@ def add_bc_num_hours(project_id) setup_project(dir, true) assert File.directory? File.join("#{dir}", '.ondemand') + + #Cleanup to avoid side effects + accept_confirm do + click_on 'Delete' + end end end