tests.sh 15.3 KB
Newer Older
1
#!/bin/bash
2 3 4 5
#
# Build and runs tests for the protobuf project.  The tests as written here are
# used by both Jenkins and Travis, though some specialized logic is required to
# handle the differences between them.
6

7 8 9 10 11 12
on_travis() {
  if [ "$TRAVIS" == "true" ]; then
    "$@"
  fi
}

13 14 15
# For when some other test needs the C++ main build, including protoc and
# libprotobuf.
internal_build_cpp() {
16 17 18 19 20
  if [ -f src/protoc ]; then
    # Already built.
    return
  fi

Josh Haberman's avatar
Josh Haberman committed
21
  if [[ $(uname -s) == "Linux" && "$TRAVIS" == "true" ]]; then
22 23 24 25 26 27 28
    # Install GCC 4.8 to replace the default GCC 4.6. We need 4.8 for more
    # decent C++ 11 support in order to compile conformance tests.
    sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
    sudo apt-get update -qq
    sudo apt-get install -qq g++-4.8
    export CXX="g++-4.8" CC="gcc-4.8"
  fi
29

30
  ./autogen.sh
31 32
  ./configure CXXFLAGS="-fPIC"  # -fPIC is needed for python cpp test.
                                # See python/setup.py for more details
33
  make -j2
34 35 36 37
}

build_cpp() {
  internal_build_cpp
38
  make check -j2
39
  cd conformance && make test_cpp && cd ..
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  # The benchmark code depends on cmake, so test if it is installed before
  # trying to do the build.
  # NOTE: The travis macOS images say they have cmake, but the xcode8.1 image
  # appears to be missing it: https://github.com/travis-ci/travis-ci/issues/6996
  if [[ $(type cmake 2>/dev/null) ]]; then
    # Verify benchmarking code can build successfully.
    git submodule init
    git submodule update
    cd third_party/benchmark && cmake -DCMAKE_BUILD_TYPE=Release && make && cd ../..
    cd benchmarks && make && ./generate-datasets && cd ..
  else
    echo ""
    echo "WARNING: Skipping validation of the bench marking code, cmake isn't installed."
    echo ""
  fi
56 57 58 59 60
}

build_cpp_distcheck() {
  ./autogen.sh
  ./configure
61 62 63
  make dist

  # List all files that should be included in the distribution package.
Feng Xiao's avatar
Feng Xiao committed
64
  git ls-files | grep "^\(java\|python\|objectivec\|csharp\|js\|ruby\|php\|cmake\|examples\)" |\
65
    grep -v ".gitignore" | grep -v "java/compatibility_tests" |\
66
    grep -v "python/compatibility_tests" | grep -v "csharp/compatibility_tests" > dist.lst
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  # Unzip the dist tar file.
  DIST=`ls *.tar.gz`
  tar -xf $DIST
  cd ${DIST//.tar.gz}
  # Check if every file exists in the dist tar file.
  FILES_MISSING=""
  for FILE in $(<../dist.lst); do
    if ! file $FILE &>/dev/null; then
      echo "$FILE is not found!"
      FILES_MISSING="$FILE $FILES_MISSING"
    fi
  done
  cd ..
  if [ ! -z "$FILES_MISSING" ]; then
    echo "Missing files in EXTRA_DIST: $FILES_MISSING"
    exit 1
  fi

  # Do the regular dist-check for C++.
86
  make distcheck -j2
87 88
}

89
build_csharp() {
90 91 92 93
  # Just for the conformance tests. We don't currently
  # need to really build protoc, but it's simplest to keep with the
  # conventions of the other builds.
  internal_build_cpp
94
  NUGET=/usr/local/bin/nuget.exe
95

96 97 98
  if [ "$TRAVIS" == "true" ]; then
    # Install latest version of Mono
    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
99
    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1397BC53640DB551
100 101 102
    echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
    sudo apt-get update -qq
    sudo apt-get install -qq mono-devel referenceassemblies-pcl nunit
103

104
    # Then install the dotnet SDK as per Ubuntu 14.04 instructions on dot.net.
105
    sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ trusty main" > /etc/apt/sources.list.d/dotnetdev.list'
106 107 108
    sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
    sudo apt-get update -qq
    sudo apt-get install -qq dotnet-dev-1.0.0-preview2-003121
109
  fi
110

111 112 113 114 115 116 117 118 119
  # Perform "dotnet new" once to get the setup preprocessing out of the
  # way. That spews a lot of output (including backspaces) into logs
  # otherwise, and can cause problems. It doesn't matter if this step
  # is performed multiple times; it's cheap after the first time anyway.
  mkdir dotnettmp
  (cd dotnettmp; dotnet new > /dev/null)
  rm -rf dotnettmp

  (cd csharp/src; dotnet restore)
120
  csharp/buildall.sh
121
  cd conformance && make test_csharp && cd ..
122 123 124

  # Run csharp compatibility test between 3.0.0 and the current version.
  csharp/compatibility_tests/v3.0.0/test.sh 3.0.0
125 126
}

Tim Swast's avatar
Tim Swast committed
127 128 129 130 131 132 133
build_golang() {
  # Go build needs `protoc`.
  internal_build_cpp
  # Add protoc to the path so that the examples build finds it.
  export PATH="`pwd`/src:$PATH"

  # Install Go and the Go protobuf compiler plugin.
Feng Xiao's avatar
Feng Xiao committed
134 135 136
  on_travis sudo apt-get update -qq
  on_travis sudo apt-get install -qq golang

Tim Swast's avatar
Tim Swast committed
137 138
  export GOPATH="$HOME/gocode"
  mkdir -p "$GOPATH/src/github.com/google"
Feng Xiao's avatar
Feng Xiao committed
139
  rm -f "$GOPATH/src/github.com/google/protobuf"
Tim Swast's avatar
Tim Swast committed
140 141 142 143 144 145 146
  ln -s "`pwd`" "$GOPATH/src/github.com/google/protobuf"
  export PATH="$GOPATH/bin:$PATH"
  go get github.com/golang/protobuf/protoc-gen-go

  cd examples && make gotest && cd ..
}

147 148 149 150
use_java() {
  version=$1
  case "$version" in
    jdk7)
151
      on_travis sudo apt-get install openjdk-7-jdk
152
      export PATH=/usr/lib/jvm/java-7-openjdk-amd64/bin:$PATH
153
      export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
154 155
      ;;
    oracle7)
