tests.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #!/bin/bash
  2. #
  3. # Build and runs tests for the protobuf project. The tests as written here are
  4. # used by both Jenkins and Travis, though some specialized logic is required to
  5. # handle the differences between them.
  6. on_travis() {
  7. if [ "$TRAVIS" == "true" ]; then
  8. "$@"
  9. fi
  10. }
  11. # For when some other test needs the C++ main build, including protoc and
  12. # libprotobuf.
  13. internal_build_cpp() {
  14. if [ -f src/protoc ]; then
  15. # Already built.
  16. return
  17. fi
  18. if [[ $(uname -s) == "Linux" && "$TRAVIS" == "true" ]]; then
  19. # Install GCC 4.8 to replace the default GCC 4.6. We need 4.8 for more
  20. # decent C++ 11 support in order to compile conformance tests.
  21. sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
  22. sudo apt-get update -qq
  23. sudo apt-get install -qq g++-4.8
  24. export CXX="g++-4.8" CC="gcc-4.8"
  25. fi
  26. ./autogen.sh
  27. ./configure
  28. make -j2
  29. }
  30. build_cpp() {
  31. internal_build_cpp
  32. make check -j2
  33. pushd conformance
  34. make test_cpp
  35. popd
  36. # Verify benchmarking code can build successfully.
  37. pushd benchmarks
  38. make && ./generate-datasets
  39. popd
  40. }
  41. build_cpp_distcheck() {
  42. ./autogen.sh
  43. ./configure
  44. make dist
  45. # List all files that should be included in the distribution package.
  46. git ls-files | grep "^\(java\|python\|objectivec\|csharp\|js\|ruby\|php\|cmake\|examples\)" |\
  47. grep -v ".gitignore" | grep -v "java/compatibility_tests" > dist.lst
  48. # Unzip the dist tar file.
  49. DIST=`ls *.tar.gz`
  50. tar -xf $DIST
  51. cd ${DIST//.tar.gz}
  52. # Check if every file exists in the dist tar file.
  53. FILES_MISSING=""
  54. for FILE in $(<../dist.lst); do
  55. if ! file $FILE &>/dev/null; then
  56. echo "$FILE is not found!"
  57. FILES_MISSING="$FILE $FILES_MISSING"
  58. fi
  59. done
  60. cd ..
  61. if [ ! -z "$FILES_MISSING" ]; then
  62. echo "Missing files in EXTRA_DIST: $FILES_MISSING"
  63. exit 1
  64. fi
  65. # Do the regular dist-check for C++.
  66. make distcheck -j2
  67. }
  68. build_csharp() {
  69. # Just for the conformance tests. We don't currently
  70. # need to really build protoc, but it's simplest to keep with the
  71. # conventions of the other builds.
  72. internal_build_cpp
  73. NUGET=/usr/local/bin/nuget.exe
  74. if [ "$TRAVIS" == "true" ]; then
  75. # Install latest version of Mono
  76. sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
  77. sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1397BC53640DB551
  78. echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
  79. sudo apt-get update -qq
  80. sudo apt-get install -qq mono-devel referenceassemblies-pcl nunit
  81. # Then install the dotnet SDK as per Ubuntu 14.04 instructions on dot.net.
  82. sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ trusty main" > /etc/apt/sources.list.d/dotnetdev.list'
  83. sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
  84. sudo apt-get update -qq
  85. sudo apt-get install -qq dotnet-dev-1.0.0-preview2-003121
  86. fi
  87. # Perform "dotnet new" once to get the setup preprocessing out of the
  88. # way. That spews a lot of output (including backspaces) into logs
  89. # otherwise, and can cause problems. It doesn't matter if this step
  90. # is performed multiple times; it's cheap after the first time anyway.
  91. mkdir dotnettmp
  92. (cd dotnettmp; dotnet new > /dev/null)
  93. rm -rf dotnettmp
  94. (cd csharp/src; dotnet restore)
  95. csharp/buildall.sh
  96. cd conformance && make test_csharp && cd ..
  97. }
  98. build_golang() {
  99. # Go build needs `protoc`.
  100. internal_build_cpp
  101. # Add protoc to the path so that the examples build finds it.
  102. export PATH="`pwd`/src:$PATH"
  103. # Install Go and the Go protobuf compiler plugin.
  104. on_travis sudo apt-get update -qq
  105. on_travis sudo apt-get install -qq golang
  106. export GOPATH="$HOME/gocode"
  107. mkdir -p "$GOPATH/src/github.com/google"
  108. rm -f "$GOPATH/src/github.com/google/protobuf"
  109. ln -s "`pwd`" "$GOPATH/src/github.com/google/protobuf"
  110. export PATH="$GOPATH/bin:$PATH"
  111. go get github.com/golang/protobuf/protoc-gen-go
  112. cd examples && make gotest && cd ..
  113. }
  114. use_java() {
  115. version=$1
  116. case "$version" in
  117. jdk7)
  118. on_travis sudo apt-get install openjdk-7-jdk
  119. export PATH=/usr/lib/jvm/java-7-openjdk-amd64/bin:$PATH
  120. export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  121. ;;
  122. oracle7)
  123. if [ "$TRAVIS" == "true" ]; then
  124. sudo apt-get install python-software-properties # for apt-add-repository
  125. echo "oracle-java7-installer shared/accepted-oracle-license-v1-1 select true" | \
  126. sudo debconf-set-selections
  127. yes | sudo apt-add-repository ppa:webupd8team/java
  128. yes | sudo apt-get install oracle-java7-installer
  129. fi;
  130. export PATH=/usr/lib/jvm/java-7-oracle/bin:$PATH
  131. export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  132. ;;
  133. esac
  134. if [ "$TRAVIS" != "true" ]; then
  135. MAVEN_LOCAL_REPOSITORY=/var/maven_local_repository
  136. MVN="$MVN -e -X --offline -Dmaven.repo.local=$MAVEN_LOCAL_REPOSITORY"
  137. fi;
  138. which java
  139. java -version
  140. $MVN -version
  141. }
  142. # --batch-mode supresses download progress output that spams the logs.
  143. MVN="mvn --batch-mode"
  144. build_java() {
  145. version=$1
  146. dir=java_$version
  147. # Java build needs `protoc`.
  148. internal_build_cpp
  149. cp -r java $dir
  150. cd $dir && $MVN clean && $MVN test
  151. cd ../..
  152. }
  153. # The conformance tests are hard-coded to work with the $ROOT/java directory.
  154. # So this can't run in parallel with two different sets of tests.
  155. build_java_with_conformance_tests() {
  156. # Java build needs `protoc`.
  157. internal_build_cpp
  158. cd java && $MVN test && $MVN install
  159. cd util && $MVN package assembly:single
  160. cd ../..
  161. cd conformance && make test_java && cd ..
  162. }
  163. build_javanano() {
  164. # Java build needs `protoc`.
  165. internal_build_cpp
  166. cd javanano && $MVN test && cd ..
  167. }
  168. build_java_jdk7() {
  169. use_java jdk7
  170. build_java_with_conformance_tests
  171. }
  172. build_java_oracle7() {
  173. use_java oracle7
  174. build_java oracle7
  175. }
  176. build_java_compatibility() {
  177. use_java jdk7
  178. internal_build_cpp
  179. # Use the unit-tests extraced from 2.5.0 to test the compatibilty between
  180. # 3.0.0-beta-4 and the current version.
  181. cd java/compatibility_tests/v2.5.0
  182. ./test.sh 3.0.0-beta-4
  183. }
  184. build_javanano_jdk7() {
  185. use_java jdk7
  186. build_javanano
  187. }
  188. build_javanano_oracle7() {
  189. use_java oracle7
  190. build_javanano
  191. }
  192. internal_install_python_deps() {
  193. if [ "$TRAVIS" != "true" ]; then
  194. return;
  195. fi
  196. # Install tox (OS X doesn't have pip).
  197. if [ $(uname -s) == "Darwin" ]; then
  198. sudo easy_install tox
  199. else
  200. sudo pip install tox
  201. fi
  202. # Only install Python2.6/3.x on Linux.
  203. if [ $(uname -s) == "Linux" ]; then
  204. sudo apt-get install -y python-software-properties # for apt-add-repository
  205. sudo apt-add-repository -y ppa:fkrull/deadsnakes
  206. sudo apt-get update -qq
  207. sudo apt-get install -y python2.6 python2.6-dev
  208. sudo apt-get install -y python3.3 python3.3-dev
  209. sudo apt-get install -y python3.4 python3.4-dev
  210. fi
  211. }
  212. build_objectivec_ios() {
  213. # Reused the build script that takes care of configuring and ensuring things
  214. # are up to date. The OS X test runs the objc conformance test, so skip it
  215. # here.
  216. # Note: travis has xctool installed, and we've looked at using it in the past
  217. # but it has ended up proving unreliable (bugs), an they are removing build
  218. # support in favor of xcbuild (or just xcodebuild).
  219. objectivec/DevTools/full_mac_build.sh \
  220. --core-only --skip-xcode-osx --skip-objc-conformance "$@"
  221. }
  222. build_objectivec_ios_debug() {
  223. build_objectivec_ios --skip-xcode-release
  224. }
  225. build_objectivec_ios_release() {
  226. build_objectivec_ios --skip-xcode-debug
  227. }
  228. build_objectivec_osx() {
  229. # Reused the build script that takes care of configuring and ensuring things
  230. # are up to date.
  231. objectivec/DevTools/full_mac_build.sh \
  232. --core-only --skip-xcode-ios
  233. }
  234. build_objectivec_cocoapods_integration() {
  235. # First, load the RVM environment in bash, needed to update ruby.
  236. source ~/.rvm/scripts/rvm
  237. # Update rvm to the latest version. This is needed to solve
  238. # https://github.com/google/protobuf/issues/1786 and may not be needed in the
  239. # future when Travis updates the default version of rvm.
  240. rvm get head
  241. # Update ruby to 2.2.3 as the default one crashes with segmentation faults
  242. # when using pod.
  243. rvm use 2.2.3 --install --binary --fuzzy
  244. # Update pod to the latest version.
  245. gem install cocoapods --no-ri --no-rdoc
  246. objectivec/Tests/CocoaPods/run_tests.sh
  247. }
  248. build_python() {
  249. internal_build_cpp
  250. internal_install_python_deps
  251. cd python
  252. # Only test Python 2.6/3.x on Linux
  253. if [ $(uname -s) == "Linux" ]; then
  254. envlist=py\{26,27,33,34\}-python
  255. else
  256. envlist=py27-python
  257. fi
  258. tox -e $envlist
  259. cd ..
  260. }
  261. build_python_cpp() {
  262. internal_build_cpp
  263. internal_install_python_deps
  264. cd python
  265. # Only test Python 2.6/3.x on Linux
  266. if [ $(uname -s) == "Linux" ]; then
  267. # py26 is currently disabled due to json_format
  268. envlist=py\{27,33,34\}-cpp
  269. else
  270. envlist=py27-cpp
  271. fi
  272. tox -e $envlist
  273. cd ..
  274. }
  275. build_ruby21() {
  276. internal_build_cpp # For conformance tests.
  277. cd ruby && bash travis-test.sh ruby-2.1 && cd ..
  278. }
  279. build_ruby22() {
  280. internal_build_cpp # For conformance tests.
  281. cd ruby && bash travis-test.sh ruby-2.2 && cd ..
  282. }
  283. build_jruby() {
  284. internal_build_cpp # For conformance tests.
  285. # TODO(xiaofeng): Upgrade to jruby-9.x. There are some broken jests to be
  286. # fixed.
  287. cd ruby && bash travis-test.sh jruby-1.7 && cd ..
  288. }
  289. build_ruby_all() {
  290. build_ruby21
  291. build_ruby22
  292. build_jruby
  293. }
  294. build_javascript() {
  295. internal_build_cpp
  296. cd js && npm install && npm test && cd ..
  297. }
  298. # Note: travis currently does not support testing more than one language so the
  299. # .travis.yml cheats and claims to only be cpp. If they add multiple language
  300. # support, this should probably get updated to install steps and/or
  301. # rvm/gemfile/jdk/etc. entries rather than manually doing the work.
  302. # .travis.yml uses matrix.exclude to block the cases where app-get can't be
  303. # use to install things.
  304. # -------- main --------
  305. if [ "$#" -ne 1 ]; then
  306. echo "
  307. Usage: $0 { cpp |
  308. cpp_distcheck |
  309. csharp |
  310. java_jdk7 |
  311. java_oracle7 |
  312. java_compatibility |
  313. javanano_jdk7 |
  314. javanano_oracle7 |
  315. objectivec_ios |
  316. objectivec_ios_debug |
  317. objectivec_ios_release |
  318. objectivec_osx |
  319. objectivec_cocoapods_integration |
  320. python |
  321. python_cpp |
  322. ruby21 |
  323. ruby22 |
  324. jruby |
  325. ruby_all)
  326. "
  327. exit 1
  328. fi
  329. set -e # exit immediately on error
  330. set -x # display all commands
  331. eval "build_$1"