tests.sh 16.2 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 -std=c++11"  # -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 42 43 44 45 46 47 48 49
build_cpp_tcmalloc() {
  internal_build_cpp
  ./configure LIBS=-ltcmalloc && make clean && make \
      PTHREAD_CFLAGS='-pthread -DGOOGLE_PROTOBUF_HEAP_CHECK_DRACONIAN' \
      check
  cd src
  PPROF_PATH=/usr/bin/google-pprof HEAPCHECK=draconian ./protobuf-test
}

50
build_cpp_distcheck() {
51 52
  # Initialize any submodules.
  git submodule update --init --recursive
53 54
  ./autogen.sh
  ./configure
55 56 57
  make dist

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

83
build_csharp() {
84
  # Required for conformance tests and to regenerate protos.
85
  internal_build_cpp
86
  NUGET=/usr/local/bin/nuget.exe
87

88 89 90 91
  # 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.
92 93 94
  # (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.)
95 96 97 98
  mkdir dotnettmp
  (cd dotnettmp; dotnet new > /dev/null)
  rm -rf dotnettmp

99 100 101
  # Check that the protos haven't broken C# codegen.
  # TODO(jonskeet): Fail if regenerating creates any changes.
  csharp/generate_protos.sh
102

103
  csharp/buildall.sh
104
  cd conformance && make test_csharp && cd ..
105 106 107

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

Tim Swast's avatar
Tim Swast committed
110 111 112 113 114 115 116
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
117
  mkdir -p "$GOPATH/src/github.com/protocolbuffers"
Feng Xiao's avatar
Feng Xiao committed
118 119
  rm -f "$GOPATH/src/github.com/protocolbuffers/protobuf"
  ln -s "`pwd`" "$GOPATH/src/github.com/protocolbuffers/protobuf"
Tim Swast's avatar
Tim Swast committed
120 121 122
  export PATH="$GOPATH/bin:$PATH"
  go get github.com/golang/protobuf/protoc-gen-go

Feng Xiao's avatar
Feng Xiao committed
123
  cd examples && PROTO_PATH="-I../src -I." make gotest && cd ..
Tim Swast's avatar
Tim Swast committed
124 125
}

126 127 128 129 130
use_java() {
  version=$1
  case "$version" in
    jdk7)
      export PATH=/usr/lib/jvm/java-7-openjdk-amd64/bin:$PATH
131
      export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
132 133 134
      ;;
    oracle7)
      export PATH=/usr/lib/jvm/java-7-oracle/bin:$PATH
135
      export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
136 137 138
      ;;
  esac

139
  MAVEN_LOCAL_REPOSITORY=/var/maven_local_repository
140
  MVN="$MVN -e -X -Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=$MAVEN_LOCAL_REPOSITORY"
141

142 143
  which java
  java -version
144
  $MVN -version
145 146
}

147
# --batch-mode supresses download progress output that spams the logs.
148
MVN="mvn --batch-mode"
149

150
build_java() {
151 152
  version=$1
  dir=java_$version
153 154
  # Java build needs `protoc`.
  internal_build_cpp
155 156
  cp -r java $dir
  cd $dir && $MVN clean && $MVN test
157 158 159
  cd ../..
}

160 161
# 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.
162
build_java_with_conformance_tests() {
163
  # Java build needs `protoc`.
164
  internal_build_cpp
165
  cd java && $MVN test && $MVN install
166
  cd util && $MVN package assembly:single
Feng Xiao's avatar
Feng Xiao committed
167
  cd ../..
168 169 170 171 172
  cd conformance && make test_java && cd ..
}

build_java_jdk7() {
  use_java jdk7
173
  build_java_with_conformance_tests
174 175 176
}
build_java_oracle7() {
  use_java oracle7
177
  build_java oracle7
178
}
179 180 181 182 183 184 185 186
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
}
187

188
build_objectivec_ios() {
189
  # Reused the build script that takes care of configuring and ensuring things
190 191
  # are up to date.  The OS X test runs the objc conformance test, so skip it
  # here.
192
  objectivec/DevTools/full_mac_build.sh \
193
      --core-only --skip-xcode-osx --skip-xcode-tvos --skip-objc-conformance "$@"
194 195
}

196 197
build_objectivec_ios_debug() {
  build_objectivec_ios --skip-xcode-release
198 199
}

200 201
build_objectivec_ios_release() {
  build_objectivec_ios --skip-xcode-debug
202 203 204
}

build_objectivec_osx() {
205 206 207
  # Reused the build script that takes care of configuring and ensuring things
  # are up to date.
  objectivec/DevTools/full_mac_build.sh \
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
      --core-only --skip-xcode-ios --skip-xcode-tvos
}

build_objectivec_tvos() {
  # Reused the build script that takes care of configuring and ensuring things
  # are up to date.  The OS X test runs the objc conformance test, so skip it
  # here.
  objectivec/DevTools/full_mac_build.sh \
      --core-only --skip-xcode-ios --skip-xcode-osx --skip-objc-conformance "$@"
}

build_objectivec_tvos_debug() {
  build_objectivec_tvos --skip-xcode-release
}

build_objectivec_tvos_release() {
  build_objectivec_tvos --skip-xcode-debug
225
}
226

227 228
build_objectivec_cocoapods_integration() {
  # Update pod to the latest version.
229
  gem install cocoapods --no_document
230 231 232
  objectivec/Tests/CocoaPods/run_tests.sh
}

233
build_python() {
234
  internal_build_cpp
235
  cd python
236
  if [ $(uname -s) == "Linux" ]; then
Jie Luo's avatar
Jie Luo committed
237
    envlist=py\{27,33,34,35,36\}-python
238 239 240 241
  else
    envlist=py27-python
  fi
  tox -e $envlist
242 243 244
  cd ..
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
build_python_version() {
  internal_build_cpp
  cd python
  envlist=$1
  tox -e $envlist
  cd ..
}

build_python27() {
  build_python_version py27-python
}

build_python33() {
  build_python_version py33-python
}

build_python34() {
  build_python_version py34-python
}

build_python35() {
  build_python_version py35-python
}

build_python36() {
  build_python_version py36-python
}

build_python37() {
  build_python_version py37-python
}

277
build_python_cpp() {
278
  internal_build_cpp
279 280
  export LD_LIBRARY_PATH=../src/.libs # for Linux
  export DYLD_LIBRARY_PATH=../src/.libs # for OS X
281
  cd python
282
  if [ $(uname -s) == "Linux" ]; then
Jie Luo's avatar
Jie Luo committed
283
    envlist=py\{27,33,34,35,36\}-cpp
284 285 286 287
  else
    envlist=py27-cpp
  fi
  tox -e $envlist
288 289 290
  cd ..
}

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
build_python_cpp_version() {
  internal_build_cpp
  export LD_LIBRARY_PATH=../src/.libs # for Linux
  export DYLD_LIBRARY_PATH=../src/.libs # for OS X
  cd python
  envlist=$1
  tox -e $envlist
  cd ..
}

build_python27_cpp() {
  build_python_cpp_version py27-cpp
}

build_python33_cpp() {
  build_python_cpp_version py33-cpp
}

build_python34_cpp() {
  build_python_cpp_version py34-cpp
}

build_python35_cpp() {
  build_python_cpp_version py35-cpp
}

build_python36_cpp() {
  build_python_cpp_version py36-cpp
}

build_python37_cpp() {
  build_python_cpp_version py37-cpp
}

325 326 327 328 329 330 331 332 333 334
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
}

335 336
build_ruby23() {
  internal_build_cpp  # For conformance tests.
337
  cd ruby && bash travis-test.sh ruby-2.3.8 && cd ..
338 339 340 341 342 343 344
}
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.
345
  cd ruby && bash travis-test.sh ruby-2.5.1 && cd ..
346
}
Paul Yang's avatar
Paul Yang committed
347 348 349
build_ruby26() {
  internal_build_cpp  # For conformance tests.
  cd ruby && bash travis-test.sh ruby-2.6.0 && cd ..
350 351
}

352 353
build_javascript() {
  internal_build_cpp
354
  cd js && npm install && npm test && cd ..
355
  cd conformance && make test_nodejs && cd ..
356 357
}

358 359 360 361 362 363
generate_php_test_proto() {
  internal_build_cpp
  pushd php/tests
  # Generate test file
  rm -rf generated
  mkdir generated
364
  ../../src/protoc --php_out=generated         \
365
    -I../../src -I.                            \
366
    proto/empty/echo.proto                     \
367 368 369 370 371 372 373 374 375 376 377 378 379 380
    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         \
381
    proto/test_wrapper_type_setters.proto      \
382
    proto/test_descriptors.proto
383
  pushd ../../src
384 385
  ./protoc --php_out=../php/tests/generated -I../php/tests -I. \
    ../php/tests/proto/test_import_descriptor_proto.proto
386 387 388 389
  popd
  popd
}

390 391
use_php() {
  VERSION=$1
Paul Yang's avatar
Paul Yang committed
392 393 394
  export PATH=/usr/local/php-${VERSION}/bin:$PATH
  export CPLUS_INCLUDE_PATH=/usr/local/php-${VERSION}/include/php/main:/usr/local/php-${VERSION}/include/php/:$CPLUS_INCLUDE_PATH
  export C_INCLUDE_PATH=/usr/local/php-${VERSION}/include/php/main:/usr/local/php-${VERSION}/include/php/:$C_INCLUDE_PATH
395
  generate_php_test_proto
396 397
}

Bo Yang's avatar
Bo Yang committed
398 399
use_php_zts() {
  VERSION=$1
Paul Yang's avatar
Paul Yang committed
400 401 402
  export PATH=/usr/local/php-${VERSION}-zts/bin:$PATH
  export CPLUS_INCLUDE_PATH=/usr/local/php-${VERSION}-zts/include/php/main:/usr/local/php-${VERSION}-zts/include/php/:$CPLUS_INCLUDE_PATH
  export C_INCLUDE_PATH=/usr/local/php-${VERSION}-zts/include/php/main:/usr/local/php-${VERSION}-zts/include/php/:$C_INCLUDE_PATH
403
  generate_php_test_proto
Bo Yang's avatar
Bo Yang committed
404 405
}

406 407
use_php_bc() {
  VERSION=$1
Paul Yang's avatar
Paul Yang committed
408 409 410
  export PATH=/usr/local/php-${VERSION}-bc/bin:$PATH
  export CPLUS_INCLUDE_PATH=/usr/local/php-${VERSION}-bc/include/php/main:/usr/local/php-${VERSION}-bc/include/php/:$CPLUS_INCLUDE_PATH
  export C_INCLUDE_PATH=/usr/local/php-${VERSION}-bc/include/php/main:/usr/local/php-${VERSION}-bc/include/php/:$C_INCLUDE_PATH
411
  generate_php_test_proto
412 413
}

414
build_php5.5() {
415
  use_php 5.5
416

417
  pushd php
418
  rm -rf vendor
Paul Yang's avatar
Paul Yang committed
419 420
  composer update
  ./vendor/bin/phpunit
421
  popd
422
  pushd conformance
423
  make test_php
424
  popd
425 426
}

427
build_php5.5_c() {
428
  use_php 5.5
429
  pushd php/tests
430
  /bin/bash ./test.sh 5.5
431
  popd
432 433 434 435
  # TODO(teboring): Add it back
  # pushd conformance
  # make test_php_c
  # popd
436 437
}

Bo Yang's avatar
Bo Yang committed
438 439
build_php5.5_zts_c() {
  use_php_zts 5.5
440
  cd php/tests && /bin/bash ./test.sh 5.5-zts && cd ../..
441 442 443 444
  # TODO(teboring): Add it back
  # pushd conformance
  # make test_php_zts_c
  # popd
445 446
}

447
build_php5.6() {
448
  use_php 5.6
449
  pushd php
450
  rm -rf vendor
Paul Yang's avatar
Paul Yang committed
451 452
  composer update
  ./vendor/bin/phpunit
453
  popd
454
  pushd conformance
455
  make test_php
456
  popd
457 458
}

459 460
build_php5.6_c() {
  use_php 5.6
461
  cd php/tests && /bin/bash ./test.sh 5.6 && cd ../..
462 463
  # TODO(teboring): Add it back
  # pushd conformance
464
  # make test_php_c
465
  # popd
466 467 468 469
}

build_php5.6_zts_c() {
  use_php_zts 5.6
470
  cd php/tests && /bin/bash ./test.sh 5.6-zts && cd ../..
471 472 473 474
  # TODO(teboring): Add it back
  # pushd conformance
  # make test_php_zts_c
  # popd
475 476
}

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

  # Install phpunit
485 486
  curl https://phar.phpunit.de/phpunit-5.6.8.phar -L -o phpunit.phar
  chmod +x phpunit.phar
