Skip to content

Commit

Permalink
add docstring, use pathlib for tags.yaml, and use contextlib.ExitStack
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangqq-coder committed Oct 14, 2024
1 parent 8487c6e commit d86e271
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
3 changes: 3 additions & 0 deletions integration-tests/constants.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
import pathlib

HOST_DETAILS: str = "/var/lib/insights/host-details.json"
MACHINE_ID_FILE: str = "/etc/insights-client/machine-id"
TAGS_FILE = pathlib.Path("/etc/insights-client/tags.yaml")
74 changes: 45 additions & 29 deletions integration-tests/test_tags.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
"""
:casecomponent: insights-client
:requirement: RHSS-291297
:subsystemteam: sst_csi_client_tools
:caseautomation: Automated
:upstream: Yes
"""

import pytest
import conftest
import contextlib
import os
import yaml
from constants import TAGS_FILE

pytestmark = pytest.mark.usefixtures("register_subman")


def test_tags(insights_client, external_inventory, test_config):
"""
:id: 3e9d5b76-7065-4ade-8397-5854a8fef83b
:title: Test tags
:description:
Test how the tags generated, and check the tags on inventory
:reference:
:tier: Tier 1
steps:
1. Register insights-client, and check satellite related
tags on the inventory if the test env is satellte.
Expand All @@ -29,7 +44,7 @@ def test_tags(insights_client, external_inventory, test_config):
"""
# Remove the tags.yaml if it exists
with contextlib.suppress(FileNotFoundError):
os.remove("/etc/insights-client/tags.yaml")
TAGS_FILE.unlink()

# Register insights
insights_client.register()
Expand All @@ -42,34 +57,35 @@ def test_tags(insights_client, external_inventory, test_config):
system_tags = external_inventory.this_system_tags()
for tag in system_tags:
assert tag["namespace"] == "satellite"
assert not os.path.exists("/etc/insights-client/tags.yaml")

# Run insights-client --group
insights_client.run("--group=first_tag")
assert os.path.exists("/etc/insights-client/tags.yaml")
assert not TAGS_FILE.exists()

with open("/etc/insights-client/tags.yaml", "r") as tags_yaml:
data_loaded = yaml.safe_load(tags_yaml)
assert data_loaded["group"] == "first_tag"
with contextlib.ExitStack() as stack:
# Run insights-client --group
insights_client.run("--group=first_tag")
stack.callback(os.remove, TAGS_FILE)
assert TAGS_FILE.exists()
with TAGS_FILE.open("r") as tags_yaml:
data_loaded = yaml.safe_load(tags_yaml)
assert data_loaded["group"] == "first_tag"

# Check new tag from inventory
system_tags = external_inventory.this_system_tags()
assert {
"namespace": "insights-client",
"key": "group",
"value": "first_tag",
} in system_tags
# Check new tag from inventory
system_tags = external_inventory.this_system_tags()
assert {
"namespace": "insights-client",
"key": "group",
"value": "first_tag",
} in system_tags

# Add new tag in tags.yaml file and check on inventory
with open("/etc/insights-client/tags.yaml", "r") as tags_yaml:
data_loaded = yaml.safe_load(tags_yaml)
data_loaded["add_by_file"] = "second_tag"
with open("/etc/insights-client/tags.yaml", "w") as tags_yaml:
yaml.dump(data_loaded, tags_yaml, default_flow_style=False)
insights_client.run()
system_tags = external_inventory.this_system_tags()
assert {
"namespace": "insights-client",
"key": "add_by_file",
"value": "second_tag",
} in system_tags
# Add new tag in tags.yaml file and check on inventory
with TAGS_FILE.open("r") as tags_yaml:
data_loaded = yaml.safe_load(tags_yaml)
data_loaded["add_by_file"] = "second_tag"
with TAGS_FILE.open("w") as tags_yaml:
yaml.dump(data_loaded, tags_yaml, default_flow_style=False)
insights_client.run()
system_tags = external_inventory.this_system_tags()
assert {
"namespace": "insights-client",
"key": "add_by_file",
"value": "second_tag",
} in system_tags

0 comments on commit d86e271

Please sign in to comment.