SEBA-971 robot test integration with jenkins

Change-Id: I47c453d90fc4cd7b9717e6a39e553a46b8d27b43
diff --git a/Makefile b/Makefile
index d9c5e70..64b2e73 100644
--- a/Makefile
+++ b/Makefile
@@ -15,6 +15,8 @@
 # Configure shell
 SHELL = bash -eu -o pipefail
 
+ROOT_DIR  := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
+
 # Variables
 VERSION                  ?= $(shell cat ./VERSION)
 CONTAINER_NAME           ?= $(notdir $(abspath .))
@@ -32,6 +34,10 @@
 DOCKER_LABEL_COMMIT_DATE ?= $(shell git diff-index --quiet HEAD -- && git show -s --format=%cd --date=iso-strict HEAD || echo "unknown" )
 DOCKER_LABEL_BUILD_DATE  ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
 
+ROBOT_MOCK_OLT_FILE             ?= $(ROOT_DIR)/demo_test/functional_test/robot-mock-olt.yaml
+ROBOT_DEBUG_LOG_OPT             ?=
+ROBOT_MISC_ARGS                 ?=
+
 help:
 	@echo "Usage: make [<target>]"
 	@echo "where available targets are:"
@@ -50,6 +56,13 @@
 	@echo
 
 all: test
+
+# target to invoke mock-olt robot tests
+functional-mock-test: ROBOT_MISC_ARGS += $(ROBOT_DEBUG_LOG_OPT)
+functional-mock-test: ROBOT_CONFIG_FILE := $(ROBOT_MOCK_OLT_FILE)
+functional-mock-test: ROBOT_FILE := $(ROOT_DIR)/demo_test/functional_test/importer.robot
+functional-mock-test: importer-functional-test
+
 proto/importer.pb.go: proto/importer.proto
 	mkdir -p proto
 	protoc --proto_path=proto \
@@ -139,3 +152,20 @@
 
 clean:
 	@echo "No cleanup available"
+
+# Check out a copy of voltha-system-tests. We will reuse its robot configuration
+# and its robot libraries
+voltha-system-tests:
+	git clone https://github.com/opencord/voltha-system-tests.git
+
+# virtualenv for the robot tools
+# VOL-2724 Invoke pip via python3 to avoid pathname too long on QA jobs
+vst_venv: voltha-system-tests
+	virtualenv -p python3 $@ && \
+	VIRTUAL_ENV_DISABLE_PROMPT=true source ./$@/bin/activate && \
+	python -m pip install -r voltha-system-tests/requirements.txt
+
+importer-functional-test: vst_venv
+	VIRTUAL_ENV_DISABLE_PROMPT=true source ./$</bin/activate ; set -u ;\
+	cd demo_test/functional_test ;\
+	robot -V $(ROBOT_CONFIG_FILE) $(ROBOT_MISC_ARGS) $(ROBOT_FILE)
diff --git a/demo_test/functional_test/README b/demo_test/functional_test/README
index f8dd0e6..daba620 100644
--- a/demo_test/functional_test/README
+++ b/demo_test/functional_test/README
@@ -30,6 +30,11 @@
       iii) At command line, type
 	robot importer.robot
 
+      Note: If ../../voltha-system-tests does not exist, and the tests are run without
+            the use_mock_redfish variable set, then two harmless warnings will be emitted
+	    due to the missing robot voltha imports. These imports are only required
+	    when use_mock_redfish==True.
+
 2. Manual testing at command line
    To build 'dm', at command line, type
 	make
diff --git a/demo_test/functional_test/importer.robot b/demo_test/functional_test/importer.robot
index 1b10dab..389028b 100644
--- a/demo_test/functional_test/importer.robot
+++ b/demo_test/functional_test/importer.robot
@@ -14,78 +14,110 @@
 
 *** Settings ***
 Documentation     Provide the function to perform funtional tests for the Redfish device-management project
-Suite Setup       Get IP AND PORT
+Suite Setup       Setup Suite
+Suite Teardown    Teardown Suite
 Library           Process
 Library           OperatingSystem
 Library           BuiltIn
 Library           String
 Library           Collections
