From 8266c67127912a7b53dac1b6a4f7823db4bcb67b Mon Sep 17 00:00:00 2001
From: Jan Tattermusch <jtattermusch@google.com>
Date: Thu, 17 Sep 2015 09:18:03 -0700
Subject: [PATCH] enable cloud_to_cloud tests with servers in a separate
 container

---
 .../Grpc.IntegrationTesting/InteropClient.cs  |   2 -
 .../build_docker_and_run_interop_tests.sh     | 111 ++++++++++++
 tools/jenkins/docker_prepare_interop_tests.sh |  79 +++++++++
 ...local.sh => docker_run_interop_servers.sh} |  35 ++--
 tools/jenkins/docker_run_interop_tests.sh     |  49 +-----
 tools/jenkins/docker_run_tests.sh             |   2 +-
 tools/jenkins/run_distribution.sh             |   2 +-
 tools/jenkins/run_jenkins.sh                  |  10 +-
 tools/run_tests/run_interop_tests.py          | 163 ++++++++++++++++--
 tools/run_tests/run_tests.py                  |   2 +-
 10 files changed, 365 insertions(+), 90 deletions(-)
 create mode 100755 tools/jenkins/build_docker_and_run_interop_tests.sh
 create mode 100755 tools/jenkins/docker_prepare_interop_tests.sh
 rename tools/jenkins/{run_local.sh => docker_run_interop_servers.sh} (63%)

diff --git a/src/csharp/Grpc.IntegrationTesting/InteropClient.cs b/src/csharp/Grpc.IntegrationTesting/InteropClient.cs
index 616093d4ae..504d798b89 100644
--- a/src/csharp/Grpc.IntegrationTesting/InteropClient.cs
+++ b/src/csharp/Grpc.IntegrationTesting/InteropClient.cs
@@ -126,8 +126,6 @@ namespace Grpc.IntegrationTesting
                     new ChannelOption(ChannelOptions.SslTargetNameOverride, options.ServerHostOverride)
                 };
             }
-            Console.WriteLine(options.ServerHost);
-            Console.WriteLine(options.ServerPort);
             var channel = new Channel(options.ServerHost, options.ServerPort, credentials, channelOptions);
             TestService.TestServiceClient client = new TestService.TestServiceClient(channel);
             await RunTestCaseAsync(client, options);
