| // Copyright 2026 The Chromium Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package androidtools |
| |
| import ( |
| "context" |
| "strconv" |
| "testing" |
| "time" |
| |
| "go.chromium.org/infra/cros/recovery/internal/env" |
| "go.chromium.org/infra/cros/recovery/tlw" |
| ) |
| |
| func TestFastbootDeviceSerial(t *testing.T) { |
| t.Parallel() |
| tests := []struct { |
| name string |
| dut *tlw.Dut |
| want string |
| }{ |
| { |
| name: "valid DUT", |
| dut: &tlw.Dut{Name: "test-dut"}, |
| want: "tcp:test-dut:" + strconv.Itoa(env.FastbootPort()), |
| }, |
| { |
| name: "nil DUT", |
| dut: nil, |
| want: "", |
| }, |
| { |
| name: "DUT with no name", |
| dut: &tlw.Dut{Name: ""}, |
| want: "", |
| }, |
| } |
| for _, tt := range tests { |
| |
| t.Run(tt.name, func(t *testing.T) { |
| t.Parallel() |
| if got := FastbootDeviceSerial(tt.dut); got != tt.want { |
| t.Errorf("FastbootDeviceSerial() = %v, want %v", got, tt.want) |
| } |
| }) |
| } |
| } |
| |
| func TestFastbootDeviceRun(t *testing.T) { |
| t.Parallel() |
| ctx := context.Background() |
| tests := []struct { |
| name string |
| dut *tlw.Dut |
| command string |
| args []string |
| wantErr bool |
| }{ |
| { |
| name: "nil DUT", |
| dut: nil, |
| command: "getvar", |
| args: []string{"version"}, |
| wantErr: true, |
| }, |
| { |
| name: "DUT with no name", |
| dut: &tlw.Dut{Name: ""}, |
| command: "getvar", |
| args: []string{"version"}, |
| wantErr: true, |
| }, |
| { |
| name: "empty command", |
| dut: &tlw.Dut{Name: "test-dut"}, |
| command: "", |
| wantErr: true, |
| }, |
| // Note: Testing successful execution requires mocking adbClient and fastbootRun, |
| // which is not feasible in this environment. |
| } |
| for _, tt := range tests { |
| |
| t.Run(tt.name, func(t *testing.T) { |
| t.Parallel() |
| _, err := FastbootDeviceRun(ctx, tt.dut, 10*time.Second, tt.command, tt.args...) |
| if (err != nil) != tt.wantErr { |
| t.Errorf("FastbootDeviceRun() error = %v, wantErr %v", err, tt.wantErr) |
| return |
| } |
| }) |
| } |
| } |
| |
| func TestFastbootRun(t *testing.T) { |
| t.Parallel() |
| ctx := context.Background() |
| tests := []struct { |
| name string |
| dut *tlw.Dut |
| deviceSerial string |
| command string |
| args []string |
| wantErr bool |
| }{ |
| { |
| name: "empty command", |
| dut: &tlw.Dut{Name: "test-dut"}, |
| command: "", |
| wantErr: true, |
| }, |
| { |
| name: "valid command no serial", |
| dut: &tlw.Dut{Name: "test-dut"}, |
| deviceSerial: "", |
| command: "getvar", |
| args: []string{"version"}, |
| wantErr: true, // Expect error because adbClient is not mocked |
| }, |
| { |
| name: "valid command with serial", |
| dut: &tlw.Dut{Name: "test-dut"}, |
| deviceSerial: "test-serial", |
| command: "getvar", |
| args: []string{"version"}, |
| wantErr: true, // Expect error because adbClient is not mocked |
| }, |
| } |
| for _, tt := range tests { |
| |
| t.Run(tt.name, func(t *testing.T) { |
| t.Parallel() |
| // We can't fully test this without mocks, so we mostly check the error path. |
| _, err := FastbootRun(ctx, tt.dut, tt.deviceSerial, 10*time.Second, tt.command, tt.args...) |
| if (err != nil) != tt.wantErr { |
| t.Errorf("FastbootRun() error = %v, wantErr %v", err, tt.wantErr) |
| return |
| } |
| }) |
| } |
| } |