U nf@sddlmZddlmZmZddlmZmZddl mZm Z ddl m Z ddl mZddlmZdd lmZdd lmZdd lmZdd lmZdd l mZeddZddZGdddeZdS))md5)loaddumps) b64decode b64encode)rloads)path) OneServer)pickling_support)exc_info)reraise) OrderedDict)open)environcCst|d}t|}||S)Nrt)rrclose) fixture_filefretr:/opt/hc_python/lib/python3.8/site-packages/pyone/tester.pyread_fixture_file s rcCs(t|d}|t||dS)Nwb)rwrite json_dumpsencoder)robjrrrrwrite_fixture_file's rc@sbeZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ dS)OneServerTesteraQ This class extends the OneServer to facilitate unit testing The idea is to be able to "record" fixtures while testing with a live OpenNebula platform. Those recordings can later be use in "replay" mode to simulate an OpenNebula platform. XMLAPI method calls are recorded as test_base/unit_test/method_signature_instance The signature is generated as the md5 of the parameters if several calls with the same signature are doing during the same unit test, instance is incremented. The order in which calls happen within the same unit_test must be deterministic. NcKs|dkrtdd}|dkr0tdddk}|dkrDtdd}t|rZt||_nt|_||_||_ | |t j ||||f|dS)NZPYONE_TEST_FIXTURE_FILEZPYONE_TEST_FIXTURE_REPLAYTrue)1yestrueZPYONE_TEST_FIXTURE_UNITinit) rgetlowerrisfiler _fixturesdict_fixture_replay _fixture_fileset_fixture_unit_testr __init__)selfurisessionrZ fixture_unittimeoutZfixture_replayoptionsrrrr-6s     zOneServerTester.__init__cCs&||jkrt|j|<|j||_dS)z Set the name of the unit test. creates an entry in the fixture tree if it does not exist. :param name: :return: N)r(r)_fixture_unit_test)r.namerrrr,Ps  z%OneServerTester.set_fixture_unit_testcCs(t}t|dd}|||S)NT) sort_keys)rrrupdate hexdigest)r. methodnameparamsZ signature_md5Zsparmsrrr_fixture_signature\s z"OneServerTester._fixture_signaturecCs,|j||||d}|s(td|S)z returns the next fixture for a given call. :param methodname: XMlRPC method :param params: the paramters passed :return: file name were to store to or read from the fixture data rzHCould not read fixture, if the tests changed you must re-record fixtures)r3r:pop Exception)r.r8r9rrrr _get_fixturebszOneServerTester._get_fixturecCsV|||}||jkr"t|j|<||j|kr>g|j||<|j|||dS)z} Will create the fixture for a given call :param methodname: :param params: :return: N)r:r3r)append)r.r8r9object signaturerrr _set_fixtureqs    zOneServerTester._set_fixturec Csd}|jr4|||}d|krttt|dnhzVzt|||}Wn>tk r}z dt t t d i}|W5d}~XYnXW5||||X|S)z Intercepts requests. In record mode they are recorded before being returned. In replay mode they are read from fixtures instead Exceptions are also captured and reraised N exception) r*r=r rrrAr _do_requestr<rrr decode)r.methodr9rrBrrrrDs zOneServerTester._do_requestcCs>t|}t|D] \}}t|tr||||<qt||S)a Parameters will be used to generate the signature of the method, an md5. So we need signatures to be deterministic. There are two sources of randomness - Python version, in particular differences in dealing with encodings - Unsorted sets. This method will add casting transformations to fix those, only required for testing. :param params: :return: a list of parameters that should generate a deterministic md5 signature. )list enumerate isinstancer)_to_ordered_dictr _cast_parms)r.r9ZlparamsiparamrrrrKs  zOneServerTester._cast_parmscCsHt|trDtt|}|D] \}}t|tr"||||<q"|S)zQ deep orders a dictionary :param param: :return: )rIr)rsorteditemsrJ)r.rMkeyvaluerrrrJs   z OneServerTester._to_ordered_dictcCsdS)Ng{Gz?rr.rrrserver_retry_intervalsz%OneServerTester.server_retry_intervalcCs|jst|j|jdS)zQ Unpdates the fixture data if we are recording. :return: N)r*rr+r(rRrrr server_closeszOneServerTester.server_close)NNNN)__name__ __module__ __qualname____doc__r-r,r:r=rArDrKrJrSrTrrrrr,s  rN)hashlibrjsonrrrbase64rrpicklerosrr Ztblibr sysr sixr collectionsrgziprrinstallrrrrrrrs