Skip to content

Commit

Permalink
api: add u.pro.services.enable.v1
Browse files Browse the repository at this point in the history
  • Loading branch information
orndorffgrant committed Jan 4, 2024
1 parent ff75677 commit 421cfa9
Show file tree
Hide file tree
Showing 10 changed files with 446 additions and 22 deletions.
169 changes: 169 additions & 0 deletions features/api_enable.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
Feature: u.pro.services.enable

Scenario Outline: u.pro.services.enable.v1 container services
Given a `<release>` `<machine_type>` machine with ubuntu-advantage-tools installed
When I run `apt-get update` with sudo
And I apt install `jq`
And I attach `contract_token` with sudo and options `--no-auto-enable`

# Basic enable
And I run shell command `pro api u.pro.services.enable.v1 --args service=esm-infra | jq .data.attributes` with sudo
Then I will see the following on stdout:
"""
{
"disabled": [],
"enabled": [
"esm-infra"
],
"messages": [],
"reboot_required": false
}
"""
When I run shell command `pro api u.pro.status.enabled_services.v1 | jq .data.attributes.enabled_services ` with sudo
Then stdout contains substring:
"""
esm-infra
"""
# Enable already enabled service succeeds
When I run shell command `pro api u.pro.services.enable.v1 --args service=esm-infra | jq .data.attributes` with sudo
Then I will see the following on stdout:
"""
{
"disabled": [],
"enabled": [],
"messages": [],
"reboot_required": false
}
"""
# Disallowing required services when required causes error
When I verify that running `pro api u.pro.services.enable.v1 --data '{"service": "ros", "enable_required_services": false}'` `with sudo` exits `1`
When I run shell command `pro api u.pro.services.enable.v1 --data \"{\\\"service\\\": \\\"ros\\\", \\\"enable_required_services\\\": false}\" | jq .errors[0]` with sudo
Then I will see the following on stdout:
"""
{
"code": "enable-blocked-required-service",
"meta": {
"required": "esm-apps",
"target": "ros"
},
"title": "Could not enable ros because esm-apps is not enabled"
}
"""
# Default allows enabling required services
When I run shell command `pro api u.pro.services.enable.v1 --args service=ros | jq .data.attributes` with sudo
Then I will see the following on stdout:
"""
{
"disabled": [],
"enabled": [
"esm-apps",
"ros"
],
"messages": [],
"reboot_required": false
}
"""
# Access only works and post enable messages work
When I run shell command `pro api u.pro.services.enable.v1 --data \"{\\\"service\\\": \\\"cis\\\", \\\"access_only\\\": true}\" | jq .data.attributes` with sudo
Then I will see the following on stdout:
"""
{
"disabled": [],
"enabled": [
"cis"
],
"messages": [
"Visit https://ubuntu.com/security/cis to learn how to use CIS"
],
"reboot_required": false
}
"""
When I run `apt-cache policy usg-common` as non-root
Then stdout contains substring:
"""
Installed: (none)
"""
# Access only on service that doesn't support it fails
When I verify that running `pro api u.pro.services.enable.v1 --data '{"service": "ros-updates", "access_only": true}'` `with sudo` exits `1`
When I run shell command `pro api u.pro.services.enable.v1 --data \"{\\\"service\\\": \\\"ros-updates\\\", \\\"access_only\\\": true}\" | jq .errors[0]` with sudo
Then I will see the following on stdout:
"""
{
"code": "entitlement-not-enabled",
"meta": {
"reason": {
"code": "enable-access-only-not-supported",
"title": "ROS ESM All Updates does not support being enabled with --access-only"
}
},
"title": "failed to enable ros-updates"
}
"""
Examples:
| release | machine_type |
| xenial | lxd-container |
| bionic | lxd-container |
| focal | lxd-container |
| jammy | lxd-container |
Scenario Outline: u.pro.services.enable.v1 vm services
Given a `<release>` `<machine_type>` machine with ubuntu-advantage-tools installed
When I run `apt-get update` with sudo
And I apt install `jq`
And I attach `contract_token` with sudo and options `--no-auto-enable`
# Basic enable
And I run shell command `pro api u.pro.services.enable.v1 --args service=livepatch | jq .data.attributes` with sudo
Then I will see the following on stdout:
"""
{
"disabled": [],
"enabled": [
"livepatch"
],
"messages": [],
"reboot_required": false
}
"""
# Enable without disable_incompatible fails
When I verify that running `pro api u.pro.services.enable.v1 --data '{"service": "realtime-kernel", "disable_incompatible_services": false}'` `with sudo` exits `1`
When I run shell command `pro api u.pro.services.enable.v1 --data \"{\\\"service\\\": \\\"realtime-kernel\\\", \\\"disable_incompatible_services\\\": false}\" | jq .errors[0]` with sudo
Then I will see the following on stdout:
"""
{
"code": "enable-blocked-incompatible-service",
"meta": {
"incompatible": "livepatch",
"target": "realtime-kernel"
},
"title": "Could not enable realtime-kernel because livepatch is enabled"
}
"""
# Enable with disable_incompatible works and variant works
When I run shell command `pro api u.pro.services.enable.v1 --data \"{\\\"service\\\": \\\"realtime-kernel\\\", \\\"variant\\\": "intel-iotg"}\" | jq .data.attributes` with sudo
Then I will see the following on stdout:
"""
{
"disabled": [
"livepatch"
],
"enabled": [
"realtime-kernel"
],
"messages": [],
"reboot_required": false
}
"""
When I run shell command `pro api u.pro.status.enabled_services.v1 | jq ".data.attributes.enabled_services | select(.name==\"realtime-kernel\")" ` with sudo
Then stdout contains substring:
"""
{
"name": "realtime-kernel",
"variant_enabled": true,
"variant_name": "intel-iotg"
}
"""
Examples:
| release | machine_type |
| jammy | lxd-vm |
28 changes: 19 additions & 9 deletions features/steps/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ def when_i_run_command(


@when("I run shell command `{command}` {user_spec}")
def when_i_run_shell_command(context, command, user_spec):
when_i_run_command(context, 'sh -c "{}"'.format(command), user_spec)
def when_i_run_shell_command(context, command, user_spec, **kwargs):
when_i_run_command(
context, 'sh -c "{}"'.format(command), user_spec, **kwargs
)


@then("I verify that the `{cmd_name}` command is not found")
Expand All @@ -108,16 +110,17 @@ def i_verify_that_running_cmd_with_spec_and_stdin_exits_with_codes(
)


@step("I verify that running `{cmd_name}` `{spec}` exits `{exit_codes}`")
@step(
"I verify that running `{cmd_name}` `{spec}` and stdin `{stdin}` exits `{exit_codes}`" # noqa
)
def then_i_verify_that_running_cmd_with_spec_and_stdin_exits_with_codes(
context, cmd_name, spec, stdin, exit_codes
def then_i_verify_that_running_cmd_with_spec_exits_with_codes(
context, cmd_name, spec, exit_codes, stdin=None
):
when_i_run_command(
context, cmd_name, spec, stdin=stdin, verify_return=False
)

logging.debug("got return code: %d", context.process.returncode)
expected_codes = exit_codes.split(",")
if str(context.process.returncode) not in expected_codes:
raise AssertionError(
Expand All @@ -127,11 +130,18 @@ def then_i_verify_that_running_cmd_with_spec_and_stdin_exits_with_codes(
)


@step("I verify that running `{cmd_name}` `{spec}` exits `{exit_codes}`")
def then_i_verify_that_running_cmd_with_spec_exits_with_codes(
context, cmd_name, spec, exit_codes
@step(
"I verify that running shell command `{cmd_name}` `{spec}` exits `{exit_codes}`" # noqa
)
@step(
"I verify that running shell command `{cmd_name}` `{spec}` and stdin `{stdin}` exits `{exit_codes}`" # noqa
)
def then_i_verify_that_running_shell_cmd_with_spec_exits_with_codes(
context, cmd_name, spec, exit_codes, stdin=None
):
when_i_run_command(context, cmd_name, spec, verify_return=False)
when_i_run_shell_command(
context, cmd_name, spec, stdin=stdin, verify_return=False
)
logging.debug("got return code: %d", context.process.returncode)
expected_codes = exit_codes.split(",")
if str(context.process.returncode) not in expected_codes:
Expand Down
1 change: 1 addition & 0 deletions uaclient/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"u.pro.security.fix.usn.plan.v1",
"u.pro.security.status.livepatch_cves.v1",
"u.pro.security.status.reboot_required.v1",
"u.pro.services.enable.v1",
"u.pro.status.enabled_services.v1",
"u.pro.status.is_attached.v1",
"u.pro.version.v1",
Expand Down
8 changes: 8 additions & 0 deletions uaclient/api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
AlreadyAttachedError,
ConnectivityError,
ContractAPIError,
EnableBlockedByIncompatibleService,
EnableBlockedByRequiredService,
EntitlementNotEnabledError,
EntitlementNotFoundError,
EntitlementsNotEnabledError,
InvalidProImage,
LockHeldError,
NonAutoAttachImageError,
UbuntuProError,
UnattachedError,
UrlError,
UserFacingError,
)
Expand All @@ -23,9 +27,13 @@
"LockHeldError",
"NonAutoAttachImageError",
"UbuntuProError",
"UnattachedError",
"UrlError",
"UserFacingError",
"EntitlementsNotEnabledError",
"EntitlementNotEnabledError",
"EnableBlockedByIncompatibleService",
"EnableBlockedByRequiredService",
]


Expand Down
Empty file.
Empty file.
Loading

0 comments on commit 421cfa9

Please sign in to comment.