TL;DR - after making changes in this directory run
make trees make testf make test
It is recommended to first run the fast tests using make testf. If those pass, then run the full test suite using make test.
And upload your change to gerrit with
git push origin HEAD:refs/for/main
Exec functions in the internal/execs subdirectories have the signature func(ctx context.Context, i execs.ExecInfo) error. To test these functions in isolation, use the GoMock generated mocks.
go.chromium.org/infra/cros/recovery/internal/execs/mocks/execs.goexecs.ExecInfointernal/execs/execs.gogo.chromium.org/infra/cros/recovery/internal/components/mocks/components.gointernal/components/components.go:mocks.MockSSHRunResponse: Mocks components.SSHRunResponsemocks.MockHostAccess: Mocks components.HostAccessmocks.MockServod: Mocks components.Servodmocks.MockCMDExecutor: Mocks components.CMDExecutorAdditionally, the following function types from components can be easily mocked by providing a test function with the same signature:
components.Runner: Mock with func(context.Context, time.Duration, string, ...string) (string, error)components.Pinger: Mock with func(context.Context, int) errorImport necessary packages:
import ( "testing" "github.com/golang/mock/gomock" "go.chromium.org/infra/cros/recovery/internal/execs" execs_mocks "go.chromium.org/infra/cros/recovery/internal/execs/mocks"\n // Optional: component mocks // "go.chromium.org/infra/cros/recovery/internal/components" // components_mocks "go.chromium.org/infra/cros/recovery/internal/components/mocks" )
Setup Controller and Mocks:
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx := context.Background()
mockInfo := execs_mocks.NewMockExecInfo(ctrl)
Set Expectations: Use mockInfo.EXPECT() to define expected calls and return values for any ExecInfo methods your function uses. Chain calls to specify arguments and return values.
// Example: Expect GetActionArgs to be called once and return ParsedArgs mockInfo.EXPECT().GetActionArgs(gomock.Any()).Return(execs.ParsedArgs{"my_arg": "some_value"}).Times(1) // Example: Expect NewServod to be called and return a mocked Servod // mockServo := components_mocks.NewMockServod(ctrl) // mockServo.EXPECT().Set(gomock.Any(), "some_cmd", gomock.Any()).Return(nil) // mockInfo.EXPECT().NewServod().Return(mockServo)
Call and Assert: Execute the function under test with mockInfo and assert the results.
err := myExecFunc(ctx, mockInfo)
if err != nil {
t.Errorf("myExecFunc() returned unexpected error: %v", err)
}
Refer to the gomock documentation for more details on setting expectations and matching arguments. See existing tests in the project for practical examples.
To test changes made to the recovery configurations in this directory, the easiest method is to use the test_labstation_repair.sh script.
test_labstation_repair.shThis script automates the process of building the necessary tools, extracting the current configuration, and scheduling a repair task on a specified labstation. This ensures your local changes are applied.
Usage:
From the root of the infra/go repository, run:
./src/infra/cros/recovery/test_labstation_repair.sh <labstation_hostname>
The script will:
paris tool.labstation device configuration to /tmp/labstation_config.json.mallet tool.mallet recovery task on the provided labstation hostname using the extracted configuration.If you need more control, you can perform the steps manually:
go build in ../cmd/paris and ../cmd/mallet.../cmd/paris/paris config -device labstation > /tmp/labstation_config.json../cmd/mallet/mallet recovery -latest -config /tmp/labstation_config.json <hostname>Refer to go/fleet-recovery-developer for more detailed guides.