Update and rename MantenerFIFO to MantenerFIFO.md
[vsorcdistro/.git] / ryu / run_tests.sh
1 #!/bin/sh
2
3 if [ -z "${PYTHON}" ]; then
4   PYTHON=python
5 fi
6
7 usage() {
8   echo "Usage: $0 [OPTION]..."
9   echo "Run Ryu's test suite(s)"
10   echo ""
11   echo "  -V, --virtual-env                Always use virtualenv.  Install automatically if not present"
12   echo "  -N, --no-virtual-env             Don't use virtualenv.  Run tests in local environment"
13   echo "  -c, --coverage                   Generate coverage report"
14   echo "  -f, --force                      Force a clean re-build of the virtual environment. Useful when dependencies have been added."
15   echo "  -p, --pycodestyle, --pep8        Just run pycodestyle(pep8)"
16   echo "  -P, --no-pycodestyle, --no-pep8  Don't run pycodestyle(pep8)"
17   echo "  -l, --pylint                     Just run pylint"
18   echo "  -i, --integrated                 Run integrated test"
19   echo "  -v, --verbose                    Run verbose pylint analysis"
20   echo "  -h, --help                       Print this usage message"
21   echo ""
22   echo "Note: with no options specified, the script will try to run the tests in a virtual environment,"
23   echo "      If no virtualenv is found, the script will ask if you would like to create one.  If you "
24   echo "      prefer to run tests NOT in a virtual environment, simply pass the -N option."
25   exit
26 }
27
28 process_option() {
29   case "$1" in
30     -h|--help) usage;;
31     -V|--virtual-env) always_venv=1; never_venv=0;;
32     -N|--no-virtual-env) always_venv=0; never_venv=1;;
33     -f|--force) force=1;;
34     -p|--pycodestyle|--pep8) just_pycodestyle=1; never_venv=1; always_venv=0;;
35     -P|--no-pycodestyle|--no-pep8) no_pycodestyle=1;;
36     -l|--pylint) just_pylint=1;;
37     -i|--integrated) integrated=1;;
38     -c|--coverage) coverage=1;;
39     -v|--verbose) verbose=1;;
40     -*) noseopts="$noseopts $1";;
41     *) noseargs="$noseargs $1"
42   esac
43 }
44
45 venv=.venv
46 with_venv=tools/with_venv.sh
47 always_venv=0
48 never_venv=0
49 just_pycodestyle=0
50 no_pycodestyle=0
51 just_pylint=0
52 integrated=0
53 force=0
54 noseargs=
55 wrapper=""
56 coverage=0
57 verbose=0
58
59 for arg in "$@"; do
60   process_option $arg
61 done
62
63 # If enabled, tell nose to collect coverage data
64 if [ $coverage -eq 1 ]; then
65     noseopts="$noseopts --with-coverage --cover-package=ryu"
66 fi
67
68 run_tests() {
69   # Just run the test suites in current environment
70   ${wrapper} rm -f ./$PLUGIN_DIR/tests.sqlite
71
72   if [ $verbose -eq 1 ]; then
73     ${wrapper} $NOSETESTS
74   else
75     ${wrapper} $NOSETESTS 2> run_tests.log
76   fi
77   # If we get some short import error right away, print the error log directly
78   RESULT=$?
79   if [ "$RESULT" -ne "0" ];
80   then
81     ERRSIZE=`wc -l run_tests.log | awk '{print \$1}'`
82     if [ $verbose -eq 0 -a "$ERRSIZE" -lt "40" ];
83     then
84         cat run_tests.log
85     fi
86   fi
87   return $RESULT
88 }
89
90 run_pylint() {
91   echo "Running pylint ..."
92   PYLINT_OPTIONS="--rcfile=.pylintrc --output-format=parseable"
93   PYLINT_INCLUDE="ryu bin/ryu bin/ryu-manager ryu/tests/bin/ryu-client"
94   export PYTHONPATH=$PYTHONPATH:.ryu
95   PYLINT_LOG=pylint.log
96
97   ${wrapper} pylint $PYLINT_OPTIONS $PYLINT_INCLUDE > $PYLINT_LOG
98   #BASE_CMD="pylint $PYLINT_OPTIONS $PYLINT_INCLUDE > $PYLINT_LOG"
99   #[ $verbose -eq 1 ] && $BASE_CMD || msg_count=`$BASE_CMD | grep 'ryu/' | wc -l`
100   #if [ $verbose -eq 0 ]; then
101   #  echo "Pylint messages count: " $msg_count
102   #fi
103   export PYTHONPATH=$OLD_PYTHONPATH
104 }
105
106 run_pycodestyle() {
107   PYCODESTYLE=$(which pycodestyle || which pep8)
108   if [ -z "${PYCODESTYLE}" ]
109   then
110     echo "Please install pycodestyle or pep8"
111     return 1
112   fi
113   echo "Running $(basename ${PYCODESTYLE}) ..."
114
115   PYCODESTYLE_OPTIONS="--repeat --show-source"
116   PYCODESTYLE_INCLUDE="ryu setup*.py"
117   PYCODESTYLE_LOG=pycodestyle.log
118   ${wrapper} ${PYCODESTYLE} $PYCODESTYLE_OPTIONS $PYCODESTYLE_INCLUDE | tee $PYCODESTYLE_LOG
119 }
120
121 run_integrated() {
122   echo "Running integrated test ..."
123
124   INTEGRATED_TEST_RUNNER="./ryu/tests/integrated/run_tests_with_ovs12.py"
125   sudo PYTHONPATH=. nosetests -s $INTEGRATED_TEST_RUNNER
126 }
127 #NOSETESTS="nosetests $noseopts $noseargs"
128 NOSETESTS="${PYTHON} ./ryu/tests/run_tests.py $noseopts $noseargs"
129
130 #if [ -n "$PLUGIN_DIR" ]
131 #then
132 #    if ! [ -f ./$PLUGIN_DIR/run_tests.py ]
133 #    then
134 #        echo "Could not find run_tests.py in plugin directory $PLUGIN_DIR"
135 #        exit 1
136 #    fi
137 #fi
138
139 if [ $never_venv -eq 0 ]
140 then
141   # Remove the virtual environment if --force used
142   if [ $force -eq 1 ]; then
143     echo "Cleaning virtualenv..."
144     rm -rf ${venv}
145   fi
146   if [ -e ${venv} ]; then
147     wrapper="${with_venv}"
148   else
149     if [ $always_venv -eq 1 ]; then
150       # Automatically install the virtualenv
151       ${PYTHON} tools/install_venv.py
152       wrapper="${with_venv}"
153     else
154       echo -e "No virtual environment found...create one? (Y/n) \c"
155       read use_ve
156       if [ "x$use_ve" = "xY" -o "x$use_ve" = "x" -o "x$use_ve" = "xy" ]; then
157         # Install the virtualenv and run the test suite in it
158         ${PYTHON} tools/install_venv.py
159         wrapper=${with_venv}
160       fi
161     fi
162   fi
163 fi
164
165 # Delete old coverage data from previous runs
166 if [ $coverage -eq 1 ]; then
167     ${wrapper} coverage erase
168 fi
169
170 if [ $just_pycodestyle -eq 1 ]; then
171     run_pycodestyle
172     exit
173 fi
174 if [ $just_pylint -eq 1 ]; then
175     run_pylint
176     exit
177 fi
178
179 if [ $integrated -eq 1 ]; then
180     run_integrated
181     exit
182 fi
183
184 run_tests
185 RV=$?
186 if [ $no_pycodestyle -eq 0 ]; then
187     run_pycodestyle
188 fi
189
190 if [ $coverage -eq 1 ]; then
191     echo "Generating coverage report in coverage.xml and covhtml/"
192     ${wrapper} coverage xml -i
193     ${wrapper} coverage html -d covhtml -i
194 fi
195
196 exit $RV