1#!/bin/sh
2
3if [ -z "${PYTHON}" ]; then
4  PYTHON=python
5fi
6
7usage() {
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
28process_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
45venv=.venv
46with_venv=tools/with_venv.sh
47always_venv=0
48never_venv=0
49just_pycodestyle=0
50no_pycodestyle=0
51just_pylint=0
52integrated=0
53force=0
54noseargs=
55wrapper=""
56coverage=0
57verbose=0
58
59for arg in "$@"; do
60  process_option $arg
61done
62
63# If enabled, tell nose to collect coverage data
64if [ $coverage -eq 1 ]; then
65    noseopts="$noseopts --with-coverage --cover-package=ryu"
66fi
67
68run_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
90run_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
106run_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
121run_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"
128NOSETESTS="${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
139if [ $never_venv -eq 0 ]
140then
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
163fi
164
165# Delete old coverage data from previous runs
166if [ $coverage -eq 1 ]; then
167    ${wrapper} coverage erase
168fi
169
170if [ $just_pycodestyle -eq 1 ]; then
171    run_pycodestyle
172    exit
173fi
174if [ $just_pylint -eq 1 ]; then
175    run_pylint
176    exit
177fi
178
179if [ $integrated -eq 1 ]; then
180    run_integrated
181    exit
182fi
183
184run_tests
185RV=$?
186if [ $no_pycodestyle -eq 0 ]; then
187    run_pycodestyle
188fi
189
190if [ $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
194fi
195
196exit $RV
197