VOL-2753 Fix loop logic, improve loop error messages

Change-Id: I3af2e8ef01251d5ac77ec96222a01edd655c0c4a
diff --git a/libraries/k8s.robot b/libraries/k8s.robot
index 9a21190..52ad12c 100644
--- a/libraries/k8s.robot
+++ b/libraries/k8s.robot
@@ -89,14 +89,17 @@
 Validate Pod Status
     [Arguments]    ${pod_name}    ${namespace}   ${expectedStatus}
     [Documentation]    To run the kubectl command and check the status of the given pod matches the expected status
-    ${length}=    Run    kubectl get pod -n ${namespace} | wc -l
-    FOR    ${index}    IN RANGE    ${length}-1
+    ${length}=    Run    kubectl get pod -n ${namespace} -o name | wc -l
+    ${matched}=    Set Variable    False
+    FOR    ${index}    IN RANGE    ${length}
         ${currentPodName}=    Run
         ...    kubectl get pod -n ${namespace} -o=jsonpath="{.items[${index}].status.containerStatuses[0].name}"
         Log    Required Pod : ${pod_name}
         Log    Current Pod: ${currentPodName}
-        Run Keyword and Ignore Error    Run Keyword If    '${currentPodName}'=='${pod_name}'    Exit For Loop
+        ${matched}=    Set Variable If    '${currentPodName}'=='${pod_name}'    True    False
+        Exit For Loop If    ${matched}
     END
+    Should Be True    ${matched}    No pod ${podname} found
     ${currentStatusofPod}=    Run
     ...    kubectl get pod -n ${namespace} -o=jsonpath="{.items[${index}].status.phase}"
     Log    ${currentStatusofPod}
@@ -268,7 +271,7 @@
     [Documentation]    Succeeds it the named POD exists
     ${rc}    ${count}    Run and Return Rc
     ...    kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep ${name}
-    Should Be True    ${count}>0
+    Should Be True    ${count}>0    Pod ${name} not found
 
 Pod Does Not Exist
     [Arguments]    ${namespace}    ${name}
@@ -276,7 +279,7 @@
     ${rc}    ${count}    Run and Return Rc And Output
     ...    kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep -c ${name}
     Should Be Equal As Integers    ${count}    0
-    Should Be True    ${count}==0
+    Should Be True    ${count}==0    Pod ${name} exists but should not
 
 Pods Do Not Exist By Label
     [Arguments]    ${namespace}    ${key}    ${value}
@@ -284,7 +287,7 @@
     ${rc}    ${count}    Run and Return Rc And Output
     ...    kubectl get -n ${namespace} pod -l${key}=${value} -o json | jq -r ".items[].metadata.name" | wc -l
     Should Be Equal As Integers    ${count}    0
-    Should Be True    ${count}==0
+    Should Be True    ${count}==0    Pod with label ${key}=${value} exists but should not
 
 Get Available Deployment Replicas
     [Arguments]    ${namespace}    ${name}
diff --git a/libraries/onos.robot b/libraries/onos.robot
index 3160c6c..921ca50 100644
--- a/libraries/onos.robot
+++ b/libraries/onos.robot
@@ -33,13 +33,15 @@
     Should Not Be Empty    ${jsondata['devices']}
     ${length}=    Get Length    ${jsondata['devices']}
     @{serial_numbers}=    Create List
+    ${matched}=    Set Variable    False
     FOR    ${INDEX}    IN RANGE    0    ${length}
         ${value}=    Get From List    ${jsondata['devices']}    ${INDEX}
         ${of_id}=    Get From Dictionary    ${value}    id
         ${sn}=    Get From Dictionary    ${value}    serial
-        Run Keyword If    '${sn}' == '${serial_number}'    Exit For Loop
+        ${matched}=    Set Variable If    '${sn}' == '${serial_number}'    True    False
+        Exit For Loop If    ${matched}
     END
-    Should Be Equal As Strings    ${sn}    ${serial_number}
+    Should Be True    ${matched}    No match for ${serial_number} found
     [Return]    ${of_id}
 
 Get ONU Port in ONOS
@@ -51,14 +53,16 @@
     Should Not Be Empty    ${jsondata['ports']}
     ${length}=    Get Length    ${jsondata['ports']}
     @{ports}=    Create List
+    ${matched}=    Set Variable    False
     FOR    ${INDEX}    IN RANGE    0    ${length}
         ${value}=    Get From List    ${jsondata['ports']}    ${INDEX}
         ${annotations}=    Get From Dictionary    ${value}    annotations
         ${onu_port}=    Get From Dictionary    ${value}    port
         ${portName}=    Get From Dictionary    ${annotations}    portName
