~canonical-platform-qa/ubuntu-system-tests/install-production-deps

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-

#
# Ubuntu System Tests
# Copyright (C) 2014-2016 Canonical
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import logging
import os

from uciconfig import (
    options,
    stacks,
    stores
)

logger = logging.getLogger(__name__)
UbuntuSystemTestsStore = stores.FileStore
config_stack = None

CONFIG_OPTIONS = [
    options.Option(
        'device_serial', help_string='Device serial'),
    options.Option(
        'device_password', default=options.MANDATORY,
        help_string='Device password'),
    options.Option(
        'output_dir', default=options.MANDATORY,
        help_string='Directory to store the tests results and artifacts.\n'
                    'WARNING: This directory will have all existing content '
                    'deleted!'),
    options.Option(
        'sim_0_pin', default=options.MANDATORY,
        help_string='SIM pin for the card in the first slot'),
    options.Option(
        'sim_1_pin',
        help_string='SIM pin for the card in the second slot'),
    options.Option(
        'wifi_ssid', default=options.MANDATORY,
        help_string='Wi-Fi SSID used to connect during setup wizard'),
    options.Option(
        'wifi_password', default=options.MANDATORY,
        help_string='Wi-Fi password used to connect during setup wizard'),
    options.Option(
        'device_security', default='Passcode',
        help_string='The security method to select during setup wizard'),
    options.Option(
        'bluetooth_device_name',
        help_string='Name of a nearby bluetooth pairing device'),
    options.Option(
        'device_phone_number',
        help_string="The phone number of the SIM card inserted in the first "
                    "SIM slot. This must be formatted exactly as you would "
                    "dial it."),
    options.Option(
        'device_phone_number2',
        help_string="The phone number of the SIM card inserted in the second "
                    "SIM slot. If the device has only one SIM slot, you can "
                    "leave this blank. This must be formatted exactly as you "
                    "would dial it."),
    options.Option(
        'telephony_service_number1',
        help_string="The phone number to call and message from "
                    "in the telephony service. In Twilio go to "
                    "https://www.twilio.com/user/account/"
                    "phone-numbers/incoming and click on the number "
                    "you want to use, then copy the 'Phone Number' "
                    "field exactly."),
    options.Option(
        'telephony_service_number2',
        help_string="The second phone number to call and message from "
                    "in the telephony service. In Twilio go to "
                    "https://www.twilio.com/user/account/"
                    "phone-numbers/incoming and click on the number "
                    "you want to use, then copy the 'Phone Number' "
                    "field exactly."),
    options.Option(
        # this is a private key, that will not be exported to
        # autopilot directly
        '_twilio_account_sid', default=options.MANDATORY,
        help_string='Account SID for Twilio. Please log into '
                    'https://www.twilio.com/login '
                    'and select \'Dashboard -> Show API Credentials\'.'
                    'Then copy and paste the Account SID value here.'),
    options.Option(
        # this is a private key, that will not be exported to
        # autopilot directly
        '_twilio_auth_token', default=options.MANDATORY,
        help_string='Auth token for Twilio account. Please log into '
                    'https://www.twilio.com/login '
                    'and select \'Dashboard -> Show API Credentials\'.'
                    'Then copy and paste the Auth token value here.'),
    options.Option(
        # this is a private key, that will not be exported to
        # autopilot directly
        'max_unity8_retry_delay', default=30000,
        help_string='The boundary for how long we should be retrying until '
                    'unity8 is started'),
    options.Option(
        'app_startup_cold_runs', default=0,
        help_string='How many iterations to run for the cold start app '
                    'performance tests?'),
    options.Option(
        'app_startup_hot_runs', default=0,
        help_string='How many iterations to run for the hot start app '
                    'performance tests?'),
    options.Option(
        'pictures_scalability_runs',
        help_string='The different loads used for scalability pictures tests '
                    'separated by comma'),
]
"""Options for tests in external debian and click packages."""


