Skip to content

Commit c8b3730

Browse files
committed
Merge branch 'test/unittest_with_config_file' into 'main'
unit_test_app: Added env config file with USB PORT info and improved test report generation See merge request app-frameworks/esp-insights!152
2 parents d7505eb + 29b1575 commit c8b3730

File tree

3 files changed

+65
-27
lines changed

3 files changed

+65
-27
lines changed

.gitlab-ci.yml

+20-18
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ before_script:
2424
- echo Checking git version
2525
- git --version
2626
- echo Cloning esp-idf - Started
27-
- git clone --depth 1 --shallow-submodules --recursive --single-branch -b ${IDF_GIT_BRANCH} --reference-if-able /Users/espressif/local_reference/esp-idf https://gitlab-ci-token:${BOT_TOKEN}@gitlab.espressif.cn:6688/espressif/esp-idf.git
27+
- git clone --depth 1 --shallow-submodules --recursive --single-branch -b ${IDF_GIT_BRANCH} --reference-if-able /Users/espressif/local_reference/esp-idf https://github.com/espressif/esp-idf.git
2828
- cd esp-idf
2929
- echo Cloning esp-idf - Done
3030
- echo Running install.sh - Started
@@ -56,7 +56,6 @@ before_script:
5656
- echo Building diagnostics_smoke_test app - Started
5757
- ls
5858
- cd $CI_PROJECT_DIR/examples/diagnostics_smoke_test
59-
6059
# Build this example for HTTPS transport, insights_auth_key.txt file is required
6160
- touch main/insights_auth_key.txt
6261
- for TARGET in $EXAMPLE_TARGETS; do
@@ -146,13 +145,17 @@ build_idf_v4.1.2:
146145

