-
Notifications
You must be signed in to change notification settings - Fork 0
/
anser_test.go
129 lines (99 loc) · 3.11 KB
/
anser_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package anser
import (
"context"
"fmt"
"testing"
"time"
"github.com/cdr/amboy"
"github.com/cdr/amboy/queue"
"github.com/deciduosity/anser/mock"
"github.com/deciduosity/anser/model"
"github.com/pkg/errors"
"github.com/stretchr/testify/suite"
)
type ApplicationSuite struct {
app *Application
env *mock.Environment
suite.Suite
}
func TestApplicationSuite(t *testing.T) {
suite.Run(t, new(ApplicationSuite))
}
func (s *ApplicationSuite) SetupSuite() {}
func (s *ApplicationSuite) TearDownSuite() {}
func (s *ApplicationSuite) SetupTest() {
s.env = mock.NewEnvironment()
s.app = &Application{env: s.env}
}
func (s *ApplicationSuite) TearDownTest() {}
func (s *ApplicationSuite) TestSetupErrorsWhenHasSetupRuns() {
s.NoError(s.app.Setup(s.env))
for i := 0; i < 10; i++ {
s.Error(s.app.Setup(s.env))
}
}
func (s *ApplicationSuite) TestApplicationSetupInternals() {
s.app.hasSetup = true
s.Error(s.app.Setup(s.env))
s.app.hasSetup = false
s.Error(s.app.Setup(nil))
}
func (s *ApplicationSuite) TestSetupCanGetNetwork() {
s.env.NetworkError = errors.New("problem")
err := s.app.Setup(s.env)
s.Error(s.app.Setup(s.env))
s.Equal(errors.Cause(err), s.env.NetworkError)
}
func (s *ApplicationSuite) TestRunMethodErrorsIfQueueHasError() {
s.env.QueueError = errors.New("problem")
ctx := context.Background()
s.NoError(s.app.Setup(s.env))
err := s.app.Run(ctx)
s.Error(err)
s.Equal(errors.Cause(err), s.env.QueueError)
}
func (s *ApplicationSuite) TestRunErrorsWithCanceledContext() {
s.NoError(s.app.Setup(s.env))
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := s.app.Run(ctx)
s.Error(err)
s.Equal(err.Error(), "migration operation canceled")
}
func (s *ApplicationSuite) TestRunDoesNotErrorWithDryRun() {
s.env.Queue = queue.NewLocalLimitedSize(2, 128)
s.NoError(s.app.Setup(s.env))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s.NoError(s.env.Queue.Start(ctx))
s.app.Options.DryRun = true
s.NoError(s.app.Run(ctx))
}
func (s *ApplicationSuite) TestLimitIsRespected() {
opts := model.GeneratorOptions{}
job := NewManualMigrationGenerator(s.env, opts, "").(*manualMigrationGenerator)
job.NS = model.Namespace{DB: "foo", Collection: "bar"}
job.Migrations = []*manualMigrationJob{
NewManualMigration(s.env, model.Manual{}).(*manualMigrationJob),
NewManualMigration(s.env, model.Manual{}).(*manualMigrationJob),
NewManualMigration(s.env, model.Manual{}).(*manualMigrationJob),
}
for idx, j := range job.Migrations {
j.SetID(fmt.Sprintf("job-test-%d", idx))
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s.env.Queue = queue.NewLocalLimitedSize(2, 128)
s.env.Network = mock.NewDependencyNetwork()
s.NoError(s.env.Queue.Start(ctx))
s.NoError(s.env.Queue.Put(ctx, job))
s.Equal(1, s.env.Queue.Stats(ctx).Total)
amboy.WaitInterval(ctx, s.env.Queue, 10*time.Millisecond)
num, err := addMigrationJobs(ctx, s.env.Queue, false, 2)
s.NoError(err)
amboy.WaitInterval(ctx, s.env.Queue, 100*time.Millisecond)
// two is the limit:
s.Equal(2, num)
// one generator plus two jobs:
s.Equal(3, s.env.Queue.Stats(ctx).Total)
}