tests.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #!/bin/bash
  2. #
  3. # Build and runs tests for the protobuf project. The tests as written here are
  4. # used by both Jenkins and Travis, though some specialized logic is required to
  5. # handle the differences between them.
  6. on_travis() {
  7. if [ "$TRAVIS" == "true" ]; then
  8. "$@"
  9. fi
  10. }
  11. # For when some other test needs the C++ main build, including protoc and
  12. # libprotobuf.
  13. internal_build_cpp() {
  14. if [ -f src/protoc ]; then
  15. # Already built.
  16. return
  17. fi
  18. if [[ $(uname -s) == "Linux" && "$TRAVIS" == "true" ]]; then
  19. # Install GCC 4.8 to replace the default GCC 4.6. We need 4.8 for more
  20. # decent C++ 11 support in order to compile conformance tests.
  21. sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
  22. sudo apt-get update -qq
  23. sudo apt-get install -qq g++-4.8
  24. export CXX="g++-4.8" CC="gcc-4.8"
  25. fi
  26. ./autogen.sh
  27. ./configure
  28. make -j2
  29. }
  30. build_cpp() {
  31. internal_build_cpp
  32. make check -j2
  33. cd conformance && make test_cpp && cd ..
  34. # Verify benchmarking code can build successfully.
  35. cd benchmarks && make && ./generate-datasets && cd ..
  36. }
  37. build_cpp_distcheck() {
  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\|cmake\|examples\)" |\
  43. grep -v ".gitignore" | grep -v "java/compatibility_tests" > dist.lst
  44. # Unzip the dist tar file.
  45. DIST=`ls *.tar.gz`
  46. tar -xf $DIST
  47. cd ${DIST//.tar.gz}
  48. # Check if every file exists in the dist tar file.
  49. FILES_MISSING=""
  50. for FILE in $(<../dist.lst); do
  51. if ! file $FILE &>/dev/null; then
  52. echo "$FILE is not found!"
  53. FILES_MISSING="$FILE $FILES_MISSING"
  54. fi
  55. done
  56. cd ..
  57. if [ ! -z "$FILES_MISSING" ]; then
  58. echo "Missing files in EXTRA_DIST: $FILES_MISSING"
  59. exit 1
  60. fi
  61. # Do the regular dist-check for C++.
  62. make distcheck -j2
  63. }
  64. build_csharp() {
  65. # Just for the conformance tests. We don't currently
  66. # need to really build protoc, but it's simplest to keep with the
  67. # conventions of the other builds.
  68. internal_build_cpp
  69. NUGET=/usr/local/bin/nuget.exe
  70. if [ "$TRAVIS" == "true" ]; then
  71. # Install latest version of Mono
  72. sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
  73. sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1397BC53640DB551
  74. echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
  75. sudo apt-get update -qq
  76. sudo apt-get install -qq mono-devel referenceassemblies-pcl nunit
  77. # Then install the dotnet SDK as per Ubuntu 14.04 instructions on dot.net.
  78. sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ trusty main" > /etc/apt/sources.list.d/dotnetdev.list'
  79. sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
  80. sudo apt-get update -qq
  81. sudo apt-get install -qq dotnet-dev-1.0.0-preview2-003121
  82. fi
  83. # Perform "dotnet new" once to get the setup preprocessing out of the
  84. # way. That spews a lot of output (including backspaces) into logs
  85. # otherwise, and can cause problems. It doesn't matter if this step
  86. # is performed multiple times; it's cheap after the first time anyway.
  87. mkdir dotnettmp
  88. (cd dotnettmp; dotnet new > /dev/null)
  89. rm -rf dotnettmp
  90. (cd csharp/src; dotnet restore)
  91. csharp/buildall.sh
  92. cd conformance && make test_csharp && cd ..
  93. }
  94. build_golang() {
  95. # Go build needs `protoc`.
  96. internal_build_cpp
  97. # Add protoc to the path so that the examples build finds it.
  98. export PATH="`pwd`/src:$PATH"
  99. # Install Go and the Go protobuf compiler plugin.
  100. on_travis sudo apt-get update -qq
  101. on_travis sudo apt-get install -qq golang
  102. export GOPATH="$HOME/gocode"
  103. mkdir -p "$GOPATH/src/github.com/google"
  104. rm -f "$GOPATH/src/github.com/google/protobuf"
  105. ln -s "`pwd`" "$GOPATH/src/github.com/google/protobuf"
  106. export PATH="$GOPATH/bin:$PATH"
  107. go get github.com/golang/protobuf/protoc-gen-go
  108. cd examples && make gotest && cd ..
  109. }
  110. use_java() {
  111. version=$1
  112. case "$version" in
  113. jdk7)
  114. on_travis sudo apt-get install openjdk-7-jdk
  115. export PATH=/usr/lib/jvm/java-7-openjdk-amd64/bin:$PATH
  116. export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  117. ;;
  118. oracle7)
  119. if [ "$TRAVIS" == "true" ]; then
  120. sudo apt-get install python-software-properties # for apt-add-repository
  121. echo "oracle-java7-installer shared/accepted-oracle-license-v1-1 select true" | \
  122. sudo debconf-set-selections
  123. yes | sudo apt-add-repository ppa:webupd8team/java
  124. yes | sudo apt-get install oracle-java7-installer
  125. fi;
  126. export PATH=/usr/lib/jvm/java-7-oracle/bin:$PATH
  127. export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  128. ;;
  129. esac
  130. if [ "$TRAVIS" != "true" ]; then
  131. MAVEN_LOCAL_REPOSITORY=/var/maven_local_repository
  132. MVN="$MVN -e -X --offline -Dmaven.repo.local=$MAVEN_LOCAL_REPOSITORY"
  133. fi;
  134. which java
  135. java -version
  136. $MVN -version
  137. }
  138. # --batch-mode supresses download progress output that spams the logs.
  139. MVN="mvn --batch-mode"
  140. build_java() {
  141. version=$1
  142. dir=java_$version
  143. # Java build needs `protoc`.
  144. internal_build_cpp
  145. cp -r java $dir
  146. cd $dir && $MVN clean && $MVN test
  147. cd ../..
  148. }
  149. # The conformance tests are hard-coded to work with the $ROOT/java directory.
  150. # So this can't run in parallel with two different sets of tests.
  151. build_java_with_conformance_tests() {
  152. # Java build needs `protoc`.
  153. internal_build_cpp
  154. cd java && $MVN test && $MVN install
  155. cd util && $MVN package assembly:single
  156. cd ../..
  157. cd conformance && make test_java && cd ..
  158. }
  159. build_javanano() {
  160. # Java build needs `protoc`.
  161. internal_build_cpp
  162. cd javanano && $MVN test && cd ..
  163. }
  164. build_java_jdk7() {
  165. use_java jdk7
  166. build_java_with_conformance_tests
  167. }
  168. build_java_oracle7() {
  169. use_java oracle7
  170. build_java oracle7
  171. }
  172. build_javanano_jdk7() {
  173. use_java jdk7
  174. build_javanano
  175. }
  176. build_javanano_oracle7() {
  177. use_java oracle7
  178. build_javanano
  179. }
  180. internal_install_python_deps() {
  181. if [ "$TRAVIS" != "true" ]; then
  182. return;
  183. fi
  184. # Install tox (OS X doesn't have pip).
  185. if [ $(uname -s) == "Darwin" ]; then
  186. sudo easy_install tox
  187. else
  188. sudo pip install tox
  189. fi
  190. # Only install Python2.6/3.x on Linux.
  191. if [ $(uname -s) == "Linux" ]; then
  192. sudo apt-get install -y python-software-properties # for apt-add-repository
  193. sudo apt-add-repository -y ppa:fkrull/deadsnakes
  194. sudo apt-get update -qq
  195. sudo apt-get install -y python2.6 python2.6-dev
  196. sudo apt-get install -y python3.3 python3.3-dev
  197. sudo apt-get install -y python3.4 python3.4-dev
  198. fi
  199. }
  200. build_objectivec_ios() {
  201. # Reused the build script that takes care of configuring and ensuring things
  202. # are up to date. The OS X test runs the objc conformance test, so skip it
  203. # here.
  204. # Note: travis has xctool installed, and we've looked at using it in the past
  205. # but it has ended up proving unreliable (bugs), an they are removing build
  206. # support in favor of xcbuild (or just xcodebuild).
  207. objectivec/DevTools/full_mac_build.sh \
  208. --core-only --skip-xcode-osx --skip-objc-conformance "$@"
  209. }
  210. build_objectivec_ios_debug() {
  211. build_objectivec_ios --skip-xcode-release
  212. }
  213. build_objectivec_ios_release() {
  214. build_objectivec_ios --skip-xcode-debug
  215. }
  216. build_objectivec_osx() {
  217. # Reused the build script that takes care of configuring and ensuring things
  218. # are up to date.
  219. objectivec/DevTools/full_mac_build.sh \
  220. --core-only --skip-xcode-ios
  221. }
  222. build_objectivec_cocoapods_integration() {
  223. # First, load the RVM environment in bash, needed to update ruby.
  224. source ~/.rvm/scripts/rvm
  225. # Update rvm to the latest version. This is needed to solve
  226. # https://github.com/google/protobuf/issues/1786 and may not be needed in the
  227. # future when Travis updates the default version of rvm.
  228. rvm get head
  229. # Update ruby to 2.2.3 as the default one crashes with segmentation faults
  230. # when using pod.
  231. rvm use 2.2.3 --install --binary --fuzzy
  232. # Update pod to the latest version.
  233. gem install cocoapods --no-ri --no-rdoc
  234. objectivec/Tests/CocoaPods/run_tests.sh
  235. }
  236. build_python() {
  237. internal_build_cpp
  238. internal_install_python_deps
  239. cd python
  240. # Only test Python 2.6/3.x on Linux
  241. if [ $(uname -s) == "Linux" ]; then
  242. envlist=py\{26,27,33,34\}-python
  243. else
  244. envlist=py27-python
  245. fi
  246. tox -e $envlist
  247. cd ..
  248. }
  249. build_python_cpp() {
  250. internal_build_cpp
  251. internal_install_python_deps
  252. export LD_LIBRARY_PATH=../src/.libs # for Linux
  253. export DYLD_LIBRARY_PATH=../src/.libs # for OS X
  254. cd python
  255. # Only test Python 2.6/3.x on Linux
  256. if [ $(uname -s) == "Linux" ]; then
  257. # py26 is currently disabled due to json_format
  258. envlist=py\{27,33,34\}-cpp
  259. else
  260. envlist=py27-cpp
  261. fi
  262. tox -e $envlist
  263. cd ..
  264. }
  265. build_ruby21() {
  266. internal_build_cpp # For conformance tests.
  267. cd ruby && bash travis-test.sh ruby-2.1 && cd ..
  268. }
  269. build_ruby22() {
  270. internal_build_cpp # For conformance tests.
  271. cd ruby && bash travis-test.sh ruby-2.2 && cd ..
  272. }
  273. build_jruby() {
  274. internal_build_cpp # For conformance tests.
  275. # TODO(xiaofeng): Upgrade to jruby-9.x. There are some broken jests to be
  276. # fixed.
  277. cd ruby && bash travis-test.sh jruby-1.7 && cd ..
  278. }
  279. build_ruby_all() {
  280. build_ruby21
  281. build_ruby22
  282. build_jruby
  283. }
  284. build_javascript() {
  285. internal_build_cpp
  286. cd js && npm install && npm test && cd ..
  287. }
  288. # Note: travis currently does not support testing more than one language so the
  289. # .travis.yml cheats and claims to only be cpp. If they add multiple language
  290. # support, this should probably get updated to install steps and/or
  291. # rvm/gemfile/jdk/etc. entries rather than manually doing the work.
  292. # .travis.yml uses matrix.exclude to block the cases where app-get can't be
  293. # use to install things.
  294. # -------- main --------
  295. if [ "$#" -ne 1 ]; then
  296. echo "
  297. Usage: $0 { cpp |
  298. cpp_distcheck |
  299. csharp |
  300. java_jdk7 |
  301. java_oracle7 |
  302. javanano_jdk7 |
  303. javanano_oracle7 |
  304. objectivec_ios |
  305. objectivec_ios_debug |
  306. objectivec_ios_release |
  307. objectivec_osx |
  308. objectivec_cocoapods_integration |
  309. python |
  310. python_cpp |
  311. ruby21 |
  312. ruby22 |
  313. jruby |
  314. ruby_all)
  315. "
  316. exit 1
  317. fi
  318. set -e # exit immediately on error
  319. set -x # display all commands
  320. eval "build_$1"