[VOL-4318] check correct indication of abort download

Further corrections:
Reset BBSIM in Teardown to make sequential running of SW Upgrade tests
possible. This makes bug [VOL-4262] as no longer needed.

Also deprecated keyword 'Restart Pod' is removed and all corresponding
test suites adapted accordingly.

Change-Id: I16b4002635ea6d23137ae93fbeb89a3846dfc484
diff --git a/libraries/k8s.robot b/libraries/k8s.robot
index 89c5fca..38da3ee 100755
--- a/libraries/k8s.robot
+++ b/libraries/k8s.robot
@@ -40,27 +40,19 @@
     Should Be Equal as Integers    ${rc}    0
     [Return]    ${port}
 
-Restart Pod
-    [Arguments]    ${namespace}    ${name}
-    [Documentation]    *DEPRECATED* Use Restart Pod By Label instead
-    ${rc}    ${restart_pod_name}=    Run and Return Rc and Output
-    ...    kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $1}'
-    Log    ${restart_pod_name}
-    Should Not Be Empty    ${restart_pod_name}    Unable to parse pod name
-    ${rc}    ${output}=    Run and Return Rc and Output
-    ...    kubectl delete pod ${restart_pod_name} -n ${namespace} --grace-period=0 --force
-    Log    ${output}
-
 Restart Pod By Label
     [Arguments]    ${namespace}    ${label_key}   ${label_value}
-    [Documentation]    Uses kubectl to force delete pod
+    [Documentation]    Uses kubectl to force delete pod(s)
     ${rc}    ${restart_pod_name}=    Run and Return Rc and Output
     ...    kubectl get pods -n ${namespace} -l ${label_key}=${label_value} --no-headers | awk '{print $1}'
     Log    ${restart_pod_name}
     Should Not Be Empty    ${restart_pod_name}    Unable to parse pod name
-    ${rc}    ${output}=    Run and Return Rc and Output
-    ...    kubectl delete pod ${restart_pod_name} -n ${namespace} --grace-period=0 --force
-    Log    ${output}
+    @{pods}=    Split String    ${restart_pod_name}    separator=${\n}
+    FOR    ${pod_name}    IN      @{pods}
+        ${rc}    ${output}=    Run and Return Rc and Output
+        ...    kubectl delete pod ${pod_name} -n ${namespace} --grace-period=0 --force
+        Log    ${output}
+    END
 
 Exec Pod
     [Arguments]    ${namespace}    ${name}    ${command}
@@ -447,9 +439,15 @@
 Pods Are Ready By Label
     [Arguments]    ${namespace}    ${key}    ${value}
     [Documentation]    Check that all pods with a label are ready
-    ${output}=    Run
-    ...    kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[].status.containerStatuses[].ready}"
-    Should Not Contain    ${output}    false
+    ${pod_names}=    Get Pod Name By Label    ${namespace}    ${key}    ${value}
+    Should Not Be Empty    ${pod_names}    Unable to parse pod name
+    @{pods}=    Split String    ${pod_names}    separator=${\n}
+    ${lenght}=    Get Length    ${pods}
+    FOR    ${I}    IN RANGE    0    ${lenght}
+        ${output}=    Run
+        ...    kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[${I}].status.containerStatuses[].ready}"
+        Should Not Contain    ${output}    false
+    END
 
 Wait For Pods Ready
     [Arguments]    ${namespace}    ${list_apps}
diff --git a/libraries/voltctl.robot b/libraries/voltctl.robot
index 91c0303..7a1ead6 100755
--- a/libraries/voltctl.robot
+++ b/libraries/voltctl.robot
@@ -878,6 +878,15 @@
     Should Be Equal As Integers    ${rc}    0
     Validate ONU Device Image    ${output}    ${ver}    ${id}    DOWNLOAD_STARTED    NO_ERROR    IMAGE_COMMITTING
 
