blob: 267eafaae80cf311ff6957365b795771226ce133 [file] [log] [blame]
Matthew Treinishd15705b2012-10-16 14:04:48 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2012 IBM
4#
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
19import re
20import shutil
21import sys
22
23from tempest.common.rest_client import RestClient
24from tempest import config
25from tempest.openstack.common import cfg
26from tempest.tests.compute import base
27
28CONF = config.TempestConfig()
29
30
31class CoverageClientJSON(RestClient):
32
33 def __init__(self, config, username, password, auth_url, tenant_name=None):
34 super(CoverageClientJSON, self).__init__(config, username, password,
35 auth_url, tenant_name)
36 self.service = self.config.compute.catalog_type
37
38 def start_coverage(self):
39 post_body = {
40 'start': {},
41 }
42 post_body = json.dumps(post_body)
43 return self.post('os-coverage/action', post_body, self.headers)
44
45 def start_coverage_combine(self):
46 post_body = {
47 'start': {
48 'combine': True,
49 },
50 }
51 post_body = json.dumps(post_body)
52 return self.post('os-coverage/action', post_body, self.headers)
53
54 def stop_coverage(self):
55 post_body = {
56 'stop': {},
57 }
58 post_body = json.dumps(post_body)
59 resp, body = self.post('os-coverage/action', post_body, self.headers)
60 body = json.loads(body)
61 return resp, body
62
63 def report_coverage_xml(self, file=None):
64 post_body = {
65 'report': {
66 'file': 'coverage.report',
67 'xml': True,
68 },
69 }
70 if file:
71 post_body['report']['file'] = file
72 post_body = json.dumps(post_body)
73 resp, body = self.post('os-coverage/action', post_body, self.headers)
74 body = json.loads(body)
75 return resp, body
76
77 def report_coverage(self, file=None):
78 post_body = {
79 'report': {
80 'file': 'coverage.report',
81 },
82 }
83 if file:
84 post_body['report']['file'] = file
85 post_body = json.dumps(post_body)
86 resp, body = self.post('os-coverage/action', post_body, self.headers)
87 body = json.loads(body)
88 return resp, body
89
90 def report_coverage_html(self, file=None):
91 post_body = {
92 'report': {
93 'file': 'coverage.report',
94 'html': True,
95 },
96 }
97 if file:
98 post_body['report']['file'] = file
99 post_body = json.dumps(post_body)
100 resp, body = self.post('os-coverage/action', post_body, self.headers)
101 body = json.loads(body)
102 return resp, body
103
104
105def parse_opts(argv):
106 cli_opts = [
107 cfg.StrOpt('command',
108 short='c',
109 default='',
110 help="This required argument is used to specify the "
111 "coverage command to run. Only 'start', "
112 "'stop', or 'report' are valid fields."),
113 cfg.StrOpt('filename',
114 default='tempest-coverage',
115 help="Specify a filename to be used for generated report "
116 "files"),
117 cfg.BoolOpt('xml',
118 default=False,
119 help='Generate XML reports instead of text'),
120 cfg.BoolOpt('html',
121 default=False,
122 help='Generate HTML reports instead of text'),
123 cfg.BoolOpt('combine',
124 default=False,
125 help='Generate a single report for all services'),
126 cfg.StrOpt('output',
127 short='o',
128 default=None,
129 help='Optional directory to copy generated coverage data or'
130 ' reports into. This directory must not already exist '
131 'it will be created')
132 ]
133 CLI = cfg.ConfigOpts()
134 CLI.register_cli_opts(cli_opts)
135 CLI(argv[1:])
136 return CLI
137
138
139def main(argv):
140 CLI = parse_opts(argv)
Attila Fazekascadcb1f2013-01-21 23:10:53 +0100141 client_args = (CONF, CONF.identity.admin_username,
142 CONF.identity.admin_password, CONF.identity.uri,
143 CONF.identity.admin_tenant_name)
Matthew Treinishd15705b2012-10-16 14:04:48 -0400144 coverage_client = CoverageClientJSON(*client_args)
145
146 if CLI.command == 'start':
147 if CLI.combine:
148 coverage_client.start_coverage_combine()
149 else:
150 coverage_client.start_coverage()
151
152 elif CLI.command == 'stop':
153 resp, body = coverage_client.stop_coverage()
154 if not resp['status'] == '200':
155 print 'coverage stop failed with: %s:' % (resp['status'] + ': '
156 + body)
157 exit(int(resp['status']))
158 path = body['path']
159 if CLI.output:
160 shutil.copytree(path, CLI.output)
161 else:
162 print "Data files located at: %s" % path
163
164 elif CLI.command == 'report':
165 if CLI.xml:
166 resp, body = coverage_client.report_coverage_xml(file=CLI.filename)
167 elif CLI.html:
168 resp, body = coverage_client.report_coverage_html(
169 file=CLI.filename)
170 else:
171 resp, body = coverage_client.report_coverage(file=CLI.filename)
172 if not resp['status'] == '200':
173 print 'coverage report failed with: %s:' % (resp['status'] + ': '
174 + body)
175 exit(int(resp['status']))
176 path = body['path']
177 if CLI.output:
178 if CLI.html:
179 shutil.copytree(path, CLI.output)
180 else:
181 path = os.path.dirname(path)
182 shutil.copytree(path, CLI.output)
183 else:
184 if not CLI.html:
185 path = os.path.dirname(path)
186 print 'Report files located at: %s' % path
187
188 else:
189 print 'Invalid command'
190 exit(1)
191
192
193if __name__ == "__main__":
194 main(sys.argv)