diff --git a/cli/dcoscli/constants.py b/cli/dcoscli/constants.py index e69de29..86bcd40 100644 --- a/cli/dcoscli/constants.py +++ b/cli/dcoscli/constants.py @@ -0,0 +1 @@ +SEGMENT_IO_WRITE_KEY = '51ybGTeFEFU1xo6u10XMDrr6kATFyRyh' diff --git a/cli/dcoscli/main.py b/cli/dcoscli/main.py index 5c3deef..40dd4c7 100644 --- a/cli/dcoscli/main.py +++ b/cli/dcoscli/main.py @@ -36,11 +36,12 @@ import analytics import dcoscli import docopt import requests -from dcos.api import constants, emitting, subcommand, util +from dcos.api import constants, emitting, subcommand, util, config +from dcoscli.constants import SEGMENT_IO_WRITE_KEY emitter = emitting.FlatEmitter() -WRITE_KEY = '51ybGTeFEFU1xo6u10XMDrr6kATFyRyh' + def main(): @@ -60,10 +61,6 @@ def main(): emitter.publish(err) return 1 - # The requests package emits INFO logs. We want to exclude those, - # even if the user has selected --log-level=INFO. - logging.getLogger('requests').setLevel(logging.WARNING) - # urllib3 is printing an SSL warning we want to silence. See # DCOS-1007. requests.packages.urllib3.disable_warnings() @@ -81,6 +78,7 @@ def main(): subproc = Popen([executable, command] + args[''], stderr=PIPE) + analytics.write_key = SEGMENT_IO_WRITE_KEY return wait_and_track(subproc) @@ -94,8 +92,16 @@ def wait_and_track(subproc): exit_code = subproc.poll() + con = config.load_from_path( + os.environ[constants.DCOS_CONFIG_ENV]) + + if con.get('core.report', True): + track(exit_code, err) + + return exit_code + +def track(exit_code, err): # segment.io analytics - analytics.write_key = WRITE_KEY try: # We don't have user id's right now, but segment.io requires # them, so we use a constant (1) @@ -110,7 +116,6 @@ def wait_and_track(subproc): # ignore segment.io exceptions pass - return exit_code def _config_log_level_environ(log_level): diff --git a/cli/tests/data/config/dcos.toml b/cli/tests/data/config/dcos.toml new file mode 100644 index 0000000..4dec770 --- /dev/null +++ b/cli/tests/data/config/dcos.toml @@ -0,0 +1,8 @@ +[subcommand] +pip_find_links = "../dist" +[marathon] +host = "localhost" +port = 8080 +[package] +sources = [ "git://github.com/mesosphere/universe.git", "https://github.com/mesosphere/universe/archive/master.zip",] +cache = "tmp/cache" diff --git a/cli/tests/data/dcos.toml b/cli/tests/data/dcos.toml deleted file mode 100644 index d7a000f..0000000 --- a/cli/tests/data/dcos.toml +++ /dev/null @@ -1,8 +0,0 @@ -[subcommand] -pip_find_links = "../dist" -[marathon] -host = "localhost" -port = 8080 -[package] -sources = [ "new_uri", "git://github.com/mesosphere/universe.git", "https://github.com/mesosphere/universe/archive/master.zip",] -cache = "tmp/cache" diff --git a/cli/tests/data/dcos/dcos_no_reporting.toml b/cli/tests/data/dcos/dcos_no_reporting.toml new file mode 100644 index 0000000..c463013 --- /dev/null +++ b/cli/tests/data/dcos/dcos_no_reporting.toml @@ -0,0 +1,2 @@ +[core] +report = false diff --git a/cli/tests/integrations/cli/test_config.py b/cli/tests/integrations/cli/test_config.py index bf71457..492bf86 100644 --- a/cli/tests/integrations/cli/test_config.py +++ b/cli/tests/integrations/cli/test_config.py @@ -12,7 +12,7 @@ from common import exec_command def env(): return { constants.PATH_ENV: os.environ[constants.PATH_ENV], - constants.DCOS_CONFIG_ENV: os.path.join("tests", "data", "dcos.toml") + constants.DCOS_CONFIG_ENV: os.path.join("tests", "data", "config", "dcos.toml") } diff --git a/cli/tests/integrations/cli/test_dcos.py b/cli/tests/integrations/cli/test_dcos.py index d40b576..db17634 100644 --- a/cli/tests/integrations/cli/test_dcos.py +++ b/cli/tests/integrations/cli/test_dcos.py @@ -6,7 +6,7 @@ from dcoscli.main import main import mock from common import exec_command -from mock import Mock +from mock import Mock, patch def test_default(): @@ -107,7 +107,6 @@ def test_log_level_flag(): assert returncode == 0 assert stdout == b"Get and set DCOS command line options\n" - assert stderr == b'' def test_capital_log_level_flag(): @@ -116,7 +115,6 @@ def test_capital_log_level_flag(): assert returncode == 0 assert stdout == b"Get and set DCOS command line options\n" - assert stderr == b'' def test_invalid_log_level_flag(): @@ -129,32 +127,41 @@ def test_invalid_log_level_flag(): b"'critical']\n") assert stderr == b'' - -def test_analytics_no_err(): - args = ['dcos'] +def _mock_analytics_run(args): with mock.patch('sys.argv', args): analytics.track = Mock() analytics.flush = Mock() - exit_code = main() - analytics.track.assert_called_with(1, 'dcos-cli', - {'cmd': ' '.join(args), - 'exit_code': 0, - 'err': None}) - analytics.flush.assert_called_with() + return main() - assert exit_code == 0 + +def test_analytics_no_err(): + args = ['dcos'] + exit_code = _mock_analytics_run(args) + analytics.track.assert_called_with(1, 'dcos-cli', + {'cmd': ' '.join(args), + 'exit_code': 0, + 'err': None}) + analytics.flush.assert_called_with() + assert exit_code == 0 def test_analytics_err(): args = ['dcos', 'marathon', 'task', 'show', 'asdf'] - with mock.patch('sys.argv', args): - analytics.track = Mock() - analytics.flush = Mock() - exit_code = main() - attrs = {'cmd': ' '.join(args), - 'exit_code': 1, - 'err': "Task 'asdf' does not exist\n"} - analytics.track.assert_called_with(1, 'dcos-cli', attrs) - analytics.flush.assert_called_with() + exit_code = _mock_analytics_run(args) + attrs = {'cmd': ' '.join(args), + 'exit_code': 1, + 'err': "Task 'asdf' does not exist\n"} + analytics.track.assert_called_with(1, 'dcos-cli', attrs) + analytics.flush.assert_called_with() + assert exit_code == 1 - assert exit_code == 1 + +def test_analytics_report_config(): + args = ['dcos'] + new_env = {constants.DCOS_CONFIG_ENV: + os.path.join('tests', 'data', 'dcos', 'dcos_no_reporting.toml')} + with patch.dict(os.environ, new_env): + exit_code = _mock_analytics_run(args) + assert analytics.track.call_count == 0 + assert analytics.flush.call_count == 0 + assert exit_code == 0