Skip to content

Commit

Permalink
Test enable and disable DR
Browse files Browse the repository at this point in the history
Change the way we to deploy to match how we deploy in OpenShift: deploy
the channel and the subscription, and then assign drpolicy.

With this change we test now enable and disable DR. The basic test test
only disable DR after relocate. We will add more tests later to cover
more scenarios.

There are 2 new trivial scripts for enabling and disabling DR, that can
be used to test various flows manually like:

    env=$PWD/test/envs/regional-dr.yaml
    cd test/basic-test

    ./deploy $env
    ./enable-dr $env
    ./failover $env
    ./disable-dr $env
    ./enable-dr $env
    ./relocate $env

Deploying and undeploying is much simpler now, using the kustomization
from the samples repo directly. Deploying the drpc was simplified to use
a yaml template instead of kustomization template.

Because we don't have any rule in the sample placementrule, we don't
control now the deployment cluster, and the --cluster argument was
removed.

This change should be merged together with this ocm-ramen-samrples PR:
RamenDR/ocm-ramen-samples#35

Signed-off-by: Nir Soffer <nsoffer@redhat.com>
  • Loading branch information
nirs committed Oct 4, 2023
1 parent eea17f3 commit 9316419
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 50 deletions.
15 changes: 3 additions & 12 deletions test/basic-test/deploy
Original file line number Diff line number Diff line change
Expand Up @@ -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())
13 changes: 13 additions & 0 deletions test/basic-test/disable-dr
Original file line number Diff line number Diff line change
@@ -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")
15 changes: 15 additions & 0 deletions test/basic-test/enable-dr
Original file line number Diff line number Diff line change
@@ -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")
2 changes: 2 additions & 0 deletions test/basic-test/run
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
base="$(dirname $0)"

"$base/deploy" "$@"
"$base/enable-dr" "$@"
"$base/failover" "$@"
"$base/relocate" "$@"
"$base/disable-dr" "$@"
"$base/undeploy" "$@"
135 changes: 97 additions & 38 deletions test/drenv/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import json
import logging
import os
import string
import sys

import yaml
Expand Down Expand Up @@ -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():
"""
Expand Down Expand Up @@ -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,
)

0 comments on commit 9316419

Please sign in to comment.