aboutsummaryrefslogtreecommitdiffstats
path: root/vm/proxyapp/proxyappclient_mocks_test.go
blob: cfd8fcf162642bfda267f40726ae431d763a5e72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2022 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package proxyapp

import (
	"context"
	"testing"

	"github.com/google/syzkaller/vm/proxyapp/mocks"
	"github.com/google/syzkaller/vm/proxyapp/proxyrpc"
	"github.com/stretchr/testify/mock"
)

var (
	_ subProcessCmd              = &mocks.SubProcessCmd{}
	_ proxyrpc.ProxyAppInterface = &mocks.ProxyAppInterface{}
)

type mockCommandRunner struct {
	*mocks.SubProcessCmd
	ctx          context.Context
	onWaitCalled chan bool
}

func makeMockCommandRunner(t *testing.T) (*mockCommandRunner, *proxyAppParams) {
	cmdRunner := &mockCommandRunner{
		SubProcessCmd: mocks.NewSubProcessCmd(t),
		onWaitCalled:  make(chan bool, 1),
	}

	params := makeTestParams()
	params.CommandRunner = func(ctx context.Context, cmd string, params ...string) subProcessCmd {
		cmdRunner.ctx = ctx
		return cmdRunner
	}
	return cmdRunner, params
}

func (cmd *mockCommandRunner) Wait() error {
	cmd.onWaitCalled <- true
	return cmd.SubProcessCmd.Wait()
}

type mockProxyAppInterface struct {
	*mocks.ProxyAppInterface
	OnLogsReceived chan bool
}

type tNewProxyAppInterface interface {
	mock.TestingT
	Cleanup(func())
}

func makeMockProxyAppInterface(t tNewProxyAppInterface) *mockProxyAppInterface {
	return &mockProxyAppInterface{
		ProxyAppInterface: mocks.NewProxyAppInterface(t),
		OnLogsReceived:    make(chan bool, 1), // 1 is enough as we read it just once
	}
}