tests.sh 11 KB

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