def get_user_config_dir():
    """Return the path to the user configuration directory."""
    conf_dir = os.environ.get(
        'XDG_CONFIG_HOME', os.path.expanduser('~/.config'))
    return conf_dir


def register_test_options():
    """Register the options for the Ubuntu System Tests."""
    for option in CONFIG_OPTIONS:
        options.option_registry.register(option)


def get_device_config_file_path():
    """Return the path of the config file copied to the device."""
    return '/tmp/ubuntu-system-tests.conf'


def get_config_stack_from_file(file_path):
    """Return config stack from specified file path."""
    return UbuntuSystemTestsStack(file_path)


def get_device_config_stack():
    """Return the config stack variable"""
    global config_stack
    if not config_stack:
        config_stack = get_config_stack_from_file(
            get_device_config_file_path())
    return config_stack


def get_test_config_values(config_stack):
    """ Return a string containing the comma separated key=value options.
    The list includes the config values and the temporal ones as well
    """
    return config_stack.get_autopilot_config_string()


class UbuntuSystemTestsStack(stacks.Stack):

    def __init__(self, conf_name='ubuntu-system-tests.conf'):
        config_path = os.path.join(
            get_user_config_dir(), conf_name)
        user_store = self.get_shared_store(UbuntuSystemTestsStore(config_path))
        super().__init__(
            [stacks.NameMatcher(user_store, None).get_sections],
            user_store, mutable_section_id=None)

    def get_missing_options(self, silent=False):
        """Ask the user for the values of the missing options.

        The values entered by the user will be saved in the config file.

        :param silent: If True then don't prompt the user for any missing
                       config options, use default values instead. If False
                       then prompt the user for any missing option values.

        """
        user_config_dir = get_user_config_dir()
        if not os.path.exists(user_config_dir):
            os.mkdir(user_config_dir)

        for key in options.option_registry.keys():
            if not self._is_option_in_store(key):
                if silent:
                    default = self.get(key, None)
                    if default:
                        self.set(key, default)
                        logger.warning('In silent mode no value specified for '
                        'option: "{k}". Using default value "{v}".'.format
                            (k=key, v=default))
                    else:
                        logger.warning('In silent mode no default value '
                        'specified for option: "{k}". Continuing with value '
                        'unset.'.format(k=key))
                else:
                    self._get_option_value_from_stdin(key)
        self.store.save()

    def _is_option_in_store(self, key):
        for store, section in self.iter_sections():
            if section.get(key, None) is not None:
                return True
        else:
            return False

    def _get_option_value_from_stdin(self, key):
        help_ = self._get_option_help_text(key)
        # This will give a pyflakes error if it is installed in py2
        # See http://pad.lv/1331267
        print(help_, end='')  # flake8: noqa
        value = self._get_value_from_user_or_default(key)
        self.set(key, value)

    def _get_option_help_text(self, key):
        help_ = options.option_registry.get_help(key)
        default = options.option_registry.get(key).default
        if default is not options.MANDATORY:
            if default:
                help_ += ' (leave blank to use default value: {})'.format(
                    default)
            else:
                help_ += ' (leave blank if not required)'
        help_ += ': '
        return help_

    def _get_value_from_user_or_default(self, key):
        value = input()
        if not value and self.get(key, None):
            # If the option was left blank, check if it has a default and
            # use it instead.
            value = self.get(key)
        return value

    def get_autopilot_config_string(self):
        """Return the configuration in a string usable by Autopilot.

        That is, a list of key=value pairs serparated by comma.

        Any keys starting with _ will be ignored.

        """
        config = ''
        for _, _, key, value in self.iter_options():
            if not key.startswith('_'):
                config += '{k}={v},'.format(k=key, v=value)

        # remove trailing ',' if it exists
        if config.endswith(','):
            config = config[:-1]
        return config

    def save(self):
        """ Save the changes done to the store """
        self.store.save()