canonical / cloud-init

Official upstream for the cloud-init: cloud instance initialization
https://cloud-init.io/
Other
2.78k stars 842 forks source link

New DeprecationWarnings and PytestMockWarnings whilst building with Python 3.12 #5175

Open dermotbradley opened 3 months ago

dermotbradley commented 3 months ago

Bug report

Alpine Edge has now upgraded its Python from 3.11.8 to 3.12.3. Whilst packaging cloud-init with the new Python version some new (mainly deprecation) warnings appear.

The deprecation warnings mainly relate to datetime.datetime.utcfromtimestamp() and datetime.datetime.utcnow().

However there are also the following warnings:

DeprecationWarning: Testing an element's truth value will raise an exception in future versions.  Use specific 'len(elem)' or 'elem is not None' test instead.

and

PytestMockWarning: Mocks returned by pytest-mock do not need to be used as context managers. The mocker fixture automatically undoes mocking at the end of a test. This warning can be ignored if it was triggered by mocking a context manager. https://pytest-mock.readthedocs.io/en/latest/remarks.html#usage-as-context-manager

Steps to reproduce the problem

Environment details

cloud-init logs

tests/unittests/analyze/test_boot.py::TestAnalyzeBoot::test_boot_invalid_distro
tests/unittests/analyze/test_boot.py::TestAnalyzeBoot::test_container_no_ci_log_line
tests/unittests/analyze/test_boot.py::TestAnalyzeBoot::test_container_ci_log_line
  /home/builder/package/src/cloud-init-24.2/cloudinit/analyze/__init__.py:131: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).
    kernel_start_timestamp = datetime.utcfromtimestamp(kernel_start)

tests/unittests/analyze/test_boot.py::TestAnalyzeBoot::test_boot_invalid_distro
tests/unittests/analyze/test_boot.py::TestAnalyzeBoot::test_container_no_ci_log_line
tests/unittests/analyze/test_boot.py::TestAnalyzeBoot::test_container_ci_log_line
  /home/builder/package/src/cloud-init-24.2/cloudinit/analyze/__init__.py:132: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).
    kernel_end_timestamp = datetime.utcfromtimestamp(kernel_end)

tests/unittests/analyze/test_boot.py::TestAnalyzeBoot::test_boot_invalid_distro
tests/unittests/analyze/test_boot.py::TestAnalyzeBoot::test_container_no_ci_log_line
tests/unittests/analyze/test_boot.py::TestAnalyzeBoot::test_container_ci_log_line
  /home/builder/package/src/cloud-init-24.2/cloudinit/analyze/__init__.py:133: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).
    ci_sysd_start_timestamp = datetime.utcfromtimestamp(ci_sysd_start)

tests/unittests/analyze/test_boot.py::TestAnalyzeBoot::test_container_ci_log_line
  /home/builder/package/src/cloud-init-24.2/cloudinit/analyze/__init__.py:141: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).
    ci_start = datetime.utcfromtimestamp(last_init_local["timestamp"])

tests/unittests/cmd/devel/test_logs.py::TestCollectLogs::test_collect_logs_creates_tarfile
  /home/builder/package/src/cloud-init-24.2/tests/unittests/cmd/devel/test_logs.py:65: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    date = datetime.utcnow().date().strftime("%Y-%m-%d")

tests/unittests/cmd/devel/test_logs.py::TestCollectLogs::test_collect_logs_creates_tarfile
tests/unittests/cmd/devel/test_logs.py::TestCollectLogs::test_collect_logs_includes_optional_userdata
  /home/builder/package/src/cloud-init-24.2/cloudinit/cmd/devel/logs.py:221: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    log_dir = datetime.utcnow().date().strftime("cloud-init-logs-%Y-%m-%d")

tests/unittests/cmd/devel/test_logs.py::TestCollectLogs::test_collect_logs_includes_optional_userdata
  /home/builder/package/src/cloud-init-24.2/tests/unittests/cmd/devel/test_logs.py:182: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    date = datetime.utcnow().date().strftime("%Y-%m-%d")

tests/unittests/reporting/test_reporting_hyperv.py: 13 warnings
  /home/builder/package/src/cloud-init-24.2/cloudinit/reporting/handlers.py:376: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).
    datetime.utcfromtimestamp(event.timestamp).isoformat() + "Z"

