tests.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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_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/protocolbuffers"
  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 -Dhttps.protocols=TLSv1.2 -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. objectivec/DevTools/full_mac_build.sh \
  160. --core-only --skip-xcode-osx --skip-xcode-tvos --skip-objc-conformance "$@"
  161. }
  162. build_objectivec_ios_debug() {
  163. build_objectivec_ios --skip-xcode-release
  164. }
  165. build_objectivec_ios_release() {
  166. build_objectivec_ios --skip-xcode-debug
  167. }
  168. build_objectivec_osx() {
  169. # Reused the build script that takes care of configuring and ensuring things
  170. # are up to date.
  171. objectivec/DevTools/full_mac_build.sh \
  172. --core-only --skip-xcode-ios --skip-xcode-tvos
  173. }
  174. build_objectivec_tvos() {
  175. # Reused the build script that takes care of configuring and ensuring things
  176. # are up to date. The OS X test runs the objc conformance test, so skip it
  177. # here.
  178. objectivec/DevTools/full_mac_build.sh \
  179. --core-only --skip-xcode-ios --skip-xcode-osx --skip-objc-conformance "$@"
  180. }
  181. build_objectivec_tvos_debug() {
  182. build_objectivec_tvos --skip-xcode-release
  183. }
  184. build_objectivec_tvos_release() {
  185. build_objectivec_tvos --skip-xcode-debug
  186. }
  187. build_objectivec_cocoapods_integration() {
  188. # Update pod to the latest version.
  189. gem install cocoapods --no-ri --no-rdoc
  190. objectivec/Tests/CocoaPods/run_tests.sh
  191. }
  192. build_python() {
  193. internal_build_cpp
  194. cd python
  195. if [ $(uname -s) == "Linux" ]; then
  196. envlist=py\{27,33,34,35,36\}-python
  197. else
  198. envlist=py27-python
  199. fi
  200. tox -e $envlist
  201. cd ..
  202. }
  203. build_python_version() {
  204. internal_build_cpp
  205. cd python
  206. envlist=$1
  207. tox -e $envlist
  208. cd ..
  209. }
  210. build_python27() {
  211. build_python_version py27-python
  212. }
  213. build_python33() {
  214. build_python_version py33-python
  215. }
  216. build_python34() {
  217. build_python_version py34-python
  218. }
  219. build_python35() {
  220. build_python_version py35-python
  221. }
  222. build_python36() {
  223. build_python_version py36-python
  224. }
  225. build_python37() {
  226. build_python_version py37-python
  227. }
  228. build_python_cpp() {
  229. internal_build_cpp
  230. export LD_LIBRARY_PATH=../src/.libs # for Linux
  231. export DYLD_LIBRARY_PATH=../src/.libs # for OS X
  232. cd python
  233. if [ $(uname -s) == "Linux" ]; then
  234. envlist=py\{27,33,34,35,36\}-cpp
  235. else
  236. envlist=py27-cpp
  237. fi
  238. tox -e $envlist
  239. cd ..
  240. }
  241. build_python_cpp_version() {
  242. internal_build_cpp
  243. export LD_LIBRARY_PATH=../src/.libs # for Linux
  244. export DYLD_LIBRARY_PATH=../src/.libs # for OS X
  245. cd python
  246. envlist=$1
  247. tox -e $envlist
  248. cd ..
  249. }
  250. build_python27_cpp() {
  251. build_python_cpp_version py27-cpp
  252. }
  253. build_python33_cpp() {
  254. build_python_cpp_version py33-cpp
  255. }
  256. build_python34_cpp() {
  257. build_python_cpp_version py34-cpp
  258. }
  259. build_python35_cpp() {
  260. build_python_cpp_version py35-cpp
  261. }
  262. build_python36_cpp() {
  263. build_python_cpp_version py36-cpp
  264. }
  265. build_python37_cpp() {
  266. build_python_cpp_version py37-cpp
  267. }
  268. build_python_compatibility() {
  269. internal_build_cpp
  270. # Use the unit-tests extraced from 2.5.0 to test the compatibilty.
  271. cd python/compatibility_tests/v2.5.0
  272. # Test between 2.5.0 and the current version.
  273. ./test.sh 2.5.0
  274. # Test between 3.0.0-beta-1 and the current version.
  275. ./test.sh 3.0.0-beta-1
  276. }
  277. build_ruby23() {
  278. internal_build_cpp # For conformance tests.
  279. cd ruby && bash travis-test.sh ruby-2.3.8 && cd ..
  280. }
  281. build_ruby24() {
  282. internal_build_cpp # For conformance tests.
  283. cd ruby && bash travis-test.sh ruby-2.4 && cd ..
  284. }
  285. build_ruby25() {
  286. internal_build_cpp # For conformance tests.
  287. cd ruby && bash travis-test.sh ruby-2.5.1 && cd ..
  288. }
  289. build_ruby26() {
  290. internal_build_cpp # For conformance tests.
  291. cd ruby && bash travis-test.sh ruby-2.6.0 && cd ..
  292. }
  293. build_javascript() {
  294. internal_build_cpp
  295. cd js && npm install && npm test && cd ..
  296. cd conformance && make test_nodejs && cd ..
  297. }
  298. generate_php_test_proto() {
  299. internal_build_cpp
  300. pushd php/tests
  301. # Generate test file
  302. rm -rf generated
  303. mkdir generated
  304. ../../src/protoc --php_out=generated \
  305. -I../../src -I. \
  306. proto/empty/echo.proto \
  307. proto/test.proto \
  308. proto/test_include.proto \
  309. proto/test_no_namespace.proto \
  310. proto/test_prefix.proto \
  311. proto/test_php_namespace.proto \
  312. proto/test_empty_php_namespace.proto \
  313. proto/test_reserved_enum_lower.proto \
  314. proto/test_reserved_enum_upper.proto \
  315. proto/test_reserved_enum_value_lower.proto \
  316. proto/test_reserved_enum_value_upper.proto \
  317. proto/test_reserved_message_lower.proto \
  318. proto/test_reserved_message_upper.proto \
  319. proto/test_service.proto \
  320. proto/test_service_namespace.proto \
  321. proto/test_wrapper_type_setters.proto \
  322. proto/test_descriptors.proto
  323. pushd ../../src
  324. ./protoc --php_out=../php/tests/generated -I../php/tests -I. \
  325. ../php/tests/proto/test_import_descriptor_proto.proto
  326. popd
  327. popd
  328. }
  329. use_php() {
  330. VERSION=$1
  331. export PATH=/usr/local/php-${VERSION}/bin:$PATH
  332. export CPLUS_INCLUDE_PATH=/usr/local/php-${VERSION}/include/php/main:/usr/local/php-${VERSION}/include/php/:$CPLUS_INCLUDE_PATH
  333. export C_INCLUDE_PATH=/usr/local/php-${VERSION}/include/php/main:/usr/local/php-${VERSION}/include/php/:$C_INCLUDE_PATH
  334. generate_php_test_proto
  335. }
  336. use_php_zts() {
  337. VERSION=$1
  338. export PATH=/usr/local/php-${VERSION}-zts/bin:$PATH
  339. export CPLUS_INCLUDE_PATH=/usr/local/php-${VERSION}-zts/include/php/main:/usr/local/php-${VERSION}-zts/include/php/:$CPLUS_INCLUDE_PATH
  340. export C_INCLUDE_PATH=/usr/local/php-${VERSION}-zts/include/php/main:/usr/local/php-${VERSION}-zts/include/php/:$C_INCLUDE_PATH
  341. generate_php_test_proto
  342. }
  343. use_php_bc() {
  344. VERSION=$1
  345. export PATH=/usr/local/php-${VERSION}-bc/bin:$PATH
  346. export CPLUS_INCLUDE_PATH=/usr/local/php-${VERSION}-bc/include/php/main:/usr/local/php-${VERSION}-bc/include/php/:$CPLUS_INCLUDE_PATH
  347. export C_INCLUDE_PATH=/usr/local/php-${VERSION}-bc/include/php/main:/usr/local/php-${VERSION}-bc/include/php/:$C_INCLUDE_PATH
  348. generate_php_test_proto
  349. }
  350. build_php5.5() {
  351. use_php 5.5
  352. pushd php
  353. rm -rf vendor
  354. composer update
  355. ./vendor/bin/phpunit
  356. popd
  357. pushd conformance
  358. make test_php
  359. popd
  360. }
  361. build_php5.5_c() {
  362. use_php 5.5
  363. pushd php/tests
  364. /bin/bash ./test.sh 5.5
  365. popd
  366. # TODO(teboring): Add it back
  367. # pushd conformance
  368. # make test_php_c
  369. # popd
  370. }
  371. build_php5.5_zts_c() {
  372. use_php_zts 5.5
  373. cd php/tests && /bin/bash ./test.sh 5.5-zts && cd ../..
  374. # TODO(teboring): Add it back
  375. # pushd conformance
  376. # make test_php_zts_c
  377. # popd
  378. }
  379. build_php5.6() {
  380. use_php 5.6
  381. pushd php
  382. rm -rf vendor
  383. composer update
  384. ./vendor/bin/phpunit
  385. popd
  386. pushd conformance
  387. make test_php
  388. popd
  389. }
  390. build_php5.6_c() {
  391. use_php 5.6
  392. cd php/tests && /bin/bash ./test.sh 5.6 && cd ../..
  393. # TODO(teboring): Add it back
  394. # pushd conformance
  395. # make test_php_c
  396. # popd
  397. }
  398. build_php5.6_zts_c() {
  399. use_php_zts 5.6
  400. cd php/tests && /bin/bash ./test.sh 5.6-zts && cd ../..
  401. # TODO(teboring): Add it back
  402. # pushd conformance
  403. # make test_php_zts_c
  404. # popd
  405. }
  406. build_php5.6_mac() {
  407. generate_php_test_proto
  408. # Install PHP
  409. curl -s https://php-osx.liip.ch/install.sh | bash -s 5.6
  410. PHP_FOLDER=`find /usr/local -type d -name "php5-5.6*"` # The folder name may change upon time
  411. export PATH="$PHP_FOLDER/bin:$PATH"
  412. # Install phpunit
  413. curl https://phar.phpunit.de/phpunit-5.6.10.phar -L -o phpunit.phar
  414. chmod +x phpunit.phar
  415. sudo mv phpunit.phar /usr/local/bin/phpunit
  416. # Install valgrind
  417. echo "#! /bin/bash" > valgrind
  418. chmod ug+x valgrind
  419. sudo mv valgrind /usr/local/bin/valgrind
  420. # Test
  421. cd php/tests && /bin/bash ./test.sh && cd ../..
  422. # TODO(teboring): Add it back
  423. # pushd conformance
  424. # make test_php_c
  425. # popd
  426. }
  427. build_php7.0() {
  428. use_php 7.0
  429. pushd php
  430. rm -rf vendor
  431. composer update
  432. ./vendor/bin/phpunit
  433. popd
  434. pushd conformance
  435. make test_php
  436. popd
  437. }
  438. build_php7.0_c() {
  439. use_php 7.0
  440. cd php/tests && /bin/bash ./test.sh 7.0 && cd ../..
  441. # TODO(teboring): Add it back
  442. # pushd conformance
  443. # make test_php_c
  444. # popd
  445. }
  446. build_php7.0_zts_c() {
  447. use_php_zts 7.0
  448. cd php/tests && /bin/bash ./test.sh 7.0-zts && cd ../..
  449. # TODO(teboring): Add it back.
  450. # pushd conformance
  451. # make test_php_zts_c
  452. # popd
  453. }
  454. build_php7.0_mac() {
  455. generate_php_test_proto
  456. # Install PHP
  457. curl -s https://php-osx.liip.ch/install.sh | bash -s 7.0
  458. PHP_FOLDER=`find /usr/local -type d -name "php7-7.0*"` # The folder name may change upon time
  459. export PATH="$PHP_FOLDER/bin:$PATH"
  460. # Install phpunit
  461. curl https://phar.phpunit.de/phpunit-5.6.0.phar -L -o phpunit.phar
  462. chmod +x phpunit.phar
  463. sudo mv phpunit.phar /usr/local/bin/phpunit
  464. # Install valgrind
  465. echo "#! /bin/bash" > valgrind
  466. chmod ug+x valgrind
  467. sudo mv valgrind /usr/local/bin/valgrind
  468. # Test
  469. cd php/tests && /bin/bash ./test.sh && cd ../..
  470. # TODO(teboring): Add it back
  471. # pushd conformance
  472. # make test_php_c
  473. # popd
  474. }
  475. build_php_compatibility() {
  476. internal_build_cpp
  477. php/tests/compatibility_test.sh
  478. }
  479. build_php7.1() {
  480. use_php 7.1
  481. pushd php
  482. rm -rf vendor
  483. composer update
  484. ./vendor/bin/phpunit
  485. popd
  486. pushd conformance
  487. make test_php
  488. popd
  489. }
  490. build_php7.1_c() {
  491. ENABLE_CONFORMANCE_TEST=$1
  492. use_php 7.1
  493. cd php/tests && /bin/bash ./test.sh 7.1 && cd ../..
  494. if [ "$ENABLE_CONFORMANCE_TEST" = "true" ]
  495. then
  496. pushd conformance
  497. make test_php_c
  498. popd
  499. fi
  500. }
  501. build_php7.1_zts_c() {
  502. use_php_zts 7.1
  503. cd php/tests && /bin/bash ./test.sh 7.1-zts && cd ../..
  504. pushd conformance
  505. # make test_php_c
  506. popd
  507. }
  508. build_php_all_32() {
  509. build_php5.5
  510. build_php5.6
  511. build_php7.0
  512. build_php7.1
  513. build_php5.5_c
  514. build_php5.6_c
  515. build_php7.0_c
  516. build_php7.1_c $1
  517. build_php5.5_zts_c
  518. build_php5.6_zts_c
  519. build_php7.0_zts_c
  520. build_php7.1_zts_c
  521. }
  522. build_php_all() {
  523. build_php_all_32 true
  524. build_php_compatibility
  525. }
  526. build_benchmark() {
  527. use_php 7.2
  528. cd kokoro/linux/benchmark && ./run.sh
  529. }
  530. # -------- main --------
  531. if [ "$#" -ne 1 ]; then
  532. echo "
  533. Usage: $0 { cpp |
  534. cpp_distcheck |
  535. csharp |
  536. java_jdk7 |
  537. java_oracle7 |
  538. java_compatibility |
  539. objectivec_ios |
  540. objectivec_ios_debug |
  541. objectivec_ios_release |
  542. objectivec_osx |
  543. objectivec_tvos |
  544. objectivec_tvos_debug |
  545. objectivec_tvos_release |
  546. objectivec_cocoapods_integration |
  547. python |
  548. python_cpp |
  549. python_compatibility |
  550. ruby23 |
  551. ruby24 |
  552. ruby25 |
  553. ruby26 |
  554. jruby |
  555. ruby_all |
  556. php5.5 |
  557. php5.5_c |
  558. php5.6 |
  559. php5.6_c |
  560. php7.0 |
  561. php7.0_c |
  562. php_compatibility |
  563. php7.1 |
  564. php7.1_c |
  565. php_all |
  566. benchmark)
  567. "
  568. exit 1
  569. fi
  570. set -e # exit immediately on error
  571. set -x # display all commands
  572. cd $(dirname $0)
  573. eval "build_$1"