Repository: emissary-ingress/emissary

Issue:
GRPC e2e test



Discussion:

Resolving patch:
diff --git a/end-to-end/010-multiple-ambassadors/fix_env.py b/end-to-end/fix_deployment.py
similarity index 67%
rename from end-to-end/010-multiple-ambassadors/fix_env.py
rename to end-to-end/fix_deployment.py
--- a/end-to-end/010-multiple-ambassadors/fix_env.py
+++ b/end-to-end/fix_deployment.py
@@ -6,10 +6,12 @@
 
 import dpath
 
-id_num = sys.argv[1]
-namespace = 'test-010-%s' % id_num
+namespace = sys.argv[1]
+ambassador_id = sys.argv[2]
+input_yaml_path = sys.argv[3]
+output_yaml_path = sys.argv[4]
 
-manifest = list(yaml.safe_load_all(open(sys.argv[2], 'r')))
+manifest = list(yaml.safe_load_all(open(input_yaml_path, 'r')))
 
 kinds_to_delete = {
     "ClusterRole": True,
@@ -28,11 +30,11 @@
         # print("Skipping %s %s" % (kind, name))
         continue
 
-    # print("Adding namespace %s to %s %s" % (namespace, kind, name))
+    print("Adding namespace %s to %s %s" % (namespace, kind, name))
     dpath.util.new(manifest, "/%d/metadata/namespace" % x, namespace)
 
     if kind == 'Deployment':
-        # print("Setting environment for %s %s" % (kind, name))
+        print("Setting environment for %s %s" % (kind, name))
         path = "/%d/spec/template/spec/containers/0/env" % x
         dpath.util.new(manifest, path, [
             {
@@ -41,10 +43,10 @@
             },
             {
                 'name': 'AMBASSADOR_ID',
-                'value': 'ambassador-%s' % id_num
+                'value': ambassador_id
             }            
         ])
     
     keep.append(manifest[x])
 
-yaml.safe_dump_all(keep, open(sys.argv[3], "w"), default_flow_style=False)
+yaml.safe_dump_all(keep, open(output_yaml_path, "w"), default_flow_style=False)
diff --git a/end-to-end/grpc-service/server.py b/end-to-end/grpc-service/server.py
new file mode 100644
--- /dev/null
+++ b/end-to-end/grpc-service/server.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+
+from concurrent import futures
+import time
+import os
+
+import grpc
+
+from grpc_reflection.v1alpha import reflection
+from grpc_reflection.v1alpha import reflection_pb2_grpc
+
+import helloworld_pb2
+import helloworld_pb2_grpc
+
+listen_address = '[::]:50051'
+_ONE_DAY_IN_SECONDS = 60 * 60 * 24
+
+
+class Greeter(helloworld_pb2_grpc.GreeterServicer):
+
+    def SayHello(self, request, context):
+        return helloworld_pb2.HelloReply(message='Hello, {} (host: {})!'.format(request.name, os.getenv("HOSTNAME")))
+
+
+def serve():
+    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
+    greeter = Greeter()
+    helloworld_pb2_grpc.add_GreeterServicer_to_server(greeter, server)
+    server.add_insecure_port(listen_address)
+
+    reflection.enable_server_reflection(["helloworld.Greeter"], server)
+    server.start()
+
+    channel = grpc.insecure_channel('localhost:%d' % 50051)
+    reflection_pb2_grpc.ServerReflectionStub(channel)
+
+    try:
+        print("Server started, listening on: {}".format(listen_address))
+        while True:
+            time.sleep(_ONE_DAY_IN_SECONDS)
+    except KeyboardInterrupt:
+        server.stop(0)
+
+
+if __name__ == '__main__':
+    serve()
diff --git a/end-to-end/grpc-service/setup.py b/end-to-end/grpc-service/setup.py
new file mode 100644
--- /dev/null
+++ b/end-to-end/grpc-service/setup.py
@@ -0,0 +1,18 @@
+from setuptools import setup, find_packages
+
+setup(
+    name="grpc-basic",
+    version="head",
+    packages=find_packages(exclude=["tests"]),
+    include_package_data=True,
+    install_requires=[
+        "grpcio",
+        "grpcio-tools"
+    ],
+    author="datawire.io",
+    author_email="dev@datawire.io",
+    url="https://github.com/datawire/ambassador-examples/grpc-basic",
+    download_url="https://github.com/datawire/ambassador-examples/grpc-basic",
+    keywords=[],
+    classifiers=[],
+)


Test patch:
diff --git a/end-to-end/009-grpc/test.sh b/end-to-end/009-grpc/test.sh
new file mode 100644
--- /dev/null
+++ b/end-to-end/009-grpc/test.sh
@@ -0,0 +1,78 @@
+#!/bin/bash
+
+set -e -o pipefail
+
+HERE=$(cd $(dirname $0); pwd)
+
+cd "$HERE"
+
+ROOT=$(cd ..; pwd)
+PATH="${ROOT}:${PATH}"
+
+source ${ROOT}/utils.sh
+
+initialize_namespace "grpc-test"
+
+# Make sure cluster-wide RBAC is set up.
+kubectl apply -f ${ROOT}/rbac.yaml
+
+kubectl cluster-info
+
+python ${ROOT}/fix_deployment.py grpc-test grpc-test ../ambassador-deployment.yaml k8s/ambassador-deployment.yaml
+
+kubectl apply -f k8s/rbac.yaml
+kubectl apply -f k8s/ambassador.yaml
+kubectl apply -f k8s/ambassador-deployment.yaml
+kubectl apply -f k8s/grpc.yaml
+# kubectl run demotest -n grpc-test --image=dwflynn/demotest:0.0.1 -- /bin/sh -c "sleep 3600"
+
+set +e +o pipefail
+
+wait_for_pods grpc-test
+
+CLUSTER=$(cluster_ip)
+APORT=$(service_port ambassador grpc-test)
+# DEMOTEST_POD=$(demotest_pod)
+
+BASEURL="http://${CLUSTER}:${APORT}"
+
+echo "Base URL $BASEURL"
+echo "Diag URL $BASEURL/ambassador/v0/diag/"
+
+wait_for_ready "$BASEURL"
+
+if ! check_diag "$BASEURL" 1 "grpc annotated"; then
+    exit 1
+fi
+
+check_grpc () {
+    number="$1"
+
+    name="Test Name $number"
+    count=$(sh grpcurl.sh -plaintext -d "{\"name\": \"$name\"}" ${CLUSTER}:${APORT} helloworld.Greeter/SayHello | \
+            jget.py /message | \
+            grep -c "Hello, $name" || true)
+    echo "$count $number"
+}
+
+echo "Starting GRPC calls"
+
+cp /dev/null count.log
+
+iterations=10
+for i in $(seq 1 ${iterations}); do
+    check_grpc $i >> count.log &
+done
+
+wait
+
+failures=$(egrep -c -v '^1 ' count.log)
+
+if [ $failures -gt 0 ]; then
+    echo "FAILED"
+    cat count.log
+    exit 1
+else
+    echo "OK"
+    exit 0
+fi
diff --git a/end-to-end/010-multiple-ambassadors/test.sh b/end-to-end/010-multiple-ambassadors/test.sh
--- a/end-to-end/010-multiple-ambassadors/test.sh
+++ b/end-to-end/010-multiple-ambassadors/test.sh
@@ -15,8 +15,8 @@ initialize_cluster
 
 kubectl cluster-info
 
-python fix_env.py 1 ../ambassador-deployment.yaml k8s/ambassador-deployment-1.yaml
-python fix_env.py 2 ../ambassador-deployment.yaml k8s/ambassador-deployment-2.yaml
+python ${ROOT}/fix_deployment.py test-010-1 ambassador-1 ../ambassador-deployment.yaml k8s/ambassador-deployment-1.yaml
+python ${ROOT}/fix_deployment.py test-010-2 ambassador-2 ../ambassador-deployment.yaml k8s/ambassador-deployment-2.yaml
 
 kubectl create namespace test-010-1
 kubectl create namespace test-010-2