#!/usr/bin/env python

# Copyright 2008-2010 VMware, Inc.  All rights reserved.
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#


import sys
import traceback
import socket

sys.path.append("/opt/vmware/lib/python/site-packages/")
import pywbem

sys.path.append("/opt/vmware/share/vami/")
import vami_cim_util
import vami_log

VAMI_SFCB_LOGFILENAME = "/opt/vmware/var/log/vami/vami-sfcb.log"

log = None

def main():

    global log
    log = vami_log.logfile(VAMI_SFCB_LOGFILENAME)

    log.message(vami_log.LOG_INFO, "Starting sfcb test script with 30 sec timeout.")
    # Set the default socket timeout to 30 seconds
    socket.setdefaulttimeout(30)

    try:
        # enumerate class in root/interop
        vami_cim_util.getDefaultCIMConnection().EnumerateClassNames(namespace='root/interop')

        # OK
        print ("OK")
        log.message(vami_log.LOG_INFO, "Class enumeration OK.")
        return 0
    except pywbem.cim_operations.CIMError as args:
        # Failed
        # print description string
        description = args[1]
        print ("FAILED: %s", description)
        log.message(vami_log.LOG_ERROR, "Class enumeration FAILED: %s" % description)

        if description.find('timed out') != -1:
            # timed out
            log.message(vami_log.LOG_ERROR, "Class enumeration TIMED OUT.")
            return 2

        # unknown failure
        return 1
    except:
        # Failed
        print ("FAILED: %s", traceback.format_exc())
        log.message(vami_log.LOG_ERROR, "Class enumeration FAILED: %s" % traceback.format_exc())
        return 1

if __name__ == "__main__":
    sys.exit( main() )
