added a new test config file

moved test config files to their own directories
refactored tests
added a test for new core.report config var
This commit is contained in:
Michael Gummelt
2015-04-15 13:45:45 -07:00
parent f146238c57
commit 9f707b88d0
7 changed files with 55 additions and 40 deletions

View File

@@ -0,0 +1 @@
SEGMENT_IO_WRITE_KEY = '51ybGTeFEFU1xo6u10XMDrr6kATFyRyh'

View File

@@ -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['<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):

View File

@@ -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"

View File

@@ -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"

View File

@@ -0,0 +1,2 @@
[core]
report = false

View File

@@ -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")
}

View File

@@ -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