Skip to content

Commit b1f980f

Browse files
authored
Merge pull request #20 from NYU-ITS/auto_slurm_account_name_assignment
Auto slurm account name assignment
2 parents 97bf683 + aa250cb commit b1f980f

File tree

4 files changed

+247
-161
lines changed

4 files changed

+247
-161
lines changed

coldfront/core/allocation/test_views.py

+62-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import logging
22

3+
from coldfront.core.allocation.views import GENERAL_RESOURCE_NAME
4+
from coldfront.core.resource.models import Resource, ResourceType
35
from coldfront.core.project.models import Project
46
from coldfront.core.school.models import School
57
from coldfront.core.user.models import UserProfile, ApproverProfile
@@ -24,7 +26,13 @@
2426
)
2527
from coldfront.core.allocation.models import (
2628
AllocationChangeRequest,
27-
AllocationChangeStatusChoice, Allocation, AllocationStatusChoice, AllocationAttributeChangeRequest,
29+
AllocationChangeStatusChoice,
30+
AllocationStatusChoice,
31+
AllocationAttributeChangeRequest,
32+
Allocation,
33+
AllocationAttribute,
34+
AllocationAttributeType,
35+
AttributeType,
2836
)
2937

3038
logging.disable(logging.CRITICAL)
@@ -415,6 +423,59 @@ def setUp(self):
415423
'resource': f'{self.allocation.resources.first().pk}',
416424
}
417425

426+
# Create resources
427+
self.resource_standard = Resource.objects.create(name="Tandon",
428+
resource_type=ResourceType.objects.create(name="Generic"))
429+
self.resource_hpc = Resource.objects.create(name=GENERAL_RESOURCE_NAME,
430+
resource_type=ResourceType.objects.create(name="Cluster"))
431+
432+
# Create AllocationAttributeType for slurm_account_name
433+
self.slurm_account_attr_type, _ = AllocationAttributeType.objects.get_or_create(
434+
name='slurm_account_name', attribute_type=AttributeType.objects.create(name='Text'),
435+
has_usage=False, is_private=False)
436+
437+
def test_allocationcreateview_post_standard_resource(self):
438+
"""Test POST to the AllocationCreateView with a standard resource and check slurm_account_name"""
439+
self.post_data['resource'] = str(self.resource_standard.pk)
440+
initial_count = self.project.allocation_set.count()
441+
442+
response = self.client.post(self.url, data=self.post_data, follow=True)
443+
444+
self.assertEqual(response.status_code, 200)
445+
self.assertContains(response, "Allocation requested.")
446+
self.assertEqual(self.project.allocation_set.count(), initial_count + 1)
447+
448+
# Verify AllocationAttribute for slurm_account_name
449+
allocation = self.project.allocation_set.latest('id')
450+
expected_slurm_name = f"pr_{self.project.pk}_{self.resource_standard.name}"
451+
452+
slurm_attr = AllocationAttribute.objects.get(
453+
allocation_attribute_type=self.slurm_account_attr_type,
454+
allocation=allocation
455+
)
456+
self.assertEqual(slurm_attr.value, expected_slurm_name)
457+
458+
def test_allocationcreateview_post_hpc_resource(self):
459+
"""Test POST to the AllocationCreateView with University HPC resource and check slurm_account_name"""
460+
self.post_data['resource'] = str(self.resource_hpc.pk)
461+
initial_count = self.project.allocation_set.count()
462+
463+
response = self.client.post(self.url, data=self.post_data, follow=True)
464+
465+
self.assertEqual(response.status_code, 200)
466+
self.assertContains(response, "Allocation requested.")
467+
self.assertEqual(self.project.allocation_set.count(), initial_count + 1)
468+
469+
# Verify AllocationAttribute for slurm_account_name
470+
allocation = self.project.allocation_set.latest('id')
471+
expected_slurm_name = f"pr_{self.project.pk}_general"
472+
473+
slurm_attr = AllocationAttribute.objects.get(
474+
allocation_attribute_type=self.slurm_account_attr_type,
475+
allocation=allocation
476+
)
477+
self.assertEqual(slurm_attr.value, expected_slurm_name)
478+
418479
def test_allocationcreateview_access(self):
419480
"""Test access to the AllocationCreateView"""
420481
self.allocation_access_tstbase(self.url)

coldfront/core/allocation/views.py

+13
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
ALLOCATION_ACCOUNT_MAPPING = import_from_settings(
8181
'ALLOCATION_ACCOUNT_MAPPING', {})
8282

83+
GENERAL_RESOURCE_NAME = "University HPC"
8384

8485
logger = logging.getLogger(__name__)
8586

@@ -529,6 +530,18 @@ def form_valid(self, form):
529530

530531
allocation_obj.resources.add(resource_obj)
531532

533+
# Automatically create slurm_account_name AllocationAttribute
534+
allocation_attribute_type_obj = AllocationAttributeType.objects.get(name='slurm_account_name')
535+
if resource_obj.name == GENERAL_RESOURCE_NAME: # "University HPC"
536+
slurm_account_name = f"pr_{allocation_obj.project.pk}_general"
537+
else:
538+
slurm_account_name = f"pr_{allocation_obj.project.pk}_{resource_obj.name}"
539+
540+
AllocationAttribute.objects.get_or_create(
541+
allocation_attribute_type=allocation_attribute_type_obj,
542+
allocation=allocation_obj,
543+
value=slurm_account_name)
544+
532545
if ALLOCATION_ACCOUNT_ENABLED and allocation_account and resource_obj.name in ALLOCATION_ACCOUNT_MAPPING:
533546

534547
allocation_attribute_type_obj = AllocationAttributeType.objects.get(

coldfront/core/school/management/commands/data/school_data.csv

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
18 Tisch School of the Arts
1919
19 NYU Abu Dhabi
2020
20 NYU Shanghai
21-
21 NYU IT
21+
21 NYU IT
22+
22 Center for Data Science

0 commit comments

Comments
 (0)