# Copyright 2022-2024 ETSI OSG/SDG TeraFlowSDN (TFS) (https://tfs.etsi.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. import json import requests,logging import pytest import subprocess import time import logging logging.basicConfig(level=logging.DEBUG) LOGGER = logging.getLogger(__name__) @pytest.fixture(scope="session", autouse=True) def start_server(): server = subprocess.Popen(['python', '-m', '__main__']) time.sleep(5) yield server.terminate() server.wait() @pytest.fixture def create_session(): BASE_URL = 'http://10.1.7.197/camara/qod/v0' URL = f"{BASE_URL}/sessions" data = { "source_ipv4_address": "192.168.1.1", "destination_ipv4_address": "192.168.1.2", "name": "Test Session", "session_profile": "Profile1", "targetMinUpstreamRate": {"value": 100, "unit": "kbps"}, "maxUpstreamRate": {"value": 1000, "unit": "kbps"}, "maxUpstreamBurstRate": {"value": 1500, "unit": "kbps"}, "targetMinDownstreamRate": {"value": 100, "unit": "kbps"}, "maxDownstreamRate": {"value": 1000, "unit": "kbps"}, "maxDownstreamBurstRate": {"value": 1500, "unit": "kbps"}, "minDuration": {"value": 10, "unit": "s"}, "maxDuration": {"value": 60, "unit": "s"}, "priority": 1, "packetDelayBudget": {"value": 100, "unit": "ms"}, "jitter": {"value": 20, "unit": "ms"}, "packetErrorLossRate": 1, "description": "Test session description" } response = requests.post(URL, json=data) print(f"Response Status Code: {response.status_code}") print(f"Response Content: {response.text}") response.raise_for_status() json_response = response.json() return json_response #def get_session(base_url, session_id): # url = f"{base_url}/sessions/{session_id}" # try: # response = requests.get(url) # response.raise_for_status() # return response.json() # except requests.exceptions.RequestException as e: # LOGGER.error(f"Request failed: {e}") # raise # # #def test_create_and_get_session(create_session): # session_data = create_session # print(f"Session Data: {session_data}") # session_id = session_data.get('session_id') # print(f"Session ID: {session_id}") # LOGGER.debug(f"Session ID: {session_id}") # BASE_URL = 'http://10.1.7.197/camara/qod/v0/' # try: # session_details = get_session(BASE_URL, session_id) # LOGGER.debug(f"Session Details: {session_details}") # LOGGER.debug('Session Details={:s}'.format(json.dumps(session_details, sort_keys=True, indent=4))) # print(f"Session Details: {session_details}") # except Exception as e: # LOGGER.error(f"Failed to retrieve session details: {e}") # raise # # # #def test_get_session(create_session): # session_id = create_session.get('session_id') # print(f"session_id':{session_id}") # BASE_URL = 'http://10.1.7.197/camara/qod/v0' # URL = f"{BASE_URL}/sessions/{session_id}" # # response = requests.get(URL) # assert response.status_code == 200, f"Expected 200, got {response.status_code}" # # response_data = response.json() # assert response_data['session_id'] == session_id # assert response_data['name'] == "Test Session" # #def test_get_all_sessions(create_session): # # BASE_URL = 'http://10.1.7.197/camara/qod/v0' # URL = f"{BASE_URL}/sessions/all" # LOGGER.debug(f'Sending GET request for all sessions...') # response = requests.get(URL, timeout=5) # LOGGER.debug('Received response status code: %s', response.status_code) # LOGGER.debug('Received response content: %s', response.text) def test_delete_session(create_session): session_id = create_session.get('session_id') BASE_URL = 'http://10.1.7.197/camara/qod/v0' URL = f"{BASE_URL}/sessions/ea3852d4-6cdc-4dc3-a8bf-4c16e19836da" LOGGER.debug(f'Sending DELETE request for session ID {session_id}...') response = requests.delete(URL, timeout=5) LOGGER.debug('Received response: %s', response.text) LOGGER.debug('Response Headers: %s', response.headers) def test_update_session(create_session): session_data = create_session session_id = session_data.get('session_id') BASE_URL = 'http://10.1.7.197/camara/qod/v0' UPDATE_URL = f"{BASE_URL}/sessions/06222e89-8610-4672-8534-184aa6760c04" updated_data = { "name": "Updated Session Name", "maxUpstreamRate": {"value": 2000, "unit": "kbps"}, "description": "Updated description for the test session" } response = requests.put(UPDATE_URL, json=updated_data) #def test_delete_all_sessions(): # BASE_URL = 'http://10.1.7.197/camara/qod/v0' # DELETE_ALL_URL = f"{BASE_URL}/sessions/delete_all" # response = requests.delete(DELETE_ALL_URL) # print(f"Status Code: {response.status_code}") # print(f"Response: {response.json()}") # assert response.status_code in [200, 204], f"Unexpected status code: {response.status_code}" # if response.status_code == 200: # assert response.json().get("message", "").startswith("All"), "Unexpected message content" # elif response.status_code == 204: # assert response.json() == {}, "Expected empty response for 204 No Content" # print("Test for delete all sessions passed.")