-
Notifications
You must be signed in to change notification settings - Fork 74
/
mock.go
118 lines (104 loc) 路 2.84 KB
/
mock.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package sidecar
import (
"context"
"errors"
"math/rand"
"os"
"strconv"
gosync "sync"
"time"
"github.com/testground/sdk-go/network"
"github.com/testground/sdk-go/runtime"
"github.com/testground/sdk-go/sync"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// A MockReactor with the information for a single instance.
// A typicial reactor watches the docker socket for containers. This one creates enough information
// for a single instace, or a single reaction for testing purposes. A mock network and inmem SDK
// client are instantiated here as well.
// To use, instantiate the NewMockReactor and run Handle(). Any messages passed through the inmem
// SDK client are handled as though the come from the mocked instance.
func NewMockReactor() (Reactor, error) {
unique := strconv.Itoa(rand.Int())
params := runtime.RunParams{
TestCase: "TestCase" + unique,
TestGroupID: "TestGroupID" + unique,
TestGroupInstanceCount: 1,
TestInstanceCount: 1,
TestInstanceRole: "TestInstanceRole" + unique,
TestPlan: "TestPlan" + unique,
TestRun: unique,
TestSidecar: true,
}
runenv := runtime.NewRunEnv(params)
network := NewMockNetwork()
client := sync.NewInmemClient()
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
return &MockReactor{
RunParams: ¶ms,
RunEnv: runenv,
Network: network,
Client: client,
Hostname: hostname,
}, nil
}
// Reactor
type MockReactor struct {
RunParams *runtime.RunParams
RunEnv *runtime.RunEnv
Network *MockNetwork
Client sync.Client
Hostname string
}
func (*MockReactor) Close() error { return nil }
func (r *MockReactor) Handle(ctx context.Context, handler InstanceHandler) error {
inst, err := NewInstance(r.Client, r.RunEnv, r.Hostname, r.Network)
if err != nil {
return err
}
return handler(ctx, inst)
}
func NewMockNetwork() *MockNetwork {
active := map[string]*network.Config{"default": {}}
configured := make([]*network.Config, 0)
mux := gosync.Mutex{}
return &MockNetwork{
Active: active,
Configured: configured,
Closed: false,
L: &mux,
}
}
// Network
type MockNetwork struct {
Active map[string]*network.Config // A map of *active* networks.
Configured []*network.Config // A list of all the configurations we've seen
Closed bool
L gosync.Locker
}
func (m *MockNetwork) Close() error {
m.Closed = true
return nil
}
func (m *MockNetwork) ConfigureNetwork(ctx context.Context, cfg *network.Config) error {
if m.Closed {
return errors.New("mock network is closed.")
}
m.L.Lock()
defer m.L.Unlock()
m.Configured = append(m.Configured, cfg)
m.Active[cfg.Network] = cfg
return nil
}
func (m *MockNetwork) ListActive() []string {
var active []string
for k := range m.Active {
active = append(active, k)
}
return active
}