File: test_analytics.py

package info (click to toggle)
python-dcos 0.2.0-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,440 kB
  • sloc: python: 8,196; sh: 194; makefile: 36
file content (147 lines) | stat: -rw-r--r-- 4,295 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
from functools import wraps

import dcoscli.analytics
import rollbar
from dcos import constants, http, util
from dcoscli.analytics import _base_properties
from dcoscli.config.main import main as config_main
from dcoscli.constants import (SEGMENT_IO_CLI_ERROR_EVENT,
                               SEGMENT_IO_CLI_EVENT, SEGMENT_URL)
from dcoscli.main import main

from mock import patch

from .common import mock_called_some_args

ANON_ID = 0
USER_ID = 'test@mail.com'


def _mock(fn):
    @wraps(fn)
    def wrapper(*args, **kwargs):
        with patch('rollbar.init'), \
                patch('rollbar.report_message'), \
                patch('dcos.http.post'), \
                patch('dcoscli.analytics.session_id'):

            dcoscli.analytics.session_id = ANON_ID
            fn()

    return wrapper


@_mock
def test_config_set():
    '''Tests that a `dcos config set core.email <email>` makes a
    segment.io identify call'''

    args = [util.which('dcos'), 'config', 'set', 'core.email', 'test@mail.com']
    env = _env_reporting()

    with patch('sys.argv', args), patch.dict(os.environ, env):
        assert config_main() == 0

        # segment.io
        assert mock_called_some_args(http.post,
                                     '{}/identify'.format(SEGMENT_URL),
                                     json={'userId': 'test@mail.com'},
                                     timeout=(1, 1))


@_mock
def test_no_exc():
    '''Tests that a command which does not raise an exception does not
    report an exception.

    '''

    args = [util.which('dcos')]
    env = _env_reporting()
    version = 'release'

    with patch('sys.argv', args), \
            patch.dict(os.environ, env), \
            patch('dcoscli.version', version):
        assert main() == 0

        # segment.io
        data = {'userId': USER_ID,
                'event': SEGMENT_IO_CLI_EVENT,
                'properties': _base_properties()}
        assert mock_called_some_args(http.post,
                                     '{}/track'.format(SEGMENT_URL),
                                     json=data,
                                     timeout=(1, 1))

        # rollbar
        assert rollbar.report_message.call_count == 0


@_mock
def test_exc():
    '''Tests that a command which does raise an exception does report an
    exception.

    '''

    args = [util.which('dcos')]
    env = _env_reporting()
    version = 'release'
    with patch('sys.argv', args), \
            patch('dcoscli.version', version), \
            patch.dict(os.environ, env), \
            patch('dcoscli.analytics.wait_and_capture',
                  return_value=(1, 'Traceback')):
        assert main() == 1

        # segment.io
        props = _base_properties()
        props['err'] = 'Traceback'
        props['exit_code'] = 1
        data = {'userId': USER_ID,
                'event': SEGMENT_IO_CLI_ERROR_EVENT,
                'properties': props}

        assert mock_called_some_args(http.post,
                                     '{}/track'.format(SEGMENT_URL),
                                     json=data,
                                     timeout=(1, 1))

        # rollbar
        props = _base_properties()
        props['exit_code'] = 1
        props['stderr'] = 'Traceback'
        rollbar.report_message.assert_called_with('Traceback', 'error',
                                                  extra_data=props)


@_mock
def test_config_reporting_false():
    '''Test that "core.reporting = false" blocks exception reporting.'''

    args = [util.which('dcos')]
    env = _env_no_reporting()
    version = 'release'

    with patch('sys.argv', args), \
            patch('dcoscli.version', version), \
            patch.dict(os.environ, env), \
            patch('dcoscli.analytics.wait_and_capture',
                  return_value=(1, 'Traceback')):

        assert main() == 1

        assert rollbar.report_message.call_count == 0
        assert http.post.call_count == 0


def _env_reporting():
    path = os.path.join('tests', 'data', 'analytics', 'dcos_reporting.toml')
    return {constants.DCOS_CONFIG_ENV: path}


def _env_no_reporting():
    path = os.path.join('tests', 'data', 'analytics', 'dcos_no_reporting.toml')
    return {constants.DCOS_CONFIG_ENV: path}