Skip to content

Commit

Permalink
Added in functionality to disable the snapshotting.
Browse files Browse the repository at this point in the history
  • Loading branch information
lizsalmon committed Jan 6, 2025
1 parent 807fead commit cdebf99
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
4 changes: 4 additions & 0 deletions actions/server.migrate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ parameters:
type: string
required: false
description: "Optional, host to migrate server to"
snapshot:
type: boolean
required: true
default: true
runner_type: python-script
6 changes: 4 additions & 2 deletions lib/openstack_api/openstack_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ def snapshot_and_migrate_server(
conn: Connection,
server_id: str,
server_status: str,
snapshot: bool,
dest_host: Optional[str] = None,
) -> None:
"""
Snapshot a server and then migrate it to a new host
Optionally snapshot a server and then migrate it to a new host
:param conn: Openstack Connection
:param server_id: Server ID to migrate
:param server_status: Status of machine to migrate - must be ACTIVE or SHUTOFF
:param dest_host: Optional host to migrate to, otherwise chosen by scheduler
"""
snapshot_server(conn=conn, server_id=server_id)
if snapshot:
snapshot_server(conn=conn, server_id=server_id)
if server_status == "ACTIVE":
live = True
elif server_status == "SHUTOFF":
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/openstack_api/test_openstack_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def test_active_migration(mock_snapshot_server, dest_host):
server_id=mock_server_id,
server_status=mock_server_status,
dest_host=dest_host,
snapshot=True,
)
mock_snapshot_server.assert_called_once_with(
conn=mock_connection, server_id=mock_server_id
Expand All @@ -42,6 +43,7 @@ def test_shutoff_migration(mock_snapshot_server, dest_host):
server_id=mock_server_id,
server_status=mock_server_status,
dest_host=dest_host,
snapshot=True,
)
mock_snapshot_server.assert_called_once_with(
conn=mock_connection, server_id=mock_server_id
Expand All @@ -52,8 +54,28 @@ def test_shutoff_migration(mock_snapshot_server, dest_host):
)


@patch("openstack_api.openstack_server.snapshot_server")
def test_no_snapshot_migration(mock_snapshot_server):
"""
Test migration with no snapshot
"""
mock_connection = MagicMock()
mock_server_id = "server1"
mock_server_status = "ACTIVE"
snapshot_and_migrate_server(
conn=mock_connection,
server_id=mock_server_id,
server_status=mock_server_status,
snapshot=False,
)
mock_snapshot_server.assert_not_called()


@patch("openstack_api.openstack_server.snapshot_server")
def test_migration_fail(mock_snapshot_server):
"""
Test failure of migration when the status is not ACTIVE or SHUTOFF
"""
mock_connection = MagicMock()
mock_server_id = "server1"
mock_server_status = "TEST"
Expand All @@ -65,6 +87,7 @@ def test_migration_fail(mock_snapshot_server):
conn=mock_connection,
server_id=mock_server_id,
server_status=mock_server_status,
snapshot=True,
)
mock_snapshot_server.assert_called_once_with(
conn=mock_connection, server_id=mock_server_id
Expand Down
12 changes: 8 additions & 4 deletions tests/lib/workflows/test_search_by_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,22 @@ def test_search_by_property_all(mock_openstackquery, mock_to_webhook, output_typ


@patch("workflows.search_by_property.to_webhook")
def test_search_by_property_migrate_webhook(mock_openstackquery):
@patch("workflows.search_by_property.openstackquery")
def test_search_by_property_migrate_webhook(mock_openstackquery, mock_to_webhook):
"""
Runs search_by_property with webhook as migrate-server
Tests search_by_property with webhook as migrate-server adds params to the query needed for the migrate action
"""

mock_query = MagicMock()
mock_cloud_account = NonCallableMock()

mock_output_type = "to_string"
mock_openstackquery.MockQuery.return_value = mock_query
params = {
"cloud_account": mock_cloud_account,
"query_type": "MockQuery",
"search_mode": NonCallableMock(),
"property_to_search_by": NonCallableMock(),
"output_type": "test",
"output_type": mock_output_type,
"properties_to_select": ["prop1", "prop2"],
"values": ["val1", "val2"],
"group_by": NonCallableMock(),
Expand All @@ -148,3 +149,6 @@ def test_search_by_property_migrate_webhook(mock_openstackquery):
mock_query.select.assert_called_once_with(
*params["properties_to_select"], "server_id", "server_status"
)
mock_to_webhook.assert_called_once_with(
webhook="migrate-server", payload=mock_query.to_props.return_value
)

0 comments on commit cdebf99

Please sign in to comment.