Home | History | Annotate | Download | only in cros
      1 # Copyright 2014 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 import os
      5 import sys
      6 from multiprocessing import Process
      7 
      8 from autotest_lib.client.bin import test
      9 from autotest_lib.client.bin import utils
     10 from autotest_lib.client.common_lib.cros import chrome
     11 
     12 policy_testserver = None
     13 
     14 
     15 class EnterpriseTest(test.test):
     16     """Base class for policy tests."""
     17 
     18     USERNAME = 'autotest (at] managedchrome.com'
     19     PASSWORD = 'test0000'
     20 
     21     def import_dmserver(self, proto_path):
     22         """Import the DM testserver from chrome source.
     23 
     24         @param proto_path: location of proto files.
     25         """
     26         telemetry_src = '/usr/local/telemetry/src'
     27         sys.path.append(os.path.join(telemetry_src,
     28                                      'chrome/browser/policy/test'))
     29         sys.path.append(os.path.join(telemetry_src,
     30                                      'net/tools/testserver'))
     31         sys.path.append(os.path.join(telemetry_src,
     32                                      'third_party/protobuf/python/google'))
     33         sys.path.append(os.path.join(telemetry_src,
     34                                      'third_party/tlslite'))
     35         sys.path.append(proto_path)
     36         global policy_testserver
     37         import policy_testserver
     38 
     39     def start_dmserver(self):
     40         """Start the local DM testserver."""
     41         policy_server_runner = policy_testserver.PolicyServerRunner()
     42         self._policy_location = os.path.join(self.tmpdir, 'policy.json')
     43         port = utils.get_unused_port()
     44         # The first argument is always ignored since it is expected to be the
     45         # path to the executable. Hence passing an empty string for first
     46         # argument.
     47         sys.argv = ['',
     48                     '--config-file=%s' % self._policy_location,
     49                     '--host=127.0.0.1',
     50                     '--log-file=%s/dm_server.log' % self.debugdir,
     51                     '--log-level=DEBUG',
     52                     '--port=%d' % port
     53                    ]
     54         self.dm_server = Process(target=policy_server_runner.main)
     55         self.dm_server.start()
     56         self.dm_server_url = 'http://127.0.0.1:%d/' % port
     57 
     58     def stop_dmserver(self):
     59         """Stop the local DM server."""
     60         os.system('wget %sconfiguration/test/exit' % self.dm_server_url)
     61         self.dm_server.join()
     62 
     63     def initialize(self):
     64         self.start_dmserver()
     65 
     66     def cleanup(self):
     67         self.stop_dmserver()
     68 
     69     def setup_policy(self, policy_blob):
     70         """Write policy blob to file used by the DM server to read policy.
     71 
     72         @param policy_blob: JSON policy blob to be written to the policy file.
     73         """
     74         with open(self._policy_location, 'w') as f:
     75             f.write(policy_blob)
     76 
     77     def create_chrome(self,
     78                       autotest_ext=False,
     79                       extra_browser_args='',
     80                       username=None,
     81                       password=None):
     82         """Create an instance of chrome for enterprise testing.
     83 
     84         @param autotest_ext: Load a component extension with privileges to
     85                              invoke chrome.autotestPrivate.
     86         @param extra_browser_args: Additional argument(s) to pass to the
     87                                    browser. It should be a string.
     88         @param username: Log in using this username instead of the default.
     89         @param password: Log in using this password instead of the default.
     90 
     91         @return: A telemetry browser instance.
     92         """
     93         extra_browser_args = (extra_browser_args +
     94                               '--device-management-url=%s ' %
     95                               self.dm_server_url +
     96                               '--enterprise-enrollment-skip-robot-auth')
     97         username = self.USERNAME if username is None else username
     98         password = self.PASSWORD if password is None else password
     99         return chrome.Chrome(extra_browser_args=extra_browser_args,
    100                              autotest_ext=autotest_ext,
    101                              disable_gaia_services=False,
    102                              gaia_login=True,
    103                              username=username,
    104                              password=password)
    105