Skip to main content

Vixpy

Python bindings for VMWare VIX API (version 1.15)

Vixpy provides helper functions on top of Python bindings for the VMWare VIX API to facilitate automated virtual machine control.

Installation

python setup.py install

Usage example

Virtual machine must have

  • vmware tools installed
  • No auto login
  • Administrator password set appropriately

Software test

import logging
import os
import shutil
import sys
import time
from vixpy import *

VM_BASE = r'c:\vm'

vm_list = {
    'XP' : VM_BASE + r'\WinXP\Windows XP Professional.vmx',
    '2K3' : VM_BASE + r'\Win2k3\Windows Server 2003 Enterprise Edition.vmx'
    }


TESTDIR = r'c:\testfiles'


def mkdirs(path):
    dname = ''
    for d in path.split(os.path.sep):
        dname = os.path.join(dname, d)
        try:
            os.mkdir(dname)
        except:
            pass


class SoftwareTest(object):
    _USER = 'administrator'
    _PASS = '12345678'

    def __init__(self, osname):
        self.name = osname
        self.path = ''
        self.log = None
        self.host = None
        self.vm = None
        self.scount = 0

        self.init_hostenv()


    def init_hostenv(self):
        self.path = os.path.join('testdata', self.name)
        shutil.rmtree(self.path, True)
        time.sleep(1)
        mkdirs(self.path)
        time.sleep(1)

        self.log = logging.getLogger(self.name)
        self.log.addHandler(logging.StreamHandler(sys.stdout))
        self.log.addHandler(logging.FileHandler(os.path.join(self.path, 'testlog.txt')))
        self.log.setLevel(logging.INFO)


    def err(self, msg, indent=0):
        self.log.info('\t'*indent + '[-] ' + msg)

    def info(self, msg, indent=0):
        self.log.info('\t'*indent + '[*] ' + msg)

    def success(self, msg, indent=0):
        self.log.info('\t'*indent + '[+] ' + msg)

    def warn(self, msg, indent=0):
        self.log.info('\t'*indent + '[!] ' + msg)


    def start_vm(self):
        self.host = VixHost()

        self.info("Opening VM")
        self.vm = self.host.open(vm_list[self.name])
        self.vm.revert(self.vm.get_namedsnapshot(self.name), True)

        self.info("Checking power state")
        if self.vm.power_state not in ("Powering On", "Powered On"):
            self.info("Powering on", 1)
            self.vm.on(gui=True)
        else:
            self.info("Already running", 1)

        self.info("Checking for vmware tools")
        if self.vm.power_state not in ("Tools Running",):
            self.info("Waiting for OS to fully load", 1)
            self.vm.wait()
        else:
            self.info("Tools already running (OS loaded)", 1)

        self.info("Logging in as %s" % self._USER)
        self.vm.login(self._USER, self._PASS, True)


    def reset_vm(self):
        self.info("Restarting VM")

        self.info("Powering off...", 1)
        # Blah - can't use reset...
        self.vm.off(True)
        if self.vm.power_state != 'Powered Off':
            raise VixError('Unknown State')

        self.info("Powering on...", 1)
        self.vm.on(gui=True)

        self.info("Checking for vmware tools - OS running state")
        if self.vm.power_state not in ("Tools Running",):
            self.info("Waiting for OS to fully load", 1)
            self.vm.wait()
        else:
            self.info("Tools already running (OS loaded)", 1)

        self.info("Logging in as %s" % self._USER)
        self.vm.login(self._USER, self._PASS, True)


    def init_testenv(self, testfiles):
        def make_path(f):
            return os.path.join(TESTDIR, os.path.basename(f))

        self.info("Creating Test environment")
        try:
            self.info("Creating test dir - %s" % TESTDIR, 1)
            self.vm.mkdir(TESTDIR)
        except VixError:
            self.warn("Directory already exists", 1)
            pass

        for f in testfiles:
            self.info("Copying %s to VM" % f, 1)
            self.vm.copy_to(f, make_path(f))

        self.success("Test environment created")


    def run_install(self):
        self.info("Running test software install")
        self.vm.run(os.path.join(TESTDIR, 'install.exe'), True, True)


    def run_uninstall(self):
        self.info("Running test software uninstall")
        self.vm.run(os.path.join(TESTDIR, 'uninstall.exe'), True, True)


    def verify(self):
        self.info("Verifying")
        self.info("Running verify script...", 1)
        self.vm.run(os.path.join(TESTDIR, 'verify.bat'), '')
        self.vm.copy_from(
                os.path.join(TESTDIR, 'verify_out.txt'),
                os.path.join(self.path, 'verify_out.txt'))


    def screen_shot(self):
        f = os.path.join(self.path, 'screen-shot-%02d.png' % self.scount)
        self.scount += 1
        self.info("Taking screen shot : %s" % f)
        open(f, 'wb').write(self.vm.screen_capture())


    def run_test(self):
        self.info("Starting Test : %s" % self.name)

        try:
            self.start_vm()
            self.screen_shot()
            self.init_testenv(['install.exe', 'uninstall.exe', 'verify.bat'])
            self.screen_shot()
            self.run_install()
            self.screen_shot()
            self.verify()
            self.screen_shot()
            self.reset_vm()
            self.screen_shot()
            self.run_uninstall()
            self.screen_shot()
        except Exception as e:
            self.err('Failed with exception')
            self.err(str(e))
        self.info("Test Finished")


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print "usage: %s <OS Name>" % sys.argv[0]
        print "  XP 2K3"
        sys.exit(-1)

    t = SoftwareTest(sys.argv[1])
    t.run_test()

Release History

  • 1.0.0
    • Python bindings rewritten to use ctypes
    • Moved to Python 3

Meta

Distributed under the MIT license. See LICENSE for more information.

https://github.com/dshikashio

Release files for Vixpy 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Vixpy 1.0.0
File Size Uploaded
Vixpy-1.0.0.tar.gz 14.5 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for Vixpy 1.0.0
File Interpreter ABI Platform
Vixpy-1.0.0-py3.7.egg Legacy Egg format - - Details
Vixpy-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 60.9 kB

Release files / Vixpy-1.0.0.tar.gz

Download URL Vixpy-1.0.0.tar.gz
Size 14.5 kB
Tags Source
SHA-256 checksum
How to use checksums
908fcc50f7b3cefe5a94b38159908c1c5dc39f11b176e003f8ce1eb974a81bc7
BLAKE2b-256 checksum
How to use checksums
a7e8fa9d71eddf919f2bb20698089864a06da8a7e9fa38e1e15e7dee0103ed7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.10

Release files / Vixpy-1.0.0-py3.7.egg

Download URL Vixpy-1.0.0-py3.7.egg
Size 31.3 kB
Tags Egg
SHA-256 checksum
How to use checksums
00c0aa1e589a978881c681639a17528970f008131da9a688f8ee15212cc09060
BLAKE2b-256 checksum
How to use checksums
09a9a7874ffa1206d060a3f9515928ff02373834596e5da55736e5ed8dbd280a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.10

Release files / Vixpy-1.0.0-py3-none-any.whl

Download URL Vixpy-1.0.0-py3-none-any.whl
Size 15.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
114879bbc492305c751a295d1c83057b1a7f3bf2e358ee6e8da073dd78f6f18e
BLAKE2b-256 checksum
How to use checksums
11c4d750b29ba9ebd46e71f26759cdac47c19944aee8a9a77fe833a8d00bc63b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.10

Release history Release notifications | RSS feed

This release

1.0.0 This release

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page