-        Run Keyword If    '${portName}' == '${onu_serial_number}'    Exit For Loop
+        ${matched}=    Set Variable If    '${portName}' == '${onu_serial_number}'    True    False
+        Exit For Loop If    ${matched}
     END
-    Should Be Equal As Strings    ${portName}    ${onu_serial_number}
+    Should Be True    ${matched}    No match for ${onu_serial_number} found
     [Return]    ${onu_port}
 
 Get FabricSwitch in ONOS
@@ -67,12 +71,15 @@
     ${jsondata}=    To Json    ${resp.content}
     Should Not Be Empty    ${jsondata['devices']}
     ${length}=    Get Length    ${jsondata['devices']}
+    ${matched}=    Set Variable    False
     FOR    ${INDEX}    IN RANGE    0    ${length}
         ${value}=    Get From List    ${jsondata['devices']}    ${INDEX}
         ${of_id}=    Get From Dictionary    ${value}    id
         ${type}=    Get From Dictionary    ${value}    type
-        Run Keyword If    '${type}' == "SWITCH"    Exit For Loop
+        ${matched}=    Set Variable If    '${type}' == "SWITCH"    True    False
+        Exit For Loop If    ${matched}
     END
+    Should Be True    ${matched}    No fabric switch found
     [Return]    ${of_id}
 
 Verify Eapol Flows Added
diff --git a/libraries/utils.robot b/libraries/utils.robot
index 59a6e55..e2cb049 100644
--- a/libraries/utils.robot
+++ b/libraries/utils.robot
@@ -92,15 +92,17 @@
         ...    wpa_cli -i ${iface} reassociate    ${ip}    ${user}
         ...    ${pass}    ${container_type}    ${container_name}
         ${passed}=    Run Keyword And Return Status    Should Contain    ${output}    OK
-        Run Keyword If    ${passed}    Exit For Loop
+        Exit For Loop If    ${passed}
     END
+    Should Be True    ${passed}    Status does not contain 'SUCCESS'
     FOR    ${i}    IN RANGE    70
         ${output}=    Login And Run Command On Remote System
         ...    wpa_cli -i ${iface} status | grep SUCCESS    ${ip}    ${user}
         ...    ${pass}    ${container_type}    ${container_name}
         ${passed}=    Run Keyword And Return Status    Should Contain    ${output}    SUCCESS
-        Run Keyword If    ${passed}    Exit For Loop
+        Exit For Loop If    ${passed}
     END
+    Should Be True    ${passed}    Status does not contain 'SUCCESS'
 
 Validate Authentication After Reassociate
     [Arguments]    ${auth_pass}    ${iface}    ${ip}    ${user}    ${pass}=${None}
diff --git a/libraries/voltctl.robot b/libraries/voltctl.robot
index f35d171..d48d693 100644
--- a/libraries/voltctl.robot
+++ b/libraries/voltctl.robot
@@ -154,10 +154,9 @@
         ${devId}=    Get From Dictionary    ${value}    id
         ${mib_state}=    Get From Dictionary    ${value}    reason
         ${matched}=    Set Variable If    '${sn}' == '${id}' or '${devId}' == '${id}'    True    False
-        Run Keyword If    ${matched}    Exit For Loop
+        Exit For Loop If    ${matched}
     END
-    Should Be True    ${matched}
-    ...    No match found for ${id} to validate device
+    Should Be True    ${matched}    No match found for ${id} to validate device
     Log    ${value}
     Should Be Equal    '${astate}'    '${admin_state}'    Device ${sn} admin_state != ${admin_state}
     ...    values=False
@@ -298,12 +297,15 @@
     ${jsondata}=    To Json    ${output}
     Log    ${jsondata}
     ${length}=    Get Length    ${jsondata}
+    ${matched}=    Set Variable    False
     FOR    ${INDEX}    IN RANGE    0    ${length}
         ${value}=    Get From List    ${jsondata}    ${INDEX}
         ${type}=    Get From Dictionary    ${value}    type
         ${peers}=    Get From Dictionary    ${value}    peers
-        Run Keyword If    '${type}' == 'PON_OLT'    Exit For Loop
+        ${matched}=    Set Variable If    '${type}' == 'PON_OLT'    True    False
+        Exit For Loop If    ${matched}
     END
+    Should Be True    ${matched}    No PON port found for OLT ${olt_device_id}
     ${length}=    Get Length    ${peers}
     FOR    ${INDEX}    IN RANGE    0    ${length}
         ${value}=    Get From List    ${peers}    ${INDEX}
@@ -326,12 +328,14 @@
     ${jsondata}=    To Json    ${output}
     Log    ${jsondata}
     ${length}=    Get Length    ${jsondata}
