On 05/24/2012 03:39 PM, tangchen wrote:
> This patch adds 3 API for libvirt_vm.
> 1) vm.define()
> 2) vm.attach_interface()
> 3) vm.detach_interface()
>
> Signed-off-by: Tang Chen<[email protected]>
> ---
> client/virt/libvirt_vm.py | 80
> +++++++++++++++++++++++++++++++++++++++++++++
> 1 files changed, 80 insertions(+), 0 deletions(-)
>
> diff --git a/client/virt/libvirt_vm.py b/client/virt/libvirt_vm.py
> index eda5e8e..5d35888 100644
> --- a/client/virt/libvirt_vm.py
> +++ b/client/virt/libvirt_vm.py
> @@ -432,6 +432,62 @@ def virsh_migrate(options, name, dest_uri, extra, uri =
> ""):
> return True
>
>
> +def _check_interface_type(type):
> + if not type:
> + logging.error("Interface type missing.")
> + return False
> +
> + if type not in ('bridge', 'network', 'user', 'ethernet', 'direct',
> + 'hostdev', 'mcast', 'server', 'client'):
> + logging.error("Unknown interface type %s", type)
> + return False
> +
> + return True
> +
> +
> +def _build_run_at_dt_interface_cmd(cmd, type, extra="", uri=""):
> + if not _check_interface_type(type):
> + return False
As usual, I also do this like you, however, from test POV, it's a wrong
behavior,
we shouldn't block invalid interface type in our codes, it's also a test
scenario.
For example, if we directly give a invalid type to virsh then I assume
virsh probably
will crash, but if we call the function before running virsh cmd, it can
only raise
your error then we will miss a important test scenario.
> +
> + if cmd.startswith("attach-interface"):
> + verb = "attach"
> + elif cmd.startswith("detach-interface"):
> + verb = "detach"
> + else:
> + logging.error("Unknown command: %s" % cmd)
> +
> + cmd += " --type %s" % type
> +
> + if extra:
> + cmd += " %s" % extra
> +
> + try:
> + virsh_cmd(cmd, uri)
> + return True
> + except error.CmdError, detail:
> + logging.error("Interface %s on domain %s failed:\n%s",
> + verb, name, detail)
> + return False
> +
> +
> +def virsh_attach_interface(name, type, extra="", uri=""):
> + """
> + Attach a nic to VM.
> + """
> + cmd = "attach-interface --domain %s " % name
> +
> + return _build_run_at_dt_interface_cmd(cmd, type, extra, uri)
> +
> +
> +def virsh_detach_interface(name, type, extra="", uri=""):
> + """
> + Detach a nic to VM.
> + """
> + cmd = "detach-interface --domain %s " % name
> +
> + return _build_run_at_dt_interface_cmd(cmd, type, extra, uri)
> +
> +
> def virsh_attach_device(name, xml_file, extra = "", uri = ""):
> """
> Attach a device to VM.
> @@ -556,6 +612,16 @@ class VM(virt_vm.BaseVM):
> return False
>
>
> + def define(self, path):
> + """
> + Define the VM.
> + """
> + if not os.path.exists(path):
> + logging.error("File %s not found." % path)
> + return False
> + return virsh_define(path, self.connect_uri)
> +
> +
> def undefine(self):
> """
> Undefine the VM.
> @@ -1234,6 +1300,20 @@ class VM(virt_vm.BaseVM):
> return result
>
>
> + def attach_interface(self, type, extra = ""):
> + """
> + Attach a nic to VM.
> + """
> + return virsh_attach_interface(self.name, type, extra,
> self.connect_uri)
attach_interface()->virsh_attach_interface()->_build_run_at_dt_interface_cmd(),
maybe,
we may reduce some calling level, otherwise, need to wrapper 3 function
in order to
use a virsh command, of course, I know you want to abstract some method
then call it.
> +
> +
> + def detach_interface(self, type, extra = ""):
> + """
> + Detach a nic from VM.
> + """
> + return virsh_detach_interface(self.name, type, extra,
> self.connect_uri)
> +
> +
> def attach_device(self, xml_file, extra = ""):
> """
> Attach a device to VM.
_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest