blob: 5b926f981fdb9dd19c4ca5cd848bb48f3c2d822a [file] [log] [blame]
Matthew Treinishd15705b2012-10-16 14:04:48 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
Kurt Taylor6a6f5be2013-04-02 18:53:47 -04003# Copyright 2012 IBM Corp.
Matthew Treinishd15705b2012-10-16 14:04:48 -04004#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License
16
17import json
18import os
Matthew Treinishd15705b2012-10-16 14:04:48 -040019import shutil
20import sys
21
Matthew Treinish90aedd12013-02-25 17:56:49 -050022from oslo.config import cfg
23
Matthew Treinishd15705b2012-10-16 14:04:48 -040024from tempest.common.rest_client import RestClient
25from tempest import config
Matthew Treinishd15705b2012-10-16 14:04:48 -040026
27CONF = config.TempestConfig()
28
29
30class CoverageClientJSON(RestClient):
31
32 def __init__(self, config, username, password, auth_url, tenant_name=None):
33 super(CoverageClientJSON, self).__init__(config, username, password,
34 auth_url, tenant_name)
35 self.service = self.config.compute.catalog_type
36
37 def start_coverage(self):
38 post_body = {
39 'start': {},
40 }
41 post_body = json.dumps(post_body)
42 return self.post('os-coverage/action', post_body, self.headers)
43
44 def start_coverage_combine(self):
45 post_body = {
46 'start': {
47 'combine': True,
48 },
49 }
50 post_body = json.dumps(post_body)
51 return self.post('os-coverage/action', post_body, self.headers)
52
53 def stop_coverage(self):
54 post_body = {
55 'stop': {},
56 }
57 post_body = json.dumps(post_body)
58 resp, body = self.post('os-coverage/action', post_body, self.headers)
59 body = json.loads(body)
60 return resp, body
61
62 def report_coverage_xml(self, file=None):
63 post_body = {
64 'report': {
65 'file': 'coverage.report',
66 'xml': True,
67 },
68 }
69 if file:
70 post_body['report']['file'] = file
71 post_body = json.dumps(post_body)
72 resp, body = self.post('os-coverage/action', post_body, self.headers)
73 body = json.loads(body)
74 return resp, body
75
76 def report_coverage(self, file=None):
77 post_body = {
78 'report': {
79 'file': 'coverage.report',
80 },
81 }
82 if file:
83 post_body['report']['file'] = file
84 post_body = json.dumps(post_body)
85 resp, body = self.post('os-coverage/action', post_body, self.headers)
86 body = json.loads(body)
87 return resp, body
88
89 def report_coverage_html(self, file=None):
90 post_body = {
91 'report': {
92 'file': 'coverage.report',
93 'html': True,
94 },
95 }
96 if file:
97 post_body['report']['file'] = file
98 post_body = json.dumps(post_body)
99 resp, body = self.post('os-coverage/action', post_body, self.headers)
100 body = json.loads(body)
101 return resp, body
102
103
104def parse_opts(argv):
105 cli_opts = [
106 cfg.StrOpt('command',
107 short='c',
108 default='',
109 help="This required argument is used to specify the "
110 "coverage command to run. Only 'start', "
111 "'stop', or 'report' are valid fields."),
112 cfg.StrOpt('filename',
113 default='tempest-coverage',
114 help="Specify a filename to be used for generated report "
115 "files"),
116 cfg.BoolOpt('xml',
117 default=False,
118 help='Generate XML reports instead of text'),
119 cfg.BoolOpt('html',
120 default=False,
121 help='Generate HTML reports instead of text'),
122 cfg.BoolOpt('combine',
123 default=False,
124 help='Generate a single report for all services'),
125 cfg.StrOpt('output',
126 short='o',
127 default=None,
128 help='Optional directory to copy generated coverage data or'
129 ' reports into. This directory must not already exist '
130 'it will be created')
131 ]
132 CLI = cfg.ConfigOpts()
133 CLI.register_cli_opts(cli_opts)
134 CLI(argv[1:])
135 return CLI
136
137
138def main(argv):
139 CLI = parse_opts(argv)
Attila Fazekascadcb1f2013-01-21 23:10:53 +0100140 client_args = (CONF, CONF.identity.admin_username,
141 CONF.identity.admin_password, CONF.identity.uri,
142 CONF.identity.admin_tenant_name)
Matthew Treinishd15705b2012-10-16 14:04:48 -0400143 coverage_client = CoverageClientJSON(*client_args)
144
145 if CLI.command == 'start':
146 if CLI.combine:
147 coverage_client.start_coverage_combine()
148 else:
149 coverage_client.start_coverage()
150
151 elif CLI.command == 'stop':
152 resp, body = coverage_client.stop_coverage()
153 if not resp['status'] == '200':
154 print 'coverage stop failed with: %s:' % (resp['status'] + ': '
155 + body)
156 exit(int(resp['status']))
157 path = body['path']
158 if CLI.output:
159 shutil.copytree(path, CLI.output)
160 else:
161 print "Data files located at: %s" % path
162
163 elif CLI.command == 'report':
164 if CLI.xml:
165 resp, body = coverage_client.report_coverage_xml(file=CLI.filename)
166 elif CLI.html:
167 resp, body = coverage_client.report_coverage_html(
Sean Dague14c68182013-04-14 15:34:30 -0400168 file=CLI.filename)
Matthew Treinishd15705b2012-10-16 14:04:48 -0400169 else:
170 resp, body = coverage_client.report_coverage(file=CLI.filename)
171 if not resp['status'] == '200':
172 print 'coverage report failed with: %s:' % (resp['status'] + ': '
173 + body)
174 exit(int(resp['status']))
175 path = body['path']
176 if CLI.output:
177 if CLI.html:
178 shutil.copytree(path, CLI.output)
179 else:
180 path = os.path.dirname(path)
181 shutil.copytree(path, CLI.output)
182 else:
183 if not CLI.html:
184 path = os.path.dirname(path)
185 print 'Report files located at: %s' % path
186
187 else:
188 print 'Invalid command'
189 exit(1)
190
191
192if __name__ == "__main__":
193 main(sys.argv)