-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmod_test.go
109 lines (85 loc) · 2.15 KB
/
mod_test.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
package simnet
import (
"bytes"
"context"
"errors"
"os"
"testing"
"github.com/stretchr/testify/require"
"go.dedis.ch/simnet/sim"
)
type testRound struct{}
func (t testRound) Before(simio sim.IO, nodes []sim.NodeInfo) error {
return nil
}
func (t testRound) Execute(simio sim.IO, nodes []sim.NodeInfo) error {
return nil
}
func (t testRound) After(simio sim.IO, nodes []sim.NodeInfo) error {
return nil
}
type testStrategy struct {
errDeploy error
errExecute error
errStats error
errClean error
}
func (e *testStrategy) Option(sim.Option) {}
func (e *testStrategy) Deploy(context.Context, sim.Round) error {
if e.errDeploy != nil {
return e.errDeploy
}
return nil
}
func (e *testStrategy) Execute(context.Context, sim.Round) error {
if e.errExecute != nil {
return e.errExecute
}
return nil
}
func (e *testStrategy) WriteStats(ctx context.Context, filepath string) error {
if e.errStats != nil {
return e.errStats
}
return nil
}
func (e *testStrategy) Clean(context.Context) error {
if e.errClean != nil {
return e.errClean
}
return nil
}
func TestSimulation_Run(t *testing.T) {
stry := &testStrategy{}
sim := NewSimulation(testRound{}, stry)
buffer := new(bytes.Buffer)
sim.out = buffer
args := []string{os.Args[0]}
require.NoError(t, sim.Run(args))
err := sim.Run([]string{})
require.Error(t, err)
require.True(t, errors.Is(err, errMissingArgs))
stry.errDeploy = errors.New("deploy")
err = sim.Run(args)
require.Error(t, err)
require.True(t, errors.Is(err, stry.errDeploy))
stry.errDeploy = nil
stry.errExecute = errors.New("execute")
err = sim.Run(args)
require.Error(t, err)
require.True(t, errors.Is(err, stry.errExecute))
stry.errExecute = nil
stry.errStats = errors.New("stats")
err = sim.Run(args)
require.Error(t, err)
require.True(t, errors.Is(err, stry.errStats))
stry.errStats = nil
stry.errClean = errors.New("clean")
err = sim.Run(args)
require.NoError(t, err)
require.Contains(t, buffer.String(), "An error occurred during cleaning")
args = []string{os.Args[0], "--do-stats"}
stry.errStats = errors.New("oops")
err = sim.Run(args)
require.EqualError(t, err, "couldn't write statistics: oops")
}