From 874f8170a1d3dc5d8ed8562ad292b1e73aec7b94 Mon Sep 17 00:00:00 2001 From: pszulczewski Date: Fri, 20 Dec 2024 11:57:02 +0100 Subject: [PATCH] Fix jnpr_device.JunosDevice.save NamedTemporaryFile mode. --- pyntc/devices/jnpr_device.py | 4 ++-- tests/unit/test_devices/test_jnpr_device.py | 22 ++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/pyntc/devices/jnpr_device.py b/pyntc/devices/jnpr_device.py index 9bb4d463..2d87329d 100644 --- a/pyntc/devices/jnpr_device.py +++ b/pyntc/devices/jnpr_device.py @@ -408,8 +408,8 @@ def save(self, filename=None): self.cu.commit() return - temp_file = NamedTemporaryFile() # pylint: disable=consider-using-with - temp_file.write(self.show("show config")) + temp_file = NamedTemporaryFile(mode="w") # pylint: disable=consider-using-with + temp_file.write(self.startup_config) temp_file.flush() with SCP(self.native) as scp: diff --git a/tests/unit/test_devices/test_jnpr_device.py b/tests/unit/test_devices/test_jnpr_device.py index c5700bb4..6ad5dd44 100644 --- a/tests/unit/test_devices/test_jnpr_device.py +++ b/tests/unit/test_devices/test_jnpr_device.py @@ -165,19 +165,19 @@ def test_show_list(self, mock_show): self.device.show(commands) self.device.show.assert_called_with(commands) + @mock.patch("pyntc.devices.jnpr_device.JunosDevice.startup_config", new_callable=mock.PropertyMock) @mock.patch("pyntc.devices.jnpr_device.SCP", autospec=True) - def test_save(self, mock_scp): - self.device.show = mock.MagicMock() - self.device.show.return_value = b"file contents" + def test_save(self, mock_scp, mock_startup_config): + mock_startup_config.return_value = "file contents" result = self.device.save(filename="saved_config") self.assertTrue(result) - self.device.show.assert_called_with("show config") + mock_startup_config.assert_called_once_with() def test_file_copy_remote_exists(self): - temp_file = NamedTemporaryFile() - temp_file.write(b"file contents") + temp_file = NamedTemporaryFile(mode="w") + temp_file.write("file contents") temp_file.flush() local_checksum = "4a8ec4fa5f01b4ab1a0ab8cbccb709f0" @@ -189,8 +189,8 @@ def test_file_copy_remote_exists(self): self.device.fs.checksum.assert_called_with("dest") def test_file_copy_remote_exists_failure(self): - temp_file = NamedTemporaryFile() - temp_file.write(b"file contents") + temp_file = NamedTemporaryFile(mode="w") + temp_file.write("file contents") temp_file.flush() self.device.fs.checksum.return_value = "deadbeef" @@ -202,8 +202,8 @@ def test_file_copy_remote_exists_failure(self): @mock.patch("pyntc.devices.jnpr_device.SCP") def test_file_copy(self, mock_scp): - temp_file = NamedTemporaryFile() - temp_file.write(b"file contents") + temp_file = NamedTemporaryFile(mode="w") + temp_file.write("file contents") temp_file.flush() local_checksum = "4a8ec4fa5f01b4ab1a0ab8cbccb709f0" @@ -241,7 +241,7 @@ def test_rollback(self, mock_scp): @mock.patch("pyntc.devices.jnpr_device.SCP", autospec=True) def test_checkpoint(self, mock_scp): self.device.show = mock.MagicMock() - self.device.show.return_value = b"file contents" + self.device.show.return_value = "file contents" self.device.checkpoint("saved_config") self.device.show.assert_called_with("show config")