tests.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. #!/bin/bash
  2. #
  3. # Build and run 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. # need to make sure the required compilers/tools are available.
  6. internal_build_cpp() {
  7. if [ -f src/protoc ]; then
  8. # Already built.
  9. return
  10. fi
  11. # Initialize any submodules.
  12. git submodule update --init --recursive
  13. ./autogen.sh
  14. ./configure CXXFLAGS="-fPIC -std=c++11" # -fPIC is needed for python cpp test.
  15. # See python/setup.py for more details
  16. make -j$(nproc)
  17. }
  18. build_cpp() {
  19. internal_build_cpp
  20. make check -j$(nproc) || (cat src/test-suite.log; false)
  21. cd conformance && make test_cpp && cd ..
  22. }
  23. build_cpp_tcmalloc() {
  24. internal_build_cpp
  25. ./configure LIBS=-ltcmalloc && make clean && make \
  26. PTHREAD_CFLAGS='-pthread -DGOOGLE_PROTOBUF_HEAP_CHECK_DRACONIAN' \
  27. check
  28. cd src
  29. PPROF_PATH=/usr/bin/google-pprof HEAPCHECK=strict ./protobuf-test
  30. }
  31. build_cpp_distcheck() {
  32. grep -q -- "-Og" src/Makefile.am &&
  33. echo "The -Og flag is incompatible with Clang versions older than 4.0." &&
  34. exit 1
  35. # Initialize any submodules.
  36. git submodule update --init --recursive
  37. ./autogen.sh
  38. ./configure
  39. make dist
  40. # List all files that should be included in the distribution package.
  41. git ls-files | grep "^\(java\|python\|objectivec\|csharp\|ruby\|php\|cmake\|examples\|src/google/protobuf/.*\.proto\)" |\
  42. grep -v ".gitignore" | grep -v "java/lite/proguard.pgcfg" |\
  43. grep -v "python/compatibility_tests" | grep -v "python/docs" | grep -v "python/.repo-metadata.json" |\
  44. grep -v "python/protobuf_distutils" | 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 -j$(nproc)
  64. }
  65. build_dist_install() {
  66. # Create a symlink pointing to python2 and put it at the beginning of $PATH.
  67. # This is necessary because the googletest build system involves a Python
  68. # script that is not compatible with Python 3. More recent googletest
  69. # versions have fixed this, but they have also removed the autotools build
  70. # system support that we rely on. This is a temporary workaround to keep the
  71. # googletest build working when the default python binary is Python 3.
  72. mkdir tmp || true
  73. pushd tmp
  74. ln -s /usr/bin/python2 ./python
  75. popd
  76. PATH=$PWD/tmp:$PATH
  77. # Initialize any submodules.
  78. git submodule update --init --recursive
  79. ./autogen.sh
  80. ./configure
  81. make dist
  82. # Unzip the dist tar file and install it.
  83. DIST=`ls *.tar.gz`
  84. tar -xf $DIST
  85. pushd ${DIST//.tar.gz}
  86. ./configure && make check -j4 && make install
  87. export LD_LIBRARY_PATH=/usr/local/lib
  88. # Try to install Java
  89. pushd java
  90. use_java jdk11
  91. $MVN install
  92. popd
  93. # Try to install Python
  94. python3 -m venv venv
  95. source venv/bin/activate
  96. pushd python
  97. python3 setup.py clean build sdist
  98. pip3 install dist/protobuf-*.tar.gz
  99. popd
  100. deactivate
  101. rm -rf python/venv
  102. }
  103. build_csharp() {
  104. # Required for conformance tests and to regenerate protos.
  105. internal_build_cpp
  106. NUGET=/usr/local/bin/nuget.exe
  107. # Disable some unwanted dotnet options
  108. export DOTNET_SKIP_FIRST_TIME_EXPERIENCE=true
  109. export DOTNET_CLI_TELEMETRY_OPTOUT=true
  110. # TODO(jtattermusch): is this still needed with "first time experience"
  111. # disabled?
  112. # Perform "dotnet new" once to get the setup preprocessing out of the
  113. # way. That spews a lot of output (including backspaces) into logs
  114. # otherwise, and can cause problems. It doesn't matter if this step
  115. # is performed multiple times; it's cheap after the first time anyway.
  116. # (It also doesn't matter if it's unnecessary, which it will be on some
  117. # systems. It's necessary on Jenkins in order to avoid unprintable
  118. # characters appearing in the JUnit output.)
  119. mkdir dotnettmp
  120. (cd dotnettmp; dotnet new > /dev/null)
  121. rm -rf dotnettmp
  122. # Check that the protos haven't broken C# codegen.
  123. # TODO(jonskeet): Fail if regenerating creates any changes.
  124. csharp/generate_protos.sh
  125. csharp/buildall.sh
  126. cd conformance && make test_csharp && cd ..
  127. # Run csharp compatibility test between 3.0.0 and the current version.
  128. csharp/compatibility_tests/v3.0.0/test.sh 3.0.0
  129. # Regression test for https://github.com/protocolbuffers/protobuf/issues/9526
  130. # - all line endings in .proto and .cs (and .csproj) files should be LF.
  131. if git ls-files --eol csharp | grep -E '\.cs|\.proto' | grep -v w/lf
  132. then
  133. echo "The files listed above have mixed or CRLF line endings; please change to LF."
  134. exit 1
  135. fi
  136. }
  137. build_golang() {
  138. # Go build needs `protoc`.
  139. internal_build_cpp
  140. # Add protoc to the path so that the examples build finds it.
  141. export PATH="`pwd`/src:$PATH"
  142. export GOPATH="$HOME/gocode"
  143. mkdir -p "$GOPATH/src/github.com/protocolbuffers"
  144. mkdir -p "$GOPATH/src/github.com/golang"
  145. rm -f "$GOPATH/src/github.com/protocolbuffers/protobuf"
  146. rm -f "$GOPATH/src/github.com/golang/protobuf"
  147. ln -s "`pwd`" "$GOPATH/src/github.com/protocolbuffers/protobuf"
  148. export PATH="$GOPATH/bin:$PATH"
  149. (cd $GOPATH/src/github.com/golang && git clone https://github.com/golang/protobuf.git && cd protobuf && git checkout v1.3.5)
  150. go install github.com/golang/protobuf/protoc-gen-go
  151. cd examples && PROTO_PATH="-I../src -I." make gotest && cd ..
  152. }
  153. use_java() {
  154. version=$1
  155. case "$version" in
  156. jdk17)
  157. export PATH=/usr/lib/jvm/java-17-openjdk-amd64/bin:$PATH
  158. export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
  159. ;;
  160. jdk11)
  161. export PATH=/usr/lib/jvm/java-11-openjdk-amd64/bin:$PATH
  162. export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
  163. ;;
  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 --quiet -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 -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_jdk8() {
  233. use_java jdk8
  234. build_java_with_conformance_tests
  235. }
  236. build_java_jdk11() {
  237. use_java jdk11
  238. build_java
  239. }
  240. build_java_jdk17() {
  241. use_java jdk17
  242. build_java
  243. }
  244. build_java_linkage_monitor() {
  245. # Linkage Monitor checks compatibility with other Google libraries
  246. # https://github.com/GoogleCloudPlatform/cloud-opensource-java/tree/master/linkage-monitor
  247. use_java jdk11
  248. internal_build_cpp
  249. # Linkage Monitor uses $HOME/.m2 local repository
  250. MVN="mvn -e -B -Dhttps.protocols=TLSv1.2"
  251. cd java
  252. # Installs the snapshot version locally
  253. $MVN install -Dmaven.test.skip=true
  254. # Linkage Monitor uses the snapshot versions installed in $HOME/.m2 to verify compatibility
  255. JAR=linkage-monitor-latest-all-deps.jar
  256. curl -v -O "https://storage.googleapis.com/cloud-opensource-java-linkage-monitor/${JAR}"
  257. # Fails if there's new linkage errors compared with baseline
  258. java -jar $JAR com.google.cloud:libraries-bom
  259. }
  260. build_objectivec_ios() {
  261. # Reused the build script that takes care of configuring and ensuring things
  262. # are up to date. The OS X test runs the objc conformance test, so skip it
  263. # here.
  264. objectivec/DevTools/full_mac_build.sh \
  265. --core-only --skip-xcode-osx --skip-xcode-tvos --skip-objc-conformance "$@"
  266. }
  267. build_objectivec_ios_debug() {
  268. build_objectivec_ios --skip-xcode-release
  269. }
  270. build_objectivec_ios_release() {
  271. build_objectivec_ios --skip-xcode-debug
  272. }
  273. build_objectivec_osx() {
  274. # Reused the build script that takes care of configuring and ensuring things
  275. # are up to date.
  276. objectivec/DevTools/full_mac_build.sh \
  277. --core-only --skip-xcode-ios --skip-xcode-tvos
  278. }
  279. build_objectivec_tvos() {
  280. # Reused the build script that takes care of configuring and ensuring things
  281. # are up to date. The OS X test runs the objc conformance test, so skip it
  282. # here.
  283. objectivec/DevTools/full_mac_build.sh \
  284. --core-only --skip-xcode-ios --skip-xcode-osx --skip-objc-conformance "$@"
  285. }
  286. build_objectivec_tvos_debug() {
  287. build_objectivec_tvos --skip-xcode-release
  288. }
  289. build_objectivec_tvos_release() {
  290. build_objectivec_tvos --skip-xcode-debug
  291. }
  292. build_python() {
  293. internal_build_cpp
  294. cd python
  295. tox --skip-missing-interpreters
  296. cd ..
  297. }
  298. build_python_version() {
  299. internal_build_cpp
  300. cd python
  301. envlist=$1
  302. tox -e $envlist
  303. cd ..
  304. }
  305. build_python37() {
  306. build_python_version py37-python
  307. }
  308. build_python38() {
  309. build_python_version py38-python
  310. }
  311. build_python39() {
  312. build_python_version py39-python
  313. }
  314. build_python310() {
  315. build_python_version py310-python
  316. }
  317. build_python_cpp() {
  318. internal_build_cpp
  319. export LD_LIBRARY_PATH=../src/.libs # for Linux
  320. export DYLD_LIBRARY_PATH=../src/.libs # for OS X
  321. cd python
  322. tox --skip-missing-interpreters
  323. cd ..
  324. }
  325. build_python_cpp_version() {
  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. envlist=$1
  331. tox -e $envlist
  332. cd ..
  333. }
  334. build_python37_cpp() {
  335. build_python_cpp_version py37-cpp
  336. }
  337. build_python38_cpp() {
  338. build_python_cpp_version py38-cpp
  339. }
  340. build_python39_cpp() {
  341. build_python_cpp_version py39-cpp
  342. }
  343. build_python310_cpp() {
  344. build_python_cpp_version py310-cpp
  345. }
  346. build_ruby23() {
  347. internal_build_cpp # For conformance tests.
  348. cd ruby && bash travis-test.sh ruby-2.3.8 && cd ..
  349. }
  350. build_ruby24() {
  351. internal_build_cpp # For conformance tests.
  352. cd ruby && bash travis-test.sh ruby-2.4 && cd ..
  353. }
  354. build_ruby25() {
  355. internal_build_cpp # For conformance tests.
  356. cd ruby && bash travis-test.sh ruby-2.5.1 && cd ..
  357. }
  358. build_ruby26() {
  359. internal_build_cpp # For conformance tests.
  360. cd ruby && bash travis-test.sh ruby-2.6.0 && cd ..
  361. }
  362. build_ruby27() {
  363. internal_build_cpp # For conformance tests.
  364. cd ruby && bash travis-test.sh ruby-2.7.0 && cd ..
  365. }
  366. build_ruby30() {
  367. internal_build_cpp # For conformance tests.
  368. cd ruby && bash travis-test.sh ruby-3.0.2 && cd ..
  369. }
  370. build_ruby31() {
  371. internal_build_cpp # For conformance tests.
  372. cd ruby && bash travis-test.sh ruby-3.1.0 && cd ..
  373. }
  374. build_jruby92() {
  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.2.20.1 && cd ..
  378. }
  379. build_jruby93() {
  380. internal_build_cpp # For conformance tests.
  381. internal_build_java jdk8 && cd .. # For Maven protobuf jar with local changes
  382. cd ruby && bash travis-test.sh jruby-9.3.4.0 && cd ..
  383. }
  384. use_php() {
  385. VERSION=$1
  386. export PATH=/usr/local/php-${VERSION}/bin:$PATH
  387. internal_build_cpp
  388. }
  389. build_php() {
  390. use_php $1
  391. pushd php
  392. rm -rf vendor
  393. php -v
  394. php -m
  395. composer update
  396. composer test
  397. popd
  398. (cd conformance && make test_php)
  399. }
  400. test_php_c() {
  401. pushd php
  402. rm -rf vendor
  403. php -v
  404. php -m
  405. composer update
  406. composer test_c
  407. popd
  408. (cd conformance && make test_php_c)
  409. }
  410. build_php_c() {
  411. use_php $1
  412. test_php_c
  413. }
  414. build_php7.0_mac() {
  415. internal_build_cpp
  416. # Install PHP
  417. curl -s https://php-osx.liip.ch/install.sh | bash -s 7.0
  418. PHP_FOLDER=`find /usr/local -type d -name "php5-7.0*"` # The folder name may change upon time
  419. test ! -z "$PHP_FOLDER"
  420. export PATH="$PHP_FOLDER/bin:$PATH"
  421. # Install Composer
  422. wget https://getcomposer.org/download/2.0.13/composer.phar --progress=dot:mega -O /usr/local/bin/composer
  423. chmod a+x /usr/local/bin/composer
  424. # Install valgrind
  425. echo "#! /bin/bash" > valgrind
  426. chmod ug+x valgrind
  427. sudo mv valgrind /usr/local/bin/valgrind
  428. # Test
  429. test_php_c
  430. }
  431. build_php7.3_mac() {
  432. internal_build_cpp
  433. # Install PHP
  434. # We can't test PHP 7.4 with these binaries yet:
  435. # https://github.com/liip/php-osx/issues/276
  436. curl -s https://php-osx.liip.ch/install.sh | bash -s 7.3
  437. PHP_FOLDER=`find /usr/local -type d -name "php5-7.3*"` # The folder name may change upon time
  438. test ! -z "$PHP_FOLDER"
  439. export PATH="$PHP_FOLDER/bin:$PATH"
  440. # Install Composer
  441. wget https://getcomposer.org/download/2.0.13/composer.phar --progress=dot:mega -O /usr/local/bin/composer
  442. chmod a+x /usr/local/bin/composer
  443. # Install valgrind
  444. echo "#! /bin/bash" > valgrind
  445. chmod ug+x valgrind
  446. sudo mv valgrind /usr/local/bin/valgrind
  447. # Test
  448. test_php_c
  449. }
  450. build_php_compatibility() {
  451. internal_build_cpp
  452. }
  453. build_php_multirequest() {
  454. use_php 7.4
  455. php/tests/multirequest.sh
  456. }
  457. build_php8.0_all() {
  458. build_php 8.0
  459. build_php 8.1
  460. build_php_c 8.0
  461. build_php_c 8.1
  462. }
  463. build_php_all_32() {
  464. build_php 7.0
  465. build_php 7.1
  466. build_php 7.4
  467. build_php_c 7.0
  468. build_php_c 7.1
  469. build_php_c 7.4
  470. build_php_c 7.1-zts
  471. build_php_c 7.2-zts
  472. build_php_c 7.5-zts
  473. }
  474. build_php_all() {
  475. build_php_all_32
  476. build_php_multirequest
  477. build_php_compatibility
  478. }
  479. build_benchmark() {
  480. use_php 7.2
  481. cd kokoro/linux/benchmark && ./run.sh
  482. }
  483. # -------- main --------
  484. if [ "$#" -ne 1 ]; then
  485. echo "
  486. Usage: $0 { cpp |
  487. cpp_distcheck |
  488. csharp |
  489. java_jdk7 |
  490. java_oracle7 |
  491. java_jdk8 |
  492. java_jdk11 |
  493. java_jdk17 |
  494. java_linkage_monitor |
  495. objectivec_ios |
  496. objectivec_ios_debug |
  497. objectivec_ios_release |
  498. objectivec_osx |
  499. objectivec_tvos |
  500. objectivec_tvos_debug |
  501. objectivec_tvos_release |
  502. python |
  503. python_cpp |
  504. python_compatibility |
  505. ruby23 |
  506. ruby24 |
  507. ruby25 |
  508. ruby26 |
  509. ruby27 |
  510. ruby30 |
  511. ruby31 |
  512. jruby92 |
  513. jruby93 |
  514. ruby_all |
  515. php_all |
  516. php_all_32 |
  517. php7.0_mac |
  518. php7.3_mac |
  519. dist_install |
  520. benchmark }
  521. "
  522. exit 1
  523. fi
  524. set -e # exit immediately on error
  525. set -x # display all commands
  526. cd $(dirname $0)
  527. eval "build_$1"