+Library           ../../voltha-system-tests/libraries/DependencyLibrary.py
+Resource           ../../voltha-system-tests/libraries/k8s.robot
 
 *** Variables ***
 @{ADDR_LIST}      192.168.4.26:8888    192.168.4.27:8888
+${timeout}        60s
+${use_mock_redfish}    False
+${use_containerized_dm}    False
+${voltha_suite_setup}    False
+${IMPORTER_POD_NAME}    redfish-importer
+${DEMOTEST_POD_NAME}    redfish-demotest
 
 *** Test Cases ***
 Add Device to Monitor
     [Documentation]    This test case excercises the API, SendDeviceList, which registers Redfish devices to monitor.
     ${EXPECTED}=    RUN    sed -e '/^\\/\\//d' -e 's/ip1/${IP1}/g' -e 's/port1/${PORT1}/g' -e 's/ip2/${IP2}/g' -e 's/port2/${PORT2}/g' tests/add_device_to_monitor.expected
-    ${OUTPUT}=    Run Process    tests/add_device_to_monitor.tc    ${IP1}    ${PORT1}    ${IP2}    ${PORT2}
-    Should Be Equal    ${EXPECTED}    ${OUTPUT.stdout}
+    ${OUTPUT}=    Run Test    tests/add_device_to_monitor.tc    ${IP1}    ${PORT1}    ${IP2}    ${PORT2}
+    Should Be Equal    ${EXPECTED}    ${OUTPUT}
 
 Clear Subscribed Events
     [Documentation]    This test case excercises the API, ClearCurrentEventList, which clears all Redfish evets currently subscribed to.
     ${EXPECTED}=    RUN    sed -e '/^\\/\\//d' -e 's/ip1/${IP1}/g' -e 's/port1/${PORT1}/g' tests/clear_all_subscribed_events.expected
-    ${OUTPUT}=    Run Process    tests/clear_all_subscribed_events.tc    ${IP1}    ${PORT1}
-    Should Be Equal    ${EXPECTED}    ${OUTPUT.stdout}
+    ${OUTPUT}=    Run Test    tests/clear_all_subscribed_events.tc    ${IP1}    ${PORT1}
+    Should Be Equal    ${EXPECTED}    ${OUTPUT}
 
 Configure Data Polling Interval
     [Documentation]    This test case excercises the API, SetFrequency, which configures the interval of data polling.
     ${EXPECTED}=    RUN    sed -e '/^\\/\\//d' -e 's/ip1/${IP1}/g' -e 's/port1/${PORT1}/g' tests/configure_data_polling_interval.expected
-    ${OUTPUT}=    Run Process    tests/configure_data_polling_interval.tc    ${IP1}    ${PORT1}
-    Should Be Equal    ${EXPECTED}    ${OUTPUT.stdout}
+    ${OUTPUT}=    Run Test    tests/configure_data_polling_interval.tc    ${IP1}    ${PORT1}
+    Should Be Equal    ${EXPECTED}    ${OUTPUT}
 
 Delete Monitored Device
     [Documentation]    This test case excercises the API, DeleteDeviceList, which deletes Redfish devices being monitored.
     ${EXPECTED}=    RUN    sed -e '/^\\/\\//d' -e 's/ip1/${IP1}/g' -e 's/port1/${PORT1}/g' -e 's/ip2/${IP2}/g' -e 's/port2/${PORT2}/g' tests/delete_monitored_device.expected
-    ${OUTPUT}=    Run Process    tests/delete_monitored_device.tc    ${IP1}    ${PORT1}    ${IP2}    ${PORT2}
-    Should Be Equal    ${EXPECTED}    ${OUTPUT.stdout}
+    ${OUTPUT}=    Run Test    tests/delete_monitored_device.tc    ${IP1}    ${PORT1}    ${IP2}    ${PORT2}
+    Should Be Equal    ${EXPECTED}    ${OUTPUT}
 
 List Devices monitored
     [Documentation]    This test case excercises the API, GetCurrentDevices, which lists all Redfish devices being monitored.
     ${EXPECTED}=    RUN    sed -e '/^\\/\\//d' -e 's/ip1/${IP1}/g' -e 's/port1/${PORT1}/g' -e 's/ip2/${IP2}/g' -e 's/port2/${PORT2}/g' tests/list_device_monitored.expected