+Abort ONU Device Image
+    [Documentation]    Aborts the given ONU software image
+    [Arguments]    ${ver}    ${id}    ${download_state}    ${expected_reason}    ${image_state}
+    ${rc}    ${output}=    Run and Return Rc and Output
+    ...    voltctl -c ${VOLTCTL_CONFIG} device onuimage abort ${ver} ${id} -o json
+    Log    ${output}
+    Should Be Equal As Integers    ${rc}    0
+    Validate ONU Device Image    ${output}    ${ver}    ${id}    ${download_state}    ${expected_reason}    ${image_state}
+
 Verify ONU Device Image Status
     [Documentation]    Verfies the ONU device image state
     [Arguments]    ${image_version}    ${dev_id}    ${download_state}    ${expected_reason}    ${image_state}
diff --git a/tests/functional/Voltha_FailureScenarios.robot b/tests/functional/Voltha_FailureScenarios.robot
index 9d9dba6..cfae5ee 100755
--- a/tests/functional/Voltha_FailureScenarios.robot
+++ b/tests/functional/Voltha_FailureScenarios.robot
@@ -681,7 +681,7 @@
     Log    ${podStatusOutput}
     ${countBforRestart}=    Run    kubectl get pods -n ${NAMESPACE} | grep Running | wc -l
     ${podName}    Set Variable     ofagent
-    Restart Pod    ${NAMESPACE}    ${podName}
+    Restart Pod By Label   ${NAMESPACE}    app    ${podName}
     Wait Until Keyword Succeeds    ${waitforRestart}    2s    Validate Pod Status    ofagent    ${NAMESPACE}
     ...    Running
     FOR    ${I}    IN RANGE    0    ${num_all_onus}
diff --git a/tests/openonu-go-adapter/Voltha_ONUReconcileTests.robot b/tests/openonu-go-adapter/Voltha_ONUReconcileTests.robot
index 14c7964..3b34d8f 100755
--- a/tests/openonu-go-adapter/Voltha_ONUReconcileTests.robot
+++ b/tests/openonu-go-adapter/Voltha_ONUReconcileTests.robot
@@ -190,7 +190,7 @@
     Run Keyword If    ${pausebeforecleanup}    Pause Execution    Press OK to continue with clean up!
     Run Keyword If    ${pausebeforecleanup}    Log    Teardown will be continued...    console=yes
     Run Keyword If    ${teardown_device}    Delete All Devices and Verify
-    Run Keyword If    ${usekill2restart}    Restart Pod    ${namespace}    open-onu
+    Run Keyword If    ${usekill2restart}    Restart Pod By Label    ${namespace}    app    adapter-open-onu
     Run Keyword Unless    ${etcdcheckintestteardown}    Wait Until Keyword Succeeds    ${timeout}    1s
     ...    Validate Onu Data In Etcd    0    ${kvstoreprefix}    without_pm_data=False
     Wait for Ports in ONOS for all OLTs      ${ONOS_SSH_IP}    ${ONOS_SSH_PORT}  0   BBSM    ${timeout}
@@ -204,7 +204,8 @@
     ...    ${olt_ssh_ip}    ${olt_user}    ${olt_pass}
     Run Keyword If    ${has_dataplane}    Sleep    60s
     #restart open-onu pod to reset crash loop back off mechansim of kubenetes
-    Run Keyword If    "${firsttest}"=="False" and "${usekill2restart}"=="True"    Restart Pod    ${namespace}    open-onu
+    Run Keyword If    "${firsttest}"=="False" and "${usekill2restart}"=="True"
+    ...    Restart Pod By Label    ${namespace}    app    adapter-open-onu
     Run Keyword If    "${firsttest}"=="False"    Sleep    35s
     ${firsttest}    Set Variable    False
     Set Suite Variable    ${firsttest}
diff --git a/tests/software-upgrades/ONU_Upgrade.robot b/tests/software-upgrades/ONU_Upgrade.robot
index a60f5d3..0f3d467 100755
--- a/tests/software-upgrades/ONU_Upgrade.robot
+++ b/tests/software-upgrades/ONU_Upgrade.robot
@@ -85,9 +85,10 @@
     ...    Check [VOL-3903] for more details
     [Tags]    functional   ONUUpgrade
     [Setup]    Start Logging    ONUUpgrade
