tests.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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/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. # 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. -I../../src -I. \
  255. proto/empty/echo.proto \
  256. proto/test.proto \
  257. proto/test_include.proto \
  258. proto/test_no_namespace.proto \
  259. proto/test_prefix.proto \
  260. proto/test_php_namespace.proto \
  261. proto/test_empty_php_namespace.proto \
  262. proto/test_reserved_enum_lower.proto \
  263. proto/test_reserved_enum_upper.proto \
  264. proto/test_reserved_enum_value_lower.proto \
  265. proto/test_reserved_enum_value_upper.proto \
  266. proto/test_reserved_message_lower.proto \
  267. proto/test_reserved_message_upper.proto \
  268. proto/test_service.proto \
  269. proto/test_service_namespace.proto \
  270. proto/test_wrapper_type_setters.proto \
  271. proto/test_descriptors.proto
  272. pushd ../../src
  273. ./protoc --php_out=../php/tests/generated -I../php/tests -I. \
  274. ../php/tests/proto/test_import_descriptor_proto.proto
  275. popd
  276. popd
  277. }
  278. use_php() {
  279. VERSION=$1
  280. PHP=`which php`
  281. PHP_CONFIG=`which php-config`
  282. PHPIZE=`which phpize`
  283. ln -sfn "/usr/local/php-${VERSION}/bin/php" $PHP
  284. ln -sfn "/usr/local/php-${VERSION}/bin/php-config" $PHP_CONFIG
  285. ln -sfn "/usr/local/php-${VERSION}/bin/phpize" $PHPIZE
  286. generate_php_test_proto
  287. }
  288. use_php_zts() {
  289. VERSION=$1
  290. PHP=`which php`
  291. PHP_CONFIG=`which php-config`
  292. PHPIZE=`which phpize`
  293. ln -sfn "/usr/local/php-${VERSION}-zts/bin/php" $PHP
  294. ln -sfn "/usr/local/php-${VERSION}-zts/bin/php-config" $PHP_CONFIG
  295. ln -sfn "/usr/local/php-${VERSION}-zts/bin/phpize" $PHPIZE
  296. generate_php_test_proto
  297. }
  298. use_php_bc() {
  299. VERSION=$1
  300. PHP=`which php`
  301. PHP_CONFIG=`which php-config`
  302. PHPIZE=`which phpize`
  303. ln -sfn "/usr/local/php-${VERSION}-bc/bin/php" $PHP
  304. ln -sfn "/usr/local/php-${VERSION}-bc/bin/php-config" $PHP_CONFIG
  305. ln -sfn "/usr/local/php-${VERSION}-bc/bin/phpize" $PHPIZE
  306. generate_php_test_proto
  307. }
  308. build_php5.5() {
  309. use_php 5.5
  310. pushd php
  311. rm -rf vendor
  312. cp -r /usr/local/vendor-5.5 vendor
  313. wget https://phar.phpunit.de/phpunit-4.8.0.phar -O /usr/bin/phpunit
  314. phpunit
  315. popd
  316. pushd conformance
  317. make test_php
  318. popd
  319. }
  320. build_php5.5_c() {
  321. use_php 5.5
  322. wget https://phar.phpunit.de/phpunit-4.8.0.phar -O /usr/bin/phpunit
  323. pushd php/tests
  324. /bin/bash ./test.sh 5.5
  325. popd
  326. # TODO(teboring): Add it back
  327. # pushd conformance
  328. # make test_php_c
  329. # popd
  330. }
  331. build_php5.5_zts_c() {
  332. use_php_zts 5.5
  333. wget https://phar.phpunit.de/phpunit-4.8.0.phar -O /usr/bin/phpunit
  334. cd php/tests && /bin/bash ./test.sh 5.5-zts && cd ../..
  335. # TODO(teboring): Add it back
  336. # pushd conformance
  337. # make test_php_zts_c
  338. # popd
  339. }
  340. build_php5.6() {
  341. use_php 5.6
  342. pushd php
  343. rm -rf vendor
  344. cp -r /usr/local/vendor-5.6 vendor
  345. wget https://phar.phpunit.de/phpunit-5.7.0.phar -O /usr/bin/phpunit
  346. phpunit
  347. popd
  348. pushd conformance
  349. make test_php
  350. popd
  351. }
  352. build_php5.6_c() {
  353. use_php 5.6
  354. wget https://phar.phpunit.de/phpunit-5.7.0.phar -O /usr/bin/phpunit
  355. cd php/tests && /bin/bash ./test.sh 5.6 && cd ../..
  356. # TODO(teboring): Add it back
  357. # pushd conformance
  358. # make test_php_c
  359. # popd
  360. }
  361. build_php5.6_zts_c() {
  362. use_php_zts 5.6
  363. wget https://phar.phpunit.de/phpunit-5.7.0.phar -O /usr/bin/phpunit
  364. cd php/tests && /bin/bash ./test.sh 5.6-zts && cd ../..
  365. # TODO(teboring): Add it back
  366. # pushd conformance
  367. # make test_php_zts_c
  368. # popd
  369. }
  370. build_php5.6_mac() {
  371. generate_php_test_proto
  372. # Install PHP
  373. curl -s https://php-osx.liip.ch/install.sh | bash -s 5.6
  374. PHP_FOLDER=`find /usr/local -type d -name "php5-5.6*"` # The folder name may change upon time
  375. export PATH="$PHP_FOLDER/bin:$PATH"
  376. # Install phpunit
  377. curl https://phar.phpunit.de/phpunit-5.6.10.phar -L -o phpunit.phar
  378. chmod +x phpunit.phar
  379. sudo mv phpunit.phar /usr/local/bin/phpunit
  380. # Install valgrind
  381. echo "#! /bin/bash" > valgrind
  382. chmod ug+x valgrind
  383. sudo mv valgrind /usr/local/bin/valgrind
  384. # Test
  385. cd php/tests && /bin/bash ./test.sh && cd ../..
  386. # TODO(teboring): Add it back
  387. # pushd conformance
  388. # make test_php_c
  389. # popd
  390. }
  391. build_php7.0() {
  392. use_php 7.0
  393. pushd php
  394. rm -rf vendor
  395. cp -r /usr/local/vendor-7.0 vendor
  396. wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
  397. phpunit
  398. popd
  399. pushd conformance
  400. make test_php
  401. popd
  402. }
  403. build_php7.0_c() {
  404. use_php 7.0
  405. wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
  406. cd php/tests && /bin/bash ./test.sh 7.0 && cd ../..
  407. # TODO(teboring): Add it back
  408. # pushd conformance
  409. # make test_php_c
  410. # popd
  411. }
  412. build_php7.0_zts_c() {
  413. use_php_zts 7.0
  414. wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
  415. cd php/tests && /bin/bash ./test.sh 7.0-zts && cd ../..
  416. # TODO(teboring): Add it back.
  417. # pushd conformance
  418. # make test_php_zts_c
  419. # popd
  420. }
  421. build_php7.0_mac() {
  422. generate_php_test_proto
  423. # Install PHP
  424. curl -s https://php-osx.liip.ch/install.sh | bash -s 7.0
  425. PHP_FOLDER=`find /usr/local -type d -name "php7-7.0*"` # The folder name may change upon time
  426. export PATH="$PHP_FOLDER/bin:$PATH"
  427. # Install phpunit
  428. curl https://phar.phpunit.de/phpunit-5.6.0.phar -L -o phpunit.phar
  429. chmod +x phpunit.phar
  430. sudo mv phpunit.phar /usr/local/bin/phpunit
  431. # Install valgrind
  432. echo "#! /bin/bash" > valgrind
  433. chmod ug+x valgrind
  434. sudo mv valgrind /usr/local/bin/valgrind
  435. # Test
  436. cd php/tests && /bin/bash ./test.sh && cd ../..
  437. # TODO(teboring): Add it back
  438. # pushd conformance
  439. # make test_php_c
  440. # popd
  441. }
  442. build_php_compatibility() {
  443. internal_build_cpp
  444. php/tests/compatibility_test.sh
  445. }
  446. build_php7.1() {
  447. use_php 7.1
  448. pushd php
  449. rm -rf vendor
  450. cp -r /usr/local/vendor-7.1 vendor
  451. wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
  452. phpunit
  453. popd
  454. pushd conformance
  455. make test_php
  456. popd
  457. }
  458. build_php7.1_c() {
  459. ENABLE_CONFORMANCE_TEST=$1
  460. use_php 7.1
  461. wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
  462. cd php/tests && /bin/bash ./test.sh 7.1 && cd ../..
  463. if [ "$ENABLE_CONFORMANCE_TEST" = "true" ]
  464. then
  465. pushd conformance
  466. make test_php_c
  467. popd
  468. fi
  469. }
  470. build_php7.1_zts_c() {
  471. use_php_zts 7.1
  472. wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
  473. cd php/tests && /bin/bash ./test.sh 7.1-zts && cd ../..
  474. pushd conformance
  475. # make test_php_c
  476. popd
  477. }
  478. build_php_all_32() {
  479. build_php5.5
  480. build_php5.6
  481. build_php7.0
  482. build_php7.1
  483. build_php5.5_c
  484. build_php5.6_c
  485. build_php7.0_c
  486. build_php7.1_c $1
  487. build_php5.5_zts_c
  488. build_php5.6_zts_c
  489. build_php7.0_zts_c
  490. build_php7.1_zts_c
  491. }
  492. build_php_all() {
  493. build_php_all_32 true
  494. build_php_compatibility
  495. }
  496. # -------- main --------
  497. if [ "$#" -ne 1 ]; then
  498. echo "
  499. Usage: $0 { cpp |
  500. cpp_distcheck |
  501. csharp |
  502. java_jdk7 |
  503. java_oracle7 |
  504. java_compatibility |
  505. objectivec_ios |
  506. objectivec_ios_debug |
  507. objectivec_ios_release |
  508. objectivec_osx |
  509. objectivec_cocoapods_integration |
  510. python |
  511. python_cpp |
  512. python_compatibility |
  513. ruby21 |
  514. ruby22 |
  515. jruby |
  516. ruby_all |
  517. php5.5 |
  518. php5.5_c |
  519. php5.6 |
  520. php5.6_c |
  521. php7.0 |
  522. php7.0_c |
  523. php_compatibility |
  524. php7.1 |
  525. php7.1_c |
  526. php_all)
  527. "
  528. exit 1
  529. fi
  530. set -e # exit immediately on error
  531. set -x # display all commands
  532. cd $(dirname $0)
  533. eval "build_$1"