147146
unit_tests:
148147
before_script: []
148+
variables:
149+
TEST_CONFIG_PATH: $CI_PROJECT_DIR/components/esp_diag_data_store/test/python_tests/env_config.yml
149150
stage: test
150151
timeout: 2 hours
151152
tags:
152153
- idf_unit_test_runner
153154
script:
154155
- *setup_idf
155-
- echo IDF PATH is $IDF_PATH
156+
# update value of usb port in env config file
157+
- cd $CI_PROJECT_DIR/components/esp_diag_data_store/test/python_tests
158+
- yq eval ".default.unit-test-app = \"$UT_USB_PORT\"" -i "env_config.yml"
156159
- cd $IDF_PATH/tools/unit-test-app
157160
# Clean any previous configuration and builds
158161
- rm -rf build sdkconfig $IDF_PATH/TEST_LOGS
@@ -164,26 +167,25 @@ unit_tests:
164167
# test application will be flashed in following step only
165168
- ./unit_test.py "data store init deinit" "data store write" "data store write read release_all"
166169
"data store wrapped_read write_till_exact_full" "data store write read release_zero read release_zero release_all"
167-
"data store validate data in RTC after crash"
168-
# Run python test cases
170+
"data store validate data in RTC after crash" --env_config_file $TEST_CONFIG_PATH
171+
# Flash diagnostics smoke test example and run the tests
172+
- cd $CI_PROJECT_DIR/examples/diagnostics_smoke_test
173+
# Clean any previous configuration and builds
174+
- rm -rf build sdkconfig
175+
# update WIFI details
176+
- echo 'CONFIG_EXAMPLE_WIFI_SSID="ES_Staff"' >> sdkconfig.defaults
177+
- echo 'CONFIG_EXAMPLE_WIFI_PASSWORD="ES@puba45"' >> sdkconfig.defaults
178+
- echo CONFIG_ESP_INSIGHTS_TRANSPORT_MQTT=y >> sdkconfig.defaults
179+
- echo $InsightsAuthKeyToken > main/insights_auth_key.txt || true
180+
- idf.py build flash -p $UT_USB_PORT
169181
- cd $CI_PROJECT_DIR/components/esp_diag_data_store/test/python_tests
182+
# Run python test cases
170183
- python3 stress_tests.py --password=${PASSWORD} --username=${USERNAME} --base_uri=${BASE_URI} --diag_uri=${DIAG_URI}
171-
# report generation
172-
- echo ******* Report Generation Started **********
173-
- cd $CI_PROJECT_DIR
174-
# Delete existing test_report directory if any and create new one
175-
- rm -rf test_report
176-
- mkdir test_report
177-
# Convert xml reports to html format
178-
- junit2html components/esp_diag_data_store/test/python_tests/*.xml test_report/python_test_results.html
179-
- cd $IDF_PATH/TEST_LOGS
180-
- result_xml=$(find . -type f -name '*.xml')
181-
- junit2html $result_xml $CI_PROJECT_DIR/test_report/result.html
182-
- echo ******* Report Generation Finished **********
184+
183185
artifacts:
184186
name: "Test_Reports"
185187
paths:
186-
- $CI_PROJECT_DIR/test_report/*.html
188+
- $CI_PROJECT_DIR/components/esp_diag_data_store/test/python_tests/*html
187189

188190
push_master_to_github:
189191
stage: deploy
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
default:
2+
unit-test-app: "port"
3+
# port value is updated run time during gitlab CI for local use update the port value manually

components/esp_diag_data_store/test/python_tests/stress_tests.py

+42-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import sys
88
import argparse
99
import json
10+
import os
11+
import glob
12+
import xml.etree.ElementTree as ET
1013

1114
from datetime import datetime, timedelta
1215

@@ -50,34 +53,33 @@ def test_100_get_crash_count(self):
5053
- Expected : crash count shall not be more than 5.
5154
"""
5255
# Arrange ------------------------------------------------------------------------
53-
from_ts = round(int(datetime.timestamp(datetime.now() - timedelta(seconds=5))))
56+
from_ts = round(int(datetime.timestamp(datetime.now() - timedelta(minutes=1))))
5457
header = {"Authorization": self.access_token}
55-
node_id = 'BiPVyKJVAenRJaBMTZ7HzE'
58+
node_id = 'eZzk4U2Xn4Aqwmitw4x6gc'
5659

5760
curr_ts = round(int(datetime.timestamp(datetime.now())))
58-
max_time = 10*60 # duration in seconds
61+
max_time = 10*60 # duration in seconds (10 minutes)
5962
crash_count = 0
6063
filt = '[{"f":"Node.ID","o":"keyword","v":["%s"]},{"f":"Type","o":"keyword","v":["crash"]}]' % node_id
6164
encoded_filter = urllib.parse.quote(filt, safe='~@#$&()*!+=:;,?/\'')
65+
6266
while (curr_ts-from_ts) < max_time:
63-
print("\n completed {} seconds out of {} crash count is {} ".format(curr_ts-from_ts, max_time, crash_count))
6467
url = self.diag_uri + "/query/filters/suggest?from_ts={}&to_ts={}&filters={}&fieldname=Type".\
6568
format(from_ts, curr_ts, encoded_filter)
6669
# Act ----------------------------------------------------------------------------
6770
resp_crash_filter = requests.get(url=url, verify=False, headers=header, cookies=None)
6871
json_resp_crash_filter = json.loads(resp_crash_filter.text)["list"]
69-
7072
# Assert -------------------------------------------------------------------------
7173
self.assertEqual(resp_crash_filter.status_code, 200, "S100.1 Unexpected status code for {} \n".format(node_id))
7274
# Check if any crash is present
7375
if len(json.loads(resp_crash_filter.text)["list"]):
74-
self.assertEqual(json_resp_crash_filter["key"], "crash", "S100.2 key is not present for {}".format(node_id))
75-
self.assertLessEqual(json_resp_crash_filter["count"], 5, "S100.3 Crash count is not same as expected for {}". format(node_id))
76-
crash_count = json_resp_crash_filter["count"]
77-
76+
self.assertEqual(json_resp_crash_filter[0]["key"], "crash", "S100.2 key is not present for {}".format(node_id))
77+
self.assertLessEqual(json_resp_crash_filter[0]["count"], 5, "S100.3 Crash count is not same as expected for {}". format(node_id))
78+
crash_count = json_resp_crash_filter[0]["count"]
7879
# update variable curr_ts after every one minute
7980
time.sleep(60)
8081
curr_ts = round(int(datetime.timestamp(datetime.now())))
82+
print("\n Final crash count after 10 minutes is {} ".format(crash_count))
8183

8284

8385
if __name__ == '__main__':
@@ -103,4 +105,35 @@ def test_100_get_crash_count(self):
103105
suite = unittest.TestSuite()
104106
suite.addTest(TestCaseInsights('test_100_get_crash_count', **kwargs))
105107
xmlrunner.XMLTestRunner(verbosity=2).run(unittest.TestSuite(suite))
108+
109+
# Test report generation
110+
for ele in (os.listdir()):
111+
if ele.endswith("xml"):
112+
os.system("junit2html {} {}.html".format(ele, os.path.splitext(ele)[0]))
113+
# Parse the XML report
114+
xml_report = ET.parse(ele)
115+
failures = xml_report.findall('.//failure')
116+
errors = xml_report.findall('.//error')
117+
if len(failures) > 0 or len(errors) > 0:
118+
print("Tests failed. see console log for details")
119+
sys.exit(1)
120+
121+
# PATH of test reports generated by IDF unit tests
122+
IDF_DIR = '../../../../esp-idf/TEST_LOGS'
123+
xml_reports = glob.glob(os.path.join(IDF_DIR, '**/*.xml'), recursive=True)
124+
xml_file_path = os.path.abspath(xml_reports[0])
125+
126+
if not xml_file_path:
127+
print("XML reports of IDF unit tests not generated")
128+
sys.exit(1)
129+
else:
130+
os.system("junit2html {} {}.html".format(xml_file_path, "unit_test_report"))
131+
# Parse the XML report
132+
xml_report = ET.parse(xml_file_path)
133+
failures = xml_report.findall('.//failure')
134+
errors = xml_report.findall('.//error')
135+
if len(failures) > 0 or len(errors) > 0:
136+
print("Tests failed. see console log for details")
137+
sys.exit(1)
138+
106139
sys.exit(0)

0 commit comments

Comments
 (0)