tests.sh 16 KB

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