file-util: replace use of deprecate datetime.datetime.utcnow()

Signed-off-by: Olivier Gayot <olivier.gayot@canonical.com>
This commit is contained in:
Olivier Gayot 2024-04-25 16:18:56 +02:00
parent fa8e49457b
commit c18d434439
2 changed files with 8 additions and 5 deletions

View File

@ -13,6 +13,7 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import datetime
import fnmatch import fnmatch
import json import json
import re import re
@ -264,11 +265,13 @@ class TestSubiquityModel(unittest.IsolatedAsyncioTestCase):
self.assertEqual(expected_error, str(ctx.exception)) self.assertEqual(expected_error, str(ctx.exception))
@mock.patch("subiquity.models.subiquity.lsb_release") @mock.patch("subiquity.models.subiquity.lsb_release")
@mock.patch("subiquitycore.file_util.datetime.datetime") @mock.patch("subiquitycore.file_util.datetime.datetime", wraps=datetime.datetime)
def test_cloud_init_files_emits_datasource_config_and_clean_script( def test_cloud_init_files_emits_datasource_config_and_clean_script(
self, datetime, lsb_release self, m_datetime, lsb_release
): ):
datetime.utcnow.return_value = "2004-03-05 ..." m_datetime.now.return_value = datetime.datetime(
2004, 3, 5, tzinfo=datetime.timezone.utc
)
main_user = IdentityData( main_user = IdentityData(
username="mainuser", crypted_password="sample_pass", hostname="somehost" username="mainuser", crypted_password="sample_pass", hostname="somehost"
) )
@ -296,7 +299,7 @@ grub_dpkg:
# NETPLAN_CONFIG_ROOT_READ_ONLY is True # NETPLAN_CONFIG_ROOT_READ_ONLY is True
"/etc/cloud/cloud.cfg.d/90-installer-network.cfg" "/etc/cloud/cloud.cfg.d/90-installer-network.cfg"
) )
header = "# Autogenerated by Subiquity: 2004-03-05 ... UTC\n" header = "# Autogenerated by Subiquity: 2004-03-05 00:00:00 UTC\n"
with self.subTest( with self.subTest(
"Stable releases Jammy do not disable cloud-init." "Stable releases Jammy do not disable cloud-init."
" NETPLAN_ROOT_READ_ONLY=True uses cloud-init networking" " NETPLAN_ROOT_READ_ONLY=True uses cloud-init networking"

View File

@ -70,7 +70,7 @@ def write_file(filename, content, **kwargs):
def generate_timestamped_header() -> str: def generate_timestamped_header() -> str:
now = datetime.datetime.utcnow() now = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
return f"# Autogenerated by Subiquity: {now} UTC\n" return f"# Autogenerated by Subiquity: {now} UTC\n"