156 157 158 159 160 161 162
      if [ "$TRAVIS" == "true" ]; then
        sudo apt-get install python-software-properties # for apt-add-repository
        echo "oracle-java7-installer shared/accepted-oracle-license-v1-1 select true" | \
          sudo debconf-set-selections
        yes | sudo apt-add-repository ppa:webupd8team/java
        yes | sudo apt-get install oracle-java7-installer
      fi;
163
      export PATH=/usr/lib/jvm/java-7-oracle/bin:$PATH
164
      export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
165 166 167
      ;;
  esac

168 169 170 171 172
  if [ "$TRAVIS" != "true" ]; then
    MAVEN_LOCAL_REPOSITORY=/var/maven_local_repository
    MVN="$MVN -e -X --offline -Dmaven.repo.local=$MAVEN_LOCAL_REPOSITORY"
  fi;

173 174
  which java
  java -version
175
  $MVN -version
176 177
}

178
# --batch-mode supresses download progress output that spams the logs.
179
MVN="mvn --batch-mode"
180

181
build_java() {
182 183
  version=$1
  dir=java_$version
184 185
  # Java build needs `protoc`.
  internal_build_cpp
186 187
  cp -r java $dir
  cd $dir && $MVN clean && $MVN test
188 189 190
  cd ../..
}

191 192
# The conformance tests are hard-coded to work with the $ROOT/java directory.
# So this can't run in parallel with two different sets of tests.
193
build_java_with_conformance_tests() {
194
  # Java build needs `protoc`.
195
  internal_build_cpp
196
  cd java && $MVN test && $MVN install
197
  cd util && $MVN package assembly:single
Feng Xiao's avatar
Feng Xiao committed
198
  cd ../..
199 200 201 202 203
  cd conformance && make test_java && cd ..
}

build_javanano() {
  # Java build needs `protoc`.
204
  internal_build_cpp
205
  cd javanano && $MVN test && cd ..
206 207 208 209
}

