-
Notifications
You must be signed in to change notification settings - Fork 568
/
mock_env.go
72 lines (59 loc) · 1.94 KB
/
mock_env.go
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
61
62
63
64
65
66
67
68
69
70
71
72
package testpachd
import (
"context"
"testing"
"golang.org/x/sync/errgroup"
"github.com/pachyderm/pachyderm/v2/src/internal/client"
"github.com/pachyderm/pachyderm/v2/src/internal/pctx"
"github.com/pachyderm/pachyderm/v2/src/internal/require"
"github.com/pachyderm/pachyderm/v2/src/internal/testetcd"
)
// MockEnv contains the basic setup for running end-to-end pachyderm tests
// entirely locally within the test process. It provides a temporary directory
// for storing data, an embedded etcd server with a connected client, as well as
// a local mock pachd instance which allows a test to hook into any pachd calls.
type MockEnv struct {
testetcd.Env
MockPachd *MockPachd
PachClient *client.APIClient
}
// NewMockEnv constructs a MockEnv for testing, which will be destroyed at the
// end of the test.
func NewMockEnv(rctx context.Context, t testing.TB, options ...InterceptorOption) *MockEnv {
etcdEnv := testetcd.NewEnv(rctx, t)
// Use an error group with a cancelable context to supervise every component
// and cancel everything if one fails
ctx, cancel := pctx.WithCancel(rctx)
eg, ctx := errgroup.WithContext(ctx)
t.Cleanup(func() {
require.NoError(t, eg.Wait())
})
t.Cleanup(cancel)
mockEnv := &MockEnv{Env: *etcdEnv}
mockEnv.Context = ctx
var err error
mockEnv.MockPachd, err = NewMockPachd(mockEnv.Context, 0, options...)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, mockEnv.MockPachd.Close())
})
eg.Go(func() error {
return errorWait(ctx, mockEnv.MockPachd.Err())
})
mockEnv.PachClient, err = client.NewFromURI(ctx, mockEnv.MockPachd.Addr.String())
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, mockEnv.PachClient.Close())
})
// TODO: supervise the PachClient connection and error the errgroup if they
// go down
return mockEnv
}
func errorWait(ctx context.Context, errChan <-chan error) error {
select {
case <-ctx.Done():
return nil
case err := <-errChan:
return err
}
}