tests.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. LAST_RELEASED=3.9.0
  9. internal_build_cpp() {
  10. if [ -f src/protoc ]; then
  11. # Already built.
  12. return
  13. fi
  14. # Initialize any submodules.
  15. git submodule update --init --recursive
  16. ./autogen.sh
  17. ./configure CXXFLAGS="-fPIC -std=c++11" # -fPIC is needed for python cpp test.
  18. # See python/setup.py for more details
  19. make -j$(nproc)
  20. }
  21. build_cpp() {
  22. internal_build_cpp
  23. make check -j$(nproc) || (cat src/test-suite.log; false)
  24. cd conformance && make test_cpp && cd ..
  25. # The benchmark code depends on cmake, so test if it is installed before
  26. # trying to do the build.
  27. if [[ $(type cmake 2>/dev/null) ]]; then
  28. # Verify benchmarking code can build successfully.
  29. cd benchmarks && make cpp-benchmark && cd ..
  30. else
  31. echo ""
  32. echo "WARNING: Skipping validation of the bench marking code, cmake isn't installed."
  33. echo ""
  34. fi
  35. }
  36. build_cpp_tcmalloc() {
  37. internal_build_cpp
  38. ./configure LIBS=-ltcmalloc && make clean && make \
  39. PTHREAD_CFLAGS='-pthread -DGOOGLE_PROTOBUF_HEAP_CHECK_DRACONIAN' \
  40. check
  41. cd src
  42. PPROF_PATH=/usr/bin/google-pprof HEAPCHECK=strict ./protobuf-test
  43. }
  44. build_cpp_distcheck() {
  45. grep -q -- "-Og" src/Makefile.am &&
  46. echo "The -Og flag is incompatible with Clang versions older than 4.0." &&
  47. exit 1
  48. # Initialize any submodules.
  49. git submodule update --init --recursive
  50. ./autogen.sh
  51. ./configure
  52. make dist
  53. # List all files that should be included in the distribution package.
  54. git ls-files | grep "^\(java\|python\|objectivec\|csharp\|js\|ruby\|php\|cmake\|examples\|src/google/protobuf/.*\.proto\)" |\
  55. grep -v ".gitignore" | grep -v "java/lite/proguard.pgcfg" |\
  56. grep -v "python/compatibility_tests" | grep -v "python/docs" | grep -v "python/.repo-metadata.json" |\
  57. grep -v "python/protobuf_distutils" | grep -v "csharp/compatibility_tests" > dist.lst
  58. # Unzip the dist tar file.
  59. DIST=`ls *.tar.gz`
  60. tar -xf $DIST
  61. cd ${DIST//.tar.gz}
  62. # Check if every file exists in the dist tar file.
  63. FILES_MISSING=""
  64. for FILE in $(<../dist.lst); do
  65. [ -f "$FILE" ] || {
  66. echo "$FILE is not found!"
  67. FILES_MISSING="$FILE $FILES_MISSING"
  68. }
  69. done
  70. cd ..
  71. if [ ! -z "$FILES_MISSING" ]; then
  72. echo "Missing files in EXTRA_DIST: $FILES_MISSING"
  73. exit 1
  74. fi
  75. # Do the regular dist-check for C++.
  76. make distcheck -j$(nproc)
  77. }
  78. build_dist_install() {
  79. # Create a symlink pointing to python2 and put it at the beginning of $PATH.
  80. # This is necessary because the googletest build system involves a Python
  81. # script that is not compatible with Python 3. More recent googletest
  82. # versions have fixed this, but they have also removed the autotools build
  83. # system support that we rely on. This is a temporary workaround to keep the
  84. # googletest build working when the default python binary is Python 3.
  85. mkdir tmp || true
  86. pushd tmp
  87. ln -s /usr/bin/python2 ./python
  88. popd
  89. PATH=$PWD/tmp:$PATH
  90. # Initialize any submodules.
  91. git submodule update --init --recursive
  92. ./autogen.sh
  93. ./configure
  94. make dist
  95. # Unzip the dist tar file and install it.
  96. DIST=`ls *.tar.gz`
  97. tar -xf $DIST
  98. pushd ${DIST//.tar.gz}
  99. ./configure && make check -j4 && make install
  100. export LD_LIBRARY_PATH=/usr/local/lib
  101. # Try to install Java
  102. pushd java
  103. use_java jdk8
  104. $MVN install
  105. popd
  106. # Try to install Python
  107. virtualenv --no-site-packages venv
  108. source venv/bin/activate
  109. pushd python
  110. python3 setup.py clean build sdist
  111. pip3 install dist/protobuf-*.tar.gz
  112. popd
  113. deactivate
  114. rm -rf python/venv
  115. }
  116. build_csharp() {
  117. # Required for conformance tests and to regenerate protos.
  118. internal_build_cpp
  119. NUGET=/usr/local/bin/nuget.exe
  120. # Disable some unwanted dotnet options
  121. export DOTNET_SKIP_FIRST_TIME_EXPERIENCE=true
  122. export DOTNET_CLI_TELEMETRY_OPTOUT=true
  123. # TODO(jtattermusch): is this still needed with "first time experience"
  124. # disabled?
  125. # Perform "dotnet new" once to get the setup preprocessing out of the
  126. # way. That spews a lot of output (including backspaces) into logs
  127. # otherwise, and can cause problems. It doesn't matter if this step
  128. # is performed multiple times; it's cheap after the first time anyway.
  129. # (It also doesn't matter if it's unnecessary, which it will be on some
  130. # systems. It's necessary on Jenkins in order to avoid unprintable
  131. # characters appearing in the JUnit output.)
  132. mkdir dotnettmp
  133. (cd dotnettmp; dotnet new > /dev/null)
  134. rm -rf dotnettmp
  135. # Check that the protos haven't broken C# codegen.
  136. # TODO(jonskeet): Fail if regenerating creates any changes.
  137. csharp/generate_protos.sh
  138. csharp/buildall.sh
  139. cd conformance && make test_csharp && cd ..
  140. # Run csharp compatibility test between 3.0.0 and the current version.
  141. csharp/compatibility_tests/v3.0.0/test.sh 3.0.0
  142. # Run csharp compatibility test between last released and the current version.
  143. csharp/compatibility_tests/v3.0.0/test.sh $LAST_RELEASED
  144. }
  145. build_golang() {
  146. # Go build needs `protoc`.
  147. internal_build_cpp
  148. # Add protoc to the path so that the examples build finds it.
  149. export PATH="`pwd`/src:$PATH"
  150. export GOPATH="$HOME/gocode"
  151. mkdir -p "$GOPATH/src/github.com/protocolbuffers"
  152. mkdir -p "$GOPATH/src/github.com/golang"
  153. rm -f "$GOPATH/src/github.com/protocolbuffers/protobuf"
  154. rm -f "$GOPATH/src/github.com/golang/protobuf"
  155. ln -s "`pwd`" "$GOPATH/src/github.com/protocolbuffers/protobuf"
  156. export PATH="$GOPATH/bin:$PATH"
  157. (cd $GOPATH/src/github.com/golang && git clone https://github.com/golang/protobuf.git && cd protobuf && git checkout v1.3.5)
  158. go install github.com/golang/protobuf/protoc-gen-go
  159. cd examples && PROTO_PATH="-I../src -I." make gotest && cd ..
  160. }
  161. use_java() {
  162. version=$1
  163. case "$version" in
  164. jdk8)
  165. export PATH=/usr/lib/jvm/java-8-openjdk-amd64/bin:$PATH
  166. export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
  167. ;;
  168. jdk7)
  169. export PATH=/usr/lib/jvm/java-7-openjdk-amd64/bin:$PATH
  170. export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  171. ;;
  172. oracle7)
  173. export PATH=/usr/lib/jvm/java-7-oracle/bin:$PATH
  174. export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  175. ;;
  176. esac
  177. MAVEN_LOCAL_REPOSITORY=/var/maven_local_repository
  178. MVN="$MVN -e -X -Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=$MAVEN_LOCAL_REPOSITORY"
  179. which java
  180. java -version
  181. $MVN -version
  182. }
  183. # --batch-mode suppresses download progress output that spams the logs.
  184. MVN="mvn --batch-mode"
  185. internal_build_java() {
  186. version=$1
  187. dir=java_$version
  188. # Java build needs `protoc`.
  189. internal_build_cpp
  190. cp -r java $dir
  191. cd $dir && $MVN clean
  192. # Skip tests here - callers will decide what tests they want to run
  193. $MVN install -pl core -Dmaven.test.skip=true
  194. }
  195. build_java() {
  196. version=$1
  197. internal_build_java $version
  198. # Skip the Kotlin tests on Oracle 7
  199. if [ "$version" == "oracle7" ]; then
  200. $MVN test -pl bom,lite,core,util
  201. else
  202. $MVN test
  203. fi
  204. cd ../..
  205. }
  206. # The conformance tests are hard-coded to work with the $ROOT/java directory.
  207. # So this can't run in parallel with two different sets of tests.
  208. build_java_with_conformance_tests() {
  209. # Java build needs `protoc`.
  210. internal_build_cpp
  211. # This local installation avoids the problem caused by a new version not yet in Maven Central
  212. cd java/bom && $MVN install
  213. cd ../..
  214. cd java/core && $MVN test && $MVN install
  215. cd ../lite && $MVN test && $MVN install
  216. cd ../util && $MVN test && $MVN install && $MVN package assembly:single
  217. if [ "$version" == "jdk8" ]; then
  218. cd ../kotlin && $MVN test && $MVN install
  219. cd ../kotlin-lite && $MVN test && $MVN install
  220. fi
  221. cd ../..
  222. cd conformance && make test_java && cd ..
  223. }
  224. build_java_jdk7() {
  225. use_java jdk7
  226. build_java_with_conformance_tests
  227. }
  228. build_java_oracle7() {
  229. use_java oracle7
  230. build_java oracle7
  231. }
  232. build_java_linkage_monitor() {
  233. # Linkage Monitor checks compatibility with other Google libraries
  234. # https://github.com/GoogleCloudPlatform/cloud-opensource-java/tree/master/linkage-monitor
  235. use_java jdk8
  236. internal_build_cpp
  237. # Linkage Monitor uses $HOME/.m2 local repository
  238. MVN="mvn -e -B -Dhttps.protocols=TLSv1.2"
  239. cd java
  240. # Installs the snapshot version locally
  241. $MVN install -Dmaven.test.skip=true
  242. # Linkage Monitor uses the snapshot versions installed in $HOME/.m2 to verify compatibility
  243. JAR=linkage-monitor-latest-all-deps.jar
  244. curl -v -O "https://storage.googleapis.com/cloud-opensource-java-linkage-monitor/${JAR}"
  245. # Fails if there's new linkage errors compared with baseline
  246. java -jar $JAR com.google.cloud:libraries-bom
  247. }
  248. build_objectivec_ios() {
  249. # Reused the build script that takes care of configuring and ensuring things
  250. # are up to date. The OS X test runs the objc conformance test, so skip it
  251. # here.
  252. objectivec/DevTools/full_mac_build.sh \
  253. --core-only --skip-xcode-osx --skip-xcode-tvos --skip-objc-conformance "$@"
  254. }
  255. build_objectivec_ios_debug() {
  256. build_objectivec_ios --skip-xcode-release
  257. }
  258. build_objectivec_ios_release() {
  259. build_objectivec_ios --skip-xcode-debug
  260. }
  261. build_objectivec_osx() {
  262. # Reused the build script that takes care of configuring and ensuring things
  263. # are up to date.
  264. objectivec/DevTools/full_mac_build.sh \
  265. --core-only --skip-xcode-ios --skip-xcode-tvos
  266. }
  267. build_objectivec_tvos() {
  268. # Reused the build script that takes care of configuring and ensuring things
  269. # are up to date. The OS X test runs the objc conformance test, so skip it
  270. # here.
  271. objectivec/DevTools/full_mac_build.sh \
  272. --core-only --skip-xcode-ios --skip-xcode-osx --skip-objc-conformance "$@"
  273. }
  274. build_objectivec_tvos_debug() {
  275. build_objectivec_tvos --skip-xcode-release
  276. }
  277. build_objectivec_tvos_release() {
  278. build_objectivec_tvos --skip-xcode-debug
  279. }
  280. build_objectivec_cocoapods_integration() {
  281. objectivec/Tests/CocoaPods/run_tests.sh
  282. }
  283. build_python() {
  284. internal_build_cpp
  285. cd python
  286. if [ $(uname -s) == "Linux" ]; then
  287. envlist=py\{35,36\}-python
  288. else
  289. envlist=py\{36\}-python
  290. fi
  291. python -m tox -e $envlist
  292. cd ..
  293. }
  294. build_python_version() {
  295. internal_build_cpp
  296. cd python
  297. envlist=$1
  298. python -m tox -e $envlist
  299. cd ..
  300. }
  301. build_python33() {
  302. build_python_version py33-python
  303. }
  304. build_python34() {
  305. build_python_version py34-python
  306. }
  307. build_python35() {
  308. build_python_version py35-python
  309. }
  310. build_python36() {
  311. build_python_version py36-python
  312. }
  313. build_python37() {
  314. build_python_version py37-python
  315. }
  316. build_python38() {
  317. build_python_version py38-python
  318. }
  319. build_python39() {
  320. build_python_version py39-python
  321. }
  322. build_python310() {
  323. build_python_version py310-python
  324. }
  325. build_python_cpp() {
  326. internal_build_cpp
  327. export LD_LIBRARY_PATH=../src/.libs # for Linux
  328. export DYLD_LIBRARY_PATH=../src/.libs # for OS X
  329. cd python
  330. if [ $(uname -s) == "Linux" ]; then
  331. envlist=py\{35,36\}-cpp
  332. else
  333. envlist=py\{36\}-cpp
  334. fi
  335. tox -e $envlist
  336. cd ..
  337. }
  338. build_python_cpp_version() {
  339. internal_build_cpp
  340. export LD_LIBRARY_PATH=../src/.libs # for Linux
  341. export DYLD_LIBRARY_PATH=../src/.libs # for OS X
  342. cd python
  343. envlist=$1
  344. tox -e $envlist
  345. cd ..
  346. }
  347. build_python33_cpp() {
  348. build_python_cpp_version py33-cpp
  349. }
  350. build_python34_cpp() {
  351. build_python_cpp_version py34-cpp
  352. }
  353. build_python35_cpp() {
  354. build_python_cpp_version py35-cpp
  355. }
  356. build_python36_cpp() {
  357. build_python_cpp_version py36-cpp
  358. }
  359. build_python37_cpp() {
  360. build_python_cpp_version py37-cpp
  361. }
  362. build_python38_cpp() {
  363. build_python_cpp_version py38-cpp
  364. }
  365. build_python39_cpp() {
  366. build_python_cpp_version py39-cpp
  367. }
  368. build_python310_cpp() {
  369. build_python_cpp_version py310-cpp
  370. }
  371. build_ruby23() {
  372. internal_build_cpp # For conformance tests.
  373. cd ruby && bash travis-test.sh ruby-2.3.8 && cd ..
  374. }
  375. build_ruby24() {
  376. internal_build_cpp # For conformance tests.
  377. cd ruby && bash travis-test.sh ruby-2.4 && cd ..
  378. }
  379. build_ruby25() {
  380. internal_build_cpp # For conformance tests.
  381. cd ruby && bash travis-test.sh ruby-2.5.1 && cd ..
  382. }
  383. build_ruby26() {
  384. internal_build_cpp # For conformance tests.
  385. cd ruby && bash travis-test.sh ruby-2.6.0 && cd ..
  386. }
  387. build_ruby27() {
  388. internal_build_cpp # For conformance tests.
  389. cd ruby && bash travis-test.sh ruby-2.7.0 && cd ..
  390. }
  391. build_ruby30() {
  392. internal_build_cpp # For conformance tests.
  393. cd ruby && bash travis-test.sh ruby-3.0.2 && cd ..
  394. }
  395. build_jruby92() {
  396. internal_build_cpp # For conformance tests.
  397. internal_build_java jdk8 && cd .. # For Maven protobuf jar with local changes
  398. cd ruby && bash travis-test.sh jruby-9.2.19.0 && cd ..
  399. }
  400. build_jruby93() {
  401. internal_build_cpp # For conformance tests.
  402. internal_build_java jdk8 && cd .. # For Maven protobuf jar with local changes
  403. cd ruby && bash travis-test.sh jruby-9.3.0.0 && cd ..
  404. }
  405. build_javascript() {
  406. internal_build_cpp
  407. NODE_VERSION=node-v12.16.3-darwin-x64
  408. NODE_TGZ="$NODE_VERSION.tar.gz"
  409. pushd /tmp
  410. curl -OL https://nodejs.org/dist/v12.16.3/$NODE_TGZ
  411. tar zxvf $NODE_TGZ
  412. export PATH=$PATH:`pwd`/$NODE_VERSION/bin
  413. popd
  414. cd js && npm install && npm test && cd ..
  415. cd conformance && make test_nodejs && cd ..
  416. }
  417. use_php() {
  418. VERSION=$1
  419. export PATH=/usr/local/php-${VERSION}/bin:$PATH
  420. internal_build_cpp
  421. }
  422. build_php() {
  423. use_php $1
  424. pushd php
  425. rm -rf vendor
  426. composer update
  427. composer test
  428. popd
  429. (cd conformance && make test_php)
  430. }
  431. test_php_c() {
  432. pushd php
  433. rm -rf vendor
  434. composer update
  435. composer test_c
  436. popd
  437. (cd conformance && make test_php_c)
  438. }
  439. build_php_c() {
  440. use_php $1
  441. test_php_c
  442. }
  443. build_php7.0_mac() {
  444. internal_build_cpp
  445. # Install PHP
  446. curl -s https://php-osx.liip.ch/install.sh | bash -s 7.0
  447. PHP_FOLDER=`find /usr/local -type d -name "php5-7.0*"` # The folder name may change upon time
  448. test ! -z "$PHP_FOLDER"
  449. export PATH="$PHP_FOLDER/bin:$PATH"
  450. # Install Composer
  451. wget https://getcomposer.org/download/2.0.13/composer.phar --progress=dot:mega -O /usr/local/bin/composer
  452. chmod a+x /usr/local/bin/composer
  453. # Install valgrind
  454. echo "#! /bin/bash" > valgrind
  455. chmod ug+x valgrind
  456. sudo mv valgrind /usr/local/bin/valgrind
  457. # Test
  458. test_php_c
  459. }
  460. build_php7.3_mac() {
  461. internal_build_cpp
  462. # Install PHP
  463. # We can't test PHP 7.4 with these binaries yet:
  464. # https://github.com/liip/php-osx/issues/276
  465. curl -s https://php-osx.liip.ch/install.sh | bash -s 7.3
  466. PHP_FOLDER=`find /usr/local -type d -name "php5-7.3*"` # The folder name may change upon time
  467. test ! -z "$PHP_FOLDER"
  468. export PATH="$PHP_FOLDER/bin:$PATH"
  469. # Install Composer
  470. wget https://getcomposer.org/download/2.0.13/composer.phar --progress=dot:mega -O /usr/local/bin/composer
  471. chmod a+x /usr/local/bin/composer
  472. # Install valgrind
  473. echo "#! /bin/bash" > valgrind
  474. chmod ug+x valgrind
  475. sudo mv valgrind /usr/local/bin/valgrind
  476. # Test
  477. test_php_c
  478. }
  479. build_php_compatibility() {
  480. internal_build_cpp
  481. php/tests/compatibility_test.sh $LAST_RELEASED
  482. }
  483. build_php_multirequest() {
  484. use_php 7.4
  485. php/tests/multirequest.sh
  486. }
  487. build_php8.0_all() {
  488. build_php 8.0
  489. build_php_c 8.0
  490. }
  491. build_php_all_32() {
  492. build_php 7.0
  493. build_php 7.1
  494. build_php 7.4
  495. build_php_c 7.0
  496. build_php_c 7.1
  497. build_php_c 7.4
  498. build_php_c 7.1-zts
  499. build_php_c 7.2-zts
  500. build_php_c 7.5-zts
  501. }
  502. build_php_all() {
  503. build_php_all_32
  504. build_php_multirequest
  505. build_php_compatibility
  506. }
  507. build_benchmark() {
  508. use_php 7.2
  509. cd kokoro/linux/benchmark && ./run.sh
  510. }
  511. # -------- main --------
  512. if [ "$#" -ne 1 ]; then
  513. echo "
  514. Usage: $0 { cpp |
  515. cpp_distcheck |
  516. csharp |
  517. java_jdk7 |
  518. java_oracle7 |
  519. java_linkage_monitor |
  520. objectivec_ios |
  521. objectivec_ios_debug |
  522. objectivec_ios_release |
  523. objectivec_osx |
  524. objectivec_tvos |
  525. objectivec_tvos_debug |
  526. objectivec_tvos_release |
  527. objectivec_cocoapods_integration |
  528. python |
  529. python_cpp |
  530. python_compatibility |
  531. ruby23 |
  532. ruby24 |
  533. ruby25 |
  534. ruby26 |
  535. ruby27 |
  536. ruby30 |
  537. jruby92 |
  538. jruby93 |
  539. ruby_all |
  540. php_all |
  541. php_all_32 |
  542. php7.0_mac |
  543. php7.3_mac |
  544. dist_install |
  545. benchmark)
  546. "
  547. exit 1
  548. fi
  549. set -e # exit immediately on error
  550. set -x # display all commands
  551. cd $(dirname $0)
  552. eval "build_$1"