build_java_jdk7() {
  use_java jdk7
210
  build_java_with_conformance_tests
211 212 213
}
build_java_oracle7() {
  use_java oracle7
214
  build_java oracle7
215
}
216 217 218 219 220 221 222 223
build_java_compatibility() {
  use_java jdk7
  internal_build_cpp
  # Use the unit-tests extraced from 2.5.0 to test the compatibilty between
  # 3.0.0-beta-4 and the current version.
  cd java/compatibility_tests/v2.5.0
  ./test.sh 3.0.0-beta-4
}
224 225 226 227 228 229 230 231 232 233

build_javanano_jdk7() {
  use_java jdk7
  build_javanano
}
build_javanano_oracle7() {
  use_java oracle7
  build_javanano
}

234
internal_install_python_deps() {
235 236 237
  if [ "$TRAVIS" != "true" ]; then
    return;
  fi
238 239 240 241 242 243
  # Install tox (OS X doesn't have pip).
  if [ $(uname -s) == "Darwin" ]; then
    sudo easy_install tox
  else
    sudo pip install tox
  fi
244
  # Only install Python2.6/3.x on Linux.
245 246 247 248 249
  if [ $(uname -s) == "Linux" ]; then
    sudo apt-get install -y python-software-properties # for apt-add-repository
    sudo apt-add-repository -y ppa:fkrull/deadsnakes
    sudo apt-get update -qq
    sudo apt-get install -y python2.6 python2.6-dev
250 251
    sudo apt-get install -y python3.3 python3.3-dev
    sudo apt-get install -y python3.4 python3.4-dev
252
  fi
253 254
}

255
build_objectivec_ios() {
256
  # Reused the build script that takes care of configuring and ensuring things
257 258 259 260 261
  # are up to date.  The OS X test runs the objc conformance test, so skip it
  # here.
  # Note: travis has xctool installed, and we've looked at using it in the past
  # but it has ended up proving unreliable (bugs), an they are removing build
  # support in favor of xcbuild (or just xcodebuild).
262
  objectivec/DevTools/full_mac_build.sh \
263
      --core-only --skip-xcode-osx --skip-objc-conformance "$@"
264 265
}

266 267
build_objectivec_ios_debug() {
  build_objectivec_ios --skip-xcode-release
268 269
}

270 271
build_objectivec_ios_release() {
  build_objectivec_ios --skip-xcode-debug
272 273 274
}

build_objectivec_osx() {
275 276 277 278
  # Reused the build script that takes care of configuring and ensuring things
  # are up to date.
  objectivec/DevTools/full_mac_build.sh \
      --core-only --skip-xcode-ios
279
}
280

281 282 283 284 285 286
build_objectivec_cocoapods_integration() {
  # Update pod to the latest version.
  gem install cocoapods --no-ri --no-rdoc
  objectivec/Tests/CocoaPods/run_tests.sh
}

287
build_python() {
288
  internal_build_cpp
289
  internal_install_python_deps
290
  cd python
291
  # Only test Python 2.6/3.x on Linux
292
  if [ $(uname -s) == "Linux" ]; then
293
    envlist=py\{26,27,33,34\}-python
294 295 296 297
  else
    envlist=py27-python
  fi
  tox -e $envlist
298 299 300 301
  cd ..
}

build_python_cpp() {
302
  internal_build_cpp
303
  internal_install_python_deps
304 305
  export LD_LIBRARY_PATH=../src/.libs # for Linux
  export DYLD_LIBRARY_PATH=../src/.libs # for OS X
306
  cd python
307
  # Only test Python 2.6/3.x on Linux
308
  if [ $(uname -s) == "Linux" ]; then
309 310
    # py26 is currently disabled due to json_format
    envlist=py\{27,33,34\}-cpp
311 312 313 314
  else
    envlist=py27-cpp
  fi
  tox -e $envlist
315 316 317
  cd ..
}

318 319 320 321 322 323 324 325 326 327
build_python_compatibility() {
  internal_build_cpp
  # Use the unit-tests extraced from 2.5.0 to test the compatibilty.
  cd python/compatibility_tests/v2.5.0
  # Test between 2.5.0 and the current version.
  ./test.sh 2.5.0
  # Test between 3.0.0-beta-1 and the current version.
  ./test.sh 3.0.0-beta-1
}

