tests.sh 15 KB

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