Skip to content

Commit

Permalink
test(network): add behave tests to ensure network errors are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
a-dubs committed Aug 20, 2024
1 parent 559c1c7 commit 81a332a
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions features/network_failures.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
@uses.config.contract_token
Feature: Ensure network errors are handled gracefully across various services

Scenario Outline: Various HTTP errors are handled gracefully on attaching contract token
# This test simulates various HTTP errors by mocking the response from the serviceclient
# when trying to attach contract token
Given a `<release>` `<machine_type>` machine with ubuntu-advantage-tools installed
When I create the file `/tmp/response-overlay.json` with the following:
"""
{
"https://contracts.canonical.com/v1/context/machines/token": [
{
"code": <response_code>,
"response": {
"error": <error_message>
}
}
]
}
"""
And I append the following on uaclient config:
"""
features:
serviceclient_url_responses: "/tmp/response-overlay.json"
"""
When I attempt to attach `contract_token` with sudo
Then stderr contains substring:
"""
Error connecting to <endpoint>: <response_code> {"error": <error_message>}
"""
Then the machine is unattached

Examples: ubuntu release
| release | machine_type | endpoint | response_code | error_message |
| jammy | lxd-container | /v1/context/machines/token | 400 | "Bad Request" |
| jammy | lxd-container | /v1/context/machines/token | 404 | "Not Found" |
| jammy | lxd-container | /v1/context/machines/token | 503 | "Bad Gateway" |

Scenario Outline: Network errors for attaching contract token are handled gracefully
# This test simulates network failure by disabling internet connection
# and then trying to attach contract token
Given a `<release>` `<machine_type>` machine with ubuntu-advantage-tools installed
When I disable any internet connection on the machine
And I attempt to attach `contract_token` with sudo
Then stderr contains substring:
"""
Failed to attach machine. See https://ubuntu.com/pro/dashboard
"""
Then the machine is unattached

Examples: ubuntu release
| release | machine_type |
| jammy | lxd-container |

Scenario Outline: Network errors for enabling Realtime kernel are handled gracefully
# This test simulates network failure by disabling internet connection
# and then trying to enable realtime-kernel
Given a `<release>` `<machine_type>` machine with ubuntu-advantage-tools installed
When I attach `contract_token` with sudo and options `--no-auto-enable`
Then the machine is attached
Then I verify that `realtime-kernel` is disabled
When I disable any internet connection on the machine
And I verify that running `pro enable realtime-kernel` `with sudo` and stdin `y\ny` exits `1`
Then stderr contains substring:
"""
Failed to connect to https://contracts.canonical.com/v1/contracts/
"""
Then I verify that `realtime-kernel` is disabled

# Realtime kernel is not supported on LXD containers so we must use a VM
Examples: ubuntu release
| release | machine_type |
| jammy | lxd-vm |

Scenario Outline: Network errors for enabling Livepatch are handled gracefully
# This test simulates network failure by disabling internet connection
# and then trying to enable livepatch
Given a `<release>` `<machine_type>` machine with ubuntu-advantage-tools installed
When I attach `contract_token` with sudo and options `--no-auto-enable`
Then the machine is attached
Then I verify that `livepatch` is disabled
When I disable any internet connection on the machine
Then I verify that running `pro enable livepatch` `with sudo` exits `1`
Then stderr contains substring:
"""
Failed to connect to https://contracts.canonical.com/v1/contracts/
"""
Then I verify that `livepatch` is disabled

Examples: ubuntu release
| release | machine_type |
| jammy | lxd-container |

0 comments on commit 81a332a

Please sign in to comment.