328
build_ruby21() {
329
  internal_build_cpp  # For conformance tests.
330 331 332
  cd ruby && bash travis-test.sh ruby-2.1 && cd ..
}
build_ruby22() {
333
  internal_build_cpp  # For conformance tests.
334 335 336
  cd ruby && bash travis-test.sh ruby-2.2 && cd ..
}
build_jruby() {
337
  internal_build_cpp  # For conformance tests.
Feng Xiao's avatar
Feng Xiao committed
338 339 340 341 342 343 344
  # TODO(xiaofeng): Upgrade to jruby-9.x. There are some broken jests to be
  # fixed.
  cd ruby && bash travis-test.sh jruby-1.7 && cd ..
}
build_ruby_all() {
  build_ruby21
  build_ruby22
345 346
  # TODO(teboring): Disable jruby test temperarily for it randomly fails.
  # https://grpc-testing.appspot.com/job/protobuf_pull_request/735/consoleFull.
347
  # build_jruby
348 349
}

350 351
build_javascript() {
  internal_build_cpp
352
  cd js && npm install && npm test && cd ..
353 354
}

355 356 357 358 359 360 361 362 363 364 365 366 367
generate_php_test_proto() {
  internal_build_cpp
  pushd php/tests
  # Generate test file
  rm -rf generated
  mkdir generated
  ../../src/protoc --php_out=generated proto/test.proto proto/test_include.proto proto/test_no_namespace.proto
  pushd ../../src
  ./protoc --php_out=../php/tests/generated google/protobuf/empty.proto
  popd
  popd
}

368 369 370 371 372 373 374 375 376 377 378
use_php() {
  VERSION=$1
  PHP=`which php`
  PHP_CONFIG=`which php-config`
  PHPIZE=`which phpize`
  rm $PHP
  rm $PHP_CONFIG
  rm $PHPIZE
  cp "/usr/bin/php$VERSION" $PHP
  cp "/usr/bin/php-config$VERSION" $PHP_CONFIG
  cp "/usr/bin/phpize$VERSION" $PHPIZE
379
  generate_php_test_proto
380 381
}

Bo Yang's avatar
Bo Yang committed
382 383 384 385 386 387 388 389
use_php_zts() {
  VERSION=$1
  PHP=`which php`
  PHP_CONFIG=`which php-config`
  PHPIZE=`which phpize`
  ln -sfn "/usr/local/php-${VERSION}-zts/bin/php" $PHP
  ln -sfn "/usr/local/php-${VERSION}-zts/bin/php-config" $PHP_CONFIG
  ln -sfn "/usr/local/php-${VERSION}-zts/bin/phpize" $PHPIZE
390
  generate_php_test_proto
Bo Yang's avatar
Bo Yang committed
391 392
}

393 394 395 396 397 398 399 400
use_php_bc() {
  VERSION=$1
  PHP=`which php`
  PHP_CONFIG=`which php-config`
  PHPIZE=`which phpize`
  ln -sfn "/usr/local/php-${VERSION}-bc/bin/php" $PHP
  ln -sfn "/usr/local/php-${VERSION}-bc/bin/php-config" $PHP_CONFIG
  ln -sfn "/usr/local/php-${VERSION}-bc/bin/phpize" $PHPIZE
401
  generate_php_test_proto
402 403
}

404
build_php5.5() {
405
  use_php 5.5
406
  pushd php
407 408 409
  rm -rf vendor
  cp -r /usr/local/vendor-5.5 vendor
  ./vendor/bin/phpunit
410
  popd
411
  pushd conformance
412 413
  # TODO(teboring): Add it back
  # make test_php
414
  popd
415 416
}

417 418 419
build_php5.5_c() {
  use_php 5.5
  cd php/tests && /bin/bash ./test.sh && cd ../..
420 421 422
  pushd conformance
  make test_php_c
  popd
423 424
}

Bo Yang's avatar
Bo Yang committed
425 426
build_php5.5_zts_c() {
  use_php_zts 5.5
427
  wget https://phar.phpunit.de/phpunit-old.phar -O /usr/bin/phpunit
Bo Yang's avatar
Bo Yang committed
428
  cd php/tests && /bin/bash ./test.sh && cd ../..
429 430 431
  pushd conformance
  make test_php_c
  popd
Bo Yang's avatar
Bo Yang committed
432 433
}

434 435
build_php5.5_32() {
  use_php_bc 5.5
436
  pushd php
437 438 439
  rm -rf vendor
  cp -r /usr/local/vendor-5.5 vendor
  ./vendor/bin/phpunit
440
  popd
441 442 443 444
  # TODO(teboring): Add conformance test.
  # pushd conformance
  # make test_php
  # popd
445 446
}

