#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: Test policy inheritance in UMC policy
## exposure: careful
## tags: [udm]
## bugs:
##  - 38663
##  - 38712

# shellcheck source=../../lib/base.sh
. "$TESTLIBPATH/base.sh" || exit 137
. udm.sh || exit 137

setup () {
	udm-test dhcp/service create \
		--position "$BASE" \
		--set service="$name"
	udm-test dhcp/subnet create \
		--superordinate "cn=$name,$BASE" \
		--set subnet="$NET" \
		--set subnetmask='255.255.255.0'
	udm-test dhcp/pool create \
		--superordinate "cn=$NET,cn=$name,$BASE" \
		--set name="pool" \
		--set range="${NET%.0}.2 ${NET%.0}.253"

	udm-test policies/dhcp_routing create \
		--position "cn=$name,$BASE" \
		--set name="p1" \
		--set routers="$router" \
		--set fixedAttributes='univentionDhcpRouters'
	udm-test dhcp/service modify \
		--dn "cn=$name,$BASE" \
		--policy-reference "cn=p1,cn=$name,$BASE"
	udm-test policies/dhcp_routing create \
		--position "cn=$name,$BASE" \
		--set name="p2" \
		--set routers="${NET%.0}.254"
	udm-test dhcp/pool modify \
		--dn "cn=pool,cn=$NET,cn=$name,$BASE" \
		--policy-reference "cn=p2,cn=$name,$BASE"
}

check () {
	wait_for_replication
	policy "cn=pool,cn=$NET,cn=$name,$BASE" -s >"$tmp/pool.sh"
	grep -Fx "univentionDhcpRouters=\"$router\"" "$tmp/pool.sh" ||
		die "Policy not applied to DHCP-Pool"
	pypolicy "cn=test,cn=$NET,cn=$name,$BASE" \
		"{'univentionDhcpRouters': {'policy': 'cn=p1,cn=$name,$BASE', 'fixed': 1, 'value': ['$router']}}" ||
		die "Policy not applied to DHCP-Pool"
}

setup
check
:
