Skip to content

Commit

Permalink
Dev: unittests: Add unit tests for pickling CommandFailure
Browse files Browse the repository at this point in the history
  • Loading branch information
liangxin1300 committed Aug 26, 2024
1 parent b336f79 commit 4c9b256
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/unittests/test_sh.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import subprocess
import unittest
import pickle
from unittest import mock

import crmsh.sh
Expand Down Expand Up @@ -193,3 +194,33 @@ def test_subprocess_run_without_input_with_input_kwargs(self):
stdin=subprocess.PIPE,
)
self.cluster_shell.local_shell.su_subprocess_run.assert_not_called()


class TestCommandFailurePickling(unittest.TestCase):
def test_pickling_unpickling(self):
# Create an instance of CommandFailure
original = crmsh.sh.CommandFailure(cmd="ls", host="localhost", user="root", msg="Permission denied")
# Pickle the object
pickled = pickle.dumps(original)
# Unpickle the object
unpickled = pickle.loads(pickled)

# Assert that the unpickled object retains the same attributes as the original
self.assertEqual(original.cmd, unpickled.cmd)
self.assertEqual(original.host, unpickled.host)
self.assertEqual(original.user, unpickled.user)
self.assertEqual(original.msg, unpickled.msg)

def test_pickling_unpickling_with_none_values(self):
# Create an instance of CommandFailure with None values for optional parameters
original = crmsh.sh.CommandFailure(cmd="ls", host=None, user=None, msg="No such file or directory")
# Pickle the object
pickled = pickle.dumps(original)
# Unpickle the object
unpickled = pickle.loads(pickled)

# Assert that the unpickled object retains the same attributes, including None values
self.assertEqual(original.cmd, unpickled.cmd)
self.assertEqual(original.host, unpickled.host)
self.assertEqual(original.user, unpickled.user)
self.assertEqual(original.msg, unpickled.msg)

0 comments on commit 4c9b256

Please sign in to comment.