📄 testclient.cpp
字号:
//%2006//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation, The Open Group.// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; Symantec Corporation; The Open Group.//// 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.////==============================================================================//// Author: Mike Brasher (mbrasher@bmc.com)//// Modified By: Karl Schopmeyer (k.schopmeyer@opengroup.org)// Mike Day (mdday@us.ibm.com)// Jenny Yu (jenny_yu@hp.com)// Bapu Patil ( bapu_patil@hp.com )// Warren Otsuka (warren_otsuka@hp.com)// Nag Boranna(nagaraja_boranna@hp.com)// Carol Ann Krug Graves, Hewlett-Packard Company// (carolann_graves@hp.com)// Amit K Arora, IBM (amita@in.ibm.com) for PEP#101// Aruran, IBM (ashanmug@in.ibm.com) for Bug# 2628////%/////////////////////////////////////////////////////////////////////////////#include <Pegasus/Common/Thread.h>#include <Pegasus/Common/Config.h>#include <Pegasus/Common/Constants.h>#include <Pegasus/Common/PegasusVersion.h>#include <Pegasus/Common/PegasusAssert.h>#include <Pegasus/Client/CIMClient.h>#include <Pegasus/Common/CIMName.h>#include <Pegasus/Common/OptionManager.h>#include <Pegasus/Common/FileSystem.h>#include <Pegasus/Common/Stopwatch.h>#include <Pegasus/Common/Exception.h>#include <Pegasus/Common/XmlWriter.h>#include <Pegasus/Common/AutoPtr.h>#if !defined(PEGASUS_OS_ZOS) && ! defined(PEGASUS_OS_HPUX) && !defined(PEGASUS_OS_LINUX) && !defined(PEGASUS_OS_AIX)// Rempve SLP #include <slp/slp.h>#endif/** Here, exit(2) is being used to signal a timeout exception, and exit(1) to any other exception, everywhere an exception is required to exit. The exit(0), is being used to signal that TestClient terminated normally*/PEGASUS_USING_PEGASUS;PEGASUS_USING_STD;CIMNamespaceName globalNamespace = CIMNamespaceName ("root/cimv2");static const CIMNamespaceName __NAMESPACE_NAMESPACE = CIMNamespaceName ("root");static const char* programVersion = "2.0";static AtomicInt errorCount(0);/** Thread Parameters Class*/class T_Parms{ public: AutoPtr<CIMClient> client; //PEP101 int verboseTest; int activeTest; int testCount; int uniqueID;};/** ErrorExit - Print out the error message as an and get out. @param - Text for error message @return - None, Terminates the program @execption - This function terminates the program ATTN: Should write to stderr*/void ErrorExit(const String& message){ cout << message << endl; exit(1);}/* Status display of the various steps. Shows message of function andtime to execute. Grow this to a class so we have start and stop and timedisplay with success/failure for each function.*/static void testStart(const String& message){ cout << "++++ " << message << " ++++" << endl;}static void testEnd(const double elapsedTime){ cout << "In " << elapsedTime << " Seconds\n\n";}/*****************************************************************// Testing Namespace operations******************************************************************/static void TestNameSpaceOperations(CIMClient* client, Boolean activeTest, Boolean verboseTest, String uniqueID){ // Get all namespaces for display using the __Namespaces function. CIMName className = "__NameSpace"; Array<CIMNamespaceName> namespaceNames; // Build the namespaces incrementally starting at the root // ATTN: 20030319 KS today we start with the "root" directory but this is wrong. We should be // starting with null (no directory) but today we get an xml error return in Pegasus // returned for this call. Note that the specification requires that the root namespace be used // when __namespace is defined but does not require that it be the root for allnamespaces. That // is a hole is the spec, not in our code. namespaceNames.append("root"); Uint32 start = 0; Uint32 end = namespaceNames.size(); do { // for all new elements in the output array for (Uint32 range = start; range < end; range ++) { // Get the next increment in naming for all a name element in the array Array<CIMInstance> instances = client->enumerateInstances(namespaceNames[range], className); for (Uint32 i = 0 ; i < instances.size(); i++) { Uint32 pos; // if we find the property and it is a string, use it. if ((pos = instances[i].findProperty("name")) != PEG_NOT_FOUND) { CIMValue value; String namespaceComponent; value = instances[i].getProperty(pos).getValue(); if (value.getType() == CIMTYPE_STRING) { value.get(namespaceComponent); String ns = namespaceNames[range].getString(); ns.append("/"); ns.append(namespaceComponent); namespaceNames.append(ns); } } } start = end; end = namespaceNames.size(); } } while (start != end); // Validate that all of the returned entities are really namespaces. It is legal for us to // have an name component that is really not a namespace (ex. root/fred/john is a namespace // but root/fred is not. // There is no clearly defined test for this so we will simply try to get something, in this // case a wellknown assoication Array<CIMNamespaceName> returnNamespaces; for (Uint32 i = 0 ; i < namespaceNames.size() ; i++) { try { CIMQualifierDecl cimQualifierDecl; cimQualifierDecl = client->getQualifier(namespaceNames[i], "Association"); returnNamespaces.append(namespaceNames[i]); } catch(CIMException& e) { if (e.getCode() != CIM_ERR_INVALID_NAMESPACE) returnNamespaces.append(namespaceNames[i]); } } cout << returnNamespaces.size() << " namespaces " << " returned." << endl; for( Uint32 cnt = 0 ; cnt < returnNamespaces.size(); cnt++ ) { cout << returnNamespaces[cnt] << endl;; } // ATTN: The following code is probably no good. KS April 2003 // If conducting active test, try to create and delete a namespace. if(activeTest) { String testNamespace = uniqueID.append("_Namespace"); if(verboseTest) cout << "Conducting Create / Delete namespace test " << endl; // Build the instance name for __namespace CIMNamespaceName testNamespaceName = CIMNamespaceName (testNamespace); String instanceName = className.getString(); instanceName.append( ".Name=\""); instanceName.append(testNamespaceName.getString()); instanceName.append("\""); if(verboseTest) { cout << "Creating " << instanceName << endl; } //if(testNamespace) Add the test for existance code here ATTN //{ // instanceNames //} // Create the new instance CIMObjectPath newInstanceName; try { // Build the new instance CIMName name = "__NameSpace"; //CIMInstance newInstance(instanceName); CIMInstance newInstance(name); newInstance.addProperty(CIMProperty(CIMName ("name"), testNamespaceName.getString())); newInstanceName = client->createInstance(__NAMESPACE_NAMESPACE, newInstance); } catch(CIMException& e) { if (e.getCode() == CIM_ERR_ALREADY_EXISTS) { newInstanceName = CIMObjectPath(instanceName); } else { PEGASUS_STD(cerr) << "CIMException NameSpace Creation: " << e.getMessage() << " Creating " << instanceName << PEGASUS_STD(endl); errorCount++; return; } } catch(Exception& e) { PEGASUS_STD(cerr) << "Exception NameSpace Creation: " << e.getMessage() << PEGASUS_STD(endl); errorCount++; return; } // Now try getting the instance. try { client->getInstance(__NAMESPACE_NAMESPACE, newInstanceName); } catch(Exception& e) { PEGASUS_STD(cerr) << "Exception NameSpace getInstance: " << e.getMessage() << " Retrieving " << instanceName << PEGASUS_STD(endl); errorCount++; return; } // Now delete the namespace try { CIMObjectPath myReference(instanceName); if(verboseTest) cout << "Deleting namespace = " << instanceName << endl; client->deleteInstance(__NAMESPACE_NAMESPACE, myReference); } catch(Exception& e) { PEGASUS_STD(cerr) << "Exception NameSpace Deletion: " << e.getMessage() << " Deleting " << instanceName << PEGASUS_STD(endl); errorCount++; return; } }}static void TestEnumerateClassNames (CIMClient* client, Boolean activeTest, Boolean verboseTest, String uniqueID){ try { Boolean deepInheritance = true; CIMName className; Array<CIMName> classNames = client->enumerateClassNames( globalNamespace, className, deepInheritance); if (verboseTest) { for (Uint32 i = 0, n = classNames.size(); i < n; i++) cout << classNames[i] << endl; } cout << classNames.size() << " ClassNames" << endl; } catch(Exception& e) { cout << "Error NameSpace Enumeration:" << endl; cout << e.getMessage() << endl; errorCount++; }}static void TestGetClass(CIMClient* client, Boolean activeTest, Boolean verboseTest, String uniqueID){ CIMClass c = client->getClass( globalNamespace, CIMName ("CIM_ComputerSystem"), false, false, true); XmlWriter::printClassElement(c);}/* This is both an active and passive class test and so uses the activetest variable*/static void TestClassOperations(CIMClient* client, Boolean ActiveTest, Boolean verboseTest, String uniqueID) { // Name of Class to use in create/delete test String cimName = uniqueID.append("_").append("PEG_TestSubClass"); CIMName testClass(cimName); // NOte that this creates a subclass of ManagedElement so will fail if // if managedelement not intalled. //Test for class already existing Array<CIMName> classNames = client->enumerateClassNames( globalNamespace, CIMName ("CIM_ManagedElement"), false); if (ActiveTest) { if (verboseTest) { cout << "Test to create, modify and delete test class " << testClass << endl; } for (Uint32 i = 0; i < classNames.size(); i++) { if (classNames[i].equal(testClass)) try { client->deleteClass(globalNamespace, testClass); } catch (CIMException& e) { cout << "TestClass " << testClass << " delete failed " << e.getMessage() << endl; errorCount++; return; } } // CreateClass: CIMClass c1(testClass, CIMName ("CIM_ManagedElement")); c1.addProperty(CIMProperty(CIMName ("count"), Uint32(99))); c1.addProperty(CIMProperty(CIMName ("ratio"), Real64(66.66))); c1.addProperty(CIMProperty(CIMName ("message"), String("Hello World"))); try { client->createClass(globalNamespace, c1); } catch (CIMException& e) { CIMStatusCode code = e.getCode(); if (code == CIM_ERR_ALREADY_EXISTS) cout << "TestClass " << testClass << " already exists during create " << endl; else { cout << "TestClass " << testClass << " create failed " << e.getMessage() << endl; errorCount++; return; } } // GetClass: CIMClass c2 = client->getClass(globalNamespace, testClass, true); if (!c1.identical(c2)) { cout << "Class SubClass Returned not equal to created" << endl; } // ATTN: This test should be uncommented when the repository implements // the localOnly flag. //PEGASUS_TEST_ASSERT(c1.identical(c2)); // Modify the class: c2.removeProperty(c2.findProperty(CIMName ("message"))); try { client->modifyClass(globalNamespace, c2); } catch (CIMException& e) { cout << "Testclass Modification failed " << e.getMessage() << endl; errorCount++; return; } // GetClass: CIMClass c3 = client->getClass(globalNamespace, testClass, true); if (!c3.identical(c2)) { cout << "Test Failed. Rtned class c3 not equal to c2" << endl;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -