behave_tests: configure nfvbench ip/port with env vars 81/72781/2
authorGwenael Lambrouin <gwenael.lambrouin@orange.com>
Tue, 13 Jul 2021 16:27:51 +0000 (18:27 +0200)
committerGwenael Lambrouin <gwenael.lambrouin@orange.com>
Thu, 22 Jul 2021 15:07:51 +0000 (17:07 +0200)
It is now possible to configure nfvbench server IP address and port number
with environment variables: NFVBENCH_SERVER_HOST and NFVBENCH_SERVER_PORT.

It is still possible to configure them in feature files, and the values found
in feature files take precedence.

This allows to have behave tests and nfvbench server running on different
machines without changing feature files, which is especially useful for
testing.

Signed-off-by: Gwenael Lambrouin <gwenael.lambrouin@orange.com>
Change-Id: I98dc7f87a1a233b90b44dfc8b26a1e63961fff3c

behave_tests/features/environment.py
behave_tests/features/steps/steps.py

index ee1aa17..12cd4cc 100644 (file)
@@ -34,6 +34,10 @@ def before_all(context):
     context.data['NODE_NAME'] = os.getenv('NODE_NAME', 'nfvbench')
     context.data['BUILD_TAG'] = os.getenv('BUILD_TAG')
 
+    # NFVbench server host and port
+    context.host_ip = os.getenv('NFVBENCH_SERVER_HOST', '127.0.0.1')
+    context.port = int(os.getenv('NFVBENCH_SERVER_PORT', '7555'))
+
 
 def before_feature(context, feature):
     context.rates = {}
index 965b0c8..8d2c83f 100644 (file)
@@ -25,6 +25,7 @@ import json
 import requests
 import subprocess
 from subprocess import DEVNULL
+from typing import Optional
 
 from nfvbench.summarizer import Formatter
 from nfvbench.traffic_gen.traffic_utils import parse_rate_str
@@ -141,21 +142,26 @@ def add_percentage_rate(context, percentage_rate):
 @when('NFVbench API is ready')
 @when('NFVbench API is ready on host {host_ip}')
 @when('NFVbench API is ready on host {host_ip} and port {port:d}')
-def start_server(context, host_ip="127.0.0.1", port=7555):
-    context.host_ip = host_ip
-    context.port = port
+def start_server(context, host_ip: Optional[str]=None, port: Optional[int]=None):
+    # NFVbench server host IP and port number have been setup from environment variables (see
+    # environment.py:before_all()).   Here we allow to override them from feature files:
+    if host_ip is not None:
+        context.host_ip = host_ip
+    if port is not None:
+        context.port = port
+
     try:
         # check if API is already available
         requests.get(
             "http://{host_ip}:{port}/status".format(host_ip=context.host_ip, port=context.port))
     except RequestException:
         cmd = ["nfvbench", "-c", context.data['config'], "--server"]
-        if host_ip != "127.0.0.1":
+        if context.host_ip != "127.0.0.1":
             cmd.append("--host")
-            cmd.append(host_ip)
-        if port != 7555:
+            cmd.append(context.host_ip)
+        if context.port != 7555:
             cmd.append("--port")
-            cmd.append(port)
+            cmd.append(str(context.port))
 
         subprocess.Popen(cmd, stdout=DEVNULL, stderr=subprocess.STDOUT)