Search
Enter Keywords:
Thursday, 28 August 2008
Home arrow Home arrow Testing with pywbem
Testing with pywbem
Written by Dan   
Thursday, 13 December 2007
Recently a colleague pointed out that our VirtualSystemManagementService's RemoveResourceSettings method (in libvirt-cim) was marked with the wrong type.  This was done early on, and probably was just a mistake, given that AddResourceSettings and ModifyResourceSettings both took the same type: an EmbeddedObject.  RemoveResourceSettings takes a reference, since anything you could possibly delete would be representable as a reference.  It's the right way, but annoying, given that I had already built a common resource management infrastructure around the idea of having a list of resource objects to (Add,Modify,Remove).

Anyway, I coded up the change to the provider itself and set out to test it.  I quickly realized that I didn't know what the CIMXML for a method call with an array of references looked like.  Up to this point, I have been unit testing the method providers by just hacking up a CIMXML template and stuffing it into wbemexec or wbemcat.  It sucks, but it works.

However, this time I was stuck.  I didn't have an example, and some guessing didn't work in a short amount of time.  I decided to try to work up something in pywbem to see if I could get not only the method call made for testing, but also a glimpse at what the CIMXML for it looked like.  I ended up with this very small bit of python code that was extremely helpful:
from pywbem import WBEMConnection, CIMInstanceName

c = WBEMConnection("http://localhost",
                   ("root", "password"),
                   "root/virt")
c.debug = True

r = CIMInstanceName("Xen_DiskResourceAllocationSettingData",
                    keybindings={"InstanceID":"pv0/xvda"})

res = c.InvokeMethod("RemoveResourceSettings",
                     "Xen_VirtualSystemManagementService",
                     ResourceSettings=[r])

print c.last_request
This connects to localhost, creates a reference, and makes the method call with that reference as a single-element array.  After the operation, the "last_request" field contains the CIMXML it sent to the server, but only if the "debug" property is set to True (something the documentation conveniently leaves out).  The result (with the boiler-plate cut out) looks like this:
<PARAMVALUE NAME="ResourceSettings" PARAMTYPE="reference">
  <VALUE.REFARRAY>
    <VALUE.REFERENCE>
      <INSTANCENAME CLASSNAME="Xen_DiskResourceAllocationSettingData">
        <KEYBINDING NAME="InstanceID">
          <KEYVALUE VALUETYPE="string">
            pv0/xvda
          </KEYVALUE>
        </KEYBINDING>
      </INSTANCENAME>
    </VALUE.REFERENCE>
  </VALUE.REFARRAY>
</PARAMVALUE>
Not that I really need it at this point, given that the pywbem case was so easy to write.  However, it provides a good example of how to not only use pywbem to figure out the proper CIMXML for an operation, but also debug it if it's wrong.
Comments
Add NewSearch
Write comment
Name:
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
Security Image
Please input the anti-spam code that you can read in the image.

Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved.

 
© 2008 danplanet
Joomla! is Free Software released under the GNU/GPL License.