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.