447 448 449 450
build_php5.5_c_32() {
  use_php_bc 5.5
  wget https://phar.phpunit.de/phpunit-old.phar -O /usr/bin/phpunit
  cd php/tests && /bin/bash ./test.sh && cd ../..
451
  # TODO(teboring): Add conformance test.
452
  # pushd conformance
453
  # make test_php_c
454
  # popd
455 456
}

457
build_php5.6() {
458
  use_php 5.6
459
  pushd php
460 461 462
  rm -rf vendor
  cp -r /usr/local/vendor-5.6 vendor
  ./vendor/bin/phpunit
463
  popd
464
  pushd conformance
465 466
  # TODO(teboring): Add it back
  # make test_php
467
  popd
468 469
}

470 471
build_php5.6_c() {
  use_php 5.6
472
  cd php/tests && /bin/bash ./test.sh && cd ../..
473 474 475
  pushd conformance
  make test_php_c
  popd
476 477
}

478
build_php5.6_mac() {
479
  generate_php_test_proto
480 481
  # Install PHP
  curl -s https://php-osx.liip.ch/install.sh | bash -s 5.6
482 483
  PHP_FOLDER=`find /usr/local -type d -name "php5-5.6*"`  # The folder name may change upon time
  export PATH="$PHP_FOLDER/bin:$PATH"
484 485

  # Install phpunit
486
  curl https://phar.phpunit.de/phpunit-5.6.10.phar -L -o phpunit.phar
487 488 489 490 491 492 493 494 495 496
  chmod +x phpunit.phar
  sudo mv phpunit.phar /usr/local/bin/phpunit

  # Install valgrind
  echo "#! /bin/bash" > valgrind
  chmod ug+x valgrind
  sudo mv valgrind /usr/local/bin/valgrind

  # Test
  cd php/tests && /bin/bash ./test.sh && cd ../..
497 498 499
  pushd conformance
  make test_php_c
  popd
500 501
}

502
build_php7.0() {
503
  use_php 7.0
504
  pushd php
505 506 507
  rm -rf vendor
  cp -r /usr/local/vendor-7.0 vendor
  ./vendor/bin/phpunit
508
  popd
509
  pushd conformance
510 511
  # TODO(teboring): Add it back
  # make test_php
512
  popd
513 514
}

515 516 517
build_php7.0_c() {
  use_php 7.0
  cd php/tests && /bin/bash ./test.sh && cd ../..
518 519 520
  pushd conformance
  make test_php_c
  popd
521 522
}

523 524 525 526 527 528 529
build_php_all() {
  build_php5.5
  build_php5.6
  build_php7.0
  build_php5.5_c
  build_php5.6_c
  # build_php7.0_c
Bo Yang's avatar
Bo Yang committed
530
  build_php5.5_zts_c
531 532
}

533 534
build_php_all_32() {
  build_php5.5_32
535
  build_php5.5_c_32
536 537
}

538 539 540 541 542 543 544 545 546 547 548 549 550
# Note: travis currently does not support testing more than one language so the
# .travis.yml cheats and claims to only be cpp.  If they add multiple language
# support, this should probably get updated to install steps and/or
# rvm/gemfile/jdk/etc. entries rather than manually doing the work.

# .travis.yml uses matrix.exclude to block the cases where app-get can't be
# use to install things.

# -------- main --------

if [ "$#" -ne 1 ]; then
  echo "
Usage: $0 { cpp |
551
            cpp_distcheck |
552 553 554
            csharp |
            java_jdk7 |
            java_oracle7 |
555
            java_compatibility |
556 557 558
            javanano_jdk7 |
            javanano_oracle7 |
            objectivec_ios |
559 560
            objectivec_ios_debug |
            objectivec_ios_release |
561
            objectivec_osx |
562
            objectivec_cocoapods_integration |
563 564
            python |
            python_cpp |
565
            python_compatibility |
566 567
            ruby21 |
            ruby22 |
Feng Xiao's avatar
Feng Xiao committed
568
            jruby |
569 570 571 572 573 574
            ruby_all |
            php5.5   |
            php5.5_c |
            php5.6   |
            php5.6_c |
            php7.0   |
575 576
            php7.0_c |
            php_all)
577 578 579 580 581 582 583
"
  exit 1
fi

set -e  # exit immediately on error
set -x  # display all commands
eval "build_$1"