github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/vm/proxyapp/proxyappclient_mocks_test.go (about)

     1  // Copyright 2022 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package proxyapp
     5  
     6  //go:generate ../../tools/mockery.sh --name subProcessCmd --exported
     7  //go:generate ../../tools/mockery.sh --name ProxyAppInterface -r
     8  
     9  import (
    10  	"context"
    11  	"testing"
    12  
    13  	"github.com/google/syzkaller/vm/proxyapp/mocks"
    14  	"github.com/google/syzkaller/vm/proxyapp/proxyrpc"
    15  	"github.com/stretchr/testify/mock"
    16  )
    17  
    18  var (
    19  	_ subProcessCmd              = &mocks.SubProcessCmd{}
    20  	_ proxyrpc.ProxyAppInterface = &mocks.ProxyAppInterface{}
    21  )
    22  
    23  type mockCommandRunner struct {
    24  	*mocks.SubProcessCmd
    25  	ctx          context.Context
    26  	onWaitCalled chan bool
    27  }
    28  
    29  func makeMockCommandRunner(t *testing.T) (*mockCommandRunner, *proxyAppParams) {
    30  	cmdRunner := &mockCommandRunner{
    31  		SubProcessCmd: mocks.NewSubProcessCmd(t),
    32  		onWaitCalled:  make(chan bool, 1),
    33  	}
    34  
    35  	params := makeTestParams()
    36  	params.CommandRunner = func(ctx context.Context, cmd string, params ...string) subProcessCmd {
    37  		cmdRunner.ctx = ctx
    38  		return cmdRunner
    39  	}
    40  	return cmdRunner, params
    41  }
    42  
    43  func (cmd *mockCommandRunner) Wait() error {
    44  	cmd.onWaitCalled <- true
    45  	return cmd.SubProcessCmd.Wait()
    46  }
    47  
    48  type mockProxyAppInterface struct {
    49  	*mocks.ProxyAppInterface
    50  	OnLogsReceived chan bool
    51  }
    52  
    53  type tNewProxyAppInterface interface {
    54  	mock.TestingT
    55  	Cleanup(func())
    56  }
    57  
    58  func makeMockProxyAppInterface(t tNewProxyAppInterface) *mockProxyAppInterface {
    59  	return &mockProxyAppInterface{
    60  		ProxyAppInterface: mocks.NewProxyAppInterface(t),
    61  		OnLogsReceived:    make(chan bool, 1), // 1 is enough as we read it just once
    62  	}
    63  }