diff --git a/tools/jenkins/build_docker_and_run_interop_tests.sh b/tools/jenkins/build_docker_and_run_interop_tests.sh
new file mode 100755
index 0000000000..d2d56d947b
--- /dev/null
+++ b/tools/jenkins/build_docker_and_run_interop_tests.sh
@@ -0,0 +1,111 @@
+#!/bin/bash
+# Copyright 2015, Google Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+#     * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#     * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+#     * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+# This script is invoked by run_interop_tests.py to accommodate
+# "interop tests under docker" scenario. You should never need to call this
+# script on your own.
+
+set -ex
+
+cd `dirname $0`/../..
+git_root=`pwd`
+cd -
+
+mkdir -p /tmp/ccache
+
+# Use image name based on Dockerfile checksum
+DOCKER_IMAGE_NAME=grpc_jenkins_slave${docker_suffix}_`sha1sum tools/jenkins/grpc_jenkins_slave/Dockerfile | cut -f1 -d\ `
+
+# Make sure docker image has been built. Should be instantaneous if so.
+docker build -t $DOCKER_IMAGE_NAME tools/jenkins/grpc_jenkins_slave$docker_suffix
+
+# Create a local branch so the child Docker script won't complain
+git branch -f jenkins-docker
+
+# Make sure the CID files are gone.
+rm -f prepare.cid server.cid client.cid
+
+# Prepare image for interop tests
+docker run \
+  -e CCACHE_DIR=/tmp/ccache \
+  -i $TTY_FLAG \
+  -v "$git_root:/var/local/jenkins/grpc" \
+  -v /tmp/ccache:/tmp/ccache \
+  --cidfile=prepare.cid \
+  $DOCKER_IMAGE_NAME \
+  bash -l /var/local/jenkins/grpc/tools/jenkins/docker_prepare_interop_tests.sh || DOCKER_FAILED="true"
+
+PREPARE_CID=`cat prepare.cid`
+
+# Create image from the container, we will spawn one docker for clients
+# and one for servers.
+INTEROP_IMAGE=interop_`uuidgen`
+docker commit $PREPARE_CID $INTEROP_IMAGE
+# remove container, possibly killing it first
+docker rm -f $PREPARE_CID || true
+echo "Successfully built image $INTEROP_IMAGE"
+
+# run interop servers under docker in the background
+docker run \
+  -d -i \
+  $SERVERS_DOCKER_EXTRA_ARGS \
+  --cidfile=server.cid \
+  $INTEROP_IMAGE bash -l /var/local/git/grpc/tools/jenkins/docker_run_interop_servers.sh
+
+SERVER_CID=`cat server.cid`
+
+SERVER_PORTS=""
+for tuple in $SERVER_PORT_TUPLES
+do
+  # lookup under which port docker exposes given internal port
+  exposed_port=`docker port $SERVER_CID ${tuple#*:} | awk -F ":" '{print $NF}'`
+
+  # override the port for corresponding cloud_to_cloud server
+  SERVER_PORTS+=" --override_server ${tuple%:*}=localhost:$exposed_port"
+  echo "${tuple%:*} server is exposed under port $exposed_port"
+done
+
+# run interop clients
+docker run \
+  -e "RUN_TESTS_COMMAND=$RUN_TESTS_COMMAND $SERVER_PORTS" \
+  -w /var/local/git/grpc \
+  -i $TTY_FLAG \
+  --net=host \
+  --cidfile=client.cid \
+  $INTEROP_IMAGE bash -l /var/local/git/grpc/tools/jenkins/docker_run_interop_tests.sh || DOCKER_FAILED="true"
+
+CLIENT_CID=`cat client.cid`
+
+echo "killing and removing server container $SERVER_CID"
+docker rm -f $SERVER_CID || true
+
+docker cp $CLIENT_CID:/var/local/git/grpc/report.xml $git_root
+docker rm -f $CLIENT_CID || true
+docker rmi -f $DOCKER_IMAGE_NAME || true
diff --git a/tools/jenkins/docker_prepare_interop_tests.sh b/tools/jenkins/docker_prepare_interop_tests.sh
new file mode 100755
index 0000000000..a1fe0b72d2
--- /dev/null
+++ b/tools/jenkins/docker_prepare_interop_tests.sh
@@ -0,0 +1,79 @@
+#!/bin/bash
+# Copyright 2015, Google Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+#     * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#     * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+#     * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+# This script is invoked by run_jekins.sh. It contains the test logic
+# that should run inside a docker container.
+set -e
+
+mkdir -p /var/local/git
+git clone --recursive /var/local/jenkins/grpc /var/local/git/grpc
+
+cd /var/local/git/grpc
+nvm use 0.12
+rvm use ruby-2.1
+
+# TODO(jtattermusch): use cleaner way to install root certs
+mkdir -p /usr/local/share/grpc
+cp etc/roots.pem /usr/local/share/grpc/
+
+# build C++ interop client & server
+make interop_client interop_server
+
+# build C# interop client & server
+make install_grpc_csharp_ext
+(cd src/csharp && mono /var/local/NuGet.exe restore Grpc.sln)
+(cd src/csharp && xbuild Grpc.sln)
+
+# build Node interop client & server
+npm install -g node-gyp
+make install_c -C /var/local/git/grpc
+(cd src/node && npm install && node-gyp rebuild)
+
+# build Ruby interop client and server
+(cd src/ruby && gem update bundler && bundle && rake compile:grpc)
+
+# TODO(jtattermusch): add python
+
+# build PHP interop client
+# TODO(jtattermusch): prerequisites for PHP should be installed sooner than here.
+# Install composer
+curl -sS https://getcomposer.org/installer | php
+mv composer.phar /usr/local/bin/composer
+# Download the patched PHP protobuf so that PHP gRPC clients can be generated
+# from proto3 schemas.
+git clone https://github.com/stanley-cheung/Protobuf-PHP.git /var/local/git/protobuf-php
+(cd src/php/ext/grpc && phpize && ./configure && make)
+rvm all do gem install ronn rake
+(cd third_party/protobuf && make install)
+(cd /var/local/git/protobuf-php \
+  && rvm all do rake pear:package version=1.0 \
+  && pear install Protobuf-1.0.tgz)
+(cd src/php && composer install)
+(cd src/php && protoc-gen-php -i tests/interop/ -o tests/interop/ tests/interop/test.proto)
diff --git a/tools/jenkins/run_local.sh b/tools/jenkins/docker_run_interop_servers.sh
similarity index 63%
rename from tools/jenkins/run_local.sh
rename to tools/jenkins/docker_run_interop_servers.sh
index 4891637bbf..9f29a65aaa 100755
--- a/tools/jenkins/run_local.sh
+++ b/tools/jenkins/docker_run_interop_servers.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 # Copyright 2015, Google Inc.
 # All rights reserved.
 #
@@ -28,32 +28,23 @@
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #
-# This script can be used to run dockerized tests that normally run
-# on Jenkins on your local machine using the working copy that
-# is currently checked out locally.
-
-# IMPORTANT: The changes to be tested need to be committed locally,
-# otherwise they won't be cloned inside the docker container.
+# This script is invoked by run_jekins.sh. It contains the test logic
+# that should run inside a docker container.
 set -e
 
-cd `dirname $0`/../..
+cd /var/local/git/grpc
+nvm use 0.12
+rvm use ruby-2.1
 
-#TODO(jtattermusch): provide way to tunnel run_tests cmdline options to run_tests.
-#TODO(jtattermusch): provide way to grab the docker image built by run_jenkins
+# If port env variable is set, run corresponding interop server on given port in background.
+# TODO(jtattermusch): ideally, run_interop_tests.py would generate the commands to run servers.
 
-# config: opt or dbg
-export config=opt
+[ -z "${SERVER_PORT_cxx}" ] || bins/opt/interop_server --enable_ssl --port=${SERVER_PORT_cxx} &
 
-# platform:
-# -- use linux to run tests under docker
-# -- use interop to run dockerized interop tests
-export platform=interop
+[ -z "${SERVER_PORT_node}" ] || node src/node/interop/interop_server.js --use_tls=true --port=${SERVER_PORT_node} &
 
-# language: one of languages supported by run_tests.py or run_interop_tests.py
-export language=all
+[ -z "${SERVER_PORT_ruby}" ] || ruby src/ruby/bin/interop/interop_server.rb --use_tls --port=${SERVER_PORT_ruby} &
 
-# architecture
-export arch=`uname -m`
+[ -z "${SERVER_PORT_csharp}" ] || (cd src/csharp/Grpc.IntegrationTesting.Server/bin/Debug && mono Grpc.IntegrationTesting.Server.exe --use_tls --port=${SERVER_PORT_csharp}) &
 
-# test run configuration is done through environment variables above
-tools/jenkins/run_jenkins.sh
+sleep infinity
diff --git a/tools/jenkins/docker_run_interop_tests.sh b/tools/jenkins/docker_run_interop_tests.sh
index d363d1c68c..29970afb25 100755
--- a/tools/jenkins/docker_run_interop_tests.sh
+++ b/tools/jenkins/docker_run_interop_tests.sh
@@ -28,55 +28,12 @@
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #
-# This script is invoked by run_jekins.sh. It contains the test logic
-# that should run inside a docker container.
+# This script is invoked by build_docker_and_run_interop_tests.sh inside
+# a docker container. You should never need to call this script on your own.
 set -e
 
-mkdir -p /var/local/git
-git clone --recursive /var/local/jenkins/grpc /var/local/git/grpc
-
-cd /var/local/git/grpc
 nvm use 0.12
 rvm use ruby-2.1
 
-# TODO(jtattermusch): use cleaner way to install root certs
-mkdir -p /usr/local/share/grpc
-cp etc/roots.pem /usr/local/share/grpc/
-
-# build C++ interop client & server
-make interop_client interop_server
-
-# build C# interop client & server
-make install_grpc_csharp_ext
-(cd src/csharp && mono /var/local/NuGet.exe restore Grpc.sln)
-(cd src/csharp && xbuild Grpc.sln)
-
-# build Node interop client & server
-npm install -g node-gyp
-make install_c -C /var/local/git/grpc
-(cd src/node && npm install && node-gyp rebuild)
-
-# build Ruby interop client and server
-(cd src/ruby && gem update bundler && bundle && rake compile:grpc)
-
-# TODO(jtattermusch): add python
-
-# build PHP interop client
-# TODO(jtattermusch): prerequisites for PHP should be installed sooner than here.
-# Install composer
-curl -sS https://getcomposer.org/installer | php
-mv composer.phar /usr/local/bin/composer
-# Download the patched PHP protobuf so that PHP gRPC clients can be generated
-# from proto3 schemas.
-git clone https://github.com/stanley-cheung/Protobuf-PHP.git /var/local/git/protobuf-php
-(cd src/php/ext/grpc && phpize && ./configure && make)
-rvm all do gem install ronn rake
-(cd third_party/protobuf && make install)
-(cd /var/local/git/protobuf-php \
-  && rvm all do rake pear:package version=1.0 \
-  && pear install Protobuf-1.0.tgz)
-(cd src/php && composer install)
-(cd src/php && protoc-gen-php -i tests/interop/ -o tests/interop/ tests/interop/test.proto)
-
 # run the cloud-to-prod interop tests
-tools/run_tests/run_interop_tests.py -l $language
+$RUN_TESTS_COMMAND
diff --git a/tools/jenkins/docker_run_tests.sh b/tools/jenkins/docker_run_tests.sh
index 7877ad2459..3595a95f5c 100755
--- a/tools/jenkins/docker_run_tests.sh
+++ b/tools/jenkins/docker_run_tests.sh
@@ -28,7 +28,7 @@
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #
-# This script is invoked by build_docker_and_run_tests.py inside a docker
+# This script is invoked by build_docker_and_run_tests.sh inside a docker
 # container. You should never need to call this script on your own.
 
 set -e
diff --git a/tools/jenkins/run_distribution.sh b/tools/jenkins/run_distribution.sh
index 49b7d306d1..64c60f1502 100755
--- a/tools/jenkins/run_distribution.sh
+++ b/tools/jenkins/run_distribution.sh
@@ -54,7 +54,7 @@ if [ "$platform" == "linux" ]; then
     docker build -t $DOCKER_IMAGE_NAME tools/jenkins/grpc_linuxbrew
 
     # run per-language homebrew installation script
-    docker run $DOCKER_IMAGE_NAME bash -l \
+    docker run --rm=true $DOCKER_IMAGE_NAME bash -l \
       -c "nvm use 0.12; \
           npm set unsafe-perm true; \
           rvm use ruby-2.1; \
diff --git a/tools/jenkins/run_jenkins.sh b/tools/jenkins/run_jenkins.sh
index 66364b3093..4bc1852823 100755
--- a/tools/jenkins/run_jenkins.sh
+++ b/tools/jenkins/run_jenkins.sh
@@ -56,10 +56,6 @@ then
 
   ./tools/run_tests/run_tests.py --use_docker -t -l $language -c $config -x report.xml $@ || true
 
-elif [ "$platform" == "interop" ]
-then
-  python tools/run_tests/run_interops.py --language=$language $@
-
 elif [ "$platform" == "windows" ]
 then
   echo "building $language on Windows"
@@ -84,6 +80,12 @@ then
   echo "building $language on FreeBSD"
 
   MAKE=gmake ./tools/run_tests/run_tests.py -t -l $language -c $config -x report.xml $@ || true
+
+elif [ "$platform" == "interop" ]
+then
+  echo "building interop tests for language $language"
+
+  ./tools/run_tests/run_interop_tests.py --use_docker -t -l $language --cloud_to_prod --server all $@ || true
 else
   echo "Unknown platform $platform"
   exit 1
diff --git a/tools/run_tests/run_interop_tests.py b/tools/run_tests/run_interop_tests.py
index e9c4ce0a0f..a2fb1243cc 100755
--- a/tools/run_tests/run_interop_tests.py
+++ b/tools/run_tests/run_interop_tests.py
@@ -34,6 +34,10 @@ import argparse
 import itertools
 import xml.etree.cElementTree as ET
 import jobset
+import os
+import subprocess
+import sys
+import time
 
 
 _CLOUD_TO_PROD_BASE_ARGS = [
@@ -41,6 +45,9 @@ _CLOUD_TO_PROD_BASE_ARGS = [
     '--server_host=grpc-test.sandbox.google.com',
     '--server_port=443']
 
+_CLOUD_TO_CLOUD_BASE_ARGS = [
+    '--server_host_override=foo.test.google.fr']
+
 # TOOD(jtattermusch) wrapped languages use this variable for location 
 # of roots.pem. We might want to use GRPC_DEFAULT_SSL_ROOTS_FILE_PATH
 # supported by C core SslCredentials instead.
@@ -48,6 +55,7 @@ _SSL_CERT_ENV = { 'SSL_CERT_FILE':'/usr/local/share/grpc/roots.pem' }
 
 # TODO(jtatttermusch) unify usage of --enable_ssl, --use_tls and --use_tls=true
 
+
 class CXXLanguage:
 
   def __init__(self):
@@ -58,6 +66,10 @@ class CXXLanguage:
     return (self.client_cmdline_base + _CLOUD_TO_PROD_BASE_ARGS +
             ['--enable_ssl','--use_prod_roots'])
 
+  def cloud_to_cloud_args(self):
+    return (self.client_cmdline_base + _CLOUD_TO_CLOUD_BASE_ARGS +
+            ['--enable_ssl'])
+
   def cloud_to_prod_env(self):
     return None
 
@@ -75,6 +87,10 @@ class CSharpLanguage:
     return (self.client_cmdline_base + _CLOUD_TO_PROD_BASE_ARGS +
             ['--use_tls'])
 
+  def cloud_to_cloud_args(self):
+    return (self.client_cmdline_base + _CLOUD_TO_CLOUD_BASE_ARGS +
+            ['--use_tls', '--use_test_ca'])
+
   def cloud_to_prod_env(self):
     return _SSL_CERT_ENV
 
@@ -92,6 +108,10 @@ class NodeLanguage:
     return (self.client_cmdline_base + _CLOUD_TO_PROD_BASE_ARGS +
             ['--use_tls=true'])
 
+  def cloud_to_cloud_args(self):
+    return (self.client_cmdline_base + _CLOUD_TO_CLOUD_BASE_ARGS +
+            ['--use_tls=true', '--use_test_ca=true'])
+
   def cloud_to_prod_env(self):
     return _SSL_CERT_ENV
 
@@ -109,6 +129,10 @@ class PHPLanguage:
     return (self.client_cmdline_base + _CLOUD_TO_PROD_BASE_ARGS +
             ['--use_tls'])
 
+  def cloud_to_cloud_args(self):
+    return (self.client_cmdline_base + _CLOUD_TO_CLOUD_BASE_ARGS +
+            ['--use_tls', '--use_test_ca'])
+
   def cloud_to_prod_env(self):
     return _SSL_CERT_ENV
 
@@ -126,6 +150,10 @@ class RubyLanguage:
     return (self.client_cmdline_base + _CLOUD_TO_PROD_BASE_ARGS +
             ['--use_tls'])
 
+  def cloud_to_cloud_args(self):
+    return (self.client_cmdline_base + _CLOUD_TO_CLOUD_BASE_ARGS +
+            ['--use_tls', '--use_test_ca'])
+
   def cloud_to_prod_env(self):
     return _SSL_CERT_ENV
 
@@ -142,6 +170,10 @@ _LANGUAGES = {
     'ruby' : RubyLanguage(),
 }
 
+# languages supported as cloud_to_cloud servers 
+# TODO(jtattermusch): enable other languages as servers as well
+_SERVERS = { 'c++' : 8010, 'node' : 8040, 'csharp': 8070 }
+
 # TODO(jtattermusch): add empty_stream once C++ start supporting it.
 # TODO(jtattermusch): add support for auth tests.
 _TEST_CASES = ['large_unary', 'empty_unary', 'ping_pong',
@@ -149,6 +181,7 @@ _TEST_CASES = ['large_unary', 'empty_unary', 'ping_pong',
                'cancel_after_begin', 'cancel_after_first_response',
                'timeout_on_sleeping_server']
 
+
 def cloud_to_prod_jobspec(language, test_case):
   """Creates jobspec for cloud-to-prod interop test"""
   cmdline = language.cloud_to_prod_args() + ['--test_case=%s' % test_case]
@@ -160,34 +193,138 @@ def cloud_to_prod_jobspec(language, test_case):
           timeout_seconds=60)
   return test_job
 
+
+def cloud_to_cloud_jobspec(language, test_case, server_name, server_host,
+                           server_port):
+  """Creates jobspec for cloud-to-cloud interop test"""
+  cmdline = language.cloud_to_cloud_args() + ['--test_case=%s' % test_case,
+     '--server_host=%s' % server_host,
+     '--server_port=%s' % server_port ]
+  test_job = jobset.JobSpec(
+          cmdline=cmdline,
+          cwd=language.client_cwd,
+          shortname="cloud_to_cloud:%s:%s_server:%s" % (language, server_name,
+                                                 test_case),
+          timeout_seconds=60)
+  return test_job
+
 argp = argparse.ArgumentParser(description='Run interop tests.')
 argp.add_argument('-l', '--language',
                   choices=['all'] + sorted(_LANGUAGES),
                   nargs='+',
-                  default=['all'])
+                  default=['all'],
+                  help='Clients to run.')
+argp.add_argument('-j', '--jobs', default=24, type=int)
+argp.add_argument('--cloud_to_prod',
+                  default=False,
+                  action='store_const',
+                  const=True,
+                  help='Run cloud_to_prod tests.')
+argp.add_argument('-s', '--server',
+                  choices=['all'] + sorted(_SERVERS),
+                  action='append',
+                  help='Run cloud_to_cloud servers in a separate docker ' +
+                       'image. Servers can only be started automatically if ' +
+                       '--use_docker option is enabled.',
+                  default=[])
+argp.add_argument('--override_server',
+                  action='append',
+                  type=lambda kv: kv.split("="),
+                  help='Use servername=HOST:PORT to explicitly specify a server. E.g. csharp=localhost:50000',
+                  default=[])
+argp.add_argument('-t', '--travis',
+                  default=False,
+                  action='store_const',
+                  const=True)
+argp.add_argument('--use_docker',
+                  default=False,
+                  action='store_const',
+                  const=True,
+                  help='Run all the interop tests under docker. That provides ' +
+                  'additional isolation and prevents the need to install ' +
+                  'language specific prerequisites. Only available on Linux.')
 args = argp.parse_args()
 
+servers = set(s for s in itertools.chain.from_iterable(_SERVERS.iterkeys()
+                                                       if x == 'all' else [x]
+                                                       for x in args.server))
+
+if args.use_docker:
+  if not args.travis:
+    print 'Seen --use_docker flag, will run interop tests under docker.'
+    print
+    print 'IMPORTANT: The changes you are testing need to be locally committed'
+    print 'because only the committed changes in the current branch will be'
+    print 'copied to the docker environment.'
+    time.sleep(5)
+
+  child_argv = [ arg for arg in sys.argv if not arg == '--use_docker' ]
+  run_tests_cmd = ('tools/run_tests/run_interop_tests.py %s' %
+                   " ".join(child_argv[1:]))
+
+  # cmdline args to pass to the container running servers.
+  servers_extra_docker_args = ''
+  server_port_tuples = ''
+  for server in servers:
+    port = _SERVERS[server]
+    servers_extra_docker_args += ' -p %s' % port
+    servers_extra_docker_args += ' -e SERVER_PORT_%s=%s' % (server.replace("+", "x"), port)
+    server_port_tuples += ' %s:%s' % (server, port)
+
+  env = os.environ.copy()
+  env['RUN_TESTS_COMMAND'] = run_tests_cmd
+  env['SERVERS_DOCKER_EXTRA_ARGS'] = servers_extra_docker_args
+  env['SERVER_PORT_TUPLES'] = server_port_tuples
+  if not args.travis:
+    env['TTY_FLAG'] = '-t'  # enables Ctrl-C when not on Jenkins.
+
+  subprocess.check_call(['tools/jenkins/build_docker_and_run_interop_tests.sh'],
+                        shell=True,
+                        env=env)
+  sys.exit(0)
+
 languages = set(_LANGUAGES[l]
                 for l in itertools.chain.from_iterable(
                       _LANGUAGES.iterkeys() if x == 'all' else [x]
                       for x in args.language))
 
