tests.sh 11 KB

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