BugFix: modify storagecapacity output format for result visualization in grafana...
[yardstick.git] / tests / ci / cover.sh
1 ##############################################################################
2 # Copyright 2015: Mirantis Inc.
3 # All Rights Reserved.
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 # yardstick comment: this is a modified copy of
17 # rally/tests/ci/cover.sh
18 ##############################################################################
19
20 show_diff () {
21     head -1 $1
22     diff -U 0 $1 $2 | sed 1,2d
23 }
24
25
26 run_coverage_test() {
27
28     ALLOWED_EXTRA_MISSING=10
29
30
31     # Stash uncommitted changes, checkout master and save coverage report
32     uncommited=$(git status --porcelain | grep -v "^??")
33     [[ -n $uncommited ]] && git stash > /dev/null
34     git checkout HEAD^
35
36     baseline_report=$(mktemp -t yardstick_coverageXXXXXXX)
37     find . -type f -name "*.pyc" -delete && python setup.py testr --coverage --testr-args="$*"
38     coverage report > $baseline_report
39     baseline_missing=$(awk 'END { print $3 }' $baseline_report)
40
41     # Checkout back and unstash uncommitted changes (if any)
42     git checkout -
43     [[ -n $uncommited ]] && git stash pop > /dev/null
44
45     # Generate and save coverage report
46     current_report=$(mktemp -t yardstick_coverageXXXXXXX)
47     find . -type f -name "*.pyc" -delete && python setup.py testr --coverage --testr-args="$*"
48     coverage report > $current_report
49     current_missing=$(awk 'END { print $3 }' $current_report)
50
51     # Show coverage details
52     allowed_missing=$((baseline_missing+ALLOWED_EXTRA_MISSING))
53
54     echo "Allowed to introduce missing lines : ${ALLOWED_EXTRA_MISSING}"
55     echo "Missing lines in master            : ${baseline_missing}"
56     echo "Missing lines in proposed change   : ${current_missing}"
57
58     if [ $allowed_missing -gt $current_missing ];
59     then
60         if [ $baseline_missing -lt $current_missing ];
61         then
62             show_diff $baseline_report $current_report
63             echo "I believe you can cover all your code with 100% coverage!"
64         else
65             echo "Thank you! You are awesome! Keep writing unit tests! :)"
66         fi
67     else
68         show_diff $baseline_report $current_report
69         echo "Please write more unit tests, we should keep our test coverage :( "
70         rm $baseline_report $current_report
71         exit 1
72     fi
73
74     rm $baseline_report $current_report
75 }