def test_validate_singularity_container_fail(self): """ A non-Singularity container should raise an error. :return: """ with self.assertRaisesMessage( ValidationError, Container. DEFAULT_ERROR_MESSAGES["invalid_singularity_container"]): Container.validate_singularity_container(self.useless_file)
def test_validate_singularity_container_fail(self): """ A non-Singularity container should raise an error. :return: """ with self.assertRaisesMessage( ValidationError, Container.DEFAULT_ERROR_MESSAGES["invalid_singularity_container"] ): Container.validate_singularity_container(self.useless_file)
def clean(self): """ Perform Singularity container file validation (it's more efficient to do here than at the model level). Fill in the values for file and file_type. :return: """ self.cleaned_data = super(ContainerForm, self).clean() # Check the file extension of the file. the_file = self.cleaned_data.get("file") if the_file is None: raise ValidationError( Container.DEFAULT_ERROR_MESSAGES["invalid_archive"], code="invalid_archive", ) upload_name = getattr(the_file, 'name', 'container.simg') upload_base, upload_ext = os.path.splitext(upload_name) upload_lower = upload_name.lower() file_type = None for ext in Container.ACCEPTED_FILE_EXTENSIONS: if upload_lower.endswith(ext): file_type = Container.ACCEPTED_FILE_EXTENSIONS[ext] break if file_type is None: raise ValidationError( Container.DEFAULT_ERROR_MESSAGES["bad_extension"], code="bad_extension") if file_type == Container.SIMG: # We need to get a file object to validate. We might have a path or we might # have to read the data out of memory. if hasattr(the_file, 'temporary_file_path'): Container.validate_singularity_container( the_file.temporary_file_path()) else: with NamedTemporaryFile(prefix=upload_base, suffix=upload_ext) as f_temp: if hasattr(the_file, 'read'): f_temp.write(the_file.read()) else: f_temp.write(the_file['content']) f_temp.flush() Container.validate_singularity_container(f_temp.name) if hasattr(the_file, 'seek') and callable(the_file.seek): the_file.seek(0) # Annotate self.instance with a marker that we already validated the container. self.instance.singularity_validated = True # Having figured out the file type, add it to self.instance manually. self.instance.file_type = file_type return self.cleaned_data
def clean(self): """ Perform Singularity container file validation (it's more efficient to do here than at the model level). Fill in the values for file and file_type. :return: """ self.cleaned_data = super(ContainerForm, self).clean() # Check the file extension of the file. the_file = self.cleaned_data["file"] upload_name = getattr(the_file, 'name', 'container.simg') upload_base, upload_ext = os.path.splitext(upload_name) upload_lower = upload_name.lower() file_type = None for ext in Container.ACCEPTED_FILE_EXTENSIONS: if upload_lower.endswith(ext): file_type = Container.ACCEPTED_FILE_EXTENSIONS[ext] break if file_type is None: raise ValidationError( Container.DEFAULT_ERROR_MESSAGES["bad_extension"], code="bad_extension" ) if file_type == Container.SIMG: # We need to get a file object to validate. We might have a path or we might # have to read the data out of memory. if hasattr(the_file, 'temporary_file_path'): Container.validate_singularity_container(the_file.temporary_file_path()) else: with NamedTemporaryFile(prefix=upload_base, suffix=upload_ext) as f_temp: if hasattr(the_file, 'read'): f_temp.write(the_file.read()) else: f_temp.write(the_file['content']) f_temp.flush() Container.validate_singularity_container(f_temp.name) if hasattr(the_file, 'seek') and callable(the_file.seek): the_file.seek(0) # Annotate self.instance with a marker that we already validated the container. self.instance.singularity_validated = True # Having figured out the file type, add it to self.instance manually. self.instance.file_type = file_type return self.cleaned_data
def test_validate_singularity_container_pass(self): """ A proper Singularity container should pass validation. :return: """ Container.validate_singularity_container(self.alpine_path)