# Copyright 2018 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.
from autotest_lib.server.cros.cfm.configurable_test.dsl import *
from autotest_lib.server import utils

AUTHOR = "wilhelmsson@google.com, kerl@google.com, chromeos-meetings@google.com"
NAME = "enterprise_CFM_PeripheralQualification.reboot_stress"
PURPOSE = "Stresses a peripheral device by repeatedly rebooting the CfM."
CRITERIA = "The device is detectable as a USB device after reboot"
TIME = "LONG"
TEST_CATEGORY = "Stress"
TEST_TYPE = "server"

DOC = """
Repeatedly reboots the CfM and verifies that the device can be enumerated
after each reboot.

The test requires a vid_pid argument (e.g. 18d1:8001) that determines which
device to detect after reboot. This enables testing custom devices from
Moblab or from a local workstation.
"""

args_dict = utils.args_to_dict(args)
vid,pid = args_dict['vid_pid'].split(':')
# The product is only informational, set it to whatever.
product = args_dict.get('product', 'customProduct')
# Interfaces are only needed when verifying them, set them to empty in this case.
interfaces = []
device = usb_device_spec.UsbDeviceSpec(vid, pid, product, interfaces)
repeat = int(args_dict.get('repeat', 10))

cfm_test = CfmTest(
    configuration=Configuration(skip_enrollment=True),
    scenario=Scenario(
        AssertUsbDevices([device]),
        RepeatTimes(repeat, Scenario(
            RebootDut(),
            AssertUsbDevices([device])
            # TODO(crbug.com/814775): mosys-info always crashes on reboot, why
            # we always have new crash files. Enable this check when that is
            # fixed.
            # AssertNoNewCrashes()
        ))
    ),
)

def run_test(machine):
    job.run_test("enterprise_CFM_PeripheralQualification",
                 cfm_test = cfm_test,
                 tag = 'reboot_stress',
                 host = hosts.create_host(machine))

parallel_simple(run_test, machines)