+    ${matched}=    Set Variable    False
     FOR    ${INDEX}    IN RANGE    0    ${length}
         ${value}=    Get From List    ${jsondata}    ${INDEX}
         ${devid}=    Get From Dictionary    ${value}    id
-        Run Keyword If    '${devid}' == '${olt_peer_id}'    Exit For Loop
-        Run Keyword If    '${INDEX}' == '${length}'    Fail    Peer id ${olt_peer_id} does not match any ONU device id;
+        ${matched}=    Set Variable If    '${devid}' == '${olt_peer_id}'    True    False
+        Exit For Loop If    ${matched}
     END
+    Should Be True    ${matched}    Peer id ${olt_peer_id} does not match any ONU device id
 
 Validate ONU Peer Id
     [Arguments]    ${olt_device_id}    ${List_ONU_Serial}
@@ -350,12 +354,15 @@
     ${jsondata}=    To Json    ${output}
     Log    ${jsondata}
     ${length}=    Get Length    ${jsondata}
+    ${matched}=    Set Variable    False
     FOR    ${INDEX}    IN RANGE    0    ${length}
         ${value}=    Get From List    ${jsondata}    ${INDEX}
         ${type}=    Get From Dictionary    ${value}    type
         ${peers}=    Get From Dictionary    ${value}    peers
-        Run Keyword If    '${type}' == 'PON_ONU'    Exit For Loop
+        ${matched}=    Set Variable If    '${type}' == 'PON_ONU'    True    False
+        Exit For Loop If    ${matched}
     END
+    Should Be True    ${matched}    No PON port found for ONU ${onu_dev_id}
     ${length}=    Get Length    ${peers}
     FOR    ${INDEX}    IN RANGE    0    ${length}
         ${value}=    Get From List    ${peers}    ${INDEX}
diff --git a/tests/functional/Voltha_AlarmTests.robot b/tests/functional/Voltha_AlarmTests.robot
index c0b2326..be8f104 100755
--- a/tests/functional/Voltha_AlarmTests.robot
+++ b/tests/functional/Voltha_AlarmTests.robot
@@ -416,13 +416,15 @@
 Raise Onu Alarm
     [Documentation]    Raise an Alarm
     [Arguments]    ${name}    ${sn}
-    ${raiseOutput}    Exec Pod    ${BBSIMCTL_NAMESPACE}     ${BBSIMCTL_POD_NAME}    bbsimctl onu alarms raise ${name} ${sn}
+    ${raiseOutput}    Exec Pod    ${BBSIMCTL_NAMESPACE}     ${BBSIMCTL_POD_NAME}
+    ...    bbsimctl onu alarms raise ${name} ${sn}
     Should Contain    ${raiseOutput}    Alarm Indication Sent
 
 Clear Onu Alarm
     [Documentation]    Raise an Alarm
     [Arguments]    ${name}    ${sn}
-    ${raiseOutput}    Exec Pod    ${BBSIMCTL_NAMESPACE}     ${BBSIMCTL_POD_NAME}    bbsimctl onu alarms clear ${name} ${sn}
+    ${raiseOutput}    Exec Pod    ${BBSIMCTL_NAMESPACE}     ${BBSIMCTL_POD_NAME}
+    ...    bbsimctl onu alarms clear ${name} ${sn}
     Should Contain    ${raiseOutput}    Alarm Indication Sent
 
 # Olt Alarms
@@ -450,13 +452,15 @@
 Raise Olt Alarm
     [Documentation]    Raise an Alarm
     [Arguments]    ${name}    ${intf_id}
-    ${raiseOutput}    Exec Pod    ${BBSIMCTL_NAMESPACE}     ${BBSIMCTL_POD_NAME}    bbsimctl olt alarms raise ${name} ${intf_id}
+    ${raiseOutput}    Exec Pod    ${BBSIMCTL_NAMESPACE}     ${BBSIMCTL_POD_NAME}
+    ...    bbsimctl olt alarms raise ${name} ${intf_id}
     Should Contain    ${raiseOutput}    Alarm Indication Sent
 
 Clear Olt Alarm
     [Documentation]    Raise an Alarm
     [Arguments]    ${name}    ${intf_id}
-    ${raiseOutput}    Exec Pod    ${BBSIMCTL_NAMESPACE}     ${BBSIMCTL_POD_NAME}    bbsimctl olt alarms clear ${name} ${intf_id}
+    ${raiseOutput}    Exec Pod    ${BBSIMCTL_NAMESPACE}     ${BBSIMCTL_POD_NAME}
+    ...    bbsimctl olt alarms clear ${name} ${intf_id}
     Should Contain    ${raiseOutput}    Alarm Indication Sent
 
 Get Device Event