Skip to content

Commit

Permalink
write new function create_secret in libvirt_secret.py
Browse files Browse the repository at this point in the history
 this func is implemented by translating keys in params to new ones,
we'd better directly pass a dict with secret settings

Signed-off-by: nanli <[email protected]>
  • Loading branch information
nanli1 committed Mar 29, 2022
1 parent 12b198e commit b251568
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions virttest/utils_libvirt/libvirt_secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,66 @@
"""

import logging
import re

from aexpect import remote

from avocado.core import exceptions

from virttest import virsh
from virttest.utils_test import libvirt
from virttest.libvirt_xml import secret_xml

LOG = logging.getLogger('avocado.' + __name__)


def create_secret(sec_dict, remote_args=None):
"""
Create a secret with 'virsh secret-define'
:param sec_dict: dict to create secret
:param remote_args: Parameters for remote host
:return: UUID of the secret
Example:
secret_dict = {'secret_ephemeral': 'no',
'secret_private': 'yes',
'description': 'secret desc',
'usage': 'volume',
'volume': '/path/to/volume'}
sec_uuid = libvirt_secret.create_secret(sec_dict=secret_dict)
"""

sec_xml = secret_xml.SecretXML()
sec_xml.setup_attrs(**sec_dict)
sec_xml.xmltreefile.write()

# define the secret and get its uuid
if remote_args:
server_ip = remote_args.get("remote_ip", "")
server_user = remote_args.get("remote_user", "")
server_pwd = remote_args.get("remote_pwd", "")
if not all([server_ip, server_user, server_pwd]):
raise exceptions.TestError("remote_[ip|user|pwd] are necessary!")
remote_virsh_session = virsh.VirshPersistent(**remote_args)
remote.scp_to_remote(server_ip, '22', server_user, server_pwd,
sec_xml.xml, sec_xml.xml, limit="",
log_filename=None, timeout=600, interface=None)
ret = remote_virsh_session.secret_define(sec_xml.xml)
remote_virsh_session.close_session()
else:
ret = virsh.secret_define(sec_xml.xml)
libvirt.check_exit_status(ret)
try:
sec_uuid = re.findall(r".+\S+(\ +\S+)\ +.+\S+",
ret.stdout_text)[0].lstrip()
except IndexError:
raise exceptions.TestError("Fail to get newly created secret uuid")

return sec_uuid


def get_secret_list(remote_virsh=None):
"""
Get secret list by virsh secret-list from local or remote host.
Expand Down

0 comments on commit b251568

Please sign in to comment.