-    [Teardown]    Run Keywords    Collect Logs
-    ...           AND             Stop Logging    ONUUpgrade
+    [Teardown]    Run Keywords    Run Keyword If    ${logging}    Collect Logs
     ...           AND             Delete All Devices and Verify
+    ...           AND             Restart And Check BBSIM    ${NAMESPACE}
+    ...           AND             Stop Logging    ONUUpgrade
     Test ONU Upgrade All OLTs
 
 Test ONU Upgrade All Activate and Commit Combinations
@@ -98,11 +99,12 @@
     ...    onu_image_name, onu_image_url, onu_image_version, onu_image_crc, onu_image_local_dir
     ...    Note: The TC expects the image url and other parameters to be common for all ONUs on all BBSim
     ...    Check [VOL-4250] for more details
-    [Tags]    functional   ONUUpgradeAllCombies    notready
+    [Tags]    functional   ONUUpgradeAllCombies
     [Setup]    Start Logging    ONUUpgradeAllCombies
-    [Teardown]    Run Keywords    Collect Logs
-    ...           AND             Stop Logging    ONUUpgradeAllCombies
+    [Teardown]    Run Keywords    Run Keyword If    ${logging}    Collect Logs
     ...           AND             Delete All Devices and Verify
+    ...           AND             Restart And Check BBSIM    ${NAMESPACE}
+    ...           AND             Stop Logging    ONUUpgradeAllCombies
     ${false_false}=    Create Dictionary    activate    false    commit    false
     ${true_false}=     Create Dictionary    activate    true     commit    false
     ${false_true}=     Create Dictionary    activate    false    commit    true
@@ -111,6 +113,7 @@
     FOR    ${item}     IN      @{flag_list}
         Test ONU Upgrade All OLTs    ${item['activate']}    ${item['commit']}
         Delete All Devices and Verify
+        Restart And Check BBSIM    ${NAMESPACE}
     END
 
 Test ONU Upgrade Correct Indication of Download Wrong Url
@@ -120,9 +123,10 @@
     ...    Check [VOL-4257] for more details
     [Tags]    functional   ONUUpgradeDownloadWrongUrl
     [Setup]    Start Logging    ONUUpgradeDownloadWrongUrl
-    [Teardown]    Run Keywords    Collect Logs
-    ...           AND             Stop Logging    ONUUpgradeDownloadWrongUrl
+    [Teardown]    Run Keywords    Run Keyword If    ${logging}    Collect Logs
     ...           AND             Delete All Devices and Verify
+    ...           AND             Restart And Check BBSIM    ${NAMESPACE}
+    ...           AND             Stop Logging    ONUUpgradeDownloadWrongUrl
     # Add OLT device
     Setup
     # Performing Sanity Test to make sure subscribers are all DHCP and pingable
@@ -151,9 +155,10 @@
     ...    Check [VOL-3935] for more details
     [Tags]    functional   ONUUpgradeDownloadFailure
     [Setup]    Start Logging    ONUUpgradeDownloadFailure
-    [Teardown]    Run Keywords    Collect Logs
-    ...           AND             Stop Logging    ONUUpgradeDownloadFailure
+    [Teardown]    Run Keywords    Run Keyword If    ${logging}    Collect Logs
     ...           AND             Delete All Devices and Verify
+    ...           AND             Restart And Check BBSIM    ${NAMESPACE}
+    ...           AND             Stop Logging    ONUUpgradeDownloadFailure
     # Add OLT device
     Setup
     # Performing Sanity Test to make sure subscribers are all DHCP and pingable
@@ -173,6 +178,34 @@
     Run Keyword If    ${has_dataplane}    Clean Up Linux
     Wait Until Keyword Succeeds    ${timeout}    2s    Perform Sanity Test
 