-# TODO(jtattermusch): make python script generate cmdline params for interop
-# tests. It's easier to manage than in a shell script.
 jobs = []
-jobNumber = 0
-for language in languages:
-  for test_case in _TEST_CASES:
-    test_job = cloud_to_prod_jobspec(language, test_case)
-    jobs.append(test_job)
-    jobNumber+=1
+if args.cloud_to_prod:
+  for language in languages:
+    for test_case in _TEST_CASES:
+      test_job = cloud_to_prod_jobspec(language, test_case)
+      jobs.append(test_job)
+
+# default servers to "localhost" and the default port
+server_addresses = dict((s, ("localhost", _SERVERS[s])) for s in servers)
+
+for server in args.override_server:
+  server_name = server[0]
+  (server_host, server_port) = server[1].split(":")
+  server_addresses[server_name] = (server_host, server_port)
+
+for server_name, server_address in server_addresses.iteritems():
+  (server_host, server_port) = server_address
+  for language in languages:
+    for test_case in _TEST_CASES:
+      test_job = cloud_to_cloud_jobspec(language,
+                                        test_case,
+                                        server_name,
+                                        server_host,
+                                        server_port)
+      jobs.append(test_job)
+
+if not jobs:
+  print "No jobs to run."
+  sys.exit(1)
 
 root = ET.Element('testsuites')
 testsuite = ET.SubElement(root, 'testsuite', id='1', package='grpc', name='tests')
 
-jobset.run(jobs, maxjobs=jobNumber, xml_report=testsuite)
+if jobset.run(jobs, newline_on_success=True, maxjobs=args.jobs, xml_report=testsuite):
+  jobset.message('SUCCESS', 'All tests passed', do_newline=True)
+else:
+  jobset.message('FAILED', 'Some tests failed', do_newline=True)
 
 tree = ET.ElementTree(root)
-tree.write('report.xml', encoding='UTF-8')
-
-
+tree.write('report.xml', encoding='UTF-8')
\ No newline at end of file
diff --git a/tools/run_tests/run_tests.py b/tools/run_tests/run_tests.py
index cbbb58d7e7..b2370537e5 100755
--- a/tools/run_tests/run_tests.py
+++ b/tools/run_tests/run_tests.py
@@ -556,7 +556,7 @@ argp.add_argument('--use_docker',
                   action='store_const',
                   const=True,
                   help="Run all the tests under docker. That provides " +
-                  "additional isolation and prevents the need to installs " +
+                  "additional isolation and prevents the need to install " +
                   "language specific prerequisites. Only available on Linux.")
 argp.add_argument('--allow_flakes',
                   default=False,
-- 
GitLab