/* -------------------------------------------------------------------------- */ /* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */ /* */ /* Licensed under the Apache License, Version 2.0 (the "License"); you may */ /* not use this file except in compliance with the License. You may obtain */ /* a copy of the License at */ /* */ /* http://www.apache.org/licenses/LICENSE-2.0 */ /* */ /* Unless required by applicable law or agreed to in writing, software */ /* distributed under the License is distributed on an "AS IS" BASIS, */ /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ /* See the License for the specific language governing permissions and */ /* limitations under the License. */ /* -------------------------------------------------------------------------- */ #include #include #include #include #include "ObjectXML.h" #include "test/OneUnitTest.h" /* ************************************************************************* */ /* ************************************************************************* */ class ObjectXMLTest : public OneUnitTest { CPPUNIT_TEST_SUITE( ObjectXMLTest ); CPPUNIT_TEST( xpath_access ); CPPUNIT_TEST( node_constructor ); CPPUNIT_TEST( doc_update ); CPPUNIT_TEST( requirements ); CPPUNIT_TEST( rank ); CPPUNIT_TEST( xpath ); CPPUNIT_TEST( xpath_value ); CPPUNIT_TEST_SUITE_END (); public: void setUp() { xmlInitParser(); }; void tearDown() { xmlCleanupParser(); }; ObjectXMLTest(){}; ~ObjectXMLTest(){}; /* ********************************************************************* */ /* ********************************************************************* */ void xpath_access() { try { ObjectXML obj(xml_history_dump); vector hostnames; hostnames = obj["/VM_POOL/VM/HISTORY/HOSTNAME"]; CPPUNIT_ASSERT(hostnames.size() == 2); CPPUNIT_ASSERT(hostnames[0] == "A_hostname"); CPPUNIT_ASSERT(hostnames[1] == "C_hostname"); } catch(runtime_error& re) { cerr << re.what() << endl; CPPUNIT_ASSERT(1 == 0); } }; void xpath_value() { int rc; string im_mad; rc = ObjectXML::xpath_value(im_mad,host.c_str(),"/HOST/IM_MAD"); CPPUNIT_ASSERT(rc == 0); CPPUNIT_ASSERT(im_mad == "im_kvm"); rc = ObjectXML::xpath_value(im_mad,host.c_str(),"/HOST/NO_IM_MAD"); CPPUNIT_ASSERT(rc == -1); }; void xpath() { try { ObjectXML obj(xml_history_dump); string str_exists; string str_no_exists; int int_exists; int int_no_exists; int int_malformed; int rc; rc = obj.xpath(str_exists,"/VM_POOL/VM/HISTORY/HOSTNAME","default_host"); CPPUNIT_ASSERT(str_exists == "A_hostname"); CPPUNIT_ASSERT(rc == 0); rc = obj.xpath(str_no_exists,"/VM_POOL/NOT_AN_ELEMENT","default_host"); CPPUNIT_ASSERT(str_no_exists == "default_host"); CPPUNIT_ASSERT(rc == -1); rc = obj.xpath(int_exists,"/VM_POOL/VM/STATE",35); CPPUNIT_ASSERT(int_exists == 1); CPPUNIT_ASSERT(rc == 0); rc = obj.xpath(int_no_exists,"/VM_POOL/NOT_AN_ELEMENT",35); CPPUNIT_ASSERT(int_no_exists == 35); CPPUNIT_ASSERT(rc == -1); rc = obj.xpath(int_malformed,"/VM_POOL/VM/USERNAME",33); CPPUNIT_ASSERT(int_malformed == 33); CPPUNIT_ASSERT(rc == -1); } catch(runtime_error& re) { cerr << re.what() << endl; CPPUNIT_ASSERT(1 == 0); } }; void node_constructor() { try { ObjectXML obj(xml_history_dump); vector vms; int num_vms; num_vms = obj.get_nodes("/VM_POOL/VM",vms); CPPUNIT_ASSERT(num_vms == 3); ObjectXML obj_vm_1(vms[0]); ObjectXML obj_vm_2(vms[1]); vector results; results = obj_vm_1["/VM/HISTORY/HOSTNAME"]; CPPUNIT_ASSERT(results.size() == 0); results.clear(); results = obj_vm_2["/VM/HISTORY/HOSTNAME"]; CPPUNIT_ASSERT(results.size() == 1); CPPUNIT_ASSERT(results[0] == "A_hostname"); obj.free_nodes(vms); } catch(runtime_error& re) { cerr << re.what() << endl; CPPUNIT_ASSERT(1 == 0); } }; void doc_update() { try { ObjectXML obj(xml_history_dump); vector hostnames; hostnames = obj["/VM_POOL/VM/HISTORY/HOSTNAME"]; CPPUNIT_ASSERT(hostnames.size() == 2); CPPUNIT_ASSERT(hostnames[0] == "A_hostname"); CPPUNIT_ASSERT(hostnames[1] == "C_hostname"); obj.update_from_str(xml_history_dump2); hostnames = obj["/VM_POOL/VM/HISTORY/HOSTNAME"]; CPPUNIT_ASSERT(hostnames.size() == 2); CPPUNIT_ASSERT(hostnames[0] == "0_hostname"); CPPUNIT_ASSERT(hostnames[1] == "1_hostname"); } catch(runtime_error& re) { cerr << re.what() << endl; CPPUNIT_ASSERT(1 == 0); } }; void requirements() { try { ObjectXML obj(host); char* err; bool res; int rc; // ----------------------------------------------------------------- // REQUIREMENTS MATCHING // ----------------------------------------------------------------- string reqs[] = { "TOTALCPU = 800", // exact value matching "TOTALCPU != 800", // Not equal "TOTALCPU > 5", // Greater than expr. "! (TOTALCPU > 5)", // Not exp. "HOSTNAME = \"ursa12\"", // Exact string matching "HOSTNAME = \"ursa*\"", // Shell wildcard "HOSTNAME = \"ursa\"", "HID = 1", "ARCH = \"*64*\"", "RUNNING_VMS < 100", "CLUSTER = \"cluster A\"", "CLUSTER = \"default\"", "CLUSTER = clusterA", "CLUSTER = \"Cluster A\"", /* // Boolean operators "HOSTNAME = \"ursa*\" & NETRX = \"13335836573\"", // Operator precedence "HOSTNAME = \"ursa*\" | HOSTNAME = \"no\" & HOSTNAME = \"no\"", "( HOSTNAME = \"ursa*\" | HOSTNAME = \"no\" ) & HOSTNAME = \"no\"", //*/ "END" }; bool results[] = { true, false, true, false, true, true, false, true, true, true, true, false, false, false /* true, true, false, //*/ }; int i = 0; while( reqs[i] != "END" ) { // cout << endl << i << " - " << reqs[i]; rc = obj.eval_bool( reqs[i], res, &err ); // cout << "··· rc: " << rc << " result: " << res << " expected: " << results[i] << endl; CPPUNIT_ASSERT( rc == 0 ); CPPUNIT_ASSERT( res == results[i] ); i++; } // Non-existing attribute compared to string value rc = obj.eval_bool( "FOO = \"BAR\"", res, &err ); CPPUNIT_ASSERT( rc == 0 ); CPPUNIT_ASSERT( res == false ); // Non-existing attribute compared to numeric value rc = obj.eval_bool( "FOO = 123", res, &err ); CPPUNIT_ASSERT( rc == 0 ); CPPUNIT_ASSERT( res == false ); // Existing string attribute compared to numeric value rc = obj.eval_bool( "HOSTNAME = 123 ", res, &err ); CPPUNIT_ASSERT( rc == 0 ); CPPUNIT_ASSERT( res == false ); // Existing numeric attribute compared to string value should work rc = obj.eval_bool( "TOTALCPU = \"800\"", res, &err ); CPPUNIT_ASSERT( rc == 0 ); CPPUNIT_ASSERT( res == true ); // Bad syntax // TODO: Right now eval_bool returns 1 in case of error, and result // is not set to false. rc = obj.eval_bool( "TOTALCPU ^ * - = abc", res, &err ); CPPUNIT_ASSERT( rc != 0 ); CPPUNIT_ASSERT( res == false ); if (err != 0) { free( err ); } } catch(runtime_error& re) { cerr << re.what() << endl; CPPUNIT_ASSERT(1 == 0); } }; void rank() { try { ObjectXML obj(host); char* err; int res; int rc; // ----------------------------------------------------------------- // RANK EXPRESSIONS // ----------------------------------------------------------------- string rank_exp[] = { "RUNNING_VMS", // Single attribute "MAX_CPU + NETTX", // Simple operations "RUNNING_VMS * 10", // Operations with fixed values "- FREE_MEM", // Unary operator "2 + 4 * 10", // Operator precedence "(2 + 4) * 10", "END" }; int results[] = { 12, 47959 + 800, 12 * 10, -7959232, 2 + 4 * 10, (2 + 4) * 10, }; int i = 0; while( rank_exp[i] != "END" ) { // cout << endl << i << " - " << rank_exp[i]; rc = obj.eval_arith( rank_exp[i], res, &err ); // cout << "··· rc: " << rc << " res: " << res << " expected: " << results[i] << endl; CPPUNIT_ASSERT( rc == 0 ); CPPUNIT_ASSERT( res == results[i] ); i++; } // Non-existing attribute rc = obj.eval_arith( "FOO", res, &err ); CPPUNIT_ASSERT( rc == 0 ); CPPUNIT_ASSERT( res == 0 ); // Non-existing attribute and operators rc = obj.eval_arith( "FOO + 10", res, &err ); CPPUNIT_ASSERT( rc == 0 ); CPPUNIT_ASSERT( res == 10 ); } catch(runtime_error& re) { cerr << re.what() << endl; CPPUNIT_ASSERT(1 == 0); } }; static const string xml_history_dump; static const string xml_history_dump2; static const string host; }; /* ************************************************************************* */ /* ************************************************************************* */ int main(int argc, char ** argv) { return OneUnitTest::main(argc, argv, ObjectXMLTest::suite()); } // ---------------------------------------------------------------------------- const string ObjectXMLTest::xml_history_dump = "00one_user_test" "VM one01" "000000000000" "000" "010" "one_user_testSecond VM0" "200000000000" "000" "000" "A_hostname000" "0000" "0002" "0one_user_testVM one" "020" "000000000000" "0001" "C_hostname200" "0000" "000" ""; const string ObjectXMLTest::xml_history_dump2 = "00one_user_test" "VM one01" "000000000000" "000" "010" "one_user_testSecond VM0" "200000000000" "000" "000" "0_hostname000" "0000" "0002" "0one_user_testVM one" "020" "000000000000" "0001" "1_hostname200" "0000" "000" ""; const string ObjectXMLTest::host = "" "1" "ursa12" "2" "im_kvm" "vmm_kvm" "tm_shared" "1273799044" "cluster A" "" " 1" " 0" " 0" " 0" " 0" " 8194368" " 800" " 0" " 7959232" " 800" " 0" " 523080" " 0" " 12" "" "" "";