tests.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 CXXFLAGS="-fPIC" # -fPIC is needed for python cpp test.
  28. # See python/setup.py for more details
  29. make -j2
  30. }
  31. build_cpp() {
  32. internal_build_cpp
  33. make check -j2
  34. cd conformance && make test_cpp && cd ..
  35. # The benchmark code depends on cmake, so test if it is installed before
  36. # trying to do the build.
  37. # NOTE: The travis macOS images say they have cmake, but the xcode8.1 image
  38. # appears to be missing it: https://github.com/travis-ci/travis-ci/issues/6996
  39. if [[ $(type cmake 2>/dev/null) ]]; then
  40. # Verify benchmarking code can build successfully.
  41. git submodule init
  42. git submodule update
  43. cd third_party/benchmark && cmake -DCMAKE_BUILD_TYPE=Release && make && cd ../..
  44. cd benchmarks && make && ./generate-datasets && cd ..
  45. else
  46. echo ""
  47. echo "WARNING: Skipping validation of the bench marking code, cmake isn't installed."
  48. echo ""
  49. fi
  50. }
  51. build_cpp_distcheck() {
  52. ./autogen.sh
  53. ./configure
  54. make dist
  55. # List all files that should be included in the distribution package.
  56. git ls-files | grep "^\(java\|python\|objectivec\|csharp\|js\|ruby\|php\|cmake\|examples\)" |\
  57. grep -v ".gitignore" | grep -v "java/compatibility_tests" > dist.lst
  58. # Unzip the dist tar file.
  59. DIST=`ls *.tar.gz`
  60. tar -xf $DIST
  61. cd ${DIST//.tar.gz}
  62. # Check if every file exists in the dist tar file.
  63. FILES_MISSING=""
  64. for FILE in $(<../dist.lst); do
  65. if ! file $FILE &>/dev/null; then
  66. echo "$FILE is not found!"
  67. FILES_MISSING="$FILE $FILES_MISSING"
  68. fi
  69. done
  70. cd ..
  71. if [ ! -z "$FILES_MISSING" ]; then
  72. echo "Missing files in EXTRA_DIST: $FILES_MISSING"
  73. exit 1
  74. fi
  75. # Do the regular dist-check for C++.
  76. make distcheck -j2
  77. }
  78. build_csharp() {
  79. # Just for the conformance tests. We don't currently
  80. # need to really build protoc, but it's simplest to keep with the
  81. # conventions of the other builds.
  82. internal_build_cpp
  83. NUGET=/usr/local/bin/nuget.exe
  84. if [ "$TRAVIS" == "true" ]; then
  85. # Install latest version of Mono
  86. sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
  87. sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1397BC53640DB551
  88. echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
  89. sudo apt-get update -qq
  90. sudo apt-get install -qq mono-devel referenceassemblies-pcl nunit
  91. # Then install the dotnet SDK as per Ubuntu 14.04 instructions on dot.net.
  92. sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ trusty main" > /etc/apt/sources.list.d/dotnetdev.list'
  93. sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
  94. sudo apt-get update -qq
  95. sudo apt-get install -qq dotnet-dev-1.0.0-preview2-003121
  96. fi
  97. # Perform "dotnet new" once to get the setup preprocessing out of the
  98. # way. That spews a lot of output (including backspaces) into logs
  99. # otherwise, and can cause problems. It doesn't matter if this step
  100. # is performed multiple times; it's cheap after the first time anyway.
  101. mkdir dotnettmp
  102. (cd dotnettmp; dotnet new > /dev/null)
  103. rm -rf dotnettmp
  104. (cd csharp/src; dotnet restore)
  105. csharp/buildall.sh
  106. cd conformance && make test_csharp && cd ..
  107. }
  108. build_golang() {
  109. # Go build needs `protoc`.
  110. internal_build_cpp
  111. # Add protoc to the path so that the examples build finds it.
  112. export PATH="`pwd`/src:$PATH"
  113. # Install Go and the Go protobuf compiler plugin.
  114. on_travis sudo apt-get update -qq
  115. on_travis sudo apt-get install -qq golang
  116. export GOPATH="$HOME/gocode"
  117. mkdir -p "$GOPATH/src/github.com/google"
  118. rm -f "$GOPATH/src/github.com/google/protobuf"
  119. ln -s "`pwd`" "$GOPATH/src/github.com/google/protobuf"
  120. export PATH="$GOPATH/bin:$PATH"
  121. go get github.com/golang/protobuf/protoc-gen-go
  122. cd examples && make gotest && cd ..
  123. }
  124. use_java() {
  125. version=$1
  126. case "$version" in
  127. jdk7)
  128. on_travis sudo apt-get install openjdk-7-jdk
  129. export PATH=/usr/lib/jvm/java-7-openjdk-amd64/bin:$PATH
  130. export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  131. ;;
  132. oracle7)
  133. if [ "$TRAVIS" == "true" ]; then
  134. sudo apt-get install python-software-properties # for apt-add-repository
  135. echo "oracle-java7-installer shared/accepted-oracle-license-v1-1 select true" | \
  136. sudo debconf-set-selections
  137. yes | sudo apt-add-repository ppa:webupd8team/java
  138. yes | sudo apt-get install oracle-java7-installer
  139. fi;
  140. export PATH=/usr/lib/jvm/java-7-oracle/bin:$PATH
  141. export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  142. ;;
  143. esac
  144. if [ "$TRAVIS" != "true" ]; then
  145. MAVEN_LOCAL_REPOSITORY=/var/maven_local_repository
  146. MVN="$MVN -e -X --offline -Dmaven.repo.local=$MAVEN_LOCAL_REPOSITORY"
  147. fi;
  148. which java
  149. java -version
  150. $MVN -version
  151. }
  152. # --batch-mode supresses download progress output that spams the logs.
  153. MVN="mvn --batch-mode"
  154. build_java() {
  155. version=$1
  156. dir=java_$version
  157. # Java build needs `protoc`.
  158. internal_build_cpp
  159. cp -r java $dir
  160. cd $dir && $MVN clean && $MVN test
  161. cd ../..
  162. }
  163. # The conformance tests are hard-coded to work with the $ROOT/java directory.
  164. # So this can't run in parallel with two different sets of tests.
  165. build_java_with_conformance_tests() {
  166. # Java build needs `protoc`.
  167. internal_build_cpp
  168. cd java && $MVN test && $MVN install
  169. cd util && $MVN package assembly:single
  170. cd ../..
  171. cd conformance && make test_java && cd ..
  172. }
  173. build_javanano() {
  174. # Java build needs `protoc`.
  175. internal_build_cpp
  176. cd javanano && $MVN test && cd ..
  177. }
  178. build_java_jdk7() {
  179. use_java jdk7
  180. build_java_with_conformance_tests
  181. }
  182. build_java_oracle7() {
  183. use_java oracle7
  184. build_java oracle7
  185. }
  186. build_java_compatibility() {
  187. use_java jdk7
  188. internal_build_cpp
  189. # Use the unit-tests extraced from 2.5.0 to test the compatibilty between
  190. # 3.0.0-beta-4 and the current version.
  191. cd java/compatibility_tests/v2.5.0
  192. ./test.sh 3.0.0-beta-4
  193. }
  194. build_javanano_jdk7() {
  195. use_java jdk7
  196. build_javanano
  197. }
  198. build_javanano_oracle7() {
  199. use_java oracle7
  200. build_javanano
  201. }
  202. internal_install_python_deps() {
  203. if [ "$TRAVIS" != "true" ]; then
  204. return;
  205. fi
  206. # Install tox (OS X doesn't have pip).
  207. if [ $(uname -s) == "Darwin" ]; then
  208. sudo easy_install tox
  209. else
  210. sudo pip install tox
  211. fi
  212. # Only install Python2.6/3.x on Linux.
  213. if [ $(uname -s) == "Linux" ]; then
  214. sudo apt-get install -y python-software-properties # for apt-add-repository
  215. sudo apt-add-repository -y ppa:fkrull/deadsnakes
  216. sudo apt-get update -qq
  217. sudo apt-get install -y python2.6 python2.6-dev
  218. sudo apt-get install -y python3.3 python3.3-dev
  219. sudo apt-get install -y python3.4 python3.4-dev
  220. fi
  221. }
  222. build_objectivec_ios() {
  223. # Reused the build script that takes care of configuring and ensuring things
  224. # are up to date. The OS X test runs the objc conformance test, so skip it
  225. # here.
  226. # Note: travis has xctool installed, and we've looked at using it in the past
  227. # but it has ended up proving unreliable (bugs), an they are removing build
  228. # support in favor of xcbuild (or just xcodebuild).
  229. objectivec/DevTools/full_mac_build.sh \
  230. --core-only --skip-xcode-osx --skip-objc-conformance "$@"
  231. }
  232. build_objectivec_ios_debug() {
  233. build_objectivec_ios --skip-xcode-release
  234. }
  235. build_objectivec_ios_release() {
  236. build_objectivec_ios --skip-xcode-debug
  237. }
  238. build_objectivec_osx() {
  239. # Reused the build script that takes care of configuring and ensuring things
  240. # are up to date.
  241. objectivec/DevTools/full_mac_build.sh \
  242. --core-only --skip-xcode-ios
  243. }
  244. build_objectivec_cocoapods_integration() {
  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. # TODO(teboring): Disable jruby test temperarily for it randomly fails.
  296. # https://grpc-testing.appspot.com/job/protobuf_pull_request/735/consoleFull.
  297. # build_jruby
  298. }
  299. build_javascript() {
  300. internal_build_cpp
  301. cd js && npm install && npm test && cd ..
  302. }
  303. generate_php_test_proto() {
  304. internal_build_cpp
  305. pushd php/tests
  306. # Generate test file
  307. rm -rf generated
  308. mkdir generated
  309. ../../src/protoc --php_out=generated proto/test.proto proto/test_include.proto proto/test_no_namespace.proto
  310. pushd ../../src
  311. ./protoc --php_out=../php/tests/generated google/protobuf/empty.proto
  312. popd
  313. popd
  314. }
  315. use_php() {
  316. VERSION=$1
  317. PHP=`which php`
  318. PHP_CONFIG=`which php-config`
  319. PHPIZE=`which phpize`
  320. rm $PHP
  321. rm $PHP_CONFIG
  322. rm $PHPIZE
  323. cp "/usr/bin/php$VERSION" $PHP
  324. cp "/usr/bin/php-config$VERSION" $PHP_CONFIG
  325. cp "/usr/bin/phpize$VERSION" $PHPIZE
  326. generate_php_test_proto
  327. }
  328. use_php_zts() {
  329. VERSION=$1
  330. PHP=`which php`
  331. PHP_CONFIG=`which php-config`
  332. PHPIZE=`which phpize`
  333. ln -sfn "/usr/local/php-${VERSION}-zts/bin/php" $PHP
  334. ln -sfn "/usr/local/php-${VERSION}-zts/bin/php-config" $PHP_CONFIG
  335. ln -sfn "/usr/local/php-${VERSION}-zts/bin/phpize" $PHPIZE
  336. generate_php_test_proto
  337. }
  338. use_php_bc() {
  339. VERSION=$1
  340. PHP=`which php`
  341. PHP_CONFIG=`which php-config`
  342. PHPIZE=`which phpize`
  343. ln -sfn "/usr/local/php-${VERSION}-bc/bin/php" $PHP
  344. ln -sfn "/usr/local/php-${VERSION}-bc/bin/php-config" $PHP_CONFIG
  345. ln -sfn "/usr/local/php-${VERSION}-bc/bin/phpize" $PHPIZE
  346. generate_php_test_proto
  347. }
  348. build_php5.5() {
  349. use_php 5.5
  350. pushd php
  351. rm -rf vendor
  352. cp -r /usr/local/vendor-5.5 vendor
  353. ./vendor/bin/phpunit
  354. popd
  355. }
  356. build_php5.5_c() {
  357. use_php 5.5
  358. cd php/tests && /bin/bash ./test.sh && cd ../..
  359. }
  360. build_php5.5_zts_c() {
  361. use_php_zts 5.5
  362. wget https://phar.phpunit.de/phpunit-old.phar -O /usr/bin/phpunit
  363. cd php/tests && /bin/bash ./test.sh && cd ../..
  364. }
  365. build_php5.5_32() {
  366. use_php_bc 5.5
  367. pushd php
  368. rm -rf vendor
  369. cp -r /usr/local/vendor-5.5 vendor
  370. ./vendor/bin/phpunit
  371. popd
  372. }
  373. build_php5.5_c_32() {
  374. use_php_bc 5.5
  375. wget https://phar.phpunit.de/phpunit-old.phar -O /usr/bin/phpunit
  376. cd php/tests && /bin/bash ./test.sh && cd ../..
  377. }
  378. build_php5.6() {
  379. use_php 5.6
  380. pushd php
  381. rm -rf vendor
  382. cp -r /usr/local/vendor-5.6 vendor
  383. ./vendor/bin/phpunit
  384. popd
  385. }
  386. build_php5.6_c() {
  387. use_php 5.6
  388. cd php/tests && /bin/bash ./test.sh && cd ../..
  389. }
  390. build_php5.6_mac() {
  391. generate_php_test_proto
  392. # Install PHP
  393. curl -s https://php-osx.liip.ch/install.sh | bash -s 5.6
  394. PHP_FOLDER=`find /usr/local -type d -name "php5-5.6*"` # The folder name may change upon time
  395. export PATH="$PHP_FOLDER/bin:$PATH"
  396. # Install phpunit
  397. curl https://phar.phpunit.de/phpunit.phar -L -o phpunit.phar
  398. chmod +x phpunit.phar
  399. sudo mv phpunit.phar /usr/local/bin/phpunit
  400. # Install valgrind
  401. echo "#! /bin/bash" > valgrind
  402. chmod ug+x valgrind
  403. sudo mv valgrind /usr/local/bin/valgrind
  404. # Test
  405. cd php/tests && /bin/bash ./test.sh && cd ../..
  406. }
  407. build_php7.0() {
  408. use_php 7.0
  409. pushd php
  410. rm -rf vendor
  411. cp -r /usr/local/vendor-7.0 vendor
  412. ./vendor/bin/phpunit
  413. popd
  414. }
  415. build_php7.0_c() {
  416. use_php 7.0
  417. cd php/tests && /bin/bash ./test.sh && cd ../..
  418. }
  419. build_php_all() {
  420. build_php5.5
  421. build_php5.6
  422. build_php7.0
  423. build_php5.5_c
  424. build_php5.6_c
  425. # build_php7.0_c
  426. build_php5.5_zts_c
  427. }
  428. build_php_all_32() {
  429. build_php5.5_32
  430. build_php5.5_c_32
  431. }
  432. # Note: travis currently does not support testing more than one language so the
  433. # .travis.yml cheats and claims to only be cpp. If they add multiple language
  434. # support, this should probably get updated to install steps and/or
  435. # rvm/gemfile/jdk/etc. entries rather than manually doing the work.
  436. # .travis.yml uses matrix.exclude to block the cases where app-get can't be
  437. # use to install things.
  438. # -------- main --------
  439. if [ "$#" -ne 1 ]; then
  440. echo "
  441. Usage: $0 { cpp |
  442. cpp_distcheck |
  443. csharp |
  444. java_jdk7 |
  445. java_oracle7 |
  446. java_compatibility |
  447. javanano_jdk7 |
  448. javanano_oracle7 |
  449. objectivec_ios |
  450. objectivec_ios_debug |
  451. objectivec_ios_release |
  452. objectivec_osx |
  453. objectivec_cocoapods_integration |
  454. python |
  455. python_cpp |
  456. ruby21 |
  457. ruby22 |
  458. jruby |
  459. ruby_all |
  460. php5.5 |
  461. php5.5_c |
  462. php5.6 |
  463. php5.6_c |
  464. php7.0 |
  465. php7.0_c |
  466. php_all)
  467. "
  468. exit 1
  469. fi
  470. set -e # exit immediately on error
  471. set -x # display all commands
  472. eval "build_$1"