Skip to content

Commit

Permalink
Implement mock of WBEMConnection to create a local WBEM
Browse files Browse the repository at this point in the history
server simulator for unit tests of pywbem and for users
of pywbem. This implements
mocks of all of the WBEMConnection methods that contact
the wbem server and  a local repository so that thee mocked methods
that respond to the WBEM requests with data from the repository.

It mocks a WBEM server so that the WBEM operations in
WBEMConnection can be run against data that the mocker adds
to a temporary repository.  This mocker depends on the
user creating classes, qualifier declarations, and instances
in the temporary repository that the operations can use
as the source of data to return with request responses.

This is not a complete implementation of a WBEMserver and
therefore creates a number of simplifications. Including:

1. For instance operations, DeepInheritance only works
correctly if there are correct cim classes. A flag (repo_lite)
allows the user to create instances only in the repository
to simplify test building and to respond without any
knowledge of subclasses since class hiearchy is available
only in class definitions, not in instances.

2. We have simplified the references and associators
operations.  At least some of the operation will operate
without haveing a full class and instance repository (repo_lite) so that
tests can be created with very simplified repositories. However
some of the instance operations (ex. CreateInstance) absolutely
require correct classes and qualifier declarations to execute.

3. The pull operations have a limitation in that this mock
WBEMConnection does not implement the filter query language (i.e. FQL)
so that the filtering that would normally be provided by FQL of
the returned instances is ignored and the same set of instances that
the corresponding non-pull request returns are returned for the Open...
and Pull... requests.

The mocker includes methods to allow adding
instances, classes, and qualifier declarations directly
to the mock repository.  They may be added directly as CIM
objects with the add_cimobjects method or compiled from mof files or
mof strings using the compile_mof and compile_mof_str methods.

NOT DONE:

1. Full set of tests on associations and the corresponding pulls. Main
 paths tested but we need more tests for variations on input
 parameters and errors especially of associations.

2. ExecQuery - See separate issue for addition of this function. issue 871

3. InvokeMethod -We agreed to leave this until we have merged the majority
of the work. The mocker for invokemethod will probably be based on a
callback to a user defined function that lets the user emulate the server
invokemethod. See Issue 871

4. Compiler install of objects. We are regularly using compile_str in
tests but need more tests on compile_mof_file. Also needs more tests for
things like multiple additive compiles, compiles with duplication
errors, etc.

5. Documentation. The documentation needs review.  It also needs more
examples. Andy reviewing documentation

6. Pulls for references and associators do not have tests

7. Extend build from mof to create paths when compiler does not for
instances going into the repository.

8. Refactor createInstance and modifyInstance to use common code.
  • Loading branch information
KSchopmeyer committed Jan 5, 2018
1 parent bcd6c36 commit 95f8d39
Show file tree
Hide file tree
Showing 12 changed files with 6,085 additions and 74 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ include README.rst
include INSTALL.md
include *.py
include pywbem/*.py
include pywbem_mock/*.py
9 changes: 9 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ Enhancements
* Docs: Clarified that the instance path returned by the `CreateInstance()`
operation method has classname, keybindings and namespace set.

* Added capability to mock WBEM Operations so that both pywbem and pywbem
users can create unit tests without requiring a working WBEM Server,
This feature allows the user to create CIM objects
in a mock WBEM Server defined with the class FakedWBEMConnection and
substitute that class for WBEMConnection to create a mock WBEM Server
that responds to wbem operations.
This enhancement is documented in the pywbem documentation section 10,
MockSupport. . See issue #838.

Bug fixes
^^^^^^^^^

Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The general pywbem web site is: http://pywbem.github.io/pywbem/index.html.
compiler.rst
utilities.rst
development.rst
mocksupport.rst
appendix.rst
changes.rst

429 changes: 429 additions & 0 deletions docs/mocksupport.rst

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# pip (in the active Python environment)
# twine
#
# Optional environment ariables/command line variables
# Optional environment variables/command line variables
# PYWBEM_COVERAGE_REPORT - When set, forces coverage to create temporary
# annotated html output html files showing lines covered and missed
# See the directory coverage_html for the html output.
Expand Down Expand Up @@ -54,6 +54,8 @@ endif
# Name of this Python package
package_name := pywbem

mock_package_name := pywbem_mock

# Determine if coverage details report generated
# The variable can be passed in as either an environment variable or
# command line variable. When set, generates a set of reports of the
Expand Down Expand Up @@ -133,6 +135,8 @@ doc_dependent_files := \
$(package_name)/_server.py \
$(package_name)/_statistics.py \
$(package_name)/config.py \
$(mock_package_name)/__init__.py \
$(mock_package_name)/_wbemconnection_mock.py\
wbemcli.py \

# PyLint config file
Expand All @@ -149,6 +153,7 @@ py_src_files := \
wbemcli \
wbemcli.py \
mof_compiler \
$(wildcard $(mock_package_name)/*.py) \

# Test log
test_log_file := test_$(python_mn_version).log
Expand All @@ -164,6 +169,7 @@ dist_manifest_in_files := \
INSTALL.md \
*.py \
$(package_name)/*.py \
$(mock_package_name)/*.py \

# Files that are dependents of the distribution archive.
# Keep in sync with dist_manifest_in_files.
Expand All @@ -173,6 +179,7 @@ dist_dependent_files := \
INSTALL.md \
$(wildcard *.py) \
$(wildcard $(package_name)/*.py) \
$(wildcard $(mock_package_name)/*.py) \

# No built-in rules needed:
.SUFFIXES:
Expand Down Expand Up @@ -460,7 +467,7 @@ endif
$(test_log_file): makefile $(package_name)/*.py testsuite/*.py coveragerc
@echo "makefile: Running tests"
rm -f $(test_log_file)
bash -c "set -o pipefail; PYTHONWARNINGS=default py.test --cov $(package_name) $(coverage_report) --cov-config coveragerc --ignore=attic --ignore=releases --ignore=testsuite/testclient -s 2>&1 |tee $(test_tmp_file)"
bash -c "set -o pipefail; PYTHONWARNINGS=default py.test --cov $(package_name) --cov $(mock_package_name) $(coverage_report) --cov-config coveragerc --ignore=attic --ignore=releases --ignore=testsuite/testclient -s 2>&1 |tee $(test_tmp_file)"
mv -f $(test_tmp_file) $(test_log_file)
@echo "makefile: Done running tests; Log file: $@"

Expand Down
2 changes: 2 additions & 0 deletions pywbem/cim_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7114,6 +7114,7 @@ class (or subclasses).
objects = []
else:
objects = [x[2] for x in result[0][2]]
print('cim_operations associators objects %r' % objects)
if not isinstance(objectname, CIMInstanceName):
for classpath, klass in objects:
klass.path = classpath
Expand Down Expand Up @@ -7438,6 +7439,7 @@ class (or subclasses).
objects = []
else:
objects = [x[2] for x in result[0][2]]
print('cim_operations references objects %r' % objects)
if not isinstance(objectname, CIMInstanceName):
for classpath, klass in objects:
klass.path = classpath
Expand Down
29 changes: 29 additions & 0 deletions pywbem_mock/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# (C) Copyright 2017 IBM Corp.
# (C) Copyright 2017 Inova Development Inc.
# All Rights Reserved
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#

"""
pywbem_mock - Unit test support for users of pywbem package that mocks
the implementation of the WBEMConnection calls to WBEM Servers.
"""

from __future__ import absolute_import

from ._wbemconnection_mock import * # noqa: F403,F401
Loading

0 comments on commit 95f8d39

Please sign in to comment.