@@ -438,8 +438,8 @@ def test_download(self, mock_message, mock_download_file):
438438
439439 @patch ('rift.VM.download_file' )
440440 @patch ('rift.VM.message' )
441- def test_download_skip_exists (self , mock_message , mock_download_file ):
442- """Test VM download skipped when local copy is present"""
441+ def test_download_force (self , mock_message , mock_download_file ):
442+ """Test VM download force remove local image when present"""
443443 url = 'http://localhost/path/to/my_image.qcow2'
444444 self .config .set (
445445 'vm' ,
@@ -455,26 +455,57 @@ def test_download_skip_exists(self, mock_message, mock_download_file):
455455 mock_image_local .return_value = tmpfile .name
456456 self .assertTrue (os .path .exists (vm .image_local ))
457457 with self .assertLogs (level = 'DEBUG' ) as cm :
458- vm ._download (False )
459- mock_message .assert_not_called ( )
460- mock_download_file .assert_not_called ( )
458+ vm ._download (True )
459+ mock_message .assert_called_once_with ( f"Download remote VM image { url } " )
460+ mock_download_file .assert_called_once_with ( url , vm . image_local )
461461 self .assertIn (
462- 'DEBUG :root:Local copy of VM image is present, skipping download of remote '
462+ 'INFO :root:Remove VM image local copy and force re- download for remote '
463463 'image' ,
464464 cm .output
465465 )
466466
467+ @patch ('rift.VM.last_modified' )
467468 @patch ('rift.VM.download_file' )
468469 @patch ('rift.VM.message' )
469- def test_download_force (self , mock_message , mock_download_file ):
470- """Test VM download force remove local image when present"""
470+ def test_download_exists_last_modified_older (
471+ self , mock_message , mock_download_file , mock_last_modified
472+ ):
473+ """Test VM download skipped when local copy is present"""
471474 url = 'http://localhost/path/to/my_image.qcow2'
472475 self .config .set (
473476 'vm' ,
474477 {
475478 'image' : url ,
476479 }
477480 )
481+ mock_last_modified .return_value = 0.0
482+ with patch (
483+ 'rift.VM.VM.image_local' , new_callable = PropertyMock
484+ ) as mock_image_local :
485+ vm = VM (self .config , platform .machine ())
486+ tmpfile = make_temp_file ("" )
487+ mock_image_local .return_value = tmpfile .name
488+ self .assertTrue (os .path .exists (vm .image_local ))
489+ vm ._download (False )
490+ mock_message .assert_not_called ()
491+ # Check download_file() has not been called
492+ mock_download_file .assert_not_called ()
493+
494+ @patch ('rift.VM.last_modified' )
495+ @patch ('rift.VM.download_file' )
496+ @patch ('rift.VM.message' )
497+ def test_download_exists_last_modified_newer (
498+ self , mock_message , mock_download_file , mock_last_modified
499+ ):
500+ """Test VM download skipped when local copy is present"""
501+ url = 'http://localhost/path/to/my_image.qcow2'
502+ self .config .set (
503+ 'vm' ,
504+ {
505+ 'image' : url ,
506+ }
507+ )
508+ mock_last_modified .return_value = float (2 ** 32 )
478509 with patch (
479510 'rift.VM.VM.image_local' , new_callable = PropertyMock
480511 ) as mock_image_local :
@@ -483,12 +514,44 @@ def test_download_force(self, mock_message, mock_download_file):
483514 mock_image_local .return_value = tmpfile .name
484515 self .assertTrue (os .path .exists (vm .image_local ))
485516 with self .assertLogs (level = 'DEBUG' ) as cm :
486- vm ._download (True )
517+ vm ._download (False )
487518 mock_message .assert_called_once_with (f"Download remote VM image { url } " )
488519 mock_download_file .assert_called_once_with (url , vm .image_local )
489520 self .assertIn (
490- 'INFO:root:Remove VM image local copy and force re-download for remote '
491- 'image' ,
521+ 'INFO:root:Remote VM image has been updated, removing local copy' ,
522+ cm .output
523+ )
524+
525+ @patch ('rift.VM.last_modified' )
526+ @patch ('rift.VM.download_file' )
527+ @patch ('rift.VM.message' )
528+ def test_download_exists_last_modified_error (
529+ self , mock_message , mock_download_file , mock_last_modified
530+ ):
531+ """Test VM download skipped when local copy is present"""
532+ url = 'http://localhost/path/to/my_image.qcow2'
533+ self .config .set (
534+ 'vm' ,
535+ {
536+ 'image' : url ,
537+ }
538+ )
539+ mock_last_modified .side_effect = RiftError ("last-modified failure" )
540+ with patch (
541+ 'rift.VM.VM.image_local' , new_callable = PropertyMock
542+ ) as mock_image_local :
543+ vm = VM (self .config , platform .machine ())
544+ tmpfile = make_temp_file ("" )
545+ mock_image_local .return_value = tmpfile .name
546+ self .assertTrue (os .path .exists (vm .image_local ))
547+ with self .assertLogs (level = 'DEBUG' ) as cm :
548+ vm ._download (False )
549+ mock_message .assert_not_called ()
550+ mock_download_file .assert_not_called ()
551+ self .assertIn (
552+ "DEBUG:root:Local copy of VM image is present, unable to get remote image "
553+ "modification date because of error (last-modified failure), skipping "
554+ "download of remote image" ,
492555 cm .output
493556 )
494557
0 commit comments