157020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley# Copyright 2015 The Chromium OS Authors. All rights reserved.
257020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley# Use of this source code is governed by a BSD-style license that can be
357020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley# found in the LICENSE file.
457020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley
557020c094a377700b0ad4d558d70062a19f1370aChristopher Wileyimport time
657020c094a377700b0ad4d558d70062a19f1370aChristopher Wileyimport uuid
757020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley
857020c094a377700b0ad4d558d70062a19f1370aChristopher Wileyfrom autotest_lib.client.common_lib import error
957020c094a377700b0ad4d558d70062a19f1370aChristopher Wileyfrom autotest_lib.client.common_lib.cros import process_watcher
1057020c094a377700b0ad4d558d70062a19f1370aChristopher Wileyfrom autotest_lib.client.common_lib.cros.fake_device_server.client_lib import \
1157020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley        meta
1257020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley
1357020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley
1457020c094a377700b0ad4d558d70062a19f1370aChristopher Wileyclass FakeGCDHelper(object):
1557020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley    """Helper object that knows how to bring up and kill fake GCD instances."""
1657020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley
1757020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley    def __init__(self, host=None):
1857020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley        """Construct an instance.
1957020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley
2057020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley        @param host: host object if the server should be started on a remote
2157020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley                host.
2257020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley
2357020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley        """
2457020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley        self._generation = str(uuid.uuid1())
2557020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley        self._process = process_watcher.ProcessWatcher(
2657020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley                '/usr/local/autotest/common_lib/cros/'
2757020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley                        'fake_device_server/server.py',
2857020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley                args=(self._generation,),
2957020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley                host=host)
3057020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley        self._meta = meta.MetaClient()
3157020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley
3257020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley
3357020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley    def start(self, timeout_seconds=30):
3457020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley        """Start this instance and confirm that it is up.
3557020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley
3657020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley        @param timeout_seconds: number of seconds to wait for server start.
3757020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley
3857020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley        """
3957020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley        self._process.start()
4057020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley        start_time = time.time()
4157020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley        while time.time() - start_time < timeout_seconds:
4257020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley            received_generation = self._meta.get_generation()
4357020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley            if self._generation == received_generation:
4457020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley                return
4557020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley            time.sleep(1)
4657020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley
4757020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley        raise error.TestError('Failed to start fake GCD server.')
4857020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley
4957020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley
5057020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley    def close(self):
5157020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley        """Close this instance."""
5257020c094a377700b0ad4d558d70062a19f1370aChristopher Wiley        self._process.close()
53