Skip to content

Commit

Permalink
A top level API for loading and deleting test configuration. Needed s…
Browse files Browse the repository at this point in the history
…ome modifications in model spec.
  • Loading branch information
pmajumdarKS committed Aug 17, 2024
1 parent e9fbfd3 commit b6c612c
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 62 deletions.
8 changes: 4 additions & 4 deletions cyperf-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,9 @@ components:
properties:
Config:
$ref: '#/components/schemas/Config'
Name:
description: The name of the configuration
type: string
SessionID:
description: The unique identifier of the session this configuration belongs
to
Expand All @@ -712,9 +715,6 @@ components:
description: The unique identifier of the CyPerf configuration
readOnly: 'true'
type: string
name:
description: The name of the configuration
type: string
type: object
ArchiveInfo:
properties:
Expand Down Expand Up @@ -5683,7 +5683,7 @@ components:
enum:
- RANDOM
- PSEUDORANDOM
- <nil>
- 'null'
type: string
StreamProfile:
properties:
Expand Down
8 changes: 4 additions & 4 deletions cyperf/models/appsec_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ class AppsecConfig(BaseModel):
AppsecConfig
""" # noqa: E501
config: Optional[Config] = Field(default=None, alias="Config")
name: Optional[StrictStr] = Field(default=None, description="The name of the configuration", alias="Name")
session_id: Optional[StrictStr] = Field(default=None, description="The unique identifier of the session this configuration belongs to", alias="SessionID")
template_id: Optional[StrictStr] = Field(default=None, description="The unique identifier of the CyPerf configuration template from which this configuration was created", alias="TemplateID")
data_model_version: Optional[StrictStr] = Field(default=None, description="The version of the data model used for this configuration", alias="dataModelVersion")
id: Optional[StrictStr] = Field(default=None, description="The unique identifier of the CyPerf configuration")
name: Optional[StrictStr] = Field(default=None, description="The name of the configuration")
__properties: ClassVar[List[str]] = ["Config", "SessionID", "TemplateID", "dataModelVersion", "id", "name"]
__properties: ClassVar[List[str]] = ["Config", "Name", "SessionID", "TemplateID", "dataModelVersion", "id"]

model_config = ConfigDict(
populate_by_name=True,
Expand Down Expand Up @@ -98,11 +98,11 @@ def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:

_obj = cls.model_validate({
"Config": Config.from_dict(obj["Config"]) if obj.get("Config") is not None else None,
"Name": obj.get("Name"),
"SessionID": obj.get("SessionID"),
"TemplateID": obj.get("TemplateID"),
"dataModelVersion": obj.get("dataModelVersion"),
"id": obj.get("id"),
"name": obj.get("name")
"id": obj.get("id")
})
return _obj

Expand Down
2 changes: 1 addition & 1 deletion cyperf/models/stream_payload_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class StreamPayloadType(str, Enum):
"""
RANDOM = 'RANDOM'
PSEUDORANDOM = 'PSEUDORANDOM'
LESS_THAN_NIL_GREATER_THAN = '<nil>'
NULL = 'null'

@classmethod
def from_json(cls, json_str: str) -> Self:
Expand Down
77 changes: 77 additions & 0 deletions cyperf/utils/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import cyperf
import urllib3
import requests
from cyperf.rest import ApiException
from pprint import pprint

import time

class TestConfig(object):
def __init__(self, configFile, agentMapping, objectiveType, objectiveValue, verifyRules):
self.file = configFile
self.agents = agentMapping
self.objective = {
'type' : objectiveType,
'value' : objectiveValue
}
self.statRules = []

class TestRunner(object):
WAP_CLIENT_ID = 'clt-wap'

def __init__(self, controller, username, password):
self.host = f'https://{controller}'
self.username = username
self.password = password

self.configuration = cyperf.Configuration(host = self.host)
self.configuration.access_token = self.__authorize__()
self.configuration.verify_ssl = False
self.apiClient = cyperf.ApiClient(self.configuration)

def __authorize__(self):
session = requests.Session()
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
session.verify = False

url = f'{self.host}/auth/realms/keysight/protocol/openid-connect/token'
headers = {"content-type" : "application/x-www-form-urlencoded"}
payload = {"username" : self.username,
"password" : self.password,
"grant_type" : "password",
"client_id" : TestRunner.WAP_CLIENT_ID}
response = session.post(url, headers = headers, data = payload, verify = False)
if response.headers.get('content-type') == 'application/json':
return response.json()['access_token']
else:
raise Exception(f'Cannot generate access token for {self.host}')

def Authorize(self):
self.configuration.access_token = self.__authorize__()

