"""NFC upload body with lease progress, lease URLs and datastore URLs.""" from __future__ import annotations from napalm_vmware.provision.transfer import ProgressFile, datastore_url, lease_url class _Clock: def __init__(self): self.now = 0.0 def __call__(self): self.now += 15.0 # each read "takes" 15 s return self.now class TestProgressFile: def test_has_a_length_so_requests_does_not_chunk(self, tmp_path): """A generator body is sent chunked; with a Content-Length header too, vSphere's NFC endpoint answers 500. A sized body avoids both.""" path = tmp_path / "disk.vmdk" path.write_bytes(b"x" * 1000) body = ProgressFile(path, lambda pct: None) assert len(body) == 1000 body.close() def test_reads_everything_and_reports_rising_progress(self, tmp_path): path = tmp_path / "disk.vmdk" path.write_bytes(b"x" * (3 * 1024 * 1024 + 5)) reports = [] body = ProgressFile(path, reports.append, clock=_Clock()) data = b"".join(iter(lambda: body.read(1024 * 1024), b"")) body.close() assert len(data) == path.stat().st_size assert reports and reports == sorted(reports) and all(0 <= r < 100 for r in reports) class TestLeaseUrl: def test_star_becomes_the_host_on_the_default_port(self): assert ( lease_url("https://*/nfc/x/disk-0.vmdk", "esx01", 443) == "https://esx01/nfc/x/disk-0.vmdk" ) def test_star_carries_a_non_default_port(self): assert ( lease_url("https://*/nfc/x/d.vmdk", "10.0.0.5", 8443) == "https://10.0.0.5:8443/nfc/x/d.vmdk" ) def test_a_named_host_is_left_alone(self): """Through vCenter the URL names the ESXi host that takes the disk.""" assert ( lease_url("https://esx02.lan/nfc/x/d.vmdk", "vc01", 443) == "https://esx02.lan/nfc/x/d.vmdk" ) def test_datastore_url_quotes_the_path(): assert ( datastore_url("https://esx01:443", "web 01/cidata.iso") == "https://esx01:443/folder/web%2001/cidata.iso" )