-    ${OUTPUT}=    Run Process    tests/list_device_monitored.tc    ${IP1}    ${PORT1}    ${IP2}    ${PORT2}
-    Should Be Equal    ${EXPECTED}    ${OUTPUT.stdout}
+    ${OUTPUT}=    Run Test    tests/list_device_monitored.tc    ${IP1}    ${PORT1}    ${IP2}    ${PORT2}
+    Should Be Equal    ${EXPECTED}    ${OUTPUT}
 
 List Subscribed Events
     [Documentation]    This test case excercises the API, GetCurrentEventList, which lists all Redfish evets currently subscribed to.
     ${EXPECTED}=    RUN    sed -e '/^\\/\\//d' -e 's/ip1/${IP1}/g' -e 's/port1/${PORT1}/g' tests/list_subscribed_events.expected
-    ${OUTPUT}=    Run Process    tests/list_subscribed_events.tc    ${IP1}    ${PORT1}
-    Should Be Equal    ${EXPECTED}    ${OUTPUT.stdout}
+    ${OUTPUT}=    Run Test    tests/list_subscribed_events.tc    ${IP1}    ${PORT1}
+    Should Be Equal    ${EXPECTED}    ${OUTPUT}
 
 List Supported Events
     [Documentation]    This test case excercises the API, GetEventList, which lists all supported Redfish events.
     ${EXPECTED}=    RUN    sed -e '/^\\/\\//d' -e 's/ip1/${IP1}/g' -e 's/port1/${PORT1}/g' tests/list_supported_events.expected
-    ${OUTPUT}=    Run Process    tests/list_supported_events.tc    ${IP1}    ${PORT1}
-    Should Be Equal    ${EXPECTED}    ${OUTPUT.stdout}
+    ${OUTPUT}=    Run Test    tests/list_supported_events.tc    ${IP1}    ${PORT1}
+    Should Be Equal    ${EXPECTED}    ${OUTPUT}
 
 Subscribe Events
     [Documentation]    This test case excercises the API, SubscribeGivenEvents, which subscribes to the specified events.
     ${EXPECTED}=    RUN    sed -e '/^\\/\\//d' -e 's/ip1/${IP1}/g' -e 's/port1/${PORT1}/g' tests/subscribe_events.expected
-    ${OUTPUT}=    Run Process    tests/subscribe_events.tc    ${IP1}    ${PORT1}
-    Should Be Equal    ${EXPECTED}    ${OUTPUT.stdout}
+    ${OUTPUT}=    Run Test    tests/subscribe_events.tc    ${IP1}    ${PORT1}
+    Should Be Equal    ${EXPECTED}    ${OUTPUT}
 
 Unsubscribe Events
     [Documentation]    This test case excercises the API, UnsubscribeGivenEvents, which unsubscribes to the specified events.
     ${EXPECTED}=    RUN    sed -e '/^\\/\\//d' -e 's/ip1/${IP1}/g' -e 's/port1/${PORT1}/g' tests/unsubscribe_events.expected
-    ${OUTPUT}=    Run Process    tests/unsubscribe_events.tc    ${IP1}    ${PORT1}
-    Should Be Equal    ${EXPECTED}    ${OUTPUT.stdout}
+    ${OUTPUT}=    Run Test    tests/unsubscribe_events.tc    ${IP1}    ${PORT1}
+    Should Be Equal    ${EXPECTED}    ${OUTPUT}
 
 Validate IP
     [Documentation]    This test case validates the format of IP, whcih is expected to be in the form of <ip>:<port>.
     ${EXPECTED}=    RUN    sed -e '/^\\/\\//d' -e 's/ip1/${IP1}/g' -e 's/port1/${PORT1}/g' tests/validate_ip.expected
-    ${OUTPUT}=    Run Process    tests/validate_ip.tc    ${IP1}    ${PORT1}
-    Should Be Equal    ${EXPECTED}    ${OUTPUT.stdout}
+    ${OUTPUT}=    Run Test    tests/validate_ip.tc    ${IP1}    ${PORT1}
+    Should Be Equal    ${EXPECTED}    ${OUTPUT}
 
 *** Keywords ***
