|
| 1 | +# Description |
| 2 | +# A sample Python REST API script to: |
| 3 | +# - Load a saved configuration file |
| 4 | +# - Enable port analyzer |
| 5 | +# - Set runtime stats |
| 6 | +# - Run traffic |
| 7 | +# - Get stats and evaluate for user defined expected stat values. |
| 8 | +# - Retrieve port capture files to local folder. |
| 9 | +# - Get test result |
| 10 | +# |
| 11 | +# Supports both Windows and Linux gateway Server. If connecting to a |
| 12 | +# |
| 13 | +# If the saved config file is located on a remote pc, this script could upload it to the gateway. |
| 14 | +# Otherwise, the saved config file must be already in the IxLoad API gateway server. |
| 15 | +# |
| 16 | +# Requirements |
| 17 | +# IxL_RestApi.py |
| 18 | +# |
| 19 | +# IxL_RestApi.py and sample scripts by: Hubert Gee |
| 20 | + |
| 21 | +import os, sys, time, signal, traceback, platform |
| 22 | +from keystackEnv import keystackObj |
| 23 | +from IxL_RestApi import * |
| 24 | + |
| 25 | +# Insert the Modules path to env in order to import IxL_RestApi.py |
| 26 | +currentDir = os.path.abspath(os.path.dirname(__file__)) |
| 27 | + |
| 28 | +# Choices of IxLoad Gateway server OS: linux or windows |
| 29 | +serverOs = keystackObj.testcaseConfigParams['configParams']['serverOs'] |
| 30 | + |
| 31 | +# Which IxLoad version are you using for your test? |
| 32 | +# To view all the installed versions, go on a web browser and enter: |
| 33 | +# http://<server ip>:8080/api/v0/applicationTypes |
| 34 | +ixLoadVersion = keystackObj.testcaseConfigParams['configParams']['ixLoadVersion'] |
| 35 | + |
| 36 | +# Do you want to delete the session at the end of the test or if the test failed? |
| 37 | +deleteSession = keystackObj.testcaseConfigParams['configParams']['deleteSession'] |
| 38 | +forceTakePortOwnership = bool(keystackObj.testcaseConfigParams['configParams']['forceTakePortOwnership']) |
| 39 | + |
| 40 | +# API-Key: Use your user API-Key if you want added security |
| 41 | +apiKey = keystackObj.testcaseConfigParams['configParams']['apiKey'] |
| 42 | +if apiKey in ['None', 'none']: apiKey = None |
| 43 | + |
| 44 | +# The saved config file to load located in the ConfigFiles folder |
| 45 | +rxfFile = keystackObj.exportedConfigFullPath |
| 46 | + |
| 47 | +if serverOs == 'windows': |
| 48 | + apiServerIp = keystackObj.moduleProperties['envParams'].get('windowsApiServerIp', None) |
| 49 | + |
| 50 | + # Where to store all of the csv result files in Windows |
| 51 | + resultsDir = keystackObj.moduleProperties['envParams'].get('windowsResultsDir', None) |
| 52 | + |
| 53 | + # Where to upload the config file or where to tell IxLoad the location if you're not uploading it. |
| 54 | + rxfFileOnServer = f'C:\\Results\\{rxfFile}' |
| 55 | + |
| 56 | +if serverOs == 'linux': |
| 57 | + apiServerIp = keystackObj.moduleProperties['envParams'].get('linuxApiServerIp', None) |
| 58 | + |
| 59 | + # Leave the 2 lines as default. For your reference only. |
| 60 | + resultsDir = keystackObj.moduleProperties['envParams'].get('linuxResultsDir', None) |
| 61 | + rxfFileOnServer = f'/mnt/ixload-share/{rxfFile}' |
| 62 | + |
| 63 | +# Where to put the downloaded csv results |
| 64 | +saveResultsInPath = keystackObj.moduleProperties['artifactsRepo'] |
| 65 | + |
| 66 | +# On the local host where you are running this script. |
| 67 | +# The path to the saved config file. In this example, get it from the current folder |
| 68 | +localConfigFileToUpload = rxfFile |
| 69 | + |
| 70 | +# The path where you want to download the csv result files to. This is mostly used if using a Linux Gateway server. |
| 71 | +# If you're using IxLoad in Windows, SSH must be installed. Otherwise, this variable will be ignored. |
| 72 | +scpDestPath = keystackObj.moduleProperties['artifactsRepo'] |
| 73 | + |
| 74 | +# For IxLoad versions prior to 8.50 that doesn't have the rest api to download results. |
| 75 | +# Set to True if you want to save run time stat results to CSV files. |
| 76 | +saveStatsToCsvFile = bool(keystackObj.testcaseConfigParams['configParams']['saveStatsToCsvFile']) |
| 77 | + |
| 78 | +# http=8080. https=8443 (https is supported starting 8.50) |
| 79 | +apiServerIpPort = keystackObj.moduleProperties['envParams']['apiServerIpPort'] |
| 80 | + |
| 81 | +licenseServerIp = keystackObj.moduleProperties['envParams']['licenseServerIp'] |
| 82 | + |
| 83 | +# licenseModel choices: 'Subscription Mode' or 'Perpetual Mode' |
| 84 | +licenseModel = keystackObj.moduleProperties['envParams']['licenseModel'] |
| 85 | + |
| 86 | +# To assign ports for testing. Format = (cardId,portId) |
| 87 | +# Traffic1@Network1 are activity names. |
| 88 | +# To get the Activity names, got to: /ixload/test/activeTest/communityList |
| 89 | +communityPortList1 = { |
| 90 | + 'chassisIp': keystackObj.moduleProperties['envParams']['communityPortList1']['chassisIp'], |
| 91 | + 'Traffic1@Network1': keystackObj.moduleProperties['envParams']['communityPortList1']['Traffic1@Network1'] |
| 92 | +} |
| 93 | + |
| 94 | +communityPortList2 = { |
| 95 | + 'chassisIp': keystackObj.moduleProperties['envParams']['communityPortList2']['chassisIp'], |
| 96 | + 'Traffic2@Network2': keystackObj.moduleProperties['envParams']['communityPortList2']['Traffic2@Network2'] |
| 97 | +} |
| 98 | + |
| 99 | +# Stat names to display at run time. |
| 100 | +# To see how to get the stat names, go to the link below for step-by-step guidance: |
| 101 | +# https://www.openixia.com/tutorials?subject=ixLoad/getStatName&page=fromApiBrowserForRestApi.html |
| 102 | +# |
| 103 | +# What this does: |
| 104 | +# Get run time stats and evaluate the stats with an operator and the expected value. |
| 105 | +# Due to stats going through ramp up and ramp down, stats will fluctuate. |
| 106 | +# Once the stat hits and maintains the expected threshold value, the stat is marked as passed. |
| 107 | +# |
| 108 | +# If evaluating stats at run time is not what you need, use PollStats() instead shown |
| 109 | +# in sample script LoadConfigFile.py |
| 110 | +# |
| 111 | +# operator options: None, >, <, <=, >= |
| 112 | +statsDict = { |
| 113 | + 'HTTPClient': [{'caption': 'TCP Connections Established', 'operator': '>', 'expect': 60}, |
| 114 | + {'caption': 'HTTP Simulated Users', 'operator': '>', 'expect': 100}, |
| 115 | + {'caption': 'HTTP Connections', 'operator': '>', 'expect': 300}, |
| 116 | + {'caption': 'HTTP Transactions', 'operator': '>', 'expect': 190}, |
| 117 | + {'caption': 'HTTP Connection Attempts', 'operator': '>', 'expect': 300} |
| 118 | + ], |
| 119 | + 'HTTPServer': [{'caption': 'TCP Connections Established', 'operator': '>=', 'expect': 1000}, |
| 120 | + {'caption': 'TCP Connection Requests Failed', 'operator': '<', 'expect': 1} |
| 121 | + ] |
| 122 | +} |
| 123 | + |
| 124 | +try: |
| 125 | + restObj = Main(apiServerIp=apiServerIp, |
| 126 | + apiServerIpPort=apiServerIpPort, |
| 127 | + osPlatform=serverOs, |
| 128 | + deleteSession=deleteSession, |
| 129 | + pollStatusInterval=1, |
| 130 | + apiKey=apiKey, |
| 131 | + generateRestLogFile=None, keystackObj=keystackObj) |
| 132 | + |
| 133 | + # sessionId is an opened existing session that you like to connect to instead of starting a new session. |
| 134 | + restObj.connect(ixLoadVersion, sessionId=None, timeout=120) |
| 135 | + |
| 136 | + restObj.configLicensePreferences(licenseServerIp=licenseServerIp, licenseModel=licenseModel) |
| 137 | + |
| 138 | + # The folder to store the results on the IxLoad Gateway server. |
| 139 | + restObj.setResultDir(resultsDir, createTimestampFolder=True) |
| 140 | + |
| 141 | + # uploadConfigFile: None or path to the config file on your local host |
| 142 | + restObj.loadConfigFile(rxfFileOnServer, uploadConfigFile=localConfigFileToUpload) |
| 143 | + |
| 144 | + restObj.assignChassisAndPorts([communityPortList1, communityPortList2]) |
| 145 | + if forceTakePortOwnership: |
| 146 | + restObj.enableForceOwnership(enable=True, enableResetPorts=True) |
| 147 | + |
| 148 | + restObj.enableAnalyzerOnAssignedPorts() |
| 149 | + |
| 150 | + # Optional: Modify the sustain time |
| 151 | + restObj.configTimeline(name='Timeline1', sustainTime=12) |
| 152 | + |
| 153 | + # Example on how to use the configActivityAttribute function to modify |
| 154 | + # some of its attributes. |
| 155 | + restObj.configActivityAttributes(communityName='Traffic1@Network1', |
| 156 | + activityName='HTTPClient1', |
| 157 | + attributes={'userObjectiveValue': 100}) |
| 158 | + |
| 159 | + runTestOperationsId = restObj.runTraffic() |
| 160 | + |
| 161 | + restObj.pollStatsAndCheckStatResults(statsDict, |
| 162 | + csvFile=saveStatsToCsvFile, |
| 163 | + csvFilePrependName=None, |
| 164 | + pollStatInterval=2, |
| 165 | + exitAfterPollingIteration=None) |
| 166 | + |
| 167 | + |
| 168 | + testResult = restObj.getTestResults() |
| 169 | + |
| 170 | + restObj.waitForActiveTestToUnconfigure() |
| 171 | + restObj.downloadResults(targetPath=saveResultsInPath) |
| 172 | + restObj.retrievePortCaptureFileForAssignedPorts(saveResultsInPath) |
| 173 | + |
| 174 | + if deleteSession: |
| 175 | + restObj.deleteSessionId() |
| 176 | + |
| 177 | +except (IxLoadRestApiException, Exception) as errMsg: |
| 178 | + print('\n%s' % traceback.format_exc()) |
| 179 | + keystackObj.logError(errMsg) |
| 180 | + keystackObj.playbookObj.overallSummaryData['exceptionErrors'].append(errMsg) |
| 181 | + if deleteSession: |
| 182 | + restObj.abortActiveTest() |
| 183 | + restObj.deleteSessionId() |
| 184 | + |
| 185 | + sys.exit(errMsg) |
| 186 | + |
| 187 | +except KeyboardInterrupt: |
| 188 | + print('\nCTRL-C detected.') |
| 189 | + keystackObj.logError('CTRL-C detected') |
| 190 | + if deleteSession: |
| 191 | + restObj.abortActiveTest() |
| 192 | + restObj.deleteSessionId() |
| 193 | + |
| 194 | + |
0 commit comments