+Test ONU Upgrade Correct Indication of Download Abort
+    [Documentation]    Validates the ONU Upgrade download abort will be indicated correctly and
+    ...    doesn't affect the system functionality
+    ...    Performs the sanity and verifies all the ONUs are authenticated/DHCP/pingable
+    ...    Check [VOL-4318] for more details
+    [Tags]    functional   ONUUpgradeDownloadAbort
+    [Setup]    Start Logging    ONUUpgradeDownloadAbort
+    [Teardown]    Run Keywords    Run Keyword If    ${logging}    Collect Logs
+    ...           AND             Delete All Devices and Verify
+    ...           AND             Stop Logging    ONUUpgradeDownloadAbort
+    # Add OLT device
+    Setup
+    # Performing Sanity Test to make sure subscribers are all DHCP and pingable
+    Run Keyword If    ${has_dataplane}    Clean Up Linux
+    Wait Until Keyword Succeeds    ${timeout}    2s    Perform Sanity Test
+    FOR    ${J}    IN RANGE    0    ${num_olts}
+        ${olt_serial_number}=    Set Variable    ${list_olts}[${J}][sn]
+        ${bbsim_rel}=    Catenate    SEPARATOR=    bbsim    ${J}
+        ${bbsim_pod}=    Get Pod Name By Label    ${NAMESPACE}    release     ${bbsim_rel}
+        Test ONU Upgrade Download Abort Per OLT    ${bbsim_pod}    ${olt_serial_number}
+        List ONUs    ${NAMESPACE}    ${bbsim_pod}
+    END
+    # Additional Verification
+    Wait Until Keyword Succeeds    ${timeout}    2s    Delete All Devices and Verify
+    Setup
+    Run Keyword If    ${has_dataplane}    Clean Up Linux
+    Wait Until Keyword Succeeds    ${timeout}    2s    Perform Sanity Test
+
 
 *** Keywords ***
 Test ONU Upgrade All OLTs
@@ -261,6 +294,40 @@
         Wait Until Keyword Succeeds    ${timeout}    5s    Perform Sanity Test     ${suppressaddsubscriber}
     END
 
+Test ONU Upgrade Download Abort Per OLT
+    [Documentation]    This keyword performs the ONU Upgrade Dowload Abort test on single OLT
+    [Arguments]    ${bbsim_pod}    ${olt_serial_number}    ${url}=${image_url}
+    FOR    ${I}    IN RANGE    0    ${num_all_onus}
+        ${src}=    Set Variable    ${hosts.src[${I}]}
+        ${dst}=    Set Variable    ${hosts.dst[${I}]}
+        Continue For Loop If    "${olt_serial_number}"!="${src['olt']}"
+        ${onu_device_id}=    Get Device ID From SN    ${src['onu']}
+        # Download Image
+        Download ONU Device Image    ${image_version}    ${url}    ${image_vendor}
+        ...    ${image_activate_on_success}    ${image_commit_on_success}    ${image_crc}    ${onu_device_id}
+        Abort ONU Device Image    ${image_version}    ${onu_device_id}
+        ...    DOWNLOAD_CANCELLED    CANCELLED_ON_REQUEST    IMAGE_DOWNLOADING
+        Wait Until Keyword Succeeds    ${timeout}    2s    Verify ONU Device Image Status    ${image_version}
+        ...    ${onu_device_id}    DOWNLOAD_CANCELLED    CANCELLED_ON_REQUEST    IMAGE_DOWNLOADING
+        #   !!!    Expected is image is not visible in list   !!!
+        Wait Until Keyword Succeeds    ${timeout}    2s    Verify ONU Device Image List    ${onu_device_id}
+        ...    ${image_version}    False    False    True
+        Wait Until Keyword Succeeds    ${timeout}    5s    Perform Sanity Test     ${suppressaddsubscriber}
+    END
+
+Restart And Check BBSIM
+    [Documentation]    This keyword restarts bbsim and waits for it to come up again
+    ...    Following steps will be executed:
+    ...    - restart bbsim adaptor
+    ...    - check bbsim adaptor is ready again
+    [Arguments]    ${namespace}
+    ${bbsim_apps}   Create List    bbsim
+    ${label_key}    Set Variable   app
+    ${bbsim_label_value}    Set Variable   bbsim
+    Restart Pod By Label    ${namespace}    ${label_key}    ${bbsim_label_value}
+    Sleep    5s
+    Wait For Pods Ready    ${namespace}    ${bbsim_apps}
+
 Setup Suite
     [Documentation]    Set up the test suite
     Common Test Suite Setup