Skip to content

Commit

Permalink
Issue #504, Changes to pywbemcli required for pywbem 1.0.0
Browse files Browse the repository at this point in the history
compatibility.

A number of changes were required to make pywbemcli compatible with both
pywbem pre version 1.0 and the incompatible differences in pywbem 1.0.0.

Generally these are issues in the tests and not in pywbemcli itself.

Changed the tests so that we create variables PYWBEM_0 and PYWBEM_1 in
a common file and use these variables to modify tests.

Modified a number of tests to account for the fact that the returned
url for many tests now includes the port in addition to the host.

Modified the invokemethod test code to create the user-defined test
provider required by pywbem version 1
Since the pywbem 1 invokemethod server responder is completely different
that the pywbem 0 version, two different InvokeMethod test driver python
files are for the invoke method tests are defined.  This applies to both the
simple_mock_invokemethod.py code and the all_types_method_mock.py
code

1. Changed name of original file to append _V0 to the name

2 Created corresponding _V1.py as the user-defined provider for
InvokeMethod tests.

3. Added if statement to each test file that tests invokemethod to use the
correct file.

4. Created new test for testing url invalid port for pywbem 1.
  • Loading branch information
KSchopmeyer authored and andy-maier committed Jun 12, 2020
1 parent 4889e13 commit f39ee84
Show file tree
Hide file tree
Showing 13 changed files with 402 additions and 192 deletions.
6 changes: 5 additions & 1 deletion pywbemtools/pywbemcli/_cmd_class.py
Expand Up @@ -25,7 +25,8 @@

import click

from pywbem import Error, CIMClassName, CIMError, CIM_ERR_NOT_FOUND, CIMClass
from pywbem import Error, CIMClassName, CIMError, ModelError, \
CIM_ERR_NOT_FOUND, CIMClass

from .pywbemcli import cli
from ._common import display_cim_objects, filter_namelist, \
Expand Down Expand Up @@ -753,6 +754,9 @@ def get_namespaces(context, namespaces):
ns_names = context.wbem_server.namespaces
ns_names.sort()
return ns_names

except ModelError:
return [context.conn.default_namespace]
except CIMError as ce:
# allow processing to continue if no interop namespace
if ce.status_code == CIM_ERR_NOT_FOUND:
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Expand Up @@ -9,7 +9,8 @@
# Direct dependencies (except pip, setuptools, wheel):

pywbem>=0.17.0
# git+https://github.com/pywbem/pywbem.git@master#egg=pywbem
# Alternative to test against current github master pywbem
#git+https://github.com/pywbem/pywbem.git@master#egg=pywbem

six>=1.10.0
# Click 7.1 has a bug with output capturing
Expand Down
File renamed without changes.
72 changes: 72 additions & 0 deletions tests/unit/all_types_method_mock_V1.py
@@ -0,0 +1,72 @@
"""
mock_pywbem test script that installs a method callback to be executed. This is
based on the CIM_Foo class in the simple_mock_model.mof test file
"""

import six
from pywbem_mock import MethodProvider

from pywbem import CIM_ERR_METHOD_NOT_AVAILABLE, CIMError, CIM_ERR_NOT_FOUND

# test that GLOBALS exist
assert "CONN" in globals()
assert 'SERVER' in globals()
assert 'VERBOSE' in globals()
global CONN # pylint: disable=global-at-module-level


class CIM_AllTypesMethodProvider(MethodProvider):
"""
User test provider for InvokeMethod using CIM_Foo and method1.
This is basis for testing passing of input parameters correctly and
generating some exceptions. It uses only one input parameter where the
value defines the test and one return parameter that provides data from the
provider, normally the value of the parameter defined with the input
parameter. Test for existence of method named method1
"""

provider_classnames = 'PyWBEM_AllTypes'

def __init__(self, cimrepository):
super(CIM_AllTypesMethodProvider, self).__init__(cimrepository)

def InvokeMethod(self, namespace, MethodName, ObjectName, Params):
"""
Simplistic test method. Validates methodname, objectname, Params
and returns rtn value 0 and one parameter
The parameters and return for Invoke method are defined in
:meth:`~pywbem_mock.MethodProvider.InvokeMethod`
"""
# validate namespace using method in BaseProvider
self.validate_namespace(namespace)

# get classname and validate. This provider uses only one class
if isinstance(ObjectName, six.string_types):
classname = ObjectName
else:
classname = ObjectName.classname

assert classname.lower() == self.provider_classnames.lower()

if MethodName != 'AllTypesMethod':
raise CIMError(CIM_ERR_METHOD_NOT_AVAILABLE)

