| // Copyright 2021 The ChromiumOS Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package dututils |
| |
| import ( |
| "context" |
| "time" |
| |
| "go.chromium.org/tast-tests/cros/common/servo" |
| "go.chromium.org/tast/core/dut" |
| "go.chromium.org/tast/core/errors" |
| "go.chromium.org/tast/core/testing" |
| ) |
| |
| // EnsureDUTIsOn checks if DUT is pingable and if not, the function waits till DUT boots up. |
| func EnsureDUTIsOn(ctx context.Context, d *dut.DUT, srvo *servo.Servo) error { |
| // Try connecting with a small timeout, if DUT is already up, it will be successful. |
| waitCtx, cancel := context.WithTimeout(ctx, 8*time.Second) |
| defer cancel() |
| |
| if err := d.WaitConnect(waitCtx); err == nil { |
| return nil |
| } |
| // If DUT is not reachable, boot it up. |
| if err := srvo.KeypressWithDuration(ctx, servo.PowerKey, servo.DurLongPress); err != nil { |
| return errors.Wrap(err, "failed to press power key") |
| } |
| |
| testing.ContextLog(ctx, "Reconnecting to DUT") |
| if err := d.WaitConnect(ctx); err != nil { |
| return errors.Wrap(err, "failed to connect to DUT") |
| } |
| testing.ContextLog(ctx, "Reconnected to DUT") |
| return nil |
| } |