Skip to content

Commit 787657f

Browse files
committed
SONARPY-37: Make the unit test import compatible with Sonarqube's >= 4.2
- Introduce new 'simple' mode, which just sums up the measures in report, without trying to locate the resources in Sonar - Add integration tests for new functionality - Move integrations test to the root directory
1 parent c276bb0 commit 787657f

42 files changed

Lines changed: 533 additions & 162 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ jdk:
66

77

88
install:
9-
- ./sonar-python-plugin/src/test/integration/setup_sonar.sh
9+
- integration-tests/setup_sonar.sh
1010

1111
before_script:
12-
- cp sonar-python-plugin/src/test/integration/sitedefs_sample.py sonar-python-plugin/src/test/integration/sitedefs.py
12+
- cp integration-tests/sitedefs_sample.py integration-tests/sitedefs.py
1313
- sudo pip install requests
1414

1515
script:
1616
- mvn install -DskipTests=true
1717
- mvn test
18-
- RAILS_ENV=production PATH=$PATH:/tmp/sonar-runner-2.4/bin sonar-python-plugin/src/test/integration/smoketest
18+
- RAILS_ENV=production PATH=$PATH:/tmp/sonar-runner-2.4/bin integration-tests/smoketest

README

Lines changed: 0 additions & 14 deletions
This file was deleted.

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
### Documentation:
2+
- http://docs.codehaus.org/display/SONAR/Python+Plugin
3+
4+
### Continuous Integration:
5+
| Hoster | Build status |
6+
| ------------- | ------------- |
7+
| [CloudBees](https://sonarplugins.ci.cloudbees.com/job/python/)| ?? |
8+
| [Travis CI](https://travis-ci.org/wenns/sonar-python) | [![Build Status](https://travis-ci.org/wenns/sonar-python.svg?branch=master)](https://travis-ci.org/wenns/sonar-python) |
9+
10+
11+
### Issue Management:
12+
- https://jira.codehaus.org/browse/SONARPY
13+
14+
### Continuous Inspection:
15+
- http://nemo.sonarqube.org/dashboard/index/org.codehaus.sonar-plugins.python:python
16+
17+
### Sample Project:
18+
- https://github.com/wenns/sample_sonar_python

sonar-python-plugin/src/test/integration/Readme.txt renamed to integration-tests/Readme.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
This directory contains automated smoke tests for a build of the Sonar
3-
C++ Community Plugin. This testsuite will check if the current build
3+
Python Plugin. This testsuite will check if the current build
44
(the .jar archive) does basically work (can be installed, can be
55
booted with, can run a basic analysis etc.) with Sonar versions you
66
care about.
@@ -21,4 +21,4 @@ care about.
2121

2222

2323
== Usage ==
24-
Just run smoketests in a shell.
24+
Just run 'smoketests' in a shell.

sonar-python-plugin/src/test/integration/setup_sonar.sh renamed to integration-tests/setup_sonar.sh

File renamed without changes.

integration-tests/sitedefs.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
# -*- mode: python; coding: iso-8859-1 -*-
3+
#
4+
# Copyright (C) 1990 - 2014 CONTACT Software GmbH
5+
# All rights reserved.
6+
# http://www.contact.de/
7+
#
8+
9+
import os
10+
11+
SONAR_HOME = "/home/wenns/sonar"
12+
SONAR_VERSIONS = ["4.2"]
13+
14+
# for Windows, 32 bit:
15+
#START_SCRIPT = os.path.join(SONAR_HOME, "sonar-%s", "bin", "windows-x86-32", "StartNTService.bat")
16+
#STOP_SCRIPT = os.path.join(SONAR_HOME, "sonar-%s", "bin", "windows-x86-32", "StopNTService.bat")
17+
18+
# for Linux, 32 bit:
19+
START_SCRIPT = os.path.join(SONAR_HOME, "sonarqube-%s", "bin", "linux-x86-32", "sonar.sh")
20+
STOP_SCRIPT = START_SCRIPT
21+
22+
# for Linux, 64 bit:
23+
#START_SCRIPT = os.path.join(SONAR_HOME, "sonarqube-%s", "bin", "linux-x86-64", "sonar.sh")
24+
#STOP_SCRIPT = START_SCRIPT

sonar-python-plugin/src/test/integration/sitedefs_sample.py renamed to integration-tests/sitedefs_sample.py

File renamed without changes.

sonar-python-plugin/src/test/integration/smoketest renamed to integration-tests/smoketest

Lines changed: 128 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,85 @@ SONAR_PLUGINS_TMPL = os.path.join(SONAR_HOME, "sonarqube-%s", "extensions", "plu
1919
SONAR_ERROR_RE = re.compile(".* ERROR .*")
2020
SONAR_WARN_RE = re.compile(".* WARN .*")
2121
INDENT = " "
22-
PROJECT_KEY = "smoketest_project_1"
23-
PROJECT_PATH = "../resources/org/sonar/plugins/python/smoketest_project_1"
2422
BASEDIR = os.path.dirname(os.path.realpath(__file__))
25-
JAR_PATH = os.path.normpath(glob(os.path.join(BASEDIR, "../../../target/*SNAPSHOT.jar"))[0])
26-
27-
28-
EXP_MEASURES = {
29-
"ncloc": 32.0,
30-
"lines": 47.0,
31-
"files": 4.0,
32-
"directories": 2.0,
33-
"functions": 7.0,
34-
"comment_lines_density": 5.9,
35-
"comment_lines": 2.0,
36-
"duplicated_lines_density": 80.9,
37-
"duplicated_lines": 38,
38-
"duplicated_blocks": 2,
39-
"duplicated_files": 2,
40-
"complexity": 15.0,
41-
"function_complexity": 2.1,
42-
"violations": 3,
43-
"violations_density": 71.9,
44-
"coverage": 43.5,
45-
"line_coverage": 36.8,
46-
"branch_coverage": 75.0,
47-
"test_success_density": 33.3,
48-
"test_failures": 1.0,
49-
"test_errors": 1.0,
50-
"tests": 3.0
51-
}
23+
JAR_PATH = os.path.normpath(glob(os.path.join(BASEDIR, "../sonar-python-plugin/target/*SNAPSHOT.jar"))[0])
24+
25+
TEST_PROJECTS_HOME = "testdata"
26+
27+
TEST_PROJECTS = [
28+
# ("smoketest_project_1",
29+
# ["-X"],
30+
# {
31+
# "ncloc": 32.0,
32+
# "lines": 47.0,
33+
# "files": 4.0,
34+
# "directories": 2.0,
35+
# "functions": 7.0,
36+
# "comment_lines_density": 5.9,
37+
# "comment_lines": 2.0,
38+
# "duplicated_lines_density": 80.9,
39+
# "duplicated_lines": 38,
40+
# "duplicated_blocks": 2,
41+
# "duplicated_files": 2,
42+
# "complexity": 15.0,
43+
# "function_complexity": 2.1,
44+
# "violations": 3,
45+
# "violations_density": 71.9,
46+
# "coverage": 43.5,
47+
# "line_coverage": 36.8,
48+
# "branch_coverage": 75.0,
49+
# "test_success_density": 33.3,
50+
# "test_failures": 1.0,
51+
# "test_errors": 1.0,
52+
# "tests": 3.0
53+
# }
54+
# ),
55+
("smoketest_project_2",
56+
["-X", "-Dsonar.python.xunit.skipDetails=true"],
57+
{
58+
"tests": 3.0,
59+
"test_failures": 1.0,
60+
"test_errors": 1.0,
61+
"skipped_tests": 1.0,
62+
"test_success_density": 33.3,
63+
"test_execution_time": 1.0,
64+
}
65+
),
66+
("smoketest_project_2",
67+
["-X", "-Dsonar.python.xunit.skipDetails=false"],
68+
{
69+
"tests": 3.0,
70+
"test_failures": 1.0,
71+
"test_errors": 1.0,
72+
"skipped_tests": 1.0,
73+
"test_success_density": 33.3,
74+
"test_execution_time": 1.0,
75+
}
76+
),
77+
("smoketest_project_3",
78+
["-X", "-Dsonar.python.xunit.skipDetails=true"],
79+
{
80+
"tests": 6.0,
81+
"test_failures": 2.0,
82+
"test_errors": 2.0,
83+
"skipped_tests": 2.0,
84+
"test_success_density": 33.3,
85+
"test_execution_time": 13.0,
86+
}
87+
),
88+
("smoketest_project_3",
89+
["-X", "-Dsonar.python.xunit.skipDetails=false"],
90+
{
91+
"tests": 3.0,
92+
"test_failures": 1.0,
93+
"test_errors": 1.0,
94+
"skipped_tests": 1.0,
95+
"test_success_density": 33.3,
96+
"test_execution_time": 7.0,
97+
}
98+
)
99+
]
100+
52101

53102
RED = ""
54103
GREEN = ""
@@ -67,16 +116,22 @@ except ImportError:
67116
pass
68117

69118

70-
def test_bootup(version):
71-
print(BRIGHT + "\nRunning smoke test for Sonar %s" % version + RESET_ALL)
119+
def runtests(version):
120+
print(BRIGHT + "\nRunning integration test for Sonar %s" % version + RESET_ALL)
72121

73122
if is_installed(version):
74123
cleanup(version)
75124
install_plugin(version)
76125
started = start_sonar(version)
77126
logsOK = checkSonarLogs(version)
78-
analysisFinishedOk = runAnalysis(version)
79-
resultingMetricsOk = checkMetrics()
127+
128+
analysisFinishedOk = True
129+
resultingMetricsOk = True
130+
for project, parameters, exp_measures in TEST_PROJECTS:
131+
projecthome = os.path.join(BASEDIR, TEST_PROJECTS_HOME, project)
132+
analysisFinishedOk &= runAnalysis(version, parameters, projecthome)
133+
resultingMetricsOk &= checkMetrics(project, exp_measures)
134+
80135
stop_sonar(version)
81136
return all([started, logsOK, analysisFinishedOk, resultingMetricsOk])
82137
else:
@@ -111,7 +166,8 @@ def start_sonar(version):
111166
sys.stdout.write(INDENT + "starting Sonar ... ")
112167
sys.stdout.flush()
113168
now = time.time()
114-
rc = subprocess.call(start_script(version), stdout=subprocess.PIPE, shell = (os.name == "nt"))
169+
rc = subprocess.call(start_script(version), stdout=subprocess.PIPE,
170+
shell=(os.name == "nt"))
115171
if rc != 0 or not wait_for_sonar(50, is_webui_up):
116172
sys.stdout.write(RED + "FAILED\n" + RESET)
117173
return False
@@ -124,7 +180,8 @@ def start_sonar(version):
124180
def stop_sonar(version):
125181
sys.stdout.write(INDENT + "stopping Sonar ... ")
126182
sys.stdout.flush()
127-
rc = subprocess.call(stop_script(version), stdout=subprocess.PIPE, shell = (os.name == "nt"))
183+
rc = subprocess.call(stop_script(version), stdout=subprocess.PIPE,
184+
shell=(os.name == "nt"))
128185
if rc != 0 or not wait_for_sonar(30, is_webui_down):
129186
sys.stdout.write(RED + "FAILED\n" + RESET)
130187
return False
@@ -216,15 +273,20 @@ def isSonarWarning(line):
216273
return SONAR_WARN_RE.match(line)
217274

218275

219-
def runAnalysis(version):
220-
logfname = "analysis_%s.log" % version
221-
sys.stdout.write(INDENT + "running analysis (see %s) ... " % logfname)
276+
def runAnalysis(version, parameters, projecthome):
277+
project = os.path.basename(projecthome)
278+
logfname = "analysis_%s_%s.log" % (version, project)
279+
INDENTx2 = INDENT + INDENT
280+
sys.stdout.write((INDENT + "running analysis of %s \n" +
281+
INDENTx2 + "with parameters '%s'\n" +
282+
INDENTx2 + "(see logfile %s) ... ")
283+
% (project, parameters, logfname))
222284
sys.stdout.flush()
223285
now = time.time()
224286
runner = "sonar-runner.bat" if os.name == 'nt' else "sonar-runner"
225287
with open(logfname, "w") as logfile:
226-
rc = subprocess.call([runner, "-X"],
227-
cwd=os.path.join(BASEDIR, PROJECT_PATH),
288+
rc = subprocess.call([runner] + parameters,
289+
cwd=projecthome,
228290
stdout=logfile, stderr=logfile)
229291
if rc == 0:
230292
label = GREEN + "OK, duration: %03.1f s\n" % (time.time() - now)
@@ -237,30 +299,38 @@ def runAnalysis(version):
237299

238300

239301
def diffDicts(d1, d2):
240-
rc = True
241302
difflist = []
242-
if d1 != d2:
243-
rc = False
244-
for k1, v1 in d1.iteritems():
245-
v2 = d2.get(k1, "<None>")
246-
if v1 != v2:
247-
difflist.append("%s %s/%s" % (k1, str(v1), str(v2)))
248-
return rc, ", ".join(difflist)
249-
250-
251-
def checkMetrics():
303+
#import pprint
304+
#pprint.pprint(d1)
305+
#pprint.pprint(d2)
306+
307+
for k1, v1 in d1.iteritems():
308+
v2 = d2.get(k1, None)
309+
if v1 != v2:
310+
difflist.append("%s %s/%s" % (k1, str(v1), str(v2)))
311+
return ", ".join(difflist)
312+
313+
314+
def checkMetrics(project_key, exp_measures):
252315
sys.stdout.write(INDENT + "checking resulting metrics ... ")
253316
sys.stdout.flush()
254317

255318
rc = False
256319
try:
257-
url = (SONAR_URL + "/api/resources?resource=" + PROJECT_KEY + "&metrics="
258-
+ ",".join(EXP_MEASURES.keys()))
320+
url = (SONAR_URL + "/api/resources?resource=" + project_key + "&metrics="
321+
+ ",".join(exp_measures.keys()))
259322
r = requests.get(url)
260-
gotMeasures = _toSimpleDict(json.loads(r.text)[0]["msr"])
261-
262-
rc, diff = diffDicts(EXP_MEASURES, gotMeasures)
263-
label = (GREEN + "OK") if rc else (RED + "FAILED, diff: " + diff)
323+
got_measures = {}
324+
json_measures = json.loads(r.text)[0].get("msr", None)
325+
if json_measures is not None:
326+
got_measures = _toSimpleDict(json_measures)
327+
328+
diff = diffDicts(exp_measures, got_measures)
329+
if diff == "":
330+
label = (GREEN + "OK")
331+
rc = True
332+
else:
333+
label = (RED + "FAILED, diff: " + diff)
264334
except requests.exceptions.ConnectionError, e:
265335
label = RED + "FAILED, reason: " + str(e)
266336

@@ -281,7 +351,7 @@ if __name__ == "__main__":
281351
badversions = []
282352
goodversions = []
283353
for sonar_version in SONAR_VERSIONS:
284-
if test_bootup(sonar_version):
354+
if runtests(sonar_version):
285355
goodversions.append(sonar_version)
286356
else:
287357
badversions.append(sonar_version)

sonar-python-plugin/src/test/resources/org/sonar/plugins/python/smoketest_project_1/.coveragerc renamed to integration-tests/testdata/smoketest_project_1/.coveragerc

File renamed without changes.

sonar-python-plugin/src/test/resources/org/sonar/plugins/python/smoketest_project_1/Makefile renamed to integration-tests/testdata/smoketest_project_1/Makefile

File renamed without changes.

0 commit comments

Comments
 (0)