-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration_job_stream_test.go
107 lines (98 loc) · 3.23 KB
/
migration_job_stream_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
package anser
import (
"context"
"testing"
"github.com/cdr/amboy/registry"
"github.com/deciduosity/anser/mock"
"github.com/deciduosity/anser/model"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStreamMigrationJob(t *testing.T) {
const (
jobTypeName = "stream-migration"
processorTypeName = "processor-name"
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
env := mock.NewEnvironment()
mh := &MigrationHelperMock{Environment: env}
migration := NewStreamMigration(env, model.Stream{})
require.Equal(t, jobTypeName, migration.Type().Name)
factory, err := registry.GetJobFactory(jobTypeName)
require.NoError(t, err)
t.Run("Factory", func(t *testing.T) {
// first test the factory
job, ok := factory().(*streamMigrationJob)
require.True(t, ok)
assert.Equal(t, job.Type().Name, jobTypeName)
})
t.Run("ProcessorNotDefined", func(t *testing.T) {
job := factory().(*streamMigrationJob)
// first we start off with a "processor not defined error"
job.MigrationHelper = mh
job.Definition.ProcessorName = processorTypeName
job.Run(ctx)
assert.True(t, job.Status().Completed)
require.True(t, job.HasErrors())
err := job.Error()
assert.Error(t, err)
assert.Contains(t, err.Error(), job.Definition.ProcessorName)
})
t.Run("Processor", func(t *testing.T) {
processor := &mock.Processor{}
env.ProcessorRegistry[processorTypeName] = processor
defer func() { delete(env.ProcessorRegistry, processorTypeName) }()
t.Run("BadIterator", func(t *testing.T) {
// now we find a poorly configured/implemented processor
job := factory().(*streamMigrationJob)
job.Definition.ProcessorName = processorTypeName
job.MigrationHelper = mh
job.Run(ctx)
assert.True(t, job.Status().Completed)
require.True(t, job.HasErrors())
err := job.Error()
assert.Error(t, err)
assert.Contains(t, err.Error(), "could not return iterator")
})
t.Run("GoodIterator", func(t *testing.T) {
// reset for next case.
// now we have a properly configured job iterator
job := factory().(*streamMigrationJob)
job.MigrationHelper = mh
job.Definition.ProcessorName = processorTypeName
processor.Cursor = &mock.Cursor{}
// TODO write mock cursor
job.Run(ctx)
assert.False(t, job.HasErrors())
assert.True(t, job.Status().Completed)
})
t.Run("IteratorError", func(t *testing.T) {
// reset for the next case:
// we have an iterator that returns an error.
job := factory().(*streamMigrationJob)
job.MigrationHelper = mh
job.Definition.ProcessorName = processorTypeName
processor.MigrateError = errors.New("has error 123")
job.Run(ctx)
assert.True(t, job.Status().Completed)
require.True(t, job.HasErrors())
err := job.Error()
assert.Error(t, err)
assert.Contains(t, err.Error(), "has error 123")
})
t.Run("NoClient", func(t *testing.T) {
env.ClientError = errors.New("no client")
job := factory().(*streamMigrationJob)
job.MigrationHelper = mh
job.Definition.ProcessorName = processorTypeName
job.Run(ctx)
assert.True(t, job.Status().Completed)
require.True(t, job.HasErrors())
err := job.Error()
assert.Error(t, err)
assert.Contains(t, err.Error(), "no client")
})
})
}