tests.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 jdk11
  104. $MVN install
  105. popd
  106. # Try to install Python
  107. python3 -m venv 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. jdk11)
  165. export PATH=/usr/lib/jvm/java-11-openjdk-amd64/bin:$PATH
  166. export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
  167. ;;
  168. jdk8)
  169. export PATH=/usr/lib/jvm/java-8-openjdk-amd64/bin:$PATH
  170. export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
  171. ;;
  172. jdk7)
  173. export PATH=/usr/lib/jvm/java-7-openjdk-amd64/bin:$PATH
  174. export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  175. ;;
  176. oracle7)
  177. export PATH=/usr/lib/jvm/java-7-oracle/bin:$PATH
  178. export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  179. ;;
  180. esac
  181. MAVEN_LOCAL_REPOSITORY=/var/maven_local_repository
  182. MVN="$MVN -e --quiet -Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=$MAVEN_LOCAL_REPOSITORY"
  183. which java
  184. java -version
  185. $MVN -version
  186. }
  187. # --batch-mode suppresses download progress output that spams the logs.
  188. MVN="mvn --batch-mode"
  189. internal_build_java() {
  190. version=$1
  191. dir=java_$version
  192. # Java build needs `protoc`.
  193. internal_build_cpp
  194. cp -r java $dir
  195. cd $dir && $MVN clean
  196. # Skip tests here - callers will decide what tests they want to run
  197. $MVN install -Dmaven.test.skip=true
  198. }
  199. build_java() {
  200. version=$1
  201. internal_build_java $version
  202. # Skip the Kotlin tests on Oracle 7
  203. if [ "$version" == "oracle7" ]; then
  204. $MVN test -pl bom,lite,core,util
  205. else
  206. $MVN test
  207. fi
  208. cd ../..
  209. }
  210. # The conformance tests are hard-coded to work with the $ROOT/java directory.
  211. # So this can't run in parallel with two different sets of tests.
  212. build_java_with_conformance_tests() {
  213. # Java build needs `protoc`.
  214. internal_build_cpp
  215. # This local installation avoids the problem caused by a new version not yet in Maven Central
  216. cd java/bom && $MVN install
  217. cd ../..
  218. cd java/core && $MVN test && $MVN install
  219. cd ../lite && $MVN test && $MVN install
  220. cd ../util && $MVN test && $MVN install && $MVN package assembly:single
  221. if [ "$version" == "jdk8" ]; then
  222. cd ../kotlin && $MVN test && $MVN install
  223. cd ../kotlin-lite && $MVN test && $MVN install
  224. fi
  225. cd ../..
  226. cd conformance && make test_java && cd ..
  227. }
  228. build_java_jdk7() {
  229. use_java jdk7
  230. build_java_with_conformance_tests
  231. }
  232. build_java_oracle7() {
  233. use_java oracle7
  234. build_java oracle7
  235. }
  236. build_java_linkage_monitor() {
  237. # Linkage Monitor checks compatibility with other Google libraries
  238. # https://github.com/GoogleCloudPlatform/cloud-opensource-java/tree/master/linkage-monitor
  239. use_java jdk11
  240. internal_build_cpp
  241. # Linkage Monitor uses $HOME/.m2 local repository
  242. MVN="mvn -e -B -Dhttps.protocols=TLSv1.2"
  243. cd java
  244. # Installs the snapshot version locally
  245. $MVN install -Dmaven.test.skip=true
  246. # Linkage Monitor uses the snapshot versions installed in $HOME/.m2 to verify compatibility
  247. JAR=linkage-monitor-latest-all-deps.jar
  248. curl -v -O "https://storage.googleapis.com/cloud-opensource-java-linkage-monitor/${JAR}"
  249. # Fails if there's new linkage errors compared with baseline
  250. java -jar $JAR com.google.cloud:libraries-bom
  251. }
  252. build_objectivec_ios() {
  253. # Reused the build script that takes care of configuring and ensuring things
  254. # are up to date. The OS X test runs the objc conformance test, so skip it
  255. # here.
  256. objectivec/DevTools/full_mac_build.sh \
  257. --core-only --skip-xcode-osx --skip-xcode-tvos --skip-objc-conformance "$@"
  258. }
  259. build_objectivec_ios_debug() {
  260. build_objectivec_ios --skip-xcode-release
  261. }
  262. build_objectivec_ios_release() {
  263. build_objectivec_ios --skip-xcode-debug
  264. }
  265. build_objectivec_osx() {
  266. # Reused the build script that takes care of configuring and ensuring things
  267. # are up to date.
  268. objectivec/DevTools/full_mac_build.sh \
  269. --core-only --skip-xcode-ios --skip-xcode-tvos
  270. }
  271. build_objectivec_tvos() {
  272. # Reused the build script that takes care of configuring and ensuring things
  273. # are up to date. The OS X test runs the objc conformance test, so skip it
  274. # here.
  275. objectivec/DevTools/full_mac_build.sh \
  276. --core-only --skip-xcode-ios --skip-xcode-osx --skip-objc-conformance "$@"
  277. }
  278. build_objectivec_tvos_debug() {
  279. build_objectivec_tvos --skip-xcode-release
  280. }
  281. build_objectivec_tvos_release() {
  282. build_objectivec_tvos --skip-xcode-debug
  283. }
  284. build_objectivec_cocoapods_integration() {
  285. objectivec/Tests/CocoaPods/run_tests.sh
  286. }
  287. build_python() {
  288. internal_build_cpp
  289. cd python
  290. tox --skip-missing-interpreters
  291. cd ..
  292. }
  293. build_python_version() {
  294. internal_build_cpp
  295. cd python
  296. envlist=$1
  297. tox -e $envlist
  298. cd ..
  299. }
  300. build_python37() {
  301. build_python_version py37-python
  302. }
  303. build_python38() {
  304. build_python_version py38-python
  305. }
  306. build_python39() {
  307. build_python_version py39-python
  308. }
  309. build_python310() {
  310. build_python_version py310-python
  311. }
  312. build_python_cpp() {
  313. internal_build_cpp
  314. export LD_LIBRARY_PATH=../src/.libs # for Linux
  315. export DYLD_LIBRARY_PATH=../src/.libs # for OS X
  316. cd python
  317. tox --skip-missing-interpreters
  318. cd ..
  319. }
  320. build_python_cpp_version() {
  321. internal_build_cpp
  322. export LD_LIBRARY_PATH=../src/.libs # for Linux
  323. export DYLD_LIBRARY_PATH=../src/.libs # for OS X
  324. cd python
  325. envlist=$1
  326. tox -e $envlist
  327. cd ..
  328. }
  329. build_python37_cpp() {
  330. build_python_cpp_version py37-cpp
  331. }
  332. build_python38_cpp() {
  333. build_python_cpp_version py38-cpp
  334. }
  335. build_python39_cpp() {
  336. build_python_cpp_version py39-cpp
  337. }
  338. build_python310_cpp() {
  339. build_python_cpp_version py310-cpp
  340. }
  341. build_ruby23() {
  342. internal_build_cpp # For conformance tests.
  343. cd ruby && bash travis-test.sh ruby-2.3.8 && cd ..
  344. }
  345. build_ruby24() {
  346. internal_build_cpp # For conformance tests.
  347. cd ruby && bash travis-test.sh ruby-2.4 && cd ..
  348. }
  349. build_ruby25() {
  350. internal_build_cpp # For conformance tests.
  351. cd ruby && bash travis-test.sh ruby-2.5.1 && cd ..
  352. }
  353. build_ruby26() {
  354. internal_build_cpp # For conformance tests.
  355. cd ruby && bash travis-test.sh ruby-2.6.0 && cd ..
  356. }
  357. build_ruby27() {
  358. internal_build_cpp # For conformance tests.
  359. cd ruby && bash travis-test.sh ruby-2.7.0 && cd ..
  360. }
  361. build_ruby30() {
  362. internal_build_cpp # For conformance tests.
  363. cd ruby && bash travis-test.sh ruby-3.0.2 && cd ..
  364. }
  365. build_ruby31() {
  366. internal_build_cpp # For conformance tests.
  367. cd ruby && bash travis-test.sh ruby-3.1.0 && cd ..
  368. }
  369. build_jruby92() {
  370. internal_build_cpp # For conformance tests.
  371. internal_build_java jdk8 && cd .. # For Maven protobuf jar with local changes
  372. cd ruby && bash travis-test.sh jruby-9.2.20.1 && cd ..
  373. }
  374. build_jruby93() {
  375. internal_build_cpp # For conformance tests.
  376. internal_build_java jdk8 && cd .. # For Maven protobuf jar with local changes
  377. cd ruby && bash travis-test.sh jruby-9.3.3.0 && cd ..
  378. }
  379. build_javascript() {
  380. internal_build_cpp
  381. NODE_VERSION=node-v12.16.3-darwin-x64
  382. NODE_TGZ="$NODE_VERSION.tar.gz"
  383. pushd /tmp
  384. curl -OL https://nodejs.org/dist/v12.16.3/$NODE_TGZ
  385. tar zxvf $NODE_TGZ
  386. export PATH=$PATH:`pwd`/$NODE_VERSION/bin
  387. popd
  388. cd js && npm install && npm test && cd ..
  389. cd conformance && make test_nodejs && cd ..
  390. }
  391. use_php() {
  392. VERSION=$1
  393. export PATH=/usr/local/php-${VERSION}/bin:$PATH
  394. internal_build_cpp
  395. }
  396. build_php() {
  397. use_php $1
  398. pushd php
  399. rm -rf vendor
  400. php -v
  401. php -m
  402. composer update
  403. composer test
  404. popd
  405. (cd conformance && make test_php)
  406. }
  407. test_php_c() {
  408. pushd php
  409. rm -rf vendor
  410. php -v
  411. php -m
  412. composer update
  413. composer test_c
  414. popd
  415. (cd conformance && make test_php_c)
  416. }
  417. build_php_c() {
  418. use_php $1
  419. test_php_c
  420. }
  421. build_php7.0_mac() {
  422. internal_build_cpp
  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 "php5-7.0*"` # The folder name may change upon time
  426. test ! -z "$PHP_FOLDER"
  427. export PATH="$PHP_FOLDER/bin:$PATH"
  428. # Install Composer
  429. wget https://getcomposer.org/download/2.0.13/composer.phar --progress=dot:mega -O /usr/local/bin/composer
  430. chmod a+x /usr/local/bin/composer
  431. # Install valgrind
  432. echo "#! /bin/bash" > valgrind
  433. chmod ug+x valgrind
  434. sudo mv valgrind /usr/local/bin/valgrind
  435. # Test
  436. test_php_c
  437. }
  438. build_php7.3_mac() {
  439. internal_build_cpp
  440. # Install PHP
  441. # We can't test PHP 7.4 with these binaries yet:
  442. # https://github.com/liip/php-osx/issues/276
  443. curl -s https://php-osx.liip.ch/install.sh | bash -s 7.3
  444. PHP_FOLDER=`find /usr/local -type d -name "php5-7.3*"` # The folder name may change upon time
  445. test ! -z "$PHP_FOLDER"
  446. export PATH="$PHP_FOLDER/bin:$PATH"
  447. # Install Composer
  448. wget https://getcomposer.org/download/2.0.13/composer.phar --progress=dot:mega -O /usr/local/bin/composer
  449. chmod a+x /usr/local/bin/composer
  450. # Install valgrind
  451. echo "#! /bin/bash" > valgrind
  452. chmod ug+x valgrind
  453. sudo mv valgrind /usr/local/bin/valgrind
  454. # Test
  455. test_php_c
  456. }
  457. build_php_compatibility() {
  458. internal_build_cpp
  459. php/tests/compatibility_test.sh $LAST_RELEASED
  460. }
  461. build_php_multirequest() {
  462. use_php 7.4
  463. php/tests/multirequest.sh
  464. }
  465. build_php8.0_all() {
  466. build_php 8.0
  467. build_php 8.1
  468. build_php_c 8.0
  469. build_php_c 8.1
  470. }
  471. build_php_all_32() {
  472. build_php 7.0
  473. build_php 7.1
  474. build_php 7.4
  475. build_php_c 7.0
  476. build_php_c 7.1
  477. build_php_c 7.4
  478. build_php_c 7.1-zts
  479. build_php_c 7.2-zts
  480. build_php_c 7.5-zts
  481. }
  482. build_php_all() {
  483. build_php_all_32
  484. build_php_multirequest
  485. build_php_compatibility
  486. }
  487. build_benchmark() {
  488. use_php 7.2
  489. cd kokoro/linux/benchmark && ./run.sh
  490. }
  491. # -------- main --------
  492. if [ "$#" -ne 1 ]; then
  493. echo "
  494. Usage: $0 { cpp |
  495. cpp_distcheck |
  496. csharp |
  497. java_jdk7 |
  498. java_oracle7 |
  499. java_linkage_monitor |
  500. objectivec_ios |
  501. objectivec_ios_debug |
  502. objectivec_ios_release |
  503. objectivec_osx |
  504. objectivec_tvos |
  505. objectivec_tvos_debug |
  506. objectivec_tvos_release |
  507. objectivec_cocoapods_integration |
  508. python |
  509. python_cpp |
  510. python_compatibility |
  511. ruby23 |
  512. ruby24 |
  513. ruby25 |
  514. ruby26 |
  515. ruby27 |
  516. ruby30 |
  517. ruby31 |
  518. jruby92 |
  519. jruby93 |
  520. ruby_all |
  521. php_all |
  522. php_all_32 |
  523. php7.0_mac |
  524. php7.3_mac |
  525. dist_install |
  526. benchmark)
  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"