behave_tests: add unit tests for TestAPI client
[nfvbench.git] / test / ut_behave_tests / test_testapi.py
1 #!/usr/bin/env python
2 # Copyright 2022 Orange
3 #
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
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
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
14 #    under the License.
15 #
16
17 """
18 Unit tests for the testapi module found in behave_tests/features/steps.
19 """
20
21 import unittest
22 from unittest.mock import call, patch
23
24 from behave_tests.features.steps.testapi import TestapiClient
25 from .test_utils import setup_logging, stub_requests_get
26
27
28 def setUpModule():
29     setup_logging(log_filename="ut_behave_tests_testapi.log")
30
31
32 class TestTestapiClient(unittest.TestCase):
33     def setUp(self):
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)
38
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'],
43                                "flow_count": "100k",
44                                "duration_sec": '10',
45                                "rate": "ndr",
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"]))
53
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")
57
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'],
62                                "flow_count": "100k",
63                                "duration_sec": '10',
64                                "rate": "90%",
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"]))
72
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")])
78
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'],
83                                "flow_count": "100k",
84                                "duration_sec": '10',
85                                "rate": "90%",
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)
91
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")])
97
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',
106                                "rate": "90%",
107                                "user_label": "amical_tc6_intensive"}
108
109         with self.assertRaises(ValueError):
110             client.find_last_result(testapi_params, scenario_tag="throughput",
111                                     nfvbench_test_input=nfvbench_test_input)