Skip to content

Commit

Permalink
Fix tests in main (#47)
Browse files Browse the repository at this point in the history
## Proposal
Currently, tests are failing in `main` and we can't release anything.
This PR fixes that.

## Changelog
- Select correct host for getting cluster health
- Added correct status override
- Add libid for client relation libs (opensearch_users.py and
opensearch_relation_provider.py)
  - These values are necessary for check-lib CI actions.
- Skipped block check in relation-broken check
- This is the result of an unrelated non-breaking bug that will be
resolved in a future PR.
- ticket here:
[DPE-1417](https://warthogs.atlassian.net/browse/DPE-1417?atlOrigin=eyJpIjoiYjc4ZDQxN2FmYTZkNDQ5MmIwZDE1ZTg3ZmNkZTUwZTciLCJwIjoiaiJ9)


[DPE-1417]:
https://warthogs.atlassian.net/browse/DPE-1417?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
  • Loading branch information
WRFitch authored Mar 2, 2023
1 parent d225339 commit 21c83d2
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/charms/opensearch/v0/helper_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def node_with_new_roles(remaining_nodes: List[Node]) -> Optional[Node]:
return Node(data.name, data.roles + ["cluster_manager"], data.ip)

@staticmethod
def max_cluster_manager_nodes(planned_units) -> (int, int):
def max_cluster_manager_nodes(planned_units) -> int:
"""Get the max number of CM nodes in a cluster."""
max_managers = planned_units
if planned_units % 2 == 0:
Expand Down
21 changes: 17 additions & 4 deletions lib/charms/opensearch/v0/opensearch_base_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def _on_opensearch_data_storage_detaching(self, _: StorageDetachingEvent):
if not self.alt_hosts:
raise OpenSearchHAError(NoNodeUpInCluster)

health = self._apply_cluster_health(wait_for_green_first=True)
health = self._apply_cluster_health(wait_for_green_first=True, use_localhost=False)
if health == "red":
raise OpenSearchHAError(ClusterHealthRed)
finally:
Expand Down Expand Up @@ -773,18 +773,30 @@ def _compute_and_broadcast_updated_topology(self, current_nodes: Optional[List[N
updated_nodes = ClusterTopology.recompute_nodes_conf(current_nodes)
self.peers_data.put_object(Scope.APP, "nodes_config", updated_nodes)

def _apply_cluster_health(self, wait_for_green_first: bool = False) -> str:
def _apply_cluster_health(
self, wait_for_green_first: bool = False, use_localhost: bool = True
) -> str:
"""Fetch cluster health and set it on the app status."""
response: Optional[Dict[str, any]] = None
if wait_for_green_first:
try:
response = ClusterState.health(self.opensearch, True, alt_hosts=self.alt_hosts)
response = ClusterState.health(
self.opensearch,
True,
host=self.unit_ip if use_localhost else None,
alt_hosts=self.alt_hosts,
)
except OpenSearchHttpError:
# it timed out, settle with current status, fetched next without the 1min wait
pass

if not response:
response = ClusterState.health(self.opensearch, False, alt_hosts=self.alt_hosts)
response = ClusterState.health(
self.opensearch,
False,
host=self.unit_ip if use_localhost else None,
alt_hosts=self.alt_hosts,
)

status = response["status"].lower()
if not self.unit.is_leader():
Expand All @@ -797,6 +809,7 @@ def _apply_cluster_health(self, wait_for_green_first: bool = False) -> str:
if status == "green":
self.status.clear(ClusterHealthRed, app=True)
self.status.clear(ClusterHealthYellow, app=True)
self.status.clear(WaitingForBusyShards, app=True)
return "green"

# some primary shards are unassigned
Expand Down
5 changes: 5 additions & 0 deletions lib/charms/opensearch/v0/opensearch_distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ def request( # noqa
check_hosts_reach: if true, performs a ping for each host
resp_status_code: whether to only return the HTTP code from the response.
retries: number of retries
Raises:
ValueError if method or endpoint are missing
OpenSearchHttpError if hosts are unreachable
requests.HTTPError if connection to opensearch fails
"""

def full_urls() -> List[str]:
Expand Down
22 changes: 13 additions & 9 deletions lib/charms/opensearch/v0/opensearch_relation_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@
& index security policies, and therefore differentiating between types of network endpoints is
unnecessary.
When specifying user permissions for this relation, a client application must send this charm valid
JSON containing the following fields:
{
"roles": ["A list of default opensearch roles to apply"],
"permissions": ["A list of default opensearch permissions to apply"],
"action_groups": ["A list of default opensearch action groups to apply"],
}
A role will be created for the relation with the permissions and action groups applied, and these
roles will be mapped to a dedicated user for the relation, which will be removed with the relation.
Default security values can be found in the opensearch documentation here:
Expand Down Expand Up @@ -49,6 +40,16 @@

logger = logging.getLogger(__name__)

# The unique Charmhub library identifier, never change it
LIBID = "c0f1d8f94bdd41a781fe2871e1922480"

# Increment this major API version when introducing breaking changes
LIBAPI = 0

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 1


class OpenSearchProvider(Object):
"""Defines functionality for the 'provides' side of the 'opensearch-client' relation.
Expand Down Expand Up @@ -143,6 +144,9 @@ def create_opensearch_users(
access_control: A dict of roles, permissions, and action groups to be applied to the
user. A new role will be created to contain the requested permissions and action
groups.
Raises:
OpenSearchUserMgmtError if user creation fails
"""
roles = access_control.get("roles")
permissions = access_control.get("permissions")
Expand Down
11 changes: 11 additions & 0 deletions lib/charms/opensearch/v0/opensearch_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@
logger = logging.getLogger(__name__)


# The unique Charmhub library identifier, never change it
LIBID = "f9da4353bd314b86acfdfa444a9517c9"

# Increment this major API version when introducing breaking changes
LIBAPI = 0

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 1


USER_ENDPOINT = "/_plugins/_security/api/internalusers"
ROLE_ENDPOINT = "/_plugins/_security/api/roles"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ async def test_database_relation_with_charm_libraries(

async with ops_test.fast_forward():
# This test shouldn't take so long
await ops_test.model.wait_for_idle(timeout=1200, status="active")
await ops_test.model.wait_for_idle(apps=ALL_APPS, timeout=1200, status="active")

# await ops_test.model.block_until(
# lambda: ops_test.model.applications[OPENSEARCH_APP_NAME].status == "active", timeout=1000
# )


@pytest.mark.client_relation
Expand Down Expand Up @@ -207,6 +211,7 @@ async def test_relation_broken(ops_test: OpsTest):
f"{OPENSEARCH_APP_NAME}:{ClientRelationName}",
f"{CLIENT_APP_NAME}:{FIRST_DATABASE_RELATION_NAME}",
)

async with ops_test.fast_forward():
await asyncio.gather(
ops_test.model.wait_for_idle(
Expand All @@ -216,9 +221,9 @@ async def test_relation_broken(ops_test: OpsTest):
ops_test.model.wait_for_idle(
apps=[OPENSEARCH_APP_NAME, TLS_CERTIFICATES_APP_NAME, SECONDARY_CLIENT_APP_NAME],
status="active",
raise_on_blocked=True,
),
)

leader_ip = await get_leader_unit_ip(ops_test)
users = await http_request(
ops_test,
Expand Down

0 comments on commit 21c83d2

Please sign in to comment.