Skip to content

Commit

Permalink
Test OCI Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dudyas6 committed Jan 14, 2025
1 parent 8edb97e commit 2399510
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 11 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ deploy_nodes_with_install: start_load_balancer
deploy_nodes: start_load_balancer
TEST_TEARDOWN=no TEST=./src/tests/test_targets.py TEST_FUNC=test_target_deploy_nodes $(MAKE) test

deploy_nodes_oci: start_load_balancer
TEST_TEARDOWN=no TEST=./src/tests/test_targets.py TEST_FUNC=test_target_deploy_nodes_oci $(MAKE) test

destroy_nodes_oci: start_load_balancer
TEST_TEARDOWN=no TEST=./src/tests/test_targets.py TEST_FUNC=test_target_destroy_nodes_oci $(MAKE) test

deploy_nodes_with_networking: start_load_balancer
TEST_TEARDOWN=no TEST=./src/tests/test_targets.py TEST_FUNC=test_target_deploy_networking_with_nodes $(MAKE) test

Expand Down Expand Up @@ -427,9 +433,3 @@ test_ctlplane_scaleup:

install_k8s_api:
TEST_TEARDOWN=false TEST=./src/tests/test_kube_api.py TEST_FUNC=test_kubeapi $(MAKE) test

destroy_crs:
oc delete cd --all-namespaces --all
oc delete aci --all-namespaces --all
oc delete infraenv --all-namespaces --all
oc delete agents --all-namespaces --all
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
import copy
import pdb
import os
import random
import string
Expand Down Expand Up @@ -151,7 +152,7 @@ def _create_bucket(
return namespace

def _upload_data_to_bucket(self, data: str, filename: str, namespace: str, bucket_name: str):
file_path = f"/tmp/{filename}"
file_path = f"/tmp/oci-{self._entity_config.cluster_id}/{filename}"

with os.path.open(file_path, "w") as f:
f.write(data)
Expand Down Expand Up @@ -312,7 +313,7 @@ def _apply_job_from_stack(
job_info = {
"stack_id": stack_id,
"display_name": display_name,
"operation": "APPLY",
"operation": "DESTROY",
"apply_job_plan_resolution": oci.resource_manager.models.ApplyJobPlanResolution(
is_use_latest_job_id=False, is_auto_approved=True
),
Expand Down Expand Up @@ -440,10 +441,66 @@ def get_ingress_and_api_vips(self) -> dict:

def destroy_all_nodes(self) -> None:
log.info("OCI Destroying all nodes")
pdb.set_trace()
if not self._cleanup_resources:
self._generate_cleanup_resources()
for resource in self._cleanup_resources[::-1]:
resource()
pass

def _generate_cleanup_resources(self, timeout_seconds: int = 1200) -> None:
namespace = self._object_storage_client.get_namespace().data
stack_name = f"stack-{self._entity_config.cluster_id}"
bucket_name = f"bucket-{self._entity_config.cluster_id}"
pre_auths = self._object_storage_client.list_preauthenticated_requests(namespace,bucket_name).data
objects = self._object_storage_client.list_objects(namespace,bucket_name).data.objects

# Find relevant stack_id and create destroy job for it
stacks = self._resource_manager_client.list_stacks(compartment_id=self._oci_compartment_oicd,display_name=stack_name).data
stack_id = stacks[0].id if stacks else None
job_info = {
"stack_id": stack_id,
"display_name": f"destroy-job-{self._entity_config.cluster_id}",
"operation": "DESTROY",
"apply_job_plan_resolution": oci.resource_manager.models.ApplyJobPlanResolution(
is_use_latest_job_id=False, is_auto_approved=True
),
}
destroy_job_details = oci.resource_manager.models.CreateJobDetails(**job_info)

# Add all relevant jobs for execution in reversed order
self._cleanup_resources.append(
CleanupResource(
self._resource_manager_client_composite_operations.delete_stack_and_wait_for_state, stack_id
)
)

self._cleanup_resources.append(
CleanupResource(self._object_storage_client.delete_bucket, namespace, bucket_name))

for pre_auth in pre_auths:
self._cleanup_resources.append(
CleanupResource(
self._object_storage_client.delete_preauthenticated_request, namespace, bucket_name, pre_auth.id
)
)

for obj in objects:
self._cleanup_resources.append(
CleanupResource(
self._object_storage_client.delete_object, namespace, bucket_name, obj.name
)
)

self._cleanup_resources.append(
CleanupResource(
self._resource_manager_client_composite_operations.create_job_and_wait_for_state,
destroy_job_details,
wait_for_states=[OciState.SUCCEEDED.value],
waiter_kwargs={"max_wait_seconds": timeout_seconds, "succeed_on_not_found": False},
)
)

def get_cluster_network(self) -> str:
pass

Expand All @@ -456,7 +513,7 @@ def prepare_nodes(self) -> None:
namespace = self._create_bucket(bucket_name)
self._upload_file_to_bucket(self._entity_config.iso_download_path, namespace, bucket_name)
url_path = self._create_pre_authenticated(
f"preauth-{self._entity_config.cluster_id}", self._entity_config.iso_download_path, namespace, bucket_name
f"preauth-{self._entity_config.cluster_id}", self._entity_config.iso_download_path, namespace, bucket_name
)

terraform_variables = self._terraform_variables(
Expand All @@ -474,7 +531,7 @@ def prepare_nodes(self) -> None:
self._config.oci_infrastructure_zip_file,
terraform_variables,
)
terraform_output = self._apply_job_from_stack(stack_id, random_name("job-"))
terraform_output = self._apply_job_from_stack(stack_id, f"apply-job-{self._entity_config.cluster_id}")
self._upload_data_to_bucket(
terraform_output, f"terraform-output-{self._entity_config.cluster_id}.yaml", namespace, bucket_name
)
Expand Down Expand Up @@ -559,3 +616,5 @@ def set_ipxe_url(self, network_name: str, ipxe_url: str):

def get_day2_static_network_data(self):
pass


11 changes: 10 additions & 1 deletion src/tests/test_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ class TestMakefileTargets(BaseTest):
def test_target_deploy_nodes(self, cluster):
cluster.prepare_nodes()

@JunitTestSuite()
def test_target_deploy_nodes_oci(self, cluster):
cluster.generate_and_download_infra_env()
cluster.nodes.prepare_nodes()

@JunitTestSuite()
def test_target_destroy_nodes_oci(self, cluster):
cluster.nodes.destroy_all_nodes()

@JunitTestSuite()
def test_target_deploy_networking_with_nodes(self, cluster):
cluster.prepare_for_installation()
Expand Down Expand Up @@ -58,7 +67,7 @@ def test_delete_clusters(self, api_client: InventoryClient, cluster_configuratio

@JunitTestSuite()
def test_destroy_available_terraform(
self, prepared_controller_configuration: BaseNodesConfig, cluster_configuration
self, prepared_controller_configuration: BaseNodesConfig, cluster_configuration
):
clusters_tf_folders = glob(f"{consts.TF_FOLDER}/*")
destroyed_clusters = 0
Expand Down

0 comments on commit 2399510

Please sign in to comment.