+Setup Suite
+    [Documentation]    Set up the test suite
+    # Common voltha-system-test related setup. Only do this with a physical OLT, when called from ONF Jenkins
+    Run Keyword If     ${voltha_suite_setup}    Common Test Suite Setup
+    # Ensure the redfish import and demotest containers are deployed and running.
+    Run Keyword If     ${use_mock_redfish}    Install Mock Redfish Server
+    Get IP AND PORT
+
+Teardown Suite
+    [Documentation]    Clean up devices if desired
+    ...    kills processes and cleans up interfaces on src+dst servers
+    Run Keyword If    ${use_mock_redfish}    Clean Up Mock Redfish Server
+
+Install Mock Redfish Server
+    Apply Kubernetes Resources    ../../kubernetes/deploy-redfish-importer.yaml    default
+    Wait Until Keyword Succeeds    ${timeout}    5s
+    ...    Validate Pod Status    ${IMPORTER_POD_NAME}    default     Running
+    Wait Until Keyword Succeeds    ${timeout}    5s
+    ...    Validate Pod Status    ${DEMOTEST_POD_NAME}    default     Running
+
+Clean Up Mock Redfish Server
+    Delete Kubernetes Resources    ../../kubernetes/deploy-redfish-importer.yaml    default
+
 Get IP AND PORT
     Sort List    ${ADDR_LIST}
     ${I1}=    Fetch From LEFT    ${ADDR_LIST}[0]    :
@@ -96,3 +128,23 @@
     Set Suite Variable    ${IP2}    ${I2}
     ${P2}=    Fetch From Right    ${ADDR_LIST}[1]    :
     Set Suite Variable    ${PORT2}    ${P2}
+
+Run Test
+    [Arguments]    @{args}
+    ${output}=    Run Keyword if    ${use_containerized_dm}
+    ...    Run Test In Container     @{args}
+    ...    ELSE
+    ...    Run Test On Host     @{args}
+    [Return]    ${output}
+
+Run Test On Host
+    [Arguments]    ${testname}    @{args}
+    ${output}=    Run Process    ${testname}    @{args}
+    [Return]    ${output.stdout}
+
+Run Test In Container
+    [Arguments]    ${testname}    @{args}
+    Copy File To Pod    default    ${DEMOTEST_POD_NAME}    ${testname}    "/test.tc"
+    ${argList}=    Evaluate  " ".join($args)
+    ${output}=    Exec Pod    default    ${DEMOTEST_POD_NAME}    sh /test.tc ${argList}
+    [Return]    ${output}
diff --git a/demo_test/functional_test/robot-mock-olt.yaml b/demo_test/functional_test/robot-mock-olt.yaml
new file mode 100644
index 0000000..9daea3a
--- /dev/null
+++ b/demo_test/functional_test/robot-mock-olt.yaml
@@ -0,0 +1,22 @@
+---
+
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+use_mock_redfish:    True
+use_containerized_dm:    True
+
+ADDR_LIST:
+  - mock-olt-1:8401
+  - mock-olt-2:8402
diff --git a/kubernetes/deploy-redfish-importer.yaml b/kubernetes/deploy-redfish-importer.yaml
index edbc582..b94b099 100755
--- a/kubernetes/deploy-redfish-importer.yaml
+++ b/kubernetes/deploy-redfish-importer.yaml
@@ -33,7 +33,7 @@
     spec:

       containers:

         - name: mock-olt-1

-          image: opencord/redfish-mockup-server:latest

+          image: opencord/redfish-mockup-server:master

           imagePullPolicy: Always

           ports:

             - containerPort: 8001

@@ -74,7 +74,7 @@
     spec:

       containers:

         - name: mock-olt-2

-          image: opencord/redfish-mockup-server:latest

+          image: opencord/redfish-mockup-server:master

           imagePullPolicy: Always

           ports:

             - containerPort: 8001

@@ -115,7 +115,7 @@
     spec:

       containers:

         - name: redfish-importer

-          image: opencord/device-management:latest

+          image: opencord/device-management:master

           imagePullPolicy: Always

           ports:

             - containerPort: 50051

@@ -158,7 +158,7 @@
     spec:

       containers:

         - name: redfish-demotest

-          image: opencord/redfish-importer-demotest:latest

+          image: opencord/redfish-importer-demotest:master

           imagePullPolicy: Always

           ports:

             - containerPort: 9999