# Test if class exists.
if not self.class_exists(namespace, classname):
raise CIMError(
CIM_ERR_NOT_FOUND,
"class {0} does not exist in CIM repository, "
"namespace {1}".format(classname, namespace))

# Return the input parameters and returnvalue == 0
return (0, Params)


# Add the the callback to the mock repository
# pylint: disable=undefined-variable
CONN.register_provider( # noqa: F821
CIM_AllTypesMethodProvider(CONN.cimrepository), # noqa: F821
CONN.default_namespace, # noqa: F821
verbose=False)
18 changes: 18 additions & 0 deletions tests/unit/cli_test_extensions.py
Expand Up @@ -9,11 +9,29 @@
import os
import pytest
import six
import pywbem

from .utils import execute_pywbemcli, assert_rc, assert_patterns, assert_lines

TEST_DIR = os.path.dirname(__file__)

# Boolean Variable that can be in individual tests to determine tests are to
# be executed. Returns True if the pywbem >=1.0.0 is use. Otherwise returns
# False. These variables can be applied to tests that are specific to one
# version of pywbem or the other
PYWBEM_0 = pywbem.__version__.startswith("0")
PYWBEM_1 = not PYWBEM_0


# This variable defines the url returned from some of the commands. Since it
# is changed with pywbem 1.0.0 to include the port number, we must dynamically
# create it for the tests depending on pywbem version
FAKEURL_STR = '//FakedUrl:5988' if PYWBEM_1 else '//FakedUrl'

# TODO remove this. Temp flag for condition that allows skipping tests that
# are failing with pywbem 1.0.0
ISSUE_100 = False


class CLITestsBase(object):
# pylint: disable=too-few-public-methods, useless-object-inheritance
Expand Down
File renamed without changes.
90 changes: 90 additions & 0 deletions tests/unit/simple_mock_invokemethod_pywbem_V1.py
@@ -0,0 +1,90 @@
"""
mock_pywbem test script that installs a a method provider for the class
CIM_Foo
"""

import six
from pywbem_mock import MethodProvider

from pywbem import CIM_ERR_METHOD_NOT_AVAILABLE, CIMError, CIM_ERR_NOT_FOUND, \
CIMInstanceName

# test that GLOBALS exist
assert "CONN" in globals()
assert 'SERVER' in globals()
assert 'VERBOSE' in globals()
global CONN # pylint: disable=global-at-module-level


class CIM_FooMethodProvider(MethodProvider):
"""
User test provider for InvokeMethod using CIM_Foo and method1.
This is basis for testing passing of input parameters correctly and
generating some exceptions. It uses only one input parameter where the
value defines the test and one return parameter that provides data from the
provider, normally the value of the parameter defined with the input
parameter. Test for existence of method named method1
"""

provider_classnames = 'CIM_Foo'

def __init__(self, cimrepository):
super(CIM_FooMethodProvider, self).__init__(cimrepository)

def InvokeMethod(self, namespace, MethodName, ObjectName, Params):
"""
Simplistic test method. Validates methodname, objectname, Params
and returns rtn value 0 and one parameter
The parameters and return for Invoke method are defined in
:meth:`~pywbem_mock.MethodProvider.InvokeMethod`
"""
# validate namespace using method in BaseProvider
self.validate_namespace(namespace)

# get classname and validate. This provider uses only one class
if isinstance(ObjectName, six.string_types):
classname = ObjectName
else:
classname = ObjectName.classname
assert classname.lower() == 'cim_foo'

# Test if class exists.
if not self.class_exists(namespace, classname):
raise CIMError(
CIM_ERR_NOT_FOUND,
"class {0} does not exist in CIM repository, "
"namespace {1}".format(classname, namespace))

if isinstance(ObjectName, CIMInstanceName):
instance_store = self.cimrepository.get_instance_store(namespace)
inst = self.find_instance(ObjectName, instance_store, copy=False)
if inst is None:
raise CIMError(
CIM_ERR_NOT_FOUND,
"Instance {0} does not exist in CIM repository, "
"namespace {1}".format(ObjectName, namespace))
# This method expects a single parameter input

return_params = []
if MethodName.lower() == 'fuzzy':
if Params:
if 'TestInOutParameter' in Params:
return_params.append(Params['TestInOutParameter'])

if 'TestRef' in Params:
return_params.append(Params['TestRef'])

return_value = Params.get('OutputRtnValue', 0)

return (return_value, return_params)

raise CIMError(CIM_ERR_METHOD_NOT_AVAILABLE)


# Add the the callback to the mock repository
# pylint: disable=undefined-variable
CONN.register_provider(CIM_FooMethodProvider(CONN.cimrepository), # noqa: F821
CONN.default_namespace, # noqa: F821
verbose=False)

0 comments on commit f39ee84

Please sign in to comment.