487 488 489 490 491 492 493 494 495
  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 ../..
496 497
  # TODO(teboring): Add it back
  # pushd conformance
498
  # make test_php_c
499
  # popd
500 501
}

502
build_php7.0() {
503
  use_php 7.0
504
  pushd php
505
  rm -rf vendor
Paul Yang's avatar
Paul Yang committed
506 507
  composer update
  ./vendor/bin/phpunit
508
  popd
509
  pushd conformance
510
  make test_php
511
  popd
512 513
}

514 515
build_php7.0_c() {
  use_php 7.0
516
  cd php/tests && /bin/bash ./test.sh 7.0 && cd ../..
517 518
  # TODO(teboring): Add it back
  # pushd conformance
519
  # make test_php_c
520
  # popd
521 522 523 524
}

build_php7.0_zts_c() {
  use_php_zts 7.0
525
  cd php/tests && /bin/bash ./test.sh 7.0-zts && cd ../..
526 527 528 529
  # TODO(teboring): Add it back.
  # pushd conformance
  # make test_php_zts_c
  # popd
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
}

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 ../..
551 552
  # TODO(teboring): Add it back
  # pushd conformance
553
  # make test_php_c
554
  # popd
555 556
}

557 558 559 560 561
build_php_compatibility() {
  internal_build_cpp
  php/tests/compatibility_test.sh
}

562 563 564 565
build_php7.1() {
  use_php 7.1
  pushd php
  rm -rf vendor
Paul Yang's avatar
Paul Yang committed
566 567
  composer update
  ./vendor/bin/phpunit
568 569
  popd
  pushd conformance
570
  make test_php
571 572 573 574
  popd
}

build_php7.1_c() {
575
  ENABLE_CONFORMANCE_TEST=$1
576
  use_php 7.1
577
  cd php/tests && /bin/bash ./test.sh 7.1 && cd ../..
578 579 580 581 582 583
  if [ "$ENABLE_CONFORMANCE_TEST" = "true" ]
  then
    pushd conformance
    make test_php_c
    popd
  fi
584 585 586 587
}

build_php7.1_zts_c() {
  use_php_zts 7.1
588
  cd php/tests && /bin/bash ./test.sh 7.1-zts && cd ../..
589 590 591 592 593
  pushd conformance
  # make test_php_c
  popd
}

594
build_php_all_32() {
595 596 597
  build_php5.5
  build_php5.6
  build_php7.0
598
  build_php7.1
599 600
  build_php5.5_c
  build_php5.6_c
601
  build_php7.0_c
602
  build_php7.1_c $1
Bo Yang's avatar
Bo Yang committed
603
  build_php5.5_zts_c
604 605
  build_php5.6_zts_c
  build_php7.0_zts_c
606
  build_php7.1_zts_c
607 608
}

609
build_php_all() {
610
  build_php_all_32 true
611 612 613
  build_php_compatibility
}

614
build_benchmark() {
615
  use_php 7.2
616 617 618
  cd kokoro/linux/benchmark && ./run.sh
}

619 620 621 622 623
# -------- main --------

if [ "$#" -ne 1 ]; then
  echo "
Usage: $0 { cpp |
624
            cpp_distcheck |
625 626 627
            csharp |
            java_jdk7 |
            java_oracle7 |
628
            java_compatibility |
629
            objectivec_ios |
630 631
            objectivec_ios_debug |
            objectivec_ios_release |
632
            objectivec_osx |
633 634 635
            objectivec_tvos |
            objectivec_tvos_debug |
            objectivec_tvos_release |
636
            objectivec_cocoapods_integration |
637 638
            python |
            python_cpp |
639
            python_compatibility |
640 641 642
            ruby23 |
            ruby24 |
            ruby25 |
Paul Yang's avatar
Paul Yang committed
643
            ruby26 |
Feng Xiao's avatar
Feng Xiao committed
644
            jruby |
645 646 647 648 649 650
            ruby_all |
            php5.5   |
            php5.5_c |
            php5.6   |
            php5.6_c |
            php7.0   |
651
            php7.0_c |
652
            php_compatibility |
653 654
            php7.1   |
            php7.1_c |
655
            php_all |
656
            benchmark)
657 658 659 660 661 662
"
  exit 1
fi

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