@@ -438,7 +438,38 @@ 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 ):
441+ def test_download_force (self , mock_message , mock_download_file ):
442+ """Test VM download force remove local image when present"""
443+ url = 'http://localhost/path/to/my_image.qcow2'
444+ self .config .set (
445+ 'vm' ,
446+ {
447+ 'image' : url ,
448+ }
449+ )
450+ with patch (
451+ 'rift.VM.VM.image_local' , new_callable = PropertyMock
452+ ) as mock_image_local :
453+ vm = VM (self .config , platform .machine ())
454+ tmpfile = make_temp_file ("" )
455+ mock_image_local .return_value = tmpfile .name
456+ self .assertTrue (os .path .exists (vm .image_local ))
457+ with self .assertLogs (level = 'DEBUG' ) as cm :
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 )
461+ self .assertIn (
462+ 'INFO:root:Remove VM image local copy and force re-download for remote '
463+ 'image' ,
464+ cm .output
465+ )
466+
467+ @patch ('rift.VM.last_modified' )
468+ @patch ('rift.VM.download_file' )
469+ @patch ('rift.VM.message' )
470+ def test_download_exists_last_modified_older (
471+ self , mock_message , mock_download_file , mock_last_modified
472+ ):
442473 """Test VM download skipped when local copy is present"""
443474 url = 'http://localhost/path/to/my_image.qcow2'
444475 self .config .set (
@@ -447,6 +478,7 @@ def test_download_skip_exists(self, mock_message, mock_download_file):
447478 'image' : url ,
448479 }
449480 )
481+ mock_last_modified .return_value = 0.0
450482 with patch (
451483 'rift.VM.VM.image_local' , new_callable = PropertyMock
452484 ) as mock_image_local :
@@ -457,24 +489,61 @@ def test_download_skip_exists(self, mock_message, mock_download_file):
457489 with self .assertLogs (level = 'DEBUG' ) as cm :
458490 vm ._download (False )
459491 mock_message .assert_not_called ()
492+ # Check download_file() has not been called
460493 mock_download_file .assert_not_called ()
494+ self .assertIn (
495+ "DEBUG:root:Local copy of VM image is already updated "
496+ f"({ int (os .path .getmtime (tmpfile .name ))} > 0), skipping download of "
497+ "remote image" ,
498+ cm .output
499+ )
500+
501+ @patch ('rift.VM.last_modified' )
502+ @patch ('rift.VM.download_file' )
503+ @patch ('rift.VM.message' )
504+ def test_download_exists_last_modified_newer (
505+ self , mock_message , mock_download_file , mock_last_modified
506+ ):
507+ """Test VM download skipped when local copy is present"""
508+ url = 'http://localhost/path/to/my_image.qcow2'
509+ self .config .set (
510+ 'vm' ,
511+ {
512+ 'image' : url ,
513+ }
514+ )
515+ mock_last_modified .return_value = float (2 ** 32 )
516+ with patch (
517+ 'rift.VM.VM.image_local' , new_callable = PropertyMock
518+ ) as mock_image_local :
519+ vm = VM (self .config , platform .machine ())
520+ tmpfile = make_temp_file ("" )
521+ mock_image_local .return_value = tmpfile .name
522+ self .assertTrue (os .path .exists (vm .image_local ))
523+ with self .assertLogs (level = 'DEBUG' ) as cm :
524+ vm ._download (False )
525+ mock_message .assert_called_once_with (f"Download remote VM image { url } " )
526+ mock_download_file .assert_called_once_with (url , vm .image_local )
461527 self .assertIn (
462- 'DEBUG:root:Local copy of VM image is present, skipping download of remote '
463- 'image' ,
528+ 'INFO:root:Remote VM image has been updated, removing local copy' ,
464529 cm .output
465530 )
466531
532+ @patch ('rift.VM.last_modified' )
467533 @patch ('rift.VM.download_file' )
468534 @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"""
535+ def test_download_exists_last_modified_error (
536+ self , mock_message , mock_download_file , mock_last_modified
537+ ):
538+ """Test VM download skipped when local copy is present"""
471539 url = 'http://localhost/path/to/my_image.qcow2'
472540 self .config .set (
473541 'vm' ,
474542 {
475543 'image' : url ,
476544 }
477545 )
546+ mock_last_modified .side_effect = RiftError ("last-modified failure" )
478547 with patch (
479548 'rift.VM.VM.image_local' , new_callable = PropertyMock
480549 ) as mock_image_local :
@@ -483,12 +552,13 @@ def test_download_force(self, mock_message, mock_download_file):
483552 mock_image_local .return_value = tmpfile .name
484553 self .assertTrue (os .path .exists (vm .image_local ))
485554 with self .assertLogs (level = 'DEBUG' ) as cm :
486- vm ._download (True )
487- mock_message .assert_called_once_with ( f"Download remote VM image { url } " )
488- mock_download_file .assert_called_once_with ( url , vm . image_local )
555+ vm ._download (False )
556+ mock_message .assert_not_called ( )
557+ mock_download_file .assert_not_called ( )
489558 self .assertIn (
490- 'INFO:root:Remove VM image local copy and force re-download for remote '
491- 'image' ,
559+ "DEBUG:root:Local copy of VM image is present, unable to get remote image "
560+ "modification date because of error (last-modified failure), skipping "
561+ "download of remote image" ,
492562 cm .output
493563 )
494564
0 commit comments