2 # Copyright 2022 Orange
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
18 Unit tests for the testapi module found in behave_tests/features/steps.
22 from unittest.mock import call, patch
24 from behave_tests.features.steps.testapi import TestapiClient
25 from .test_utils import setup_logging, stub_requests_get
29 setup_logging(log_filename="ut_behave_tests_testapi.log")
32 class TestTestapiClient(unittest.TestCase):
34 patcher = patch('behave_tests.features.steps.testapi.requests')
35 self.mock_requests = patcher.start()
36 self.mock_requests.get.side_effect = stub_requests_get
37 self.addCleanup(patcher.stop)
39 def test_find_characterization_throughput_on_page_1(self):
40 client = TestapiClient("http://127.0.0.1:8000/api/v1/results")
41 testapi_params = {"project_name": "nfvbench", "case_name": "characterization"}
42 nfvbench_test_input = {"frame_sizes": ['64'],
46 "user_label": "amical_tc18_loopback"}
47 last_result = client.find_last_result(testapi_params,
48 scenario_tag="throughput",
49 nfvbench_test_input=nfvbench_test_input)
50 self.assertIsNotNone(last_result)
51 self.assertEqual(16765582, last_result["synthesis"]["total_tx_rate"])
52 self.assertEqual(25, round(last_result["synthesis"]["avg_delay_usec"]))
54 self.mock_requests.get.assert_called_once_with(
55 "http://127.0.0.1:8000/api/v1/results?"
56 "project=nfvbench&case=characterization&criteria=PASS&page=1")
58 def test_find_characterization_latency_on_page_2(self):
59 client = TestapiClient("http://127.0.0.1:8000/api/v1/results")
60 testapi_params = {"project_name": "nfvbench", "case_name": "characterization"}
61 nfvbench_test_input = {"frame_sizes": ['768'],
65 "user_label": "amical_tc6_intensive"}
66 last_result = client.find_last_result(testapi_params,
67 scenario_tag="latency",
68 nfvbench_test_input=nfvbench_test_input)
69 self.assertIsNotNone(last_result)
70 self.assertEqual(262275, last_result["synthesis"]["total_tx_rate"])
71 self.assertEqual(353, round(last_result["synthesis"]["avg_delay_usec"]))
73 self.mock_requests.get.assert_has_calls([
74 call("http://127.0.0.1:8000/api/v1/results?"
75 "project=nfvbench&case=characterization&criteria=PASS&page=1"),
76 call("http://127.0.0.1:8000/api/v1/results?"
77 "project=nfvbench&case=characterization&criteria=PASS&page=2")])
79 def test_no_result_found(self):
80 client = TestapiClient("http://127.0.0.1:8000/api/v1/results")
81 testapi_params = {"project_name": "nfvbench", "case_name": "characterization"}
82 nfvbench_test_input = {"frame_sizes": ['768'],
86 "user_label": "toto_titi_tata"} # User label not in test data
87 last_result = client.find_last_result(testapi_params,
88 scenario_tag="throughput",
89 nfvbench_test_input=nfvbench_test_input)
90 self.assertIsNone(last_result)
92 self.mock_requests.get.assert_has_calls([
93 call("http://127.0.0.1:8000/api/v1/results?"
94 "project=nfvbench&case=characterization&criteria=PASS&page=1"),
95 call("http://127.0.0.1:8000/api/v1/results?"
96 "project=nfvbench&case=characterization&criteria=PASS&page=2")])
98 def test_requests_errors(self):
99 """Check that an exception is raised in case of problem with requests."""
100 client = TestapiClient("http://127.0.0.1:8000/api/v1/results")
101 testapi_params = {"project_name": "foo", # non-existent project
102 "case_name": "characterization"}
103 nfvbench_test_input = {"frame_sizes": ['768'],
104 "flow_count": "100k",
105 "duration_sec": '10',
107 "user_label": "amical_tc6_intensive"}
109 with self.assertRaises(ValueError):
110 client.find_last_result(testapi_params, scenario_tag="throughput",
111 nfvbench_test_input=nfvbench_test_input)