diff --git a/ccm/tests_ccm_cluster.sh b/ccm/tests_ccm_cluster.sh index 9fe5bb1..4902c4a 100755 --- a/ccm/tests_ccm_cluster.sh +++ b/ccm/tests_ccm_cluster.sh @@ -7,4 +7,5 @@ set -x # run tests CLI_TEST_MASTER_PROXY=true \ CLI_TEST_SSH_KEY_PATH=/dcos-cli/mesosphere-aws.key \ +echo "dcos_acs_token = \"$DCOS_ACS_TOKEN\"" >> /dcos-cli/cli/tests/data/dcos.toml && \ ./bin/start_tests.sh diff --git a/cli/tests/data/analytics/dcos_no_reporting.toml b/cli/tests/data/analytics/dcos_no_reporting.toml deleted file mode 100644 index c93fd88..0000000 --- a/cli/tests/data/analytics/dcos_no_reporting.toml +++ /dev/null @@ -1,3 +0,0 @@ -[core] -reporting = false -dcos_url = "http://dcos.snakeoil.mesosphere.com" diff --git a/cli/tests/data/analytics/dcos_reporting.toml b/cli/tests/data/analytics/dcos_reporting.toml deleted file mode 100644 index e5f9c8f..0000000 --- a/cli/tests/data/analytics/dcos_reporting.toml +++ /dev/null @@ -1,4 +0,0 @@ -[core] -dcos_acs_token = "foobar" -dcos_url = "https://dcos.snakeoil.mesosphere.com" -reporting = true diff --git a/cli/tests/data/analytics/dcos_reporting_with_url.toml b/cli/tests/data/analytics/dcos_reporting_with_url.toml deleted file mode 100644 index bf02038..0000000 --- a/cli/tests/data/analytics/dcos_reporting_with_url.toml +++ /dev/null @@ -1,3 +0,0 @@ -[core] -reporting = true -dcos_url = "http://dcos.snakeoil.mesosphere.com" diff --git a/cli/tests/data/auth/dcos_with_credentials.toml b/cli/tests/data/auth/dcos_with_credentials.toml deleted file mode 100644 index affd359..0000000 --- a/cli/tests/data/auth/dcos_with_credentials.toml +++ /dev/null @@ -1,2 +0,0 @@ -[core] -reporting = false diff --git a/cli/tests/data/auth/dcos_without_credentials.toml b/cli/tests/data/auth/dcos_without_credentials.toml deleted file mode 100644 index affd359..0000000 --- a/cli/tests/data/auth/dcos_without_credentials.toml +++ /dev/null @@ -1,2 +0,0 @@ -[core] -reporting = false diff --git a/cli/tests/data/config/missing_params_dcos.toml b/cli/tests/data/config/missing_params_dcos.toml deleted file mode 100644 index 95e2f57..0000000 --- a/cli/tests/data/config/missing_params_dcos.toml +++ /dev/null @@ -1,4 +0,0 @@ -[core] -reporting = false -[package] -cosmos_url = "http://localhost:7070" diff --git a/cli/tests/data/marathon/missing_marathon_params.toml b/cli/tests/data/marathon/missing_marathon_params.toml deleted file mode 100644 index 9b0f34f..0000000 --- a/cli/tests/data/marathon/missing_marathon_params.toml +++ /dev/null @@ -1,5 +0,0 @@ -[core] -reporting = false -[marathon] -[package] -cosmos_url = "http://localhost:7070" diff --git a/cli/tests/data/ssl/ssl.toml b/cli/tests/data/ssl/ssl.toml deleted file mode 100644 index 06f84fe..0000000 --- a/cli/tests/data/ssl/ssl.toml +++ /dev/null @@ -1,6 +0,0 @@ -[package] -cosmos_url = "http://localhost:7070" -[core] -timeout = 5 -dcos_url = "https://dcos.snakeoil.mesosphere.com" -reporting = false diff --git a/cli/tests/integrations/common.py b/cli/tests/integrations/common.py index 0497fd1..0332f46 100644 --- a/cli/tests/integrations/common.py +++ b/cli/tests/integrations/common.py @@ -397,6 +397,44 @@ def app(path, app_id, wait=True): watch_all_deployments() +@contextlib.contextmanager +def update_config(name, value, env=None): + """ Context manager for altering config for tests + + :param key: + :type key: str + :param value: + :type value: str + ;param env: env vars + :type env: dict + :rtype: None + """ + + returncode, stdout, _ = exec_command( + ['dcos', 'config', 'show', name], env) + + result = None + # config param already exists + if returncode == 0: + result = json.loads('"' + stdout.decode('utf-8').strip() + '"') + + # if we are setting a value + if value is not None: + config_set(name, value, env) + # only unset if the config param already exists + elif result is not None: + config_unset(name, env) + + try: + yield + finally: + # return config to previous state + if result is not None: + config_set(name, result, env) + else: + exec_command(['dcos', 'config', 'unset', name], env) + + @contextlib.contextmanager def package(package_name, deploy=False, args=[]): """Context manager that deploys an app on entrance, and removes it on diff --git a/cli/tests/integrations/test_auth.py b/cli/tests/integrations/test_auth.py index ae972d4..743932f 100644 --- a/cli/tests/integrations/test_auth.py +++ b/cli/tests/integrations/test_auth.py @@ -4,7 +4,7 @@ from dcos import constants import pytest -from .common import assert_command, config_set, exec_command +from .common import assert_command, exec_command, update_config @pytest.fixture @@ -31,23 +31,22 @@ def test_version(): def test_logout_no_token(env): - exec_command(['dcos', 'config', 'unset', 'core.dcos_acs_token'], env=env) - - returncode, _, stderr = exec_command( - ['dcos', 'config', 'show', 'core.dcos_acs_token'], env=env) - assert returncode == 1 - assert stderr == b"Property 'core.dcos_acs_token' doesn't exist\n" + with update_config("core.dcos_acs_token", None, env): + returncode, _, stderr = exec_command( + ['dcos', 'config', 'show', 'core.dcos_acs_token'], env=env) + assert returncode == 1 + assert stderr == b"Property 'core.dcos_acs_token' doesn't exist\n" def test_logout_with_token(env): - config_set('core.dcos_acs_token', "foobar", env=env) - stderr = b"[core.dcos_acs_token]: changed\n" - assert_command( - ['dcos', 'config', 'set', 'core.dcos_acs_token', 'faketoken'], - stderr=stderr, - env=env) + with update_config("core.dcos_acs_token", "foobar", env): + stderr = b"[core.dcos_acs_token]: changed\n" + assert_command( + ['dcos', 'config', 'set', 'core.dcos_acs_token', 'faketoken'], + stderr=stderr, + env=env) - stderr = b'Removed [core.dcos_acs_token]\n' - assert_command(['dcos', 'auth', 'logout'], - stderr=stderr, - env=env) + stderr = b'Removed [core.dcos_acs_token]\n' + assert_command(['dcos', 'auth', 'logout'], + stderr=stderr, + env=env) diff --git a/cli/tests/integrations/test_config.py b/cli/tests/integrations/test_config.py index bfca8f3..c943245 100644 --- a/cli/tests/integrations/test_config.py +++ b/cli/tests/integrations/test_config.py @@ -6,7 +6,8 @@ from dcos import constants import pytest -from .common import assert_command, config_set, config_unset, exec_command +from .common import (assert_command, config_set, config_unset, + exec_command, update_config) @pytest.fixture @@ -20,17 +21,6 @@ def env(): return r -@pytest.fixture -def missing_env(): - r = os.environ.copy() - r.update({ - constants.PATH_ENV: os.environ[constants.PATH_ENV], - constants.DCOS_CONFIG_ENV: - os.path.join("tests", "data", "config", "missing_params_dcos.toml") - }) - return r - - def test_help(): with open('tests/data/help/config.txt') as content: assert_command(['dcos', 'config', '--help'], @@ -80,13 +70,15 @@ def test_invalid_dcos_url(env): stderr = b'Please check url \'abc.com\'. Missing http(s)://\n' assert_command(['dcos', 'config', 'set', 'core.dcos_url', 'abc.com'], stderr=stderr, - returncode=1) + returncode=1, + env=env) def test_get_top_property(env): stderr = ( b"Property 'core' doesn't fully specify a value - " b"possible properties are:\n" + b"core.dcos_acs_token\n" b"core.dcos_url\n" b"core.reporting\n" b"core.ssl_verify\n" @@ -95,10 +87,11 @@ def test_get_top_property(env): assert_command(['dcos', 'config', 'show', 'core'], stderr=stderr, - returncode=1) + returncode=1, + env=env) -def test_set_package_sources_property(env): +def test_set_package_sources_property(): notice = (b"This config property has been deprecated. " b"Please add your repositories with `dcos package repo add`\n") assert_command(['dcos', 'config', 'set', 'package.sources', '[\"foo\"]'], @@ -106,7 +99,7 @@ def test_set_package_sources_property(env): returncode=1) -def test_set_core_email_property(env): +def test_set_core_email_property(): notice = (b"This config property has been deprecated.\n") assert_command(['dcos', 'config', 'set', 'core.email', 'foo@bar.com'], stderr=notice, @@ -189,6 +182,7 @@ def test_unset_top_property(env): stderr = ( b"Property 'core' doesn't fully specify a value - " b"possible properties are:\n" + b"core.dcos_acs_token\n" b"core.dcos_url\n" b"core.reporting\n" b"core.ssl_verify\n" @@ -216,10 +210,10 @@ def test_set_property_key(env): env=env) -def test_set_missing_property(missing_env): - config_set('core.dcos_url', 'http://localhost:8080', missing_env) - _get_value('core.dcos_url', 'http://localhost:8080', missing_env) - config_unset('core.dcos_url', missing_env) +def test_set_missing_property(env): + with update_config("core.dcos_url", None, env=env): + config_set('core.dcos_url', 'http://localhost:8080', env) + _get_value('core.dcos_url', 'http://localhost:8080', env) def test_set_core_property(env): @@ -272,19 +266,15 @@ def test_bad_port_fail_url_validation(env): 'http://localhost:bad_port/', env) -def test_timeout(missing_env): - config_set('marathon.url', 'http://1.2.3.4', missing_env) - config_set('core.timeout', '1', missing_env) +def test_timeout(env): + with update_config('marathon.url', 'http://1.2.3.4', env): + with update_config('core.timeout', '1', env): + returncode, stdout, stderr = exec_command( + ['dcos', 'marathon', 'app', 'list'], env=env) - returncode, stdout, stderr = exec_command( - ['dcos', 'marathon', 'app', 'list'], env=missing_env) - - assert returncode == 1 - assert stdout == b'' - assert "(connect timeout=1)".encode('utf-8') in stderr - - config_unset('core.timeout', missing_env) - config_unset('marathon.url', missing_env) + assert returncode == 1 + assert stdout == b'' + assert "(connect timeout=1)".encode('utf-8') in stderr def test_parse_error(): diff --git a/cli/tests/integrations/test_marathon.py b/cli/tests/integrations/test_marathon.py index db01223..764b050 100644 --- a/cli/tests/integrations/test_marathon.py +++ b/cli/tests/integrations/test_marathon.py @@ -11,7 +11,8 @@ from six.moves.BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer from .common import (app, assert_command, assert_lines, config_set, config_unset, exec_command, list_deployments, popen_tty, - show_app, watch_all_deployments, watch_deployment) + show_app, update_config, watch_all_deployments, + watch_deployment) _ZERO_INSTANCE_APP_INSTANCES = 100 @@ -43,24 +44,24 @@ def test_about(): @pytest.fixture -def missing_env(): - env = os.environ.copy() - env.update({ +def env(): + r = os.environ.copy() + r.update({ constants.PATH_ENV: os.environ[constants.PATH_ENV], - constants.DCOS_CONFIG_ENV: - os.path.join("tests", "data", "marathon", - "missing_marathon_params.toml") + constants.DCOS_CONFIG_ENV: os.path.join("tests", "data", "dcos.toml"), }) - return env + + return r -def test_missing_config(missing_env): - assert_command( - ['dcos', 'marathon', 'app', 'list'], - returncode=1, - stderr=(b'Missing required config parameter: "core.dcos_url". ' - b'Please run `dcos config set core.dcos_url `.\n'), - env=missing_env) +def test_missing_config(env): + with update_config("core.dcos_url", None, env): + assert_command( + ['dcos', 'marathon', 'app', 'list'], + returncode=1, + stderr=(b'Missing required config parameter: "core.dcos_url". ' + b'Please run `dcos config set core.dcos_url `.\n'), + env=env) def test_empty_list(): diff --git a/cli/tests/integrations/test_ssl.py b/cli/tests/integrations/test_ssl.py index 492fca3..2696685 100644 --- a/cli/tests/integrations/test_ssl.py +++ b/cli/tests/integrations/test_ssl.py @@ -4,7 +4,7 @@ from dcos import constants import pytest -from .common import config_set, config_unset, exec_command +from .common import config_set, exec_command, update_config @pytest.fixture @@ -12,13 +12,21 @@ def env(): r = os.environ.copy() r.update({ constants.PATH_ENV: os.environ[constants.PATH_ENV], - constants.DCOS_CONFIG_ENV: os.path.join("tests", - "data", "ssl", "ssl.toml"), + constants.DCOS_CONFIG_ENV: os.path.join("tests", "data", "dcos.toml"), }) return r +@pytest.yield_fixture(autouse=True) +def setup_env(env): + config_set("core.dcos_url", "https://dcos.snakeoil.mesosphere.com", env) + try: + yield + finally: + config_set("core.dcos_url", "http://dcos.snakeoil.mesosphere.com", env) + + def test_dont_verify_ssl_with_env_var(env): env[constants.DCOS_SSL_VERIFY_ENV] = 'false' @@ -31,77 +39,68 @@ def test_dont_verify_ssl_with_env_var(env): def test_dont_verify_ssl_with_config(env): - config_set('core.ssl_verify', 'false', env) - - returncode, stdout, stderr = exec_command( - ['dcos', 'marathon', 'app', 'list'], env) - assert returncode == 0 - assert stderr == b'' - - config_unset('core.ssl_verify', env) + with update_config('core.ssl_verify', 'false', env): + returncode, stdout, stderr = exec_command( + ['dcos', 'marathon', 'app', 'list'], env) + assert returncode == 0 + assert stderr == b'' def test_verify_ssl_without_cert_env_var(env): env[constants.DCOS_SSL_VERIFY_ENV] = 'true' - - returncode, stdout, stderr = exec_command( - ['dcos', 'marathon', 'app', 'list'], env) - assert returncode == 1 - assert "certificate verify failed" in stderr.decode('utf-8') + with update_config('core.ssl_verify', None, env): + returncode, stdout, stderr = exec_command( + ['dcos', 'marathon', 'app', 'list'], env) + assert returncode == 1 + assert "certificate verify failed" in stderr.decode('utf-8') env.pop(constants.DCOS_SSL_VERIFY_ENV) def test_verify_ssl_without_cert_config(env): - config_set('core.ssl_verify', 'true', env) - - returncode, stdout, stderr = exec_command( - ['dcos', 'marathon', 'app', 'list'], env) - assert returncode == 1 - assert "certificate verify failed" in stderr.decode('utf-8') - - config_unset('core.ssl_verify', env) + with update_config('core.ssl_verify', 'true', env): + returncode, stdout, stderr = exec_command( + ['dcos', 'marathon', 'app', 'list'], env) + assert returncode == 1 + assert "certificate verify failed" in stderr.decode('utf-8') def test_verify_ssl_with_bad_cert_env_var(env): env[constants.DCOS_SSL_VERIFY_ENV] = 'tests/data/ssl/fake.pem' - returncode, stdout, stderr = exec_command( - ['dcos', 'marathon', 'app', 'list'], env) - assert returncode == 1 - assert "PEM lib" in stderr.decode('utf-8') # wrong private key + with update_config('core.ssl_verify', None, env): + returncode, stdout, stderr = exec_command( + ['dcos', 'marathon', 'app', 'list'], env) + assert returncode == 1 + assert "PEM lib" in stderr.decode('utf-8') # wrong private key env.pop(constants.DCOS_SSL_VERIFY_ENV) def test_verify_ssl_with_bad_cert_config(env): - config_set('core.ssl_verify', 'tests/data/ssl/fake.pem', env) - - returncode, stdout, stderr = exec_command( - ['dcos', 'marathon', 'app', 'list'], env) - assert returncode == 1 - assert "PEM lib" in stderr.decode('utf-8') # wrong private key - - config_unset('core.ssl_verify', env) + with update_config('core.ssl_verify', 'tests/data/ssl/fake.pem', env): + returncode, stdout, stderr = exec_command( + ['dcos', 'marathon', 'app', 'list'], env) + assert returncode == 1 + assert "PEM lib" in stderr.decode('utf-8') # wrong private key def test_verify_ssl_with_good_cert_env_var(env): env[constants.DCOS_SSL_VERIFY_ENV] = '/dcos-cli/adminrouter/snakeoil.crt' - returncode, stdout, stderr = exec_command( - ['dcos', 'marathon', 'app', 'list'], env) - assert returncode == 0 - assert stderr == b'' + with update_config('core.ssl_verify', None, env): + returncode, stdout, stderr = exec_command( + ['dcos', 'marathon', 'app', 'list'], env) + assert returncode == 0 + assert stderr == b'' env.pop(constants.DCOS_SSL_VERIFY_ENV) def test_verify_ssl_with_good_cert_config(env): - config_set('core.ssl_verify', '/dcos-cli/adminrouter/snakeoil.crt', env) - - returncode, stdout, stderr = exec_command( - ['dcos', 'marathon', 'app', 'list'], env) - assert returncode == 0 - assert stderr == b'' - - config_unset('core.ssl_verify', env) + with update_config( + 'core.ssl_verify', '/dcos-cli/adminrouter/snakeoil.crt', env): + returncode, stdout, stderr = exec_command( + ['dcos', 'marathon', 'app', 'list'], env) + assert returncode == 0 + assert stderr == b'' diff --git a/cli/tests/unit/test_http_auth.py b/cli/tests/unit/test_http_auth.py index 8c8da35..ebe3e34 100644 --- a/cli/tests/unit/test_http_auth.py +++ b/cli/tests/unit/test_http_auth.py @@ -121,7 +121,8 @@ def test_request_with_bad_auth_acl(mock, req_mock, auth_mock): with pytest.raises(DCOSException) as e: http._request_with_auth(mock, "method", mock.url) - assert e.exconly().split(':')[1].strip() == "Authentication failed" + msg = "Your core.dcos_acs_token is invalid. Please run: `dcos auth login`" + assert e.exconly().split(':', 1)[1].strip() == msg @patch('requests.Response') @@ -138,7 +139,8 @@ def test_request_with_bad_oauth(mock, req_mock, auth_mock): with pytest.raises(DCOSException) as e: http._request_with_auth(mock, "method", mock.url) - assert e.exconly().split(':')[1].strip() == "Authentication failed" + msg = "Your core.dcos_acs_token is invalid. Please run: `dcos auth login`" + assert e.exconly().split(':', 1)[1].strip() == msg @patch('requests.Response')