Skip to content

Commit

Permalink
test(support-bundle): agent with node selector
Browse files Browse the repository at this point in the history
Ref: 5614

Signed-off-by: Chin-Ya Huang <chin-ya.huang@suse.com>
  • Loading branch information
c3y1huang committed Mar 22, 2023
1 parent 1e5d645 commit 16fa062
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 5 deletions.
19 changes: 14 additions & 5 deletions manager/integration/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
SETTING_SNAPSHOT_FAST_REPLICA_REBUILD_ENABLED = "fast-replica-rebuild-enabled"
SETTING_CONCURRENT_VOLUME_BACKUP_RESTORE = \
"concurrent-volume-backup-restore-per-node-limit"
SETTING_NODE_SELECTOR = "system-managed-components-node-selector"

SNAPSHOT_DATA_INTEGRITY_IGNORED = "ignored"
SNAPSHOT_DATA_INTEGRITY_DISABLED = "disabled"
Expand Down Expand Up @@ -3008,19 +3009,23 @@ def wait_longhorn_node_zone_reset(client):
assert lh_node.zone == ''


def set_k8s_node_zone_label(core_api, node_name, zone_name):
k8s_zone_label = get_k8s_zone_label()

def set_k8s_node_label(core_api, node_name, key, value):
payload = {
"metadata": {
"labels": {
k8s_zone_label: zone_name}
key: value}
}
}

core_api.patch_node(node_name, body=payload)


def set_k8s_node_zone_label(core_api, node_name, zone_name):
k8s_zone_label = get_k8s_zone_label()

set_k8s_node_label(core_api, node_name, k8s_zone_label, zone_name)


def get_k8s_zone_label():
ver_api = get_version_api_client()
k8s_ver_data = ver_api.get_code()
Expand Down Expand Up @@ -5188,13 +5193,17 @@ def delete_support_bundle(node_id, name, client):
return requests.delete(support_bundle_url)


def download_support_bundle(node_id, name, client): # NOQA
def download_support_bundle(node_id, name, client, target_path=""): # NOQA
url = get_support_bundle_url(client)
support_bundle_url = '{}/{}/{}'.format(url, node_id, name)
download_url = '{}/download'.format(support_bundle_url)
r = requests.get(download_url, allow_redirects=True, timeout=300)
r.raise_for_status()

if target_path != "":
with open(target_path, 'wb') as f:
f.write(r.content)


def get_all_support_bundle_manager_deployments(apps_api): # NOQA
name_prefix = 'longhorn-support-bundle-manager'
Expand Down
75 changes: 75 additions & 0 deletions manager/integration/tests/test_support_bundle.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import pytest
import time
import os
import zipfile

from tempfile import TemporaryDirectory

from common import apps_api # NOQA
from common import client # NOQA
from common import core_api # NOQA

from common import check_all_support_bundle_managers_deleted
from common import create_support_bundle
from common import delete_and_wait_deployment
from common import download_support_bundle
from common import get_all_support_bundle_manager_deployments
from common import set_k8s_node_label
from common import update_setting
from common import wait_for_support_bundle_cleanup
from common import wait_for_support_bundle_state

from common import SETTING_NODE_SELECTOR
from common import SETTING_SUPPORT_BUNDLE_FAILED_LIMIT


Expand Down Expand Up @@ -141,3 +149,70 @@ def create_failed_support_bundles(client, apps_api, number=1): # NOQA
namespace=deployments[0].metadata.namespace
)
wait_for_support_bundle_state("Error", node_id, name, client)


@pytest.mark.support_bundle # NOQA
def test_support_bundle_agent_with_node_selector(client, core_api, request): # NOQA
"""
Scenario: support bundle agent should respect node selector
Issue: https://github.com/longhorn/longhorn/issues/5614
Given there are some nodes labeled
And "system-managed-components-node-selector" is set with node label
When a support bundle is generated
Then should be able to download the support bundle successfully
And support bundle should include only the labeled nodes in node collection
"""
nodes = client.list_node()
labeled_nodes = [nodes[1], nodes[2]]
for node in labeled_nodes:
set_k8s_node_label(core_api, node.name, "foo", "bar")
def finalizer():
for node in labeled_nodes:
set_k8s_node_label(core_api, node.name, "foo", None)
update_setting(client, SETTING_NODE_SELECTOR, None)
request.addfinalizer(finalizer)

update_setting(client, SETTING_NODE_SELECTOR, f'foo:bar')

resp = create_support_bundle(client)
node_id = resp['id']
name = resp['name']

wait_for_support_bundle_state("ReadyForDownload", node_id, name, client)

# The temporary directory will be automatically deleted outside of the
# "with" context manager.
with TemporaryDirectory(prefix="supportbundle-") as temp_dir:
download_path = f'{temp_dir}/{0}.zip'.format(name)
download_support_bundle(node_id, name, client,
target_path=download_path)

with zipfile.ZipFile(download_path, 'r') as zip:
node_names = [f"{node.name}" for node in labeled_nodes]
check_bundled_nodes_matches(node_names, zip, temp_dir)

wait_for_support_bundle_cleanup(client)
check_all_support_bundle_managers_deleted()


def check_bundled_nodes_matches(node_names, zip, temp_dir):
expect_node_zips = [f"{node}.zip" for node in node_names]
bundle_name = os.path.dirname(zip.namelist()[0])
bundle_node_dir = f'{bundle_name}/nodes'
bundle_nodes = [
f for f in zip.namelist() if f.startswith(bundle_node_dir)
]

for node in bundle_nodes:
zip.extract(node, f'{temp_dir}')

node_zips = os.listdir(f'{temp_dir}/{bundle_name}/nodes')
assert set(node_zips) == set(expect_node_zips), \
f'Nodes zipped in bundle do not match. \n' \
f'Expect = {expect_node_zips}\n' \
f'Got = {node_zips}\n'

0 comments on commit 16fa062

Please sign in to comment.