Skip to content

Commit

Permalink
Rest API test
Browse files Browse the repository at this point in the history
  • Loading branch information
vg12345 committed May 20, 2024
1 parent 039d531 commit 5d431ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@

import http
import json
import random
import time
import requests


def generate_port_name():
"""
Generate port name
"""
port_guid = f'{random.randrange(16**16):016x}'
port_num = random.randint(10, 99)
return f'{port_guid}_{port_num}'


def test_exclude_list_rest_api():
"""
Test exclude list via plugin REST API
Expand All @@ -25,14 +35,15 @@ def test_exclude_list_rest_api():
url = "http://127.0.0.1:8977/excluded"

excluded_ports = [
("0123456789aaabbb_1", 0), # Add forever
("9876543210cccddd_2", 30), # Add for 30 seconds
("3456789012eeefff_3", 0) # Add forever
(generate_port_name(), 0), # Add forever
(generate_port_name(), 30), # Add for 30 seconds
(generate_port_name(), 0) # Add forever
]

# Get (empty) list content
response = requests.get(url, timeout=5)
assert response.status_code == http.client.OK
assert all(char.isspace() for char in response.text)
print(" - test: get exclusion list and ensure it's empty -- PASS")

# Add ports to excluded list
Expand All @@ -51,7 +62,7 @@ def test_exclude_list_rest_api():
assert port_name in response.text
print(" - test: get added ports from exclusion list -- PASS")

# Wait until 2nd port TTL is expired
# Wait until second port TTL is expired
ttl_seconds = excluded_ports[1][1]
time.sleep(ttl_seconds + 1)

Expand All @@ -60,11 +71,23 @@ def test_exclude_list_rest_api():
assert response.status_code == http.client.OK
for (index, pair) in enumerate(excluded_ports):
port_name = pair[0]
if (index == 1):
if index == 1:
assert port_name not in response.text
else:
assert port_name in response.text
print(" - test: auto-remove of port from exclusion list after TTL is expired -- PASS")

# Test forced remove of third port
port_name = excluded_ports[2][1]
response = requests.put(url, data=json.dumps([port_name]), timeout=5)
assert response.status_code == http.client.OK
for (index, pair) in enumerate(excluded_ports):
if index == 1 or index == 2:
assert port_name not in response.text
else:
assert port_name in response.text
print(" - test: forced remove of port from exclusion list -- PASS")


if __name__ == '__main__':
test_exclude_list_rest_api()
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import copy
import argparse
import random
from os import _exit
from os.path import exists
from collections import OrderedDict
import requests
Expand Down Expand Up @@ -261,7 +260,7 @@ def excluded_ports_simulation(endpoint):
removed_ports_str = '[' + ','.join(removed_ports) + ']'
requests.delete(url=url, data=removed_ports_str, timeout=5)

def create_ports(config:dict,ports_num: int):
def create_ports(config:dict, ports_num: int):
"""
Create an array of ports in size of ports_num
"""
Expand Down

0 comments on commit 5d431ac

Please sign in to comment.