def LoadConfigs(self, configFiles):
apiInstance = cyperf.ConfigurationsApi(self.apiClient)
results = []
for cFile in configFiles:
response = apiInstance.api_v2_configs_operations_import_post (cFile)
while 'IN_PROGRESS' == response.state:
response = apiInstance.api_v2_configs_operations_import_id_get(response.id)
if 'SUCCESS' == response.state:
results += response.result

return [cyperf.AppsecConfig.from_dict(e['configData']) for e in results]

def DeleteConfig(self, configs):
apiInstance = cyperf.ConfigurationsApi(self.apiClient)
for config in configs:
apiInstance.api_v2_configs_config_id_delete (config.id)

def Run(self, config):
pprint (f"Loading configuration {config.file} ...")
configs = self.LoadConfigs ([config.file])
pprint (f"Loaded configuration {config.file} ...")
time.sleep(10)
pprint (f"Deleting configuration {config.file} ...")
self.DeleteConfig (configs)
pprint (f"Deleted configuration {config.file} ...")

2 changes: 1 addition & 1 deletion docs/ApiV2SessionsSessionIdConfigGet200Response.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**config** | [**Config**](Config.md) | | [optional]
**name** | **str** | The name of the configuration | [optional]
**session_id** | **str** | The unique identifier of the session this configuration belongs to | [optional] [readonly]
**template_id** | **str** | The unique identifier of the CyPerf configuration template from which this configuration was created | [optional] [readonly]
**data_model_version** | **str** | The version of the data model used for this configuration | [optional] [readonly]
**id** | **str** | The unique identifier of the CyPerf configuration | [optional] [readonly]
**name** | **str** | The name of the configuration | [optional]
**data** | [**AppsecConfig**](AppsecConfig.md) | | [optional]
**total_count** | **int** | | [optional]

Expand Down
2 changes: 1 addition & 1 deletion docs/AppsecConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**config** | [**Config**](Config.md) | | [optional]
**name** | **str** | The name of the configuration | [optional]
**session_id** | **str** | The unique identifier of the session this configuration belongs to | [optional] [readonly]
**template_id** | **str** | The unique identifier of the CyPerf configuration template from which this configuration was created | [optional] [readonly]
**data_model_version** | **str** | The version of the data model used for this configuration | [optional] [readonly]
**id** | **str** | The unique identifier of the CyPerf configuration | [optional] [readonly]
**name** | **str** | The name of the configuration | [optional]

## Example

Expand Down
2 changes: 1 addition & 1 deletion docs/StreamPayloadType.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

* `PSEUDORANDOM` (value: `'PSEUDORANDOM'`)

* `LESS_THAN_NIL_GREATER_THAN` (value: `'<nil>'`)
* `NULL` (value: `'null'`)

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down
Binary file added samples/configs/Msoft_tput_smartNIC_passthru.zip
Binary file not shown.
57 changes: 7 additions & 50 deletions samples/cyperf_ipv4_http.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,10 @@
import cyperf
import urllib3
import requests
from cyperf.rest import ApiException
from pprint import pprint
from cyperf.utils.test import TestRunner, TestConfig

class TestConfig(object):
def __init__(self, configFile, agentMapping, objectiveType, objectiveValue, verifyRules):
self.config = configFile
self.agents = agentMapping
self.objective = {
'type' : objectiveType,
'value' : objectiveValue
}
self.statRules = []

class TestRunner(object):
WAP_CLIENT_ID = 'clt-wap'

def __init__(self, controller, username, password):
self.host = f'https://{controller}'
self.username = username
self.password = password

self.configuration = cyperf.Configuration(host = self.host)
self.configuration.access_token = self.__authorize__()
self.apiClient = cyperf.ApiClient(configuration)

def __authorize__(self):
session = requests.Session()
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
session.verify = False

url = f'{self.host}/auth/realms/keysight/protocol/openid-connect/token'
headers = {"content-type" : "application/x-www-form-urlencoded"}
payload = {"username" : self.username,
"password" : self.password,
"grant_type" : "password",
"client_id" : TestRunner.WAP_CLIENT_ID}
response = session.post(url, headers = headers, data = payload, verify = False)
if response.headers.get('content-type') == 'application/json':
return response.json()['access_token']
else:
raise Exception(f'Cannot generate access token for {self.host}')

def authorize(self):
self.configuration.access_token = self.__authorize__()

def run(self, config):
pass
tests = [
TestConfig ('samples/configs/Msoft_tput_smartNIC_passthru.zip', {'IP Network Segment 1': ['10.36.75.235'], 'IP Network Segment 2': ['10.36.75.246']}, 'Throughput', 12500000000, None)
]

runner = TestRunner ('10.36.75.241', 'admin', 'CyPerf&Keysight#1')
print(runner.configuration.access_token)

for config in tests:
runner.Run (config)

0 comments on commit b6c612c

Please sign in to comment.