Merge lp:~canonical-platform-qa/ubuntu-test-cases/memevent-nfss-data-file into lp:~canonical-platform-qa/ubuntu-test-cases/memevent

Proposed by Christopher Lee
Status: Merged
Approved by: Christopher Lee
Approved revision: 337
Merged at revision: 335
Proposed branch: lp:~canonical-platform-qa/ubuntu-test-cases/memevent-nfss-data-file
Merge into: lp:~canonical-platform-qa/ubuntu-test-cases/memevent
Diff against target: 313 lines (+157/-77)
2 files modified
tests/memevent/support_scripts/generate_nfss_result_file.py (+81/-77)
tests/memevent/support_scripts/tests/test_nfss_result_generator.py (+76/-0)
To merge this branch: bzr merge lp:~canonical-platform-qa/ubuntu-test-cases/memevent-nfss-data-file
Reviewer Review Type Date Requested Status
Canonical Platform QA Team Pending
Review via email: mp+240671@code.launchpad.net

Commit message

Update the nfss upload script to generate single json files (per test) that can be uploaded to the nfss backend via the infrastructure supplied mechanisms.

Description of the change

Update the nfss upload script to generate single json files (per test) that can be uploaded to the nfss backend via the infrastructure supplied mechanisms.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added directory 'tests/memevent/support_scripts'
=== added file 'tests/memevent/support_scripts/__init__.py'
=== renamed file 'tests/memevent/nfss_upload_results.py' => 'tests/memevent/support_scripts/generate_nfss_result_file.py'
--- tests/memevent/nfss_upload_results.py 2014-10-22 21:39:57 +0000
+++ tests/memevent/support_scripts/generate_nfss_result_file.py 2014-11-06 00:30:46 +0000
@@ -1,10 +1,14 @@
1#!/usr/bin/python31#!/usr/bin/python3
22
3"""This script takes memevent-test result json files and uploads the3"""This script takes memevent-test result json files and generates a single
4interesting data contained within to a nfss backend.4containing json file for each test.
55
6It will process any memory result files found in 'source_file_path matching the6These files can then be uploaded to the nfss backend.
7naming scheme: "memory_usage_*.json"7
8The script takes two arguments <source file path> and <destination path>
9It will process any memory result files found in <source file_path> matching
10the naming scheme: "memory_usage_*.json" and write the amalgamated results file
11into <destination path>
812
9"""13"""
1014
@@ -18,10 +22,6 @@
18from collections import defaultdict22from collections import defaultdict
19from glob import glob23from glob import glob
2024
21source_file_path = ""
22upload_script = ""
23nfss_config = ""
24
2525
26def _get_run_details(app_name):26def _get_run_details(app_name):
27 return dict(27 return dict(
@@ -52,50 +52,19 @@
52 return "Unknown"52 return "Unknown"
5353
5454
55def upload_json_details(run_details, app_details):
56 app_run_details = run_details.copy()
57 app_run_details["events"] = app_details
58
59 _upload_data(run_details['application_name'], app_run_details)
60
61
62def _upload_data(test_name, run_json):
63 try:
64 run_json_string = json.dumps(run_json)
65 except ValueError as e:
66 print("Error: Data does not appear to be valid json: %s" % e)
67 sys.exit(3)
68
69 print("Uploading data for :memevent:-", test_name)
70 global upload_script
71 global nfss_config
72 try:
73 upload_process = subprocess.Popen(
74 [upload_script, nfss_config, 'memevent', test_name],
75 stdin=subprocess.PIPE,
76 stdout=subprocess.PIPE,
77 stderr=subprocess.PIPE,
78 )
79 stdout, stderr = upload_process.communicate(
80 input=run_json_string.encode()
81 )
82 print("stdout: {}\n\nstderr: {}".format(stdout, stderr))
83 except Exception as e:
84 print("Something went terribly wrong: ", e)
85 if upload_process.returncode != 0:
86 raise subprocess.CalledProcessError('Failed to upload to nfss')
87
88
89def _get_files_app_name_and_test(filename):55def _get_files_app_name_and_test(filename):
90 """return tuple containing (appname, testname)."""56 """return tuple containing (appname, testname)."""
91 app_name_search = re.search(57 app_name_search = re.search(
92 'memory_usage_([a-zA-Z-]*).(.*)\.json',58 'memory_usage_([a-zA-Z-]*).(.*)\.json',
93 filename59 filename
94 )60 )
61 if "" in app_name_search.groups():
62 raise ValueError("Cannot find app and/or test name: %s" % filename)
95 return (app_name_search.group(1), app_name_search.group(2))63 return (app_name_search.group(1), app_name_search.group(2))
9664
9765
98def get_application_readings(json_data):66def get_application_readings(json_data):
67 """Returns a list of result event data."""
99 app_results = []68 app_results = []
100 pids = json_data["pids"]69 pids = json_data["pids"]
101 for reading in json_data["readings"]:70 for reading in json_data["readings"]:
@@ -115,6 +84,8 @@
11584
11685
117def get_application_results(json_filepath):86def get_application_results(json_filepath):
87 """Load a json file into memory, return the a list of event readings from
88 it."""
118 with open(json_filepath, "r") as f:89 with open(json_filepath, "r") as f:
119 json_data = json.load(f)90 json_data = json.load(f)
120 return get_application_readings(json_data)91 return get_application_readings(json_data)
@@ -122,63 +93,96 @@
12293
123def get_application_details(application_files):94def get_application_details(application_files):
124 """For every file this application has grab out the details and return a95 """For every file this application has grab out the details and return a
125 list dictionaries containing reading details.96 list of dictionaries containing reading details.
97
98 Given a list of json files for an application extract its results and
99 return a list of results for this application.
126100
127 """101 """
128 app_result = []102 app_result = []
129 for json_file in application_files:103 for json_file in application_files:
130 app_result.extend(get_application_results(json_file))104 json_results = get_application_results(json_file)
105 app_result.extend(json_results)
131 return app_result106 return app_result
132107
133108
134def map_files_to_applications():109def map_files_to_applications(test_file_paths_list):
135 """For any memory result files that exist, return a dictionary whos keys110 """For any memory result files that exist at **source_file_path**, return a
136 are the applications name mapped to the file.111 dictionary whos keys are the applications name and the values the file path
112 for that apps results.
137113
138 We can then produce a single json result for each application regardless of114 We can then produce a single json result for each application regardless of
139 there being many tests / results for it.115 there being many tests / results for it.
140116
141 """117 """
142
143 global source_file_path
144 json_results_filename_pattern = os.path.join(
145 source_file_path,
146 "memory_usage_*.json"
147 )
148 file_map = defaultdict(list)118 file_map = defaultdict(list)
149 for json_result_file in glob(json_results_filename_pattern):119 for json_result_file in test_file_paths_list:
150 app_name, test_name = _get_files_app_name_and_test(json_result_file)120 app_name, test_name = _get_files_app_name_and_test(json_result_file)
151 file_map[app_name].append(json_result_file)121 file_map[app_name].append(json_result_file)
152 return file_map122 return file_map
153123
154124
155def usage():125def get_all_applications_details(test_result_files):
156 print("{} <source file path> <nfss upload script> <nfss config file>"126 """Create a map of [application name] = [list of result event data]."""
157 .format(sys.argv[0]))
158
159
160def main():
161 if len(sys.argv) != 4:
162 usage()
163 exit(1)
164
165 global source_file_path
166 source_file_path = sys.argv[1]
167
168 global upload_script
169 upload_script = sys.argv[2]
170
171 global nfss_config
172 nfss_config = sys.argv[3]
173
174 app_details = dict()127 app_details = dict()
175 file_map = map_files_to_applications()128 file_map = map_files_to_applications(test_result_files)
176 for app_name in file_map.keys():129 for app_name in file_map.keys():
177 app_details[app_name] = get_application_details(file_map[app_name])130 app_details[app_name] = get_application_details(file_map[app_name])
178131
179 for app_name in app_details.keys():132 return app_details
133
134
135def get_test_result_files(source_file_path):
136 json_results_filename_pattern = os.path.join(
137 source_file_path,
138 "memory_usage_*.json"
139 )
140 return glob(json_results_filename_pattern)
141
142
143def usage(extra_msg=None):
144 print("{} <source file path> <destination file>".format(sys.argv[0]))
145 if extra_msg:
146 print(extra_msg)
147
148
149def raise_on_invalid_args(argv):
150 if len(argv) != 3:
151 raise RuntimeError("Not enough arguments")
152
153
154def parse_args(argv):
155 try:
156 source_file_path = argv[1]
157 destination_file = argv[2]
158 return (source_file_path, destination_file)
159 except IndexError:
160 raise RuntimeError("Need both source path and destination file.")
161
162
163def main():
164 try:
165 raise_on_invalid_args(sys.argv)
166 source_file_path, destination_file = parse_args(sys.argv)
167 except RuntimeError as e:
168 usage(e)
169 exit(1)
170
171 test_result_files = get_test_result_files(source_file_path)
172 all_app_details = get_all_applications_details(test_result_files)
173
174 # all_app_details is a map of ['appname'] => [list|event data]
175 for app_name in all_app_details.keys():
180 run_details = _get_run_details(app_name)176 run_details = _get_run_details(app_name)
181 upload_json_details(run_details, app_details[app_name])177 run_details["events"] = all_app_details[app_name]
178 # write to file for application.
179 file_output = os.path.join(
180 destination_file,
181 "{app_name}_results.json".format(app_name=app_name)
182 )
183 print("Writing results for %s to %s" %(app_name, file_output))
184 with open(file_output, "w") as f:
185 json.dump(run_details, f)
182186
183187
184if __name__ == '__main__':188if __name__ == '__main__':
185189
=== added directory 'tests/memevent/support_scripts/tests'
=== added file 'tests/memevent/support_scripts/tests/__init__.py'
=== added file 'tests/memevent/support_scripts/tests/test_nfss_result_generator.py'
--- tests/memevent/support_scripts/tests/test_nfss_result_generator.py 1970-01-01 00:00:00 +0000
+++ tests/memevent/support_scripts/tests/test_nfss_result_generator.py 2014-11-06 00:30:46 +0000
@@ -0,0 +1,76 @@
1from testtools import TestCase, ExpectedException
2from testtools.matchers import Equals
3
4from support_scripts import generate_nfss_result_file as gen_nfss
5
6
7class ScriptInteractionTests(TestCase):
8 """Ensure the script reacts as expected."""
9 def test_parse_args_returns_source_and_destination_files(self):
10 test_args = ["executable", "source", "destination"]
11 self.assertThat(
12 gen_nfss.parse_args(test_args),
13 Equals(("source", "destination"))
14 )
15
16 def test_raise_on_invalid_args_doesnt_raise_with_correct_args(self):
17 gen_nfss.raise_on_invalid_args(["executable", "source", "destination"])
18
19 def test_raise_on_invalid_args_raises_with_incorrect_args(self):
20 with ExpectedException(RuntimeError):
21 gen_nfss.raise_on_invalid_args([])
22
23
24class AppnameAndTestnameExtractionTest(TestCase):
25 """Tests for _get_files_app_name_and_test."""
26
27 def test_returns_appname_testname(self):
28 test_filename = "/some/path/memory_usage_AppName.TestName.json"
29 app_name, test_name = gen_nfss._get_files_app_name_and_test(
30 test_filename
31 )
32
33 self.assertThat(app_name, Equals("AppName"))
34 self.assertThat(test_name, Equals("TestName"))
35
36 def test_raises_exception_if_filename_missformated(self):
37 test_filename = "/some/path/memory_usage_AppName.json"
38 exception_msg = "Cannot find app and/or test name: %s" % test_filename
39 with ExpectedException(ValueError, exception_msg):
40 gen_nfss._get_files_app_name_and_test(test_filename)
41
42
43class ApplicationDetailsTests(TestCase):
44 def test_map_files_to_args_gets_multiple_files_for_app(self):
45 test_filenames = [
46 "memory_usage_AppName.Test1.json",
47 "memory_usage_AppName.Test2.json",
48 ]
49 file_map = gen_nfss.map_files_to_applications(test_filenames)
50
51 self.assertDictEqual(file_map, dict(AppName=test_filenames))
52
53 def test_map_files_to_args_only_gets_files_for_app(self):
54 test_filenames = [
55 "memory_usage_AppName.Test1.json",
56 "memory_usage_AppName.Test2.json",
57 "memory_usage_SecondAppName.Test2.json",
58 ]
59 file_map = gen_nfss.map_files_to_applications(test_filenames)
60
61 self.assertListEqual(
62 file_map['AppName'],
63 [
64 "memory_usage_AppName.Test1.json",
65 "memory_usage_AppName.Test2.json"
66 ]
67 )
68
69 # get_application_details
70
71 # get_application_results
72 # Loads file and reads in json data
73
74 # get_application_readings
75 # Reads given json data and produces a list of results for the application
76 # Needs example data.

Subscribers

People subscribed via source and target branches

to all changes: