tests.sh 15.1 KB
Newer Older
1
#!/bin/bash
2
#
3 4 5
# Build and runs tests for the protobuf project. We use this script to run
# tests on kokoro (Ubuntu and MacOS). It can run locally as well but you
# will need to make sure the required compilers/tools are available.
6

7 8 9
# For when some other test needs the C++ main build, including protoc and
# libprotobuf.
internal_build_cpp() {
10 11 12 13 14
  if [ -f src/protoc ]; then
    # Already built.
    return
  fi

15 16 17
  # Initialize any submodules.
  git submodule update --init --recursive

18
  ./autogen.sh
19 20
  ./configure CXXFLAGS="-fPIC"  # -fPIC is needed for python cpp test.
                                # See python/setup.py for more details
Feng Xiao's avatar
Feng Xiao committed
21
  make -j4
22 23 24 25
}

build_cpp() {
  internal_build_cpp
Feng Xiao's avatar
Feng Xiao committed
26
  make check -j4 || (cat src/test-suite.log; false)
27
  cd conformance && make test_cpp && cd ..
28

29 30 31 32
  # The benchmark code depends on cmake, so test if it is installed before
  # trying to do the build.
  if [[ $(type cmake 2>/dev/null) ]]; then
    # Verify benchmarking code can build successfully.
Yilun Chong's avatar
Yilun Chong committed
33
    cd benchmarks && make cpp-benchmark && cd ..
34 35 36 37 38
  else
    echo ""
    echo "WARNING: Skipping validation of the bench marking code, cmake isn't installed."
    echo ""
  fi
39 40 41
}

build_cpp_distcheck() {
42 43
  # Initialize any submodules.
  git submodule update --init --recursive
44 45
  ./autogen.sh
  ./configure
46 47 48
  make dist

  # List all files that should be included in the distribution package.
49
  git ls-files | grep "^\(java\|python\|objectivec\|csharp\|js\|ruby\|php\|cmake\|examples\|src/google/protobuf/.*\.proto\)" |\
50
    grep -v ".gitignore" | grep -v "java/compatibility_tests" |\
51
    grep -v "python/compatibility_tests" | grep -v "csharp/compatibility_tests" > dist.lst
52 53 54 55 56 57 58
  # 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
Feng Xiao's avatar
Feng Xiao committed
59
    [ -f "$FILE" ] || {
60 61
      echo "$FILE is not found!"
      FILES_MISSING="$FILE $FILES_MISSING"
Feng Xiao's avatar
Feng Xiao committed
62
    }
63 64 65 66 67 68 69 70
  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++.
Feng Xiao's avatar
Feng Xiao committed
71
  make distcheck -j4
72 73
}

74
build_csharp() {
75
  # Required for conformance tests and to regenerate protos.
76
  internal_build_cpp
77
  NUGET=/usr/local/bin/nuget.exe
78

79 80 81 82
  # 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.
83 84 85
  # (It also doesn't matter if it's unnecessary, which it will be on some
  # systems. It's necessary on Jenkins in order to avoid unprintable
  # characters appearing in the JUnit output.)
86 87 88 89
  mkdir dotnettmp
  (cd dotnettmp; dotnet new > /dev/null)
  rm -rf dotnettmp

90 91 92
  # Check that the protos haven't broken C# codegen.
  # TODO(jonskeet): Fail if regenerating creates any changes.
  csharp/generate_protos.sh
93

94
  csharp/buildall.sh
95
  cd conformance && make test_csharp && cd ..
96 97 98

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

Tim Swast's avatar
Tim Swast committed
101 102 103 104 105 106 107
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"

  export GOPATH="$HOME/gocode"
Feng Xiao's avatar
Feng Xiao committed
108
  mkdir -p "$GOPATH/src/github.com/protocolbuffers"
Feng Xiao's avatar
Feng Xiao committed
109 110
  rm -f "$GOPATH/src/github.com/protocolbuffers/protobuf"
  ln -s "`pwd`" "$GOPATH/src/github.com/protocolbuffers/protobuf"
Tim Swast's avatar
Tim Swast committed
111 112 113
  export PATH="$GOPATH/bin:$PATH"
  go get github.com/golang/protobuf/protoc-gen-go

Feng Xiao's avatar
Feng Xiao committed
114
  cd examples && PROTO_PATH="-I../src -I." make gotest && cd ..
Tim Swast's avatar
Tim Swast committed
115 116
}

117 118 119 120 121
use_java() {
  version=$1
  case "$version" in
    jdk7)
      export PATH=/usr/lib/jvm/java-7-openjdk-amd64/bin:$PATH
122
      export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
123 124 125
      ;;
    oracle7)
      export PATH=/usr/lib/jvm/java-7-oracle/bin:$PATH
126
      export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
127 128 129
      ;;
  esac

130 131
  MAVEN_LOCAL_REPOSITORY=/var/maven_local_repository
  MVN="$MVN -e -X --offline -Dmaven.repo.local=$MAVEN_LOCAL_REPOSITORY"
132

133 134
  which java
  java -version
135
  $MVN -version
136 137
}

138
# --batch-mode supresses download progress output that spams the logs.
139
MVN="mvn --batch-mode"
140

141
build_java() {
142 143
  version=$1
  dir=java_$version
144 145
  # Java build needs `protoc`.
  internal_build_cpp
146 147
  cp -r java $dir
  cd $dir && $MVN clean && $MVN test
148 149 150
  cd ../..
}

151 152
# 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.
153
build_java_with_conformance_tests() {
154
  # Java build needs `protoc`.
155
  internal_build_cpp
156
  cd java && $MVN test && $MVN install
157
  cd util && $MVN package assembly:single
Feng Xiao's avatar
Feng Xiao committed
158
  cd ../..
159 160 161 162 163
  cd conformance && make test_java && cd ..
}

build_java_jdk7() {
  use_java jdk7
164
  build_java_with_conformance_tests
165 166 167
}
build_java_oracle7() {
  use_java oracle7
168
  build_java oracle7
169
}
170 171 172 173 174 175 176 177
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
}
178

179
build_objectivec_ios() {
180
  # Reused the build script that takes care of configuring and ensuring things
181 182 183 184 185
  # 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).
186
  objectivec/DevTools/full_mac_build.sh \
187
      --core-only --skip-xcode-osx --skip-objc-conformance "$@"
188 189
}

190 191
build_objectivec_ios_debug() {
  build_objectivec_ios --skip-xcode-release
192 193
}

194 195
build_objectivec_ios_release() {
  build_objectivec_ios --skip-xcode-debug
196 197 198
}

build_objectivec_osx() {
199 200 201 202
  # 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
203
}
204

205 206 207 208 209 210
build_objectivec_cocoapods_integration() {
  # Update pod to the latest version.
  gem install cocoapods --no-ri --no-rdoc
  objectivec/Tests/CocoaPods/run_tests.sh
}

211
build_python() {
212
  internal_build_cpp
213
  cd python
214
  if [ $(uname -s) == "Linux" ]; then
Jie Luo's avatar
Jie Luo committed
215
    envlist=py\{27,33,34,35,36\}-python
216 217 218 219
  else
    envlist=py27-python
  fi
  tox -e $envlist
220 221 222 223
  cd ..
}

build_python_cpp() {
224
  internal_build_cpp
225 226
  export LD_LIBRARY_PATH=../src/.libs # for Linux
  export DYLD_LIBRARY_PATH=../src/.libs # for OS X
227
  cd python
228
  if [ $(uname -s) == "Linux" ]; then
Jie Luo's avatar
Jie Luo committed
229
    envlist=py\{27,33,34,35,36\}-cpp
230 231 232 233
  else
    envlist=py27-cpp
  fi
  tox -e $envlist
234 235 236
  cd ..
}

237 238 239 240 241 242 243 244 245 246
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
}

247
build_ruby21() {
248
  internal_build_cpp  # For conformance tests.
249 250 251
  cd ruby && bash travis-test.sh ruby-2.1 && cd ..
}
build_ruby22() {
252
  internal_build_cpp  # For conformance tests.
253 254
  cd ruby && bash travis-test.sh ruby-2.2 && cd ..
}
255 256 257 258 259 260 261 262 263 264 265 266
build_ruby23() {
  internal_build_cpp  # For conformance tests.
  cd ruby && bash travis-test.sh ruby-2.3 && cd ..
}
build_ruby24() {
  internal_build_cpp  # For conformance tests.
  cd ruby && bash travis-test.sh ruby-2.4 && cd ..
}
build_ruby25() {
  internal_build_cpp  # For conformance tests.
  cd ruby && bash travis-test.sh ruby-2.5.0 && cd ..
}
Feng Xiao's avatar
Feng Xiao committed
267 268 269
build_ruby_all() {
  build_ruby21
  build_ruby22
270 271 272
  build_ruby23
  build_ruby24
  build_ruby25
273 274
}

275 276
build_javascript() {
  internal_build_cpp
277
  cd js && npm install && npm test && cd ..
278
  cd conformance && make test_nodejs && cd ..
279 280
}

281 282 283 284 285 286
generate_php_test_proto() {
  internal_build_cpp
  pushd php/tests
  # Generate test file
  rm -rf generated
  mkdir generated
287
  ../../src/protoc --php_out=generated         \
288
    proto/empty/echo.proto                     \
289 290 291 292 293 294 295 296 297 298 299 300 301 302
    proto/test.proto                           \
    proto/test_include.proto                   \
    proto/test_no_namespace.proto              \
    proto/test_prefix.proto                    \
    proto/test_php_namespace.proto             \
    proto/test_empty_php_namespace.proto       \
    proto/test_reserved_enum_lower.proto       \
    proto/test_reserved_enum_upper.proto       \
    proto/test_reserved_enum_value_lower.proto \
    proto/test_reserved_enum_value_upper.proto \
    proto/test_reserved_message_lower.proto    \
    proto/test_reserved_message_upper.proto    \
    proto/test_service.proto                   \
    proto/test_service_namespace.proto         \
303
    proto/test_descriptors.proto
304
  pushd ../../src
305 306
  ./protoc --php_out=../php/tests/generated -I../php/tests -I. \
    ../php/tests/proto/test_import_descriptor_proto.proto
307 308 309 310
  popd
  popd
}

311 312 313 314 315
use_php() {
  VERSION=$1
  PHP=`which php`
  PHP_CONFIG=`which php-config`
  PHPIZE=`which phpize`
316 317 318
  ln -sfn "/usr/local/php-${VERSION}/bin/php" $PHP
  ln -sfn "/usr/local/php-${VERSION}/bin/php-config" $PHP_CONFIG
  ln -sfn "/usr/local/php-${VERSION}/bin/phpize" $PHPIZE
319
  generate_php_test_proto
320 321
}

Bo Yang's avatar
Bo Yang committed
322 323 324 325 326 327 328 329
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
330
  generate_php_test_proto
Bo Yang's avatar
Bo Yang committed
331 332
}

333 334 335 336 337 338 339 340
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
341
  generate_php_test_proto
342 343
}

344
build_php5.5() {
345
  use_php 5.5
346

347
  pushd php
348 349
  rm -rf vendor
  cp -r /usr/local/vendor-5.5 vendor
350 351
  wget https://phar.phpunit.de/phpunit-4.8.0.phar -O /usr/bin/phpunit
  phpunit
352
  popd
353
  pushd conformance
354
  make test_php
355
  popd
356 357
}

358
build_php5.5_c() {
359 360
  use_php 5.5
  wget https://phar.phpunit.de/phpunit-4.8.0.phar -O /usr/bin/phpunit
361
  pushd php/tests
362
  /bin/bash ./test.sh 5.5
363
  popd
364 365 366 367
  # TODO(teboring): Add it back
  # pushd conformance
  # make test_php_c
  # popd
368 369
}

Bo Yang's avatar
Bo Yang committed
370 371
build_php5.5_zts_c() {
  use_php_zts 5.5
372
  wget https://phar.phpunit.de/phpunit-4.8.0.phar -O /usr/bin/phpunit
373
  cd php/tests && /bin/bash ./test.sh 5.5-zts && cd ../..
374 375 376 377
  # TODO(teboring): Add it back
  # pushd conformance
  # make test_php_zts_c
  # popd
378 379
}

380
build_php5.6() {
381
  use_php 5.6
382
  pushd php
383 384
  rm -rf vendor
  cp -r /usr/local/vendor-5.6 vendor
385 386
  wget https://phar.phpunit.de/phpunit-5.7.0.phar -O /usr/bin/phpunit
  phpunit
387
  popd
388
  pushd conformance
389
  make test_php
390
  popd
391 392
}

393 394
build_php5.6_c() {
  use_php 5.6
395
  wget https://phar.phpunit.de/phpunit-5.7.0.phar -O /usr/bin/phpunit
396
  cd php/tests && /bin/bash ./test.sh 5.6 && cd ../..
397 398
  # TODO(teboring): Add it back
  # pushd conformance
399
  # make test_php_c
400
  # popd
401 402 403 404 405
}

build_php5.6_zts_c() {
  use_php_zts 5.6
  wget https://phar.phpunit.de/phpunit-5.7.0.phar -O /usr/bin/phpunit
406
  cd php/tests && /bin/bash ./test.sh 5.6-zts && cd ../..
407 408 409 410
  # TODO(teboring): Add it back
  # pushd conformance
  # make test_php_zts_c
  # popd
411 412
}

413
build_php5.6_mac() {
414
  generate_php_test_proto
415 416
  # Install PHP
  curl -s https://php-osx.liip.ch/install.sh | bash -s 5.6
417 418
  PHP_FOLDER=`find /usr/local -type d -name "php5-5.6*"`  # The folder name may change upon time
  export PATH="$PHP_FOLDER/bin:$PATH"
419 420

  # Install phpunit
421
  curl https://phar.phpunit.de/phpunit-5.6.10.phar -L -o phpunit.phar
422 423 424 425 426 427 428 429 430 431
  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 ../..
432 433
  # TODO(teboring): Add it back
  # pushd conformance
434
  # make test_php_c
435
  # popd
436 437
}

438
build_php7.0() {
439
  use_php 7.0
440
  pushd php
441 442
  rm -rf vendor
  cp -r /usr/local/vendor-7.0 vendor
443 444
  wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
  phpunit
445
  popd
446
  pushd conformance
447
  make test_php
448
  popd
449 450
}

451 452
build_php7.0_c() {
  use_php 7.0
453
  wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
454
  cd php/tests && /bin/bash ./test.sh 7.0 && cd ../..
455 456
  # TODO(teboring): Add it back
  # pushd conformance
457
  # make test_php_c
458
  # popd
459 460 461 462 463
}

build_php7.0_zts_c() {
  use_php_zts 7.0
  wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
464
  cd php/tests && /bin/bash ./test.sh 7.0-zts && cd ../..
465 466 467 468
  # TODO(teboring): Add it back.
  # pushd conformance
  # make test_php_zts_c
  # popd
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
}

build_php7.0_mac() {
  generate_php_test_proto
  # Install PHP
  curl -s https://php-osx.liip.ch/install.sh | bash -s 7.0
  PHP_FOLDER=`find /usr/local -type d -name "php7-7.0*"`  # The folder name may change upon time
  export PATH="$PHP_FOLDER/bin:$PATH"

  # Install phpunit
  curl https://phar.phpunit.de/phpunit-5.6.0.phar -L -o phpunit.phar
  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 ../..
490 491
  # TODO(teboring): Add it back
  # pushd conformance
492
  # make test_php_c
493
  # popd
494 495
}

496 497 498 499 500
build_php_compatibility() {
  internal_build_cpp
  php/tests/compatibility_test.sh
}

501 502 503 504 505 506 507 508 509
build_php7.1() {
  use_php 7.1
  pushd php
  rm -rf vendor
  cp -r /usr/local/vendor-7.1 vendor
  wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
  phpunit
  popd
  pushd conformance
510
  make test_php
511 512 513 514
  popd
}

build_php7.1_c() {
515
  ENABLE_CONFORMANCE_TEST=$1
516 517
  use_php 7.1
  wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
518
  cd php/tests && /bin/bash ./test.sh 7.1 && cd ../..
519 520 521 522 523 524
  if [ "$ENABLE_CONFORMANCE_TEST" = "true" ]
  then
    pushd conformance
    make test_php_c
    popd
  fi
525 526 527 528 529
}

build_php7.1_zts_c() {
  use_php_zts 7.1
  wget https://phar.phpunit.de/phpunit-5.6.0.phar -O /usr/bin/phpunit
530
  cd php/tests && /bin/bash ./test.sh 7.1-zts && cd ../..
531 532 533 534 535
  pushd conformance
  # make test_php_c
  popd
}

536
build_php_all_32() {
537 538 539
  build_php5.5
  build_php5.6
  build_php7.0
540
  build_php7.1
541 542
  build_php5.5_c
  build_php5.6_c
543
  build_php7.0_c
544
  build_php7.1_c $1
Bo Yang's avatar
Bo Yang committed
545
  build_php5.5_zts_c
546 547
  build_php5.6_zts_c
  build_php7.0_zts_c
548
  build_php7.1_zts_c
549 550
}

551
build_php_all() {
552
  build_php_all_32 true
553 554 555
  build_php_compatibility
}

556 557 558 559 560
# -------- main --------

if [ "$#" -ne 1 ]; then
  echo "
Usage: $0 { cpp |
561
            cpp_distcheck |
562 563 564
            csharp |
            java_jdk7 |
            java_oracle7 |
565
            java_compatibility |
566
            objectivec_ios |
567 568
            objectivec_ios_debug |
            objectivec_ios_release |
569
            objectivec_osx |
570
            objectivec_cocoapods_integration |
571 572
            python |
            python_cpp |
573
            python_compatibility |
574 575
            ruby21 |
            ruby22 |
Feng Xiao's avatar
Feng Xiao committed
576
            jruby |
577 578 579 580 581 582
            ruby_all |
            php5.5   |
            php5.5_c |
            php5.6   |
            php5.6_c |
            php7.0   |
583
            php7.0_c |
584
            php_compatibility |
585 586
            php7.1   |
            php7.1_c |
587
            php_all)
588 589 590 591 592 593
"
  exit 1
fi

set -e  # exit immediately on error
set -x  # display all commands
594
cd $(dirname $0)
595
eval "build_$1"