@@ -620,18 +620,21 @@ def test_verify_image_success_without_md5(self, requests_mock,
620
620
image_download .verify_image (image_location )
621
621
hashlib_mock .assert_called_with ('sha512' )
622
622
623
+ @mock .patch .object (standby .LOG , 'warning' , autospec = True )
623
624
@mock .patch ('hashlib.new' , autospec = True )
624
625
@mock .patch ('builtins.open' , autospec = True )
625
626
@mock .patch ('requests.get' , autospec = True )
626
627
def test_verify_image_success_with_md5_fallback (self , requests_mock ,
627
- open_mock , hash_mock ):
628
+ open_mock , hash_mock ,
629
+ warn_mock ):
628
630
CONF .set_override ('md5_enabled' , True )
629
631
image_info = _build_fake_image_info ()
630
632
image_info ['os_hash_algo' ] = 'algo-beyond-milky-way'
631
633
image_info ['os_hash_value' ] = 'mysterious-alien-codes'
632
634
image_info ['checksum' ] = 'd41d8cd98f00b204e9800998ecf8427e'
633
635
response = requests_mock .return_value
634
636
response .status_code = 200
637
+ hash_mock .return_value .name = 'md5'
635
638
hexdigest_mock = hash_mock .return_value .hexdigest
636
639
hexdigest_mock .return_value = image_info ['checksum' ]
637
640
image_location = '/foo/bar'
@@ -643,7 +646,11 @@ def test_verify_image_success_with_md5_fallback(self, requests_mock,
643
646
hash_mock .assert_has_calls ([
644
647
mock .call ('md5' ),
645
648
mock .call ().__bool__ (),
649
+ mock .call ('md5' ),
646
650
mock .call ().hexdigest ()])
651
+ warn_mock .assert_called_once_with (
652
+ mock .ANY ,
653
+ {'provided' : 'algo-beyond-milky-way' , 'detected' : 'md5' })
647
654
648
655
@mock .patch ('hashlib.new' , autospec = True )
649
656
@mock .patch ('builtins.open' , autospec = True )
@@ -1706,7 +1713,39 @@ def test_download_image_retries_success(self, sleep_mock, requests_mock,
1706
1713
sleep_mock .assert_called_with (10 )
1707
1714
self .assertEqual (2 , sleep_mock .call_count )
1708
1715
1709
- def test_download_image_and_checksum (self , requests_mock , hash_mock ):
1716
+ @mock .patch .object (standby .LOG , 'warning' , autospec = True )
1717
+ def test_download_image_and_checksum (self , warn_mock , requests_mock ,
1718
+ hash_mock ):
1719
+ content = ['SpongeBob' , 'SquarePants' ]
1720
+ fake_cs = "019fe036425da1c562f2e9f5299820bf"
1721
+ cs_response = mock .Mock ()
1722
+ cs_response .status_code = 200
1723
+ cs_response .text = fake_cs + '\n '
1724
+ response = mock .Mock ()
1725
+ response .status_code = 200
1726
+ response .iter_content .return_value = content
1727
+ requests_mock .side_effect = [cs_response , response ]
1728
+
1729
+ image_info = _build_fake_image_info ()
1730
+ image_info ['os_hash_algo' ] = 'sha512'
1731
+ image_info ['os_hash_value' ] = 'http://example.com/checksum'
1732
+ hash_mock .return_value .hexdigest .return_value = fake_cs
1733
+ hash_mock .return_value .name = 'sha512'
1734
+ image_download = standby .ImageDownload (image_info )
1735
+
1736
+ self .assertEqual (content , list (image_download ))
1737
+ requests_mock .assert_has_calls ([
1738
+ mock .call ('http://example.com/checksum' , cert = None , verify = True ,
1739
+ stream = True , proxies = {}, timeout = 60 ),
1740
+ mock .call (image_info ['urls' ][0 ], cert = None , verify = True ,
1741
+ stream = True , proxies = {}, timeout = 60 ),
1742
+ ])
1743
+ self .assertEqual (fake_cs , image_download ._hash_algo .hexdigest ())
1744
+ warn_mock .assert_not_called ()
1745
+
1746
+ @mock .patch .object (standby .LOG , 'warning' , autospec = True )
1747
+ def test_download_image_and_checksum_warning_on_mismatch (
1748
+ self , warn_mock , requests_mock , hash_mock ):
1710
1749
content = ['SpongeBob' , 'SquarePants' ]
1711
1750
fake_cs = "019fe036425da1c562f2e9f5299820bf"
1712
1751
cs_response = mock .Mock ()
@@ -1721,6 +1760,7 @@ def test_download_image_and_checksum(self, requests_mock, hash_mock):
1721
1760
image_info ['os_hash_algo' ] = 'sha512'
1722
1761
image_info ['os_hash_value' ] = 'http://example.com/checksum'
1723
1762
hash_mock .return_value .hexdigest .return_value = fake_cs
1763
+ hash_mock .return_value .name = 'md5'
1724
1764
image_download = standby .ImageDownload (image_info )
1725
1765
1726
1766
self .assertEqual (content , list (image_download ))
@@ -1731,6 +1771,9 @@ def test_download_image_and_checksum(self, requests_mock, hash_mock):
1731
1771
stream = True , proxies = {}, timeout = 60 ),
1732
1772
])
1733
1773
self .assertEqual (fake_cs , image_download ._hash_algo .hexdigest ())
1774
+ warn_mock .assert_called_once_with (
1775
+ mock .ANY ,
1776
+ {'provided' : 'sha512' , 'detected' : 'md5' })
1734
1777
1735
1778
def test_download_image_and_checksum_md5 (self , requests_mock , hash_mock ):
1736
1779
0 commit comments