diff --git a/test/basic-test/deploy b/test/basic-test/deploy index a2447e770..3aee12a3c 100755 --- a/test/basic-test/deploy +++ b/test/basic-test/deploy @@ -6,17 +6,8 @@ from drenv import test test.start("deploy", __file__) -test.add_argument( - "--cluster", - help="Cluster name to deploy on (default first cluster)", -) args = test.parse_args() -cluster = args.cluster or test.env["clusters"][0] -test.info("Deploying on cluster '%s'", cluster) - -test.deploy(cluster) -test.wait_for_drpc_status() -test.wait_until_drpc_is_stable() - -test.info("Application was deployed successfully") +test.info("Deploying application") +test.deploy() +test.info("Application running on cluster '%s'", test.lookup_cluster()) diff --git a/test/basic-test/disable-dr b/test/basic-test/disable-dr new file mode 100755 index 000000000..4548856c6 --- /dev/null +++ b/test/basic-test/disable-dr @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +# SPDX-FileCopyrightText: The RamenDR authors +# SPDX-License-Identifier: Apache-2.0 + +from drenv import test + +test.start("disable-dr", __file__) +args = test.parse_args() + +test.info("Disable DR") +test.disable_dr() +test.info("DR was disabled") diff --git a/test/basic-test/enable-dr b/test/basic-test/enable-dr new file mode 100755 index 000000000..629280faf --- /dev/null +++ b/test/basic-test/enable-dr @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 + +# SPDX-FileCopyrightText: The RamenDR authors +# SPDX-License-Identifier: Apache-2.0 + +from drenv import test + +test.start("enable-dr", __file__) +args = test.parse_args() + +test.info("Enable DR") +test.enable_dr() +test.wait_for_drpc_status() +test.wait_until_drpc_is_stable() +test.info("DR enabled") diff --git a/test/basic-test/run b/test/basic-test/run index e410e28ad..31a1a7256 100755 --- a/test/basic-test/run +++ b/test/basic-test/run @@ -6,6 +6,8 @@ base="$(dirname $0)" "$base/deploy" "$@" +"$base/enable-dr" "$@" "$base/failover" "$@" "$base/relocate" "$@" +"$base/disable-dr" "$@" "$base/undeploy" "$@" diff --git a/test/drenv/test.py b/test/drenv/test.py index b326e006b..2d3a4dafa 100644 --- a/test/drenv/test.py +++ b/test/drenv/test.py @@ -5,7 +5,6 @@ import json import logging import os -import string import sys import yaml @@ -93,34 +92,116 @@ def _excepthook(t, v, tb): log.exception("test failed", exc_info=(t, v, tb)) -def deploy(cluster): +def deploy(): """ Deploy application on cluster. """ + info("Deploying channel") + kubectl.apply( + f"--kustomize={config['repo']}/channel?ref={config['branch']}", + context=env["hub"], + log=debug, + ) info("Deploying subscription based application") - yaml = _kustomization(cluster) - with drenv.kustomization_yaml(yaml) as tmpdir: - kubectl.apply( - f"--kustomize={tmpdir}", - context=env["hub"], - log=debug, - ) + kubectl.apply( + f"--kustomize={config['repo']}/subscription?ref={config['branch']}", + context=env["hub"], + log=debug, + ) def undeploy(): """ Undeploy an application. """ + info("Undeploying channel") + kubectl.delete( + f"--kustomize={config['repo']}/channel?ref={config['branch']}", + "--ignore-not-found", + context=env["hub"], + log=debug, + ) info("Undeploying subscription based application") - yaml = _kustomization("unused") - with drenv.kustomization_yaml(yaml) as tmpdir: - kubectl.delete( - "--ignore-not-found", - f"--kustomize={tmpdir}", - context=env["hub"], - log=debug, + kubectl.delete( + f"--kustomize={config['repo']}/subscription?ref={config['branch']}", + "--ignore-not-found", + context=env["hub"], + log=debug, + ) + + +def enable_dr(): + """ + Enable DR for deployed application. + """ + cluster = lookup_cluster() + + # TODO: support placement + info("Setting placementrule scheduler to ramen") + _patch_placementrule({"spec": {"schedulerName": "ramen"}}) + + drpc = f""" +apiVersion: ramendr.openshift.io/v1alpha1 +kind: DRPlacementControl +metadata: + name: busybox-drpc + namespace: {config['namespace']} + labels: + app: {config['name']} +spec: + preferredCluster: {cluster} + drPolicyRef: + name: dr-policy + placementRef: + kind: PlacementRule + name: busybox-placement + pvcSelector: + matchLabels: + appname: busybox +""" + kubectl.apply("--filename=-", input=drpc, context=env["hub"], log=debug) + + +def disable_dr(): + """ + Disable DR for deployed application. + """ + drpc = _lookup_app_resource("drpc") + if not drpc: + debug("drpc already removed") + return + + info("Deleting '%s'", drpc) + kubectl.delete( + drpc, + "--ignore-not-found", + f"--namespace={config['namespace']}", + context=env["hub"], + log=debug, + ) + + # TODO: support placement + info("Clearing placementrule scheduler") + _patch_placementrule({"spec": {"schedulerName": None}}) + + +def _patch_placementrule(patch): + placementrule = _lookup_app_resource("placementrule") + if not placementrule: + raise RuntimeError( + f"Cannot find placementrule for application {config['name']}" ) + kubectl.patch( + placementrule, + "--patch", + json.dumps(patch), + "--type=merge", + f"--namespace={config['namespace']}", + context=env["hub"], + log=debug, + ) + def target_cluster(): """ @@ -323,25 +404,3 @@ def _lookup_app_resource(kind): context=env["hub"], ) return out.rstrip() - - -def _kustomization(cluster): - yaml = """ -resources: - - ${repo}/subscriptions?ref=${branch} - - ${repo}/subscriptions/busybox?ref=${branch} -patches: - - target: - kind: DRPlacementControl - name: busybox-drpc - patch: |- - - op: replace - path: /spec/preferredCluster - value: ${cluster} -""" - template = string.Template(yaml) - return template.substitute( - repo=config["repo"], - branch=config["branch"], - cluster=cluster, - )