ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
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
# Copyright 2017 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
 
import logging
import tempfile
 
from autotest_lib.client.bin import utils
from autotest_lib.client.common_lib import error
from autotest_lib.client.common_lib import file_utils
from autotest_lib.client.common_lib.cros import arc, arc_util
from autotest_lib.client.common_lib.cros import chrome
from autotest_lib.client.cros import cryptohome
from autotest_lib.client.cros.input_playback import keyboard
 
 
_CHROME_EXEC_TIME = 'chrome-exec'
_LOGIN_TIME = 'login-prompt-visible'
_LOGOUT_STARTED_TIME = 'logout-started'
_LOGOUT_TIMEOUT = 60  # logout should finsih in 60 seconds.
 
 
class platform_LogoutPerf(arc.ArcTest):
    """Measured the time for signing off from a logged on user session.
 
    The test mainly measures the time for signing off a session and raises
    an exception if it could not be finished in time. First, it uses telemetry
    to login a GAIA account and waits for container boots up if the
    device supports ARC++, then validates the account. Next, it injects the
    ctrl+shift+q twice to logout the session, then waits for new login screen
    , i.e. the event of 'login-visible-prompt' to calculate the time elapsed
    for logging out.
    """
    version = 1
 
 
    def _get_latest_uptime(self, filename):
        with open('/tmp/uptime-' + filename) as statfile:
            values = map(lambda l: float(l.split()[0]),
                         statfile.readlines())
        logging.info('timestamp of %s -> %s ', filename, values[-1])
        return values[-1]
 
 
    def _validate(self):
        # Validate if the environment is expected.
        if not cryptohome.is_vault_mounted(
                user=chrome.NormalizeEmail(self.username)):
            raise error.TestFail('Expected to find a mounted vault for %s'
                                 % self.username)
        tab = self.cr.browser.tabs.New()
        tab.Navigate('http://accounts.google.com')
        tab.WaitForDocumentReadyStateToBeComplete()
        res = tab.EvaluateJavaScript('''
                var res = '',
                    divs = document.getElementsByTagName('div');
                for (var i = 0; i < divs.length; i++) {
                    res = divs[i].textContent;
                    if (res.search('%s') > 1) {
                    break;
                    }
                }
                res;
        ''' % self.username)
        if not res:
            raise error.TestFail('No references to %s on accounts page.'
                                 % self.username)
        tab.Close()
 
 
    def initialize(self):
        self.keyboard = keyboard.Keyboard()
        self.username, password = arc_util.get_test_account_info()
        # Login a user session.
        if utils.is_arc_available():
            super(platform_LogoutPerf, self).initialize(
                    gaia_login=True,
                    disable_arc_opt_in=False)
            self.cr = self._chrome
        else:
            with tempfile.NamedTemporaryFile() as cap:
                file_utils.download_file(arc_util._ARCP_URL, cap.name)
                password = cap.read().rstrip()
            self.cr = chrome.Chrome(gaia_login=True,
                                    username=self.username,
                                    password=password)
 
 
    def arc_setup(self):
        # Do nothing here
        logging.info('No ARC++ specific setup required')
 
 
    def arc_teardown(self):
        # Do nothing here
        logging.info('No ARC++ specific teardown required')
 
 
    def cleanup(self):
        self.keyboard.close()
        if utils.is_arc_available():
            super(platform_LogoutPerf, self).cleanup()
 
 
    def run_once(self):
        # Validate the current GAIA login session
        self._validate()
 
        # Start signing out the session, wait until the new event of
        # 'login-prompt-visible'.
        self.keyboard.press_key('ctrl+shift+q')
        self.keyboard.press_key('ctrl+shift+q')
 
        # Get current login prompt timestamp
        login_timestamp = self._get_latest_uptime(_LOGIN_TIME)
 
        # Poll for new login prompt timestamp
        utils.poll_for_condition(
                lambda: self._get_latest_uptime(_LOGIN_TIME) != login_timestamp,
                exception=error.TestFail('Timeout: Could not sign off in time'),
                timeout=_LOGOUT_TIMEOUT,
                sleep_interval=2,
                desc='Polling for user to be logged out.')
 
        logout_started = self._get_latest_uptime(_LOGOUT_STARTED_TIME)
        logging.info('logout started @%s', logout_started)
        chrome_exec = self._get_latest_uptime(_CHROME_EXEC_TIME)
        logging.info('chrome restarts @%s', chrome_exec)
        elapsed_time = float(chrome_exec) - float(logout_started)
        logging.info('The elapsed time for signout is %s', elapsed_time)
        self.output_perf_value(description='Seconds elapsed for Signout',
                               value=elapsed_time,
                               higher_is_better=False,
                               units='seconds')