# Path: tests/whitebox/integration/_keyutils.py
# class RandomKeyTmpFile:
#     """
#     Generate a random passphrase and put it in a temporary file.
#     """
# 
#     # pylint: disable=consider-using-with
#     def __init__(self, key_bytes=32):
#         """
#         Initializer
# 
#         :param int key_bytes: the desired length of the key in bytes
#         """
#         self._tmpfile = NamedTemporaryFile("wb")
#         with open("/dev/urandom", "rb") as random:
#             random_bytes = random.read(key_bytes)
#             self._tmpfile.write(random_bytes)
#             self._tmpfile.flush()
# 
#     def tmpfile_name(self):
#         """
#         Get the name of the temporary file.
#         """
#         return self._tmpfile.name
# 
#     def close(self):
#         """
#         Close and delete the temporary file.
#         """
#         self._tmpfile.close()
# 
#     def __enter__(self):
#         """
#         For use with the "with" keyword.
# 
#         :return str: the path of the file with the random key
#         """
#         return self._tmpfile.name
# 
#     def __exit__(self, exc_type, exc_value, traceback):
#         try:
#             self._tmpfile.close()
#         except Exception as error:
#             if exc_value is None:
#                 raise error
# 
#             raise error from exc_value


from stratis_cli import StratisCliErrorCodes
from stratis_cli._errors import (
    StratisCliEngineError,
    StratisCliKeyfileNotFoundError,
    StratisCliNameConflictError,
)
from .._keyutils import RandomKeyTmpFile
from .._misc import RUNNER, TEST_RUNNER, SimTestCase
"""
Test 'set'.
"""

# isort: LOCAL


_ERROR = StratisCliErrorCodes.ERROR


class TestKeySet(SimTestCase):
    """
    Test setting a key in the keyring.
    """

    _MENU = ["--propagate", "key", "set"]
    _KEYNAME = "testkey"

    def test_set(self):
        """
        Setting should succeed.
        """
        with RandomKeyTmpFile() as fname:
            command_line = self._MENU + [self._KEYNAME, "--keyfile-path", fname]
            TEST_RUNNER(command_line)

    def test_set_conflict(self):
        """
        Setting should fail due to name conflict.
        """

# Path: tests/whitebox/integration/_keyutils.py
# class RandomKeyTmpFile:
#     """
#     Generate a random passphrase and put it in a temporary file.
#     """
# 
#     # pylint: disable=consider-using-with
#     def __init__(self, key_bytes=32):
#         """
#         Initializer
# 
#         :param int key_bytes: the desired length of the key in bytes
#         """
#         self._tmpfile = NamedTemporaryFile("wb")
#         with open("/dev/urandom", "rb") as random:
#             random_bytes = random.read(key_bytes)
#             self._tmpfile.write(random_bytes)
#             self._tmpfile.flush()
# 
#     def tmpfile_name(self):
#         """
#         Get the name of the temporary file.
#         """
#         return self._tmpfile.name
# 
#     def close(self):
#         """
#         Close and delete the temporary file.
#         """
#         self._tmpfile.close()
# 
#     def __enter__(self):
#         """
#         For use with the "with" keyword.
# 
#         :return str: the path of the file with the random key
#         """
#         return self._tmpfile.name
# 
#     def __exit__(self, exc_type, exc_value, traceback):
#         try:
#             self._tmpfile.close()
#         except Exception as error:
#             if exc_value is None:
#                 raise error
# 
#             raise error from exc_value


# Path: tests/whitebox/integration/key/test_set.py
from stratis_cli import StratisCliErrorCodes
from stratis_cli._errors import (
    StratisCliEngineError,
    StratisCliKeyfileNotFoundError,
    StratisCliNameConflictError,
)
from .._keyutils import RandomKeyTmpFile
from .._misc import RUNNER, TEST_RUNNER, SimTestCase

"""
Test 'set'.
"""

# isort: LOCAL


_ERROR = StratisCliErrorCodes.ERROR


class TestKeySet(SimTestCase):
    """
    Test setting a key in the keyring.
    """

    _MENU = ["--propagate", "key", "set"]
    _KEYNAME = "testkey"

    def test_set(self):
        """
        Setting should succeed.
        """
        with RandomKeyTmpFile() as fname:
            command_line = self._MENU + [self._KEYNAME, "--keyfile-path", fname]
            TEST_RUNNER(command_line)

    def test_set_conflict(self):
        """
        Setting should fail due to name conflict.
        """

        with RandomKeyTmpFile() as fname: