tests.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. #!/bin/bash
  2. #
  3. # Build and runs tests for the protobuf project. We use this script to run
  4. # tests on kokoro (Ubuntu and MacOS). It can run locally as well but you
  5. # will need to make sure the required compilers/tools are available.
  6. # For when some other test needs the C++ main build, including protoc and
  7. # libprotobuf.
  8. internal_build_cpp() {
  9. if [ -f src/protoc ]; then
  10. # Already built.
  11. return
  12. fi
  13. # Initialize any submodules.
  14. git submodule update --init --recursive
  15. ./autogen.sh
  16. ./configure CXXFLAGS="-fPIC" # -fPIC is needed for python cpp test.
  17. # See python/setup.py for more details
  18. make -j4
  19. }
  20. build_cpp() {
  21. internal_build_cpp
  22. make check -j4 || (cat src/test-suite.log; false)
  23. cd conformance && make test_cpp && cd ..
  24. # The benchmark code depends on cmake, so test if it is installed before
  25. # trying to do the build.
  26. if [[ $(type cmake 2>/dev/null) ]]; then
  27. # Verify benchmarking code can build successfully.
  28. cd benchmarks && make cpp-benchmark && cd ..
  29. else
  30. echo ""
  31. echo "WARNING: Skipping validation of the bench marking code, cmake isn't installed."
  32. echo ""
  33. fi
  34. }
  35. build_cpp_distcheck() {
  36. # Initialize any submodules.
  37. git submodule update --init --recursive
  38. ./autogen.sh
  39. ./configure
  40. make dist
  41. # List all files that should be included in the distribution package.
  42. git ls-files | grep "^\(java\|python\|objectivec\|csharp\|js\|ruby\|php\|cmake\|examples\|src/google/protobuf/.*\.proto\)" |\
  43. grep -v ".gitignore" | grep -v "java/compatibility_tests" |\
  44. grep -v "python/compatibility_tests" | grep -v "csharp/compatibility_tests" > dist.lst
  45. # Unzip the dist tar file.
  46. DIST=`ls *.tar.gz`
  47. tar -xf $DIST
  48. cd ${DIST//.tar.gz}
  49. # Check if every file exists in the dist tar file.
  50. FILES_MISSING=""
  51. for FILE in $(<../dist.lst); do
  52. [ -f "$FILE" ] || {
  53. echo "$FILE is not found!"
  54. FILES_MISSING="$FILE $FILES_MISSING"
  55. }
  56. done
  57. cd ..
  58. if [ ! -z "$FILES_MISSING" ]; then
  59. echo "Missing files in EXTRA_DIST: $FILES_MISSING"
  60. exit 1
  61. fi
  62. # Do the regular dist-check for C++.
  63. make distcheck -j4
  64. }
  65. build_csharp() {
  66. # Required for conformance tests and to regenerate protos.
  67. internal_build_cpp
  68. NUGET=/usr/local/bin/nuget.exe
  69. # Perform "dotnet new" once to get the setup preprocessing out of the
  70. # way. That spews a lot of output (including backspaces) into logs
  71. # otherwise, and can cause problems. It doesn't matter if this step
  72. # is performed multiple times; it's cheap after the first time anyway.
  73. # (It also doesn't matter if it's unnecessary, which it will be on some
  74. # systems. It's necessary on Jenkins in order to avoid unprintable
  75. # characters appearing in the JUnit output.)
  76. mkdir dotnettmp
  77. (cd dotnettmp; dotnet new > /dev/null)
  78. rm -rf dotnettmp
  79. # Check that the protos haven't broken C# codegen.
  80. # TODO(jonskeet): Fail if regenerating creates any changes.
  81. csharp/generate_protos.sh
  82. csharp/buildall.sh
  83. cd conformance && make test_csharp && cd ..
  84. # Run csharp compatibility test between 3.0.0 and the current version.
  85. csharp/compatibility_tests/v3.0.0/test.sh 3.0.0
  86. }
  87. build_golang() {
  88. # Go build needs `protoc`.
  89. internal_build_cpp
  90. # Add protoc to the path so that the examples build finds it.
  91. export PATH="`pwd`/src:$PATH"
  92. export GOPATH="$HOME/gocode"
  93. mkdir -p "$GOPATH/src/github.com/google"
  94. rm -f "$GOPATH/src/github.com/protocolbuffers/protobuf"
  95. ln -s "`pwd`" "$GOPATH/src/github.com/protocolbuffers/protobuf"
  96. export PATH="$GOPATH/bin:$PATH"
  97. go get github.com/golang/protobuf/protoc-gen-go
  98. cd examples && PROTO_PATH="-I../src -I." make gotest && cd ..
  99. }
  100. use_java() {
  101. version=$1
  102. case "$version" in
  103. jdk7)
  104. export PATH=/usr/lib/jvm/java-7-openjdk-amd64/bin:$PATH
  105. export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  106. ;;
  107. oracle7)
  108. export PATH=/usr/lib/jvm/java-7-oracle/bin:$PATH
  109. export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  110. ;;
  111. esac
  112. MAVEN_LOCAL_REPOSITORY=/var/maven_local_repository
  113. MVN="$MVN -e -X --offline -Dmaven.repo.local=$MAVEN_LOCAL_REPOSITORY"
  114. which java
  115. java -version
  116. $MVN -version
  117. }
  118. # --batch-mode supresses download progress output that spams the logs.
  119. MVN="mvn --batch-mode"
  120. build_java() {
  121. version=$1
  122. dir=java_$version
  123. # Java build needs `protoc`.
  124. internal_build_cpp
  125. cp -r java $dir
  126. cd $dir && $MVN clean && $MVN test
  127. cd ../..
  128. }
  129. # The conformance tests are hard-coded to work with the $ROOT/java directory.
  130. # So this can't run in parallel with two different sets of tests.
  131. build_java_with_conformance_tests() {
  132. # Java build needs `protoc`.
  133. internal_build_cpp
  134. cd java && $MVN test && $MVN install
  135. cd util && $MVN package assembly:single
  136. cd ../..
  137. cd conformance && make test_java && cd ..
  138. }
  139. build_java_jdk7() {
  140. use_java jdk7
  141. build_java_with_conformance_tests
  142. }
  143. build_java_oracle7() {
  144. use_java oracle7
  145. build_java oracle7
  146. }
  147. build_java_compatibility() {
  148. use_java jdk7
  149. internal_build_cpp
  150. # Use the unit-tests extraced from 2.5.0 to test the compatibilty between
  151. # 3.0.0-beta-4 and the current version.
  152. cd java/compatibility_tests/v2.5.0
  153. ./test.sh 3.0.0-beta-4
  154. }
  155. build_objectivec_ios() {
  156. # Reused the build script that takes care of configuring and ensuring things
  157. # are up to date. The OS X test runs the objc conformance test, so skip it
  158. # here.
  159. # Note: travis has xctool installed, and we've looked at using it in the past
  160. # but it has ended up proving unreliable (bugs), an they are removing build
  161. # support in favor of xcbuild (or just xcodebuild).
  162. objectivec/DevTools/full_mac_build.sh \
  163. --core-only --skip-xcode-osx --skip-objc-conformance "$@"
  164. }
  165. build_objectivec_ios_debug() {
  166. build_objectivec_ios --skip-xcode-release
  167. }
  168. build_objectivec_ios_release() {
  169. build_objectivec_ios --skip-xcode-debug
  170. }
  171. build_objectivec_osx() {
  172. # Reused the build script that takes care of configuring and ensuring things
  173. # are up to date.
  174. objectivec/DevTools/full_mac_build.sh \
  175. --core-only --skip-xcode-ios
  176. }
  177. build_objectivec_cocoapods_integration() {
  178. # Update pod to the latest version.
  179. gem install cocoapods --no-ri --no-rdoc
  180. objectivec/Tests/CocoaPods/run_tests.sh
  181. }
  182. build_python() {
  183. internal_build_cpp
  184. cd python
  185. if [ $(uname -s) == "Linux" ]; then
  186. envlist=py\{27,33,34,35,36\}-python
  187. else
  188. envlist=py27-python
  189. fi
  190. tox -e $envlist
  191. cd ..
  192. }
  193. build_python_cpp() {
  194. internal_build_cpp
  195. export LD_LIBRARY_PATH=../src/.libs # for Linux
  196. export DYLD_LIBRARY_PATH=../src/.libs # for OS X
  197. cd python
  198. if [ $(uname -s) == "Linux" ]; then
  199. envlist=py\{27,33,34,35,36\}-cpp
  200. else
  201. envlist=py27-cpp
  202. fi
  203. tox -e $envlist
  204. cd ..
  205. }
  206. build_python_compatibility() {
  207. internal_build_cpp
  208. # Use the unit-tests extraced from 2.5.0 to test the compatibilty.
  209. cd python/compatibility_tests/v2.5.0
  210. # Test between 2.5.0 and the current version.
  211. ./test.sh 2.5.0
  212. # Test between 3.0.0-beta-1 and the current version.
  213. ./test.sh 3.0.0-beta-1
  214. }
  215. build_ruby21() {
  216. internal_build_cpp # For conformance tests.
  217. cd ruby && bash travis-test.sh ruby-2.1 && cd ..
  218. }
  219. build_ruby22() {
  220. internal_build_cpp # For conformance tests.
  221. cd ruby && bash travis-test.sh ruby-2.2 && cd ..
  222. }
  223. build_ruby23() {
  224. internal_build_cpp # For conformance tests.
  225. cd ruby && bash travis-test.sh ruby-2.3 && cd ..
  226. }
  227. build_ruby24() {
  228. internal_build_cpp # For conformance tests.
  229. cd ruby && bash travis-test.sh ruby-2.4 && cd ..
  230. }
  231. build_ruby25() {
  232. internal_build_cpp # For conformance tests.
  233. cd ruby && bash travis-test.sh ruby-2.5.0 && cd ..
  234. }
  235. build_ruby_all() {
  236. build_ruby21
  237. build_ruby22
  238. build_ruby23
  239. build_ruby24
  240. build_ruby25
  241. }
  242. build_javascript() {
  243. internal_build_cpp
  244. cd js && npm install && npm test && cd ..
  245. cd conformance && make test_nodejs && cd ..
  246. }
  247. generate_php_test_proto() {
  248. internal_build_cpp
  249. pushd php/tests
  250. # Generate test file
  251. rm -rf generated
  252. mkdir generated
  253. ../../src/protoc --php_out=generated \
  254. proto/empty/echo.proto \
  255. proto/test.proto \
  256. proto/test_include.proto \
  257. proto/test_no_namespace.proto \
  258. proto/test_prefix.proto \
  259. proto/test_php_namespace.proto \
  260. proto/test_empty_php_namespace.proto \
  261. proto/test_reserved_enum_lower.proto \
  262. proto/test_reserved_enum_upper.proto \
  263. proto/test_reserved_enum_value_lower.proto \
  264. proto/test_reserved_enum_value_upper.proto \
  265. proto/test_reserved_message_lower.proto \
  266. proto/test_reserved_message_upper.proto \
  267. proto/test_service.proto \
  268. proto/test_service_namespace.proto \
  269. proto/test_descriptors.proto
  270. pushd ../../src
  271. ./protoc --php_out=../php/tests/generated -I../php/tests -I. \
  272. ../php/tests/proto/test_import_descriptor_proto.proto
  273. popd
  274. popd
  275. }
  276. use_php() {
  277. VERSION=$1
  278. PHP=`which php`
  279. PHP_CONFIG=`which php-config`
  280. PHPIZE=`which phpize`
  281. ln -sfn "/usr/local/php-${VERSION}/bin/php" $PHP
  282. ln -sfn "/usr/local/php-${VERSION}/bin/php-config" $PHP_CONFIG
  283. ln -sfn "/usr/local/php-${VERSION}/bin/phpize" $PHPIZE
  284. generate_php_test_proto
  285. }
  286. use_php_zts() {
  287. VERSION=$1
  288. PHP=`which php`
  289. PHP_CONFIG=`which php-config`
  290. PHPIZE=`which phpize`
  291. ln -sfn "/usr/local/php-${VERSION}-zts/bin/php" $PHP
  292. ln -sfn "/usr/local/php-${VERSION}-zts/bin/php-config" $PHP_CONFIG
  293. ln -sfn "/usr/local/php-${VERSION}-zts/bin/phpize" $PHPIZE
  294. generate_php_test_proto
  295. }
  296. use_php_bc() {
  297. VERSION=$1
  298. PHP=`which php`
  299. PHP_CONFIG=`which php-config`
  300. PHPIZE=`which phpize`
  301. ln -sfn "/usr/local/php-${VERSION}-bc/bin/php" $PHP
  302. ln -sfn "/usr/local/php-${VERSION}-bc/bin/php-config" $PHP_CONFIG
  303. ln -sfn "/usr/local/php-${VERSION}-bc/bin/phpize" $PHPIZE
  304. generate_php_test_proto
  305. }
  306. build_php5.5() {
  307. use_php 5.5
  308. pushd php
  309. rm -rf vendor
  310. cp -r /usr/local/vendor-5.5 vendor
  311. wget https://phar.phpunit.de/phpunit-4.8.0.phar -O /usr/bin/phpunit
  312. phpunit
  313. popd
  314. pushd conformance
  315. make test_php
  316. popd
  317. }
  318. build_php5.5_c() {
  319. use_php 5.5
  320. wget https://phar.phpunit.de/phpunit-4.8.0.phar -O /usr/bin/phpunit
  321. pushd php/tests
  322. /bin/bash ./test.sh 5.5
  323. popd
  324. # TODO(teboring): Add it back
  325. # pushd conformance
  326. # make test_php_c
  327. # popd
  328. }
  329. build_php5.5_zts_c() {
  330. use_php_zts 5.5
  331. wget https://phar.phpunit.de/phpunit-4.8.0.phar -O /usr/bin/phpunit
  332. cd php/tests && /bin/bash ./test.sh 5.5-zts && cd ../..
  333. # TODO(teboring): Add it back
  334. # pushd conformance
  335. # make test_php_zts_c
  336. # popd
  337. }
  338. build_php5.6() {
  339. use_php 5.6
  340. pushd php
  341. rm -rf vendor
  342. cp -r /usr/local/vendor-5.6 vendor
  343. wget https://phar.phpunit.de/phpunit-5.7.0.phar -O /usr/bin/phpunit
  344. phpunit
  345. popd
  346. pushd conformance
  347. make test_php
  348. popd
  349. }
  350. build_php5.6_c() {
  351. use_php 5.6
  352. wget https://phar.phpunit.de/phpunit-5.7.0.phar -O /usr/bin/phpunit
  353. cd php/tests && /bin/bash ./test.sh 5.6 && cd ../..
  354. # TODO(teboring): Add it back
  355. # pushd conformance
  356. # make test_php_c
  357. # popd
  358. }
  359. build_php5.6_zts_c() {
  360. use_php_zts 5.6
  361. wget https://phar.phpunit.de/phpunit-5.7.0.phar -O /usr/bin/phpunit
  362. cd php/tests && /bin/bash ./test.sh 5.6-zts && cd ../..
  363. # TODO(teboring): Add it back
  364. # pushd conformance
  365. # make test_php_zts_c
  366. # popd
  367. }
  368. build_php5.6_mac() {
  369. generate_php_test_proto
  370. # Install PHP
  371. curl -s https://php-osx.liip.ch/install.sh | bash -s 5.6
  372. PHP_FOLDER=`find /usr/local -type d -name "php5-5.6*"` # The folder name may change upon time
  373. export PATH="$PHP_FOLDER/bin:$PATH"
  374. # Install phpunit
  375. curl https://phar.phpunit.de/phpunit-5.6.10.phar -L -o phpunit.phar
  376. chmod +x phpunit.phar
  377. sudo mv phpunit.phar /usr/local/bin/phpunit
  378. # Install valgrind
  379. echo "#! /bin/bash" > valgrind
  380. chmod ug+x valgrind
  381. sudo mv valgrind /usr/local/bin/valgrind
  382. # Test
  383. cd php/tests && /bin/bash ./test.sh && cd ../..
  384. # TODO(teboring): Add it back
  385. # pushd conformance
  386. # make test_php_c
  387. # popd
  388. }
  389. build_php7.0() {
  390. use_php 7.0
  391. pushd php
  392. rm -rf vendor
  393. cp -r /usr/local/vendor-7.0 vendor
  394. wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
  395. phpunit
  396. popd
  397. pushd conformance
  398. make test_php
  399. popd
  400. }
  401. build_php7.0_c() {
  402. use_php 7.0
  403. wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
  404. cd php/tests && /bin/bash ./test.sh 7.0 && cd ../..
  405. # TODO(teboring): Add it back
  406. # pushd conformance
  407. # make test_php_c
  408. # popd
  409. }
  410. build_php7.0_zts_c() {
  411. use_php_zts 7.0
  412. wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
  413. cd php/tests && /bin/bash ./test.sh 7.0-zts && cd ../..
  414. # TODO(teboring): Add it back.
  415. # pushd conformance
  416. # make test_php_zts_c
  417. # popd
  418. }
  419. build_php7.0_mac() {
  420. generate_php_test_proto
  421. # Install PHP
  422. curl -s https://php-osx.liip.ch/install.sh | bash -s 7.0
  423. PHP_FOLDER=`find /usr/local -type d -name "php7-7.0*"` # The folder name may change upon time
  424. export PATH="$PHP_FOLDER/bin:$PATH"
  425. # Install phpunit
  426. curl https://phar.phpunit.de/phpunit-5.6.0.phar -L -o phpunit.phar
  427. chmod +x phpunit.phar
  428. sudo mv phpunit.phar /usr/local/bin/phpunit
  429. # Install valgrind
  430. echo "#! /bin/bash" > valgrind
  431. chmod ug+x valgrind
  432. sudo mv valgrind /usr/local/bin/valgrind
  433. # Test
  434. cd php/tests && /bin/bash ./test.sh && cd ../..
  435. # TODO(teboring): Add it back
  436. # pushd conformance
  437. # make test_php_c
  438. # popd
  439. }
  440. build_php_compatibility() {
  441. internal_build_cpp
  442. php/tests/compatibility_test.sh
  443. }
  444. build_php7.1() {
  445. use_php 7.1
  446. pushd php
  447. rm -rf vendor
  448. cp -r /usr/local/vendor-7.1 vendor
  449. wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
  450. phpunit
  451. popd
  452. pushd conformance
  453. make test_php
  454. popd
  455. }
  456. build_php7.1_c() {
  457. ENABLE_CONFORMANCE_TEST=$1
  458. use_php 7.1
  459. wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
  460. cd php/tests && /bin/bash ./test.sh 7.1 && cd ../..
  461. if [ "$ENABLE_CONFORMANCE_TEST" = "true" ]
  462. then
  463. pushd conformance
  464. make test_php_c
  465. popd
  466. fi
  467. }
  468. build_php7.1_zts_c() {
  469. use_php_zts 7.1
  470. wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
  471. cd php/tests && /bin/bash ./test.sh 7.1-zts && cd ../..
  472. pushd conformance
  473. # make test_php_c
  474. popd
  475. }
  476. build_php_all_32() {
  477. build_php5.5
  478. build_php5.6
  479. build_php7.0
  480. build_php7.1
  481. build_php5.5_c
  482. build_php5.6_c
  483. build_php7.0_c
  484. build_php7.1_c $1
  485. build_php5.5_zts_c
  486. build_php5.6_zts_c
  487. build_php7.0_zts_c
  488. build_php7.1_zts_c
  489. }
  490. build_php_all() {
  491. build_php_all_32 true
  492. build_php_compatibility
  493. }
  494. # -------- main --------
  495. if [ "$#" -ne 1 ]; then
  496. echo "
  497. Usage: $0 { cpp |
  498. cpp_distcheck |
  499. csharp |
  500. java_jdk7 |
  501. java_oracle7 |
  502. java_compatibility |
  503. objectivec_ios |
  504. objectivec_ios_debug |
  505. objectivec_ios_release |
  506. objectivec_osx |
  507. objectivec_cocoapods_integration |
  508. python |
  509. python_cpp |
  510. python_compatibility |
  511. ruby21 |
  512. ruby22 |
  513. jruby |
  514. ruby_all |
  515. php5.5 |
  516. php5.5_c |
  517. php5.6 |
  518. php5.6_c |
  519. php7.0 |
  520. php7.0_c |
  521. php_compatibility |
  522. php7.1 |
  523. php7.1_c |
  524. php_all)
  525. "
  526. exit 1
  527. fi
  528. set -e # exit immediately on error
  529. set -x # display all commands
  530. cd $(dirname $0)
  531. eval "build_$1"