tests.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. # Initialize any submodules.
  80. git submodule update --init --recursive
  81. ./autogen.sh
  82. ./configure
  83. make dist
  84. # Unzip the dist tar file and install it.
  85. DIST=`ls *.tar.gz`
  86. tar -xf $DIST
  87. pushd ${DIST//.tar.gz}
  88. ./configure && make check -j4 && make install
  89. export LD_LIBRARY_PATH=/usr/local/lib
  90. # Try to install Java
  91. pushd java
  92. use_java jdk8
  93. $MVN install
  94. popd
  95. # Try to install Python
  96. virtualenv --no-site-packages venv
  97. source venv/bin/activate
  98. pushd python
  99. python setup.py clean build sdist
  100. pip install dist/protobuf-*.tar.gz
  101. popd
  102. deactivate
  103. rm -rf python/venv
  104. }
  105. build_csharp() {
  106. # Required for conformance tests and to regenerate protos.
  107. internal_build_cpp
  108. NUGET=/usr/local/bin/nuget.exe
  109. # Disable some unwanted dotnet options
  110. export DOTNET_SKIP_FIRST_TIME_EXPERIENCE=true
  111. export DOTNET_CLI_TELEMETRY_OPTOUT=true
  112. # TODO(jtattermusch): is this still needed with "first time experience"
  113. # disabled?
  114. # Perform "dotnet new" once to get the setup preprocessing out of the
  115. # way. That spews a lot of output (including backspaces) into logs
  116. # otherwise, and can cause problems. It doesn't matter if this step
  117. # is performed multiple times; it's cheap after the first time anyway.
  118. # (It also doesn't matter if it's unnecessary, which it will be on some
  119. # systems. It's necessary on Jenkins in order to avoid unprintable
  120. # characters appearing in the JUnit output.)
  121. mkdir dotnettmp
  122. (cd dotnettmp; dotnet new > /dev/null)
  123. rm -rf dotnettmp
  124. # Check that the protos haven't broken C# codegen.
  125. # TODO(jonskeet): Fail if regenerating creates any changes.
  126. csharp/generate_protos.sh
  127. csharp/buildall.sh
  128. cd conformance && make test_csharp && cd ..
  129. # Run csharp compatibility test between 3.0.0 and the current version.
  130. csharp/compatibility_tests/v3.0.0/test.sh 3.0.0
  131. # Run csharp compatibility test between last released and the current version.
  132. csharp/compatibility_tests/v3.0.0/test.sh $LAST_RELEASED
  133. }
  134. build_golang() {
  135. # Go build needs `protoc`.
  136. internal_build_cpp
  137. # Add protoc to the path so that the examples build finds it.
  138. export PATH="`pwd`/src:$PATH"
  139. export GOPATH="$HOME/gocode"
  140. mkdir -p "$GOPATH/src/github.com/protocolbuffers"
  141. mkdir -p "$GOPATH/src/github.com/golang"
  142. rm -f "$GOPATH/src/github.com/protocolbuffers/protobuf"
  143. rm -f "$GOPATH/src/github.com/golang/protobuf"
  144. ln -s "`pwd`" "$GOPATH/src/github.com/protocolbuffers/protobuf"
  145. export PATH="$GOPATH/bin:$PATH"
  146. (cd $GOPATH/src/github.com/golang && git clone https://github.com/golang/protobuf.git && cd protobuf && git checkout v1.3.5)
  147. go install github.com/golang/protobuf/protoc-gen-go
  148. cd examples && PROTO_PATH="-I../src -I." make gotest && cd ..
  149. }
  150. use_java() {
  151. version=$1
  152. case "$version" in
  153. jdk8)
  154. export PATH=/usr/lib/jvm/java-8-openjdk-amd64/bin:$PATH
  155. export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
  156. ;;
  157. jdk7)
  158. export PATH=/usr/lib/jvm/java-7-openjdk-amd64/bin:$PATH
  159. export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  160. ;;
  161. oracle7)
  162. export PATH=/usr/lib/jvm/java-7-oracle/bin:$PATH
  163. export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  164. ;;
  165. esac
  166. MAVEN_LOCAL_REPOSITORY=/var/maven_local_repository
  167. MVN="$MVN -e -X -Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=$MAVEN_LOCAL_REPOSITORY"
  168. which java
  169. java -version
  170. $MVN -version
  171. }
  172. # --batch-mode suppresses download progress output that spams the logs.
  173. MVN="mvn --batch-mode"
  174. build_java() {
  175. version=$1
  176. dir=java_$version
  177. # Java build needs `protoc`.
  178. internal_build_cpp
  179. cp -r java $dir
  180. cd $dir && $MVN clean
  181. # Skip the Kotlin tests on Oracle 7
  182. if [ "$version" == "oracle7" ]; then
  183. $MVN test -pl bom,lite,core,util
  184. else
  185. $MVN test
  186. fi
  187. cd ../..
  188. }
  189. # The conformance tests are hard-coded to work with the $ROOT/java directory.
  190. # So this can't run in parallel with two different sets of tests.
  191. build_java_with_conformance_tests() {
  192. # Java build needs `protoc`.
  193. internal_build_cpp
  194. # This local installation avoids the problem caused by a new version not yet in Maven Central
  195. cd java/bom && $MVN install
  196. cd ../..
  197. cd java && $MVN test && $MVN install
  198. cd util && $MVN package assembly:single
  199. cd ../..
  200. cd conformance && make test_java && cd ..
  201. }
  202. build_java_jdk7() {
  203. use_java jdk7
  204. build_java_with_conformance_tests
  205. }
  206. build_java_oracle7() {
  207. use_java oracle7
  208. build_java oracle7
  209. }
  210. build_java_linkage_monitor() {
  211. # Linkage Monitor checks compatibility with other Google libraries
  212. # https://github.com/GoogleCloudPlatform/cloud-opensource-java/tree/master/linkage-monitor
  213. use_java jdk8
  214. internal_build_cpp
  215. # Linkage Monitor uses $HOME/.m2 local repository
  216. MVN="mvn -e -B -Dhttps.protocols=TLSv1.2"
  217. cd java
  218. # Installs the snapshot version locally
  219. $MVN install -Dmaven.test.skip=true
  220. # Linkage Monitor uses the snapshot versions installed in $HOME/.m2 to verify compatibility
  221. JAR=linkage-monitor-latest-all-deps.jar
  222. curl -v -O "https://storage.googleapis.com/cloud-opensource-java-linkage-monitor/${JAR}"
  223. # Fails if there's new linkage errors compared with baseline
  224. java -jar $JAR com.google.cloud:libraries-bom
  225. }
  226. build_objectivec_ios() {
  227. # Reused the build script that takes care of configuring and ensuring things
  228. # are up to date. The OS X test runs the objc conformance test, so skip it
  229. # here.
  230. objectivec/DevTools/full_mac_build.sh \
  231. --core-only --skip-xcode-osx --skip-xcode-tvos --skip-objc-conformance "$@"
  232. }
  233. build_objectivec_ios_debug() {
  234. build_objectivec_ios --skip-xcode-release
  235. }
  236. build_objectivec_ios_release() {
  237. build_objectivec_ios --skip-xcode-debug
  238. }
  239. build_objectivec_osx() {
  240. # Reused the build script that takes care of configuring and ensuring things
  241. # are up to date.
  242. objectivec/DevTools/full_mac_build.sh \
  243. --core-only --skip-xcode-ios --skip-xcode-tvos
  244. }
  245. build_objectivec_tvos() {
  246. # Reused the build script that takes care of configuring and ensuring things
  247. # are up to date. The OS X test runs the objc conformance test, so skip it
  248. # here.
  249. objectivec/DevTools/full_mac_build.sh \
  250. --core-only --skip-xcode-ios --skip-xcode-osx --skip-objc-conformance "$@"
  251. }
  252. build_objectivec_tvos_debug() {
  253. build_objectivec_tvos --skip-xcode-release
  254. }
  255. build_objectivec_tvos_release() {
  256. build_objectivec_tvos --skip-xcode-debug
  257. }
  258. build_objectivec_cocoapods_integration() {
  259. objectivec/Tests/CocoaPods/run_tests.sh
  260. }
  261. build_python() {
  262. internal_build_cpp
  263. cd python
  264. if [ $(uname -s) == "Linux" ]; then
  265. envlist=py\{27,33,34,35,36\}-python
  266. else
  267. envlist=py\{27,36\}-python
  268. fi
  269. python -m tox -e $envlist
  270. cd ..
  271. }
  272. build_python_version() {
  273. internal_build_cpp
  274. cd python
  275. envlist=$1
  276. python -m tox -e $envlist
  277. cd ..
  278. }
  279. build_python27() {
  280. build_python_version py27-python
  281. }
  282. build_python33() {
  283. build_python_version py33-python
  284. }
  285. build_python34() {
  286. build_python_version py34-python
  287. }
  288. build_python35() {
  289. build_python_version py35-python
  290. }
  291. build_python36() {
  292. build_python_version py36-python
  293. }
  294. build_python37() {
  295. build_python_version py37-python
  296. }
  297. build_python38() {
  298. build_python_version py38-python
  299. }
  300. build_python39() {
  301. build_python_version py39-python
  302. }
  303. build_python_cpp() {
  304. internal_build_cpp
  305. export LD_LIBRARY_PATH=../src/.libs # for Linux
  306. export DYLD_LIBRARY_PATH=../src/.libs # for OS X
  307. cd python
  308. if [ $(uname -s) == "Linux" ]; then
  309. envlist=py\{27,33,34,35,36\}-cpp
  310. else
  311. envlist=py\{27,36\}-cpp
  312. fi
  313. tox -e $envlist
  314. cd ..
  315. }
  316. build_python_cpp_version() {
  317. internal_build_cpp
  318. export LD_LIBRARY_PATH=../src/.libs # for Linux
  319. export DYLD_LIBRARY_PATH=../src/.libs # for OS X
  320. cd python
  321. envlist=$1
  322. tox -e $envlist
  323. cd ..
  324. }
  325. build_python27_cpp() {
  326. build_python_cpp_version py27-cpp
  327. }
  328. build_python33_cpp() {
  329. build_python_cpp_version py33-cpp
  330. }
  331. build_python34_cpp() {
  332. build_python_cpp_version py34-cpp
  333. }
  334. build_python35_cpp() {
  335. build_python_cpp_version py35-cpp
  336. }
  337. build_python36_cpp() {
  338. build_python_cpp_version py36-cpp
  339. }
  340. build_python37_cpp() {
  341. build_python_cpp_version py37-cpp
  342. }
  343. build_python38_cpp() {
  344. build_python_cpp_version py38-cpp
  345. }
  346. build_python39_cpp() {
  347. build_python_cpp_version py39-cpp
  348. }
  349. build_ruby23() {
  350. internal_build_cpp # For conformance tests.
  351. cd ruby && bash travis-test.sh ruby-2.3.8 && cd ..
  352. }
  353. build_ruby24() {
  354. internal_build_cpp # For conformance tests.
  355. cd ruby && bash travis-test.sh ruby-2.4 && cd ..
  356. }
  357. build_ruby25() {
  358. internal_build_cpp # For conformance tests.
  359. cd ruby && bash travis-test.sh ruby-2.5.1 && cd ..
  360. }
  361. build_ruby26() {
  362. internal_build_cpp # For conformance tests.
  363. cd ruby && bash travis-test.sh ruby-2.6.0 && cd ..
  364. }
  365. build_ruby27() {
  366. internal_build_cpp # For conformance tests.
  367. cd ruby && bash travis-test.sh ruby-2.7.0 && cd ..
  368. }
  369. build_ruby30() {
  370. internal_build_cpp # For conformance tests.
  371. cd ruby && bash travis-test.sh ruby-3.0.0 && cd ..
  372. }
  373. build_jruby() {
  374. internal_build_cpp # For conformance tests.
  375. cd ruby && bash travis-test.sh jruby-9.2.11.1 && cd ..
  376. }
  377. build_javascript() {
  378. internal_build_cpp
  379. NODE_VERSION=node-v12.16.3-darwin-x64
  380. NODE_TGZ="$NODE_VERSION.tar.gz"
  381. pushd /tmp
  382. curl -OL https://nodejs.org/dist/v12.16.3/$NODE_TGZ
  383. tar zxvf $NODE_TGZ
  384. export PATH=$PATH:`pwd`/$NODE_VERSION/bin
  385. popd
  386. cd js && npm install && npm test && cd ..
  387. cd conformance && make test_nodejs && cd ..
  388. }
  389. use_php() {
  390. VERSION=$1
  391. export PATH=/usr/local/php-${VERSION}/bin:$PATH
  392. internal_build_cpp
  393. }
  394. build_php() {
  395. use_php $1
  396. pushd php
  397. rm -rf vendor
  398. composer update
  399. composer test
  400. popd
  401. (cd conformance && make test_php)
  402. }
  403. test_php_c() {
  404. pushd php
  405. rm -rf vendor
  406. composer update
  407. composer test_c
  408. popd
  409. (cd conformance && make test_php_c)
  410. }
  411. build_php_c() {
  412. use_php $1
  413. test_php_c
  414. }
  415. build_php7.0_mac() {
  416. internal_build_cpp
  417. # Install PHP
  418. curl -s https://php-osx.liip.ch/install.sh | bash -s 7.0
  419. PHP_FOLDER=`find /usr/local -type d -name "php5-7.0*"` # The folder name may change upon time
  420. test ! -z "$PHP_FOLDER"
  421. export PATH="$PHP_FOLDER/bin:$PATH"
  422. # Install Composer
  423. wget https://getcomposer.org/download/2.0.13/composer.phar --progress=dot:mega -O /usr/local/bin/composer
  424. chmod a+x /usr/local/bin/composer
  425. # Install valgrind
  426. echo "#! /bin/bash" > valgrind
  427. chmod ug+x valgrind
  428. sudo mv valgrind /usr/local/bin/valgrind
  429. # Test
  430. test_php_c
  431. }
  432. build_php7.3_mac() {
  433. internal_build_cpp
  434. # Install PHP
  435. # We can't test PHP 7.4 with these binaries yet:
  436. # https://github.com/liip/php-osx/issues/276
  437. curl -s https://php-osx.liip.ch/install.sh | bash -s 7.3
  438. PHP_FOLDER=`find /usr/local -type d -name "php5-7.3*"` # The folder name may change upon time
  439. test ! -z "$PHP_FOLDER"
  440. export PATH="$PHP_FOLDER/bin:$PATH"
  441. # Install Composer
  442. wget https://getcomposer.org/download/2.0.13/composer.phar --progress=dot:mega -O /usr/local/bin/composer
  443. chmod a+x /usr/local/bin/composer
  444. # Install valgrind
  445. echo "#! /bin/bash" > valgrind
  446. chmod ug+x valgrind
  447. sudo mv valgrind /usr/local/bin/valgrind
  448. # Test
  449. test_php_c
  450. }
  451. build_php_compatibility() {
  452. internal_build_cpp
  453. php/tests/compatibility_test.sh $LAST_RELEASED
  454. }
  455. build_php_multirequest() {
  456. use_php 7.4
  457. php/tests/multirequest.sh
  458. }
  459. build_php8.0_all() {
  460. build_php 8.0
  461. build_php_c 8.0
  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_linkage_monitor |
  492. objectivec_ios |
  493. objectivec_ios_debug |
  494. objectivec_ios_release |
  495. objectivec_osx |
  496. objectivec_tvos |
  497. objectivec_tvos_debug |
  498. objectivec_tvos_release |
  499. objectivec_cocoapods_integration |
  500. python |
  501. python_cpp |
  502. python_compatibility |
  503. ruby23 |
  504. ruby24 |
  505. ruby25 |
  506. ruby26 |
  507. ruby27 |
  508. ruby30 |
  509. jruby |
  510. ruby_all |
  511. php_all |
  512. php_all_32 |
  513. php7.0_mac |
  514. php7.3_mac |
  515. dist_install |
  516. benchmark)
  517. "
  518. exit 1
  519. fi
  520. set -e # exit immediately on error
  521. set -x # display all commands
  522. cd $(dirname $0)
  523. eval "build_$1"