From 81a332aaa3044c5bb09c823650a4d0bae7cf7d9a Mon Sep 17 00:00:00 2001 From: a-dubs Date: Tue, 13 Aug 2024 11:57:46 -0400 Subject: [PATCH] test(network): add behave tests to ensure network errors are handled --- features/network_failures.feature | 92 +++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 features/network_failures.feature diff --git a/features/network_failures.feature b/features/network_failures.feature new file mode 100644 index 0000000000..70f5ab3856 --- /dev/null +++ b/features/network_failures.feature @@ -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 `` `` 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": { + "error": + } + } + ] + } + """ + 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 : {"error": } + """ + 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 `` `` 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 `` `` 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 `` `` 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 |