import logging

import pytest
import sdk_cmd
import sdk_install
import sdk_marathon
import sdk_plan
import sdk_utils
from tests import config

log = logging.getLogger(__name__)

POD_NAMES = ['hello-0', 'world-0', 'world-1']
LOCAL_REGION = 'USA'
REMOTE_REGION = 'Europe'


@pytest.fixture
def local_service():
    try:
        sdk_install.install(
            config.PACKAGE_NAME,
            config.SERVICE_NAME,
            3,
            additional_options={
                "service": {
                    "scenario": "MULTI_REGION",
                    "allow_region_awareness": True
                }
            })

        yield
    finally:
        sdk_install.uninstall(config.PACKAGE_NAME, config.SERVICE_NAME)


@pytest.fixture
def remote_service():
    try:
        sdk_install.install(
            config.PACKAGE_NAME,
            config.SERVICE_NAME,
            3,
            additional_options={
                "service": {
                    "scenario": "MULTI_REGION",
                    "allow_region_awareness": True,
                    "region": REMOTE_REGION
                }
            })

        yield
    finally:
        sdk_install.uninstall(config.PACKAGE_NAME, config.SERVICE_NAME)


@pytest.mark.dcos_min_version('1.11')
@pytest.mark.region_awareness
@sdk_utils.dcos_ee_only
def test_nodes_deploy_to_local_region_by_default(local_service):
    for pod_name in POD_NAMES:
        pod_region = get_pod_region(config.SERVICE_NAME, pod_name)

        assert pod_region == LOCAL_REGION


@pytest.mark.dcos_min_version('1.11')
@pytest.mark.region_awareness
@sdk_utils.dcos_ee_only
def test_nodes_can_deploy_to_remote_region(remote_service):
    for pod_name in POD_NAMES:
        pod_region = get_pod_region(config.SERVICE_NAME, pod_name)

        assert pod_region == REMOTE_REGION


@pytest.mark.dcos_min_version('1.11')
@pytest.mark.region_awareness
@sdk_utils.dcos_ee_only
def test_region_config_update_does_not_succeed(local_service):
    sdk_plan.wait_for_completed_deployment(config.SERVICE_NAME)
    change_region_config(REMOTE_REGION)
    plan = sdk_plan.get_deployment_plan(config.SERVICE_NAME)

    assert plan.get('errors', [])


def change_region_config(region_name):
    service_config = sdk_marathon.get_config(config.SERVICE_NAME)
    service_config['env']['SERVICE_REGION'] = region_name
    sdk_marathon.update_app(config.SERVICE_NAME, service_config, wait_for_completed_deployment=False)


def get_pod_region(service_name, pod_name):
    info = sdk_cmd.service_request(
        'GET', service_name, '/v1/pod/{}/info'.format(pod_name)
    ).json()[0]['info']

    return [l['value'] for l in info['labels']['labels'] if l['key'] == 'offer_region'][0]

@@ -16,22 +16,52 @@
 %0Aimport 
-pytest
+dcos%0Aimport pytest%0Aimport shakedown%0A
 %0Aimport 
@@ -123,16 +123,33 @@
 dk_plan%0A
+import sdk_tasks%0A
 import s
@@ -181,16 +181,62 @@
 t config
+%0Afrom tests.conftest import configure_universe
 %0A%0Alog = 
@@ -1449,16 +1449,36 @@
 default(
+configure_universe, 
 local_se
@@ -1763,16 +1763,36 @@
 _region(
+configure_universe, 
 remote_s
@@ -2087,151 +2087,242 @@
 eed(
-local_service):%0A    sdk_plan.wait_for_completed_deployment(config.SERVICE_NAME)%0A    change_region_config(REMOTE_REGION)%0A    plan = sdk_plan.get
+configure_universe, local_service):%0A    change_region_config(REMOTE_REGION)%0A    sdk_plan.wait_for_plan_status(config.SERVICE_NAME, 'deploy', 'ERROR', timeout_seconds=180)%0A%0A    change_region_config(None)%0A    sdk_plan.wait_for_completed
 _dep
@@ -2328,21 +2328,16 @@
 ployment
-_plan
 (config.
@@ -2352,43 +2352,29 @@
 NAME
-)%0A%0A    assert plan.get('errors', %5B%5D
+, timeout_seconds=180
 )%0A%0A%0A
@@ -2478,16 +2478,110 @@
 E_NAME)%0A
+    if region_name is None:%0A        del service_config%5B'env'%5D%5B'SERVICE_REGION'%5D%0A    else:%0A    
     serv
@@ -2630,16 +2630,17 @@
 on_name%0A
+%0A
     sdk_

Check that scheduler has relaunched before getting deploy plan (#2416)