tests/unittests/reporting/test_reporting_hyperv.py::TextKvpReporter::test_get_boot_telemetry
  /home/builder/package/src/cloud-init-24.2/cloudinit/sources/helpers/azure.py:122: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).
    datetime.utcfromtimestamp(kernel_start).isoformat() + "Z",

tests/unittests/reporting/test_reporting_hyperv.py::TextKvpReporter::test_get_boot_telemetry
  /home/builder/package/src/cloud-init-24.2/cloudinit/sources/helpers/azure.py:123: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).
    datetime.utcfromtimestamp(user_start).isoformat() + "Z",

tests/unittests/reporting/test_reporting_hyperv.py::TextKvpReporter::test_get_boot_telemetry
  /home/builder/package/src/cloud-init-24.2/cloudinit/sources/helpers/azure.py:124: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).
    datetime.utcfromtimestamp(cloudinit_activation).isoformat() + "Z",

tests/unittests/sources/azure/test_errors.py: 48 warnings
  /home/builder/package/src/cloud-init-24.2/tests/unittests/sources/azure/test_errors.py:23: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    timestamp = datetime.datetime.utcnow()

tests/unittests/sources/azure/test_errors.py: 13 warnings
tests/unittests/sources/azure/test_kvp.py: 2 warnings
tests/unittests/sources/test_azure.py: 107 warnings
tests/unittests/sources/test_azure_helper.py: 11 warnings
  /home/builder/package/src/cloud-init-24.2/cloudinit/sources/azure/errors.py:55: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    self.timestamp = datetime.utcnow()

tests/unittests/sources/azure/test_kvp.py::TestReportSuccessToHost::test_report_success_to_host
  /home/builder/package/src/cloud-init-24.2/tests/unittests/sources/azure/test_kvp.py:14: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    timestamp = datetime.utcnow()

tests/unittests/sources/azure/test_kvp.py: 1 warning
tests/unittests/sources/test_azure.py: 49 warnings
  /home/builder/package/src/cloud-init-24.2/cloudinit/sources/azure/kvp.py:52: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    f"timestamp={datetime.utcnow().isoformat()}",

tests/unittests/sources/test_azure.py: 67 warnings
tests/unittests/sources/test_azure_helper.py: 17 warnings
  /home/builder/package/src/cloud-init-24.2/cloudinit/sources/helpers/azure.py:1011: DeprecationWarning: Testing an element's truth value will raise an exception in future versions.  Use specific 'len(elem)' or 'elem is not None' test instead.
    if not root.find("./wa:ProvisioningSection", cls.NAMESPACES):

tests/unittests/sources/test_azure.py: 17 warnings
  /home/builder/package/src/cloud-init-24.2/tests/unittests/sources/test_azure.py:293: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    timestamp = datetime.datetime.utcnow()

tests/unittests/sources/test_gce.py::TestDataSourceGCE::test_has_expired
tests/unittests/sources/test_gce.py::TestDataSourceGCE::test_has_expired
  /home/builder/package/src/cloud-init-24.2/cloudinit/sources/DataSourceGCE.py:237: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    return datetime.datetime.utcnow() > expire_time

tests/unittests/test_cli.py: 13 warnings
  /home/builder/package/src/cloud-init-24.2/tests/unittests/test_cli.py:41: PytestMockWarning: Mocks returned by pytest-mock do not need to be used as context managers. The mocker fixture automatically undoes mocking at the end of a test. This warning can be ignored if it was triggered by mocking a context manager. https://pytest-mock.readthedocs.io/en/latest/remarks.html#usage-as-context-manager
    with mocker.patch(

tests/unittests/test_log.py::TestCloudInitLogger::test_logger_uses_gmtime
  /home/builder/package/src/cloud-init-24.2/tests/unittests/test_log.py:44: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    utc_before = datetime.datetime.utcnow() - datetime.timedelta(0, 0.5)

tests/unittests/test_log.py::TestCloudInitLogger::test_logger_uses_gmtime
  /home/builder/package/src/cloud-init-24.2/tests/unittests/test_log.py:46: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    utc_after = datetime.datetime.utcnow() + datetime.timedelta(0, 0.5)
TheRealFalcon commented 3 months ago

Thanks for the report @dermotbradley . I can confirm that on recent versions of python we see these warnings when running tests.