1 |
|
2 |
|
3 | const _ = require('lodash');
|
4 | const expect = require('chai').expect;
|
5 | const Promise = require('bluebird');
|
6 | const TestConfig = require('../config');
|
7 | const Utils = require('../utils');
|
8 | const Subtask = require('../../app/models/subtask');
|
9 | const Subtasks = require('../../app/services/subtasks');
|
10 | const Tasks = require('../../app/services/tasks');
|
11 | const createEsClient = require('../../config/elasticsearch');
|
12 | const createRedisClient = require('../../config/redis');
|
13 | const config = require('../../config/index');
|
14 |
|
15 | const log = config.log;
|
16 |
|
17 | Promise.longStackTraces();
|
18 | Promise.onPossiblyUnhandledRejection((error) => log.error('Likely error: ', error.stack));
|
19 |
|
20 | const TASK_NAME = 'testTask';
|
21 |
|
22 | describe('tasks service', function () {
|
23 | this.timeout(5000);
|
24 |
|
25 | let source = null;
|
26 | let redis = null;
|
27 | let utils = null;
|
28 | let tasks = null;
|
29 | let subtasks = null;
|
30 |
|
31 | before((done) => {
|
32 | source = createEsClient(TestConfig.elasticsearch.source);
|
33 | redis = createRedisClient(TestConfig.redis.host, TestConfig.redis.port);
|
34 | tasks = new Tasks(redis);
|
35 | subtasks = new Subtasks(redis);
|
36 | utils = new Utils();
|
37 |
|
38 | utils.deleteAllTemplates(source)
|
39 | .finally(() => utils.deleteAllIndices(source))
|
40 | .finally(() => redis.flushdb())
|
41 | .finally(() => done());
|
42 | });
|
43 |
|
44 | afterEach((done) => {
|
45 | utils.deleteAllTemplates(source)
|
46 | .finally(() => utils.deleteAllIndices(source))
|
47 | .finally(() => redis.flushdb())
|
48 | .finally(() => done());
|
49 | });
|
50 |
|
51 | it('should check that source and destination exist', (done) => {
|
52 | const task = {
|
53 | source: TestConfig.elasticsearch.source,
|
54 | destination: TestConfig.elasticsearch.destination,
|
55 | transfer: {
|
56 | documents: {
|
57 | fromIndices: '*'
|
58 | }
|
59 | }
|
60 | };
|
61 |
|
62 | tasks.ensureSourceAndDestExist(task.source, task.destination)
|
63 | .then(() => done())
|
64 | .catch((err) => err ? done(err) : done('fail'));
|
65 | });
|
66 |
|
67 | it('can pass path', (done) => {
|
68 | const task = {
|
69 | source: {host: 'es', port: 9200, path: '/'},
|
70 | destination: TestConfig.elasticsearch.destination,
|
71 | transfer: {
|
72 | documents: {
|
73 | fromIndices: '*'
|
74 | }
|
75 | }
|
76 | };
|
77 |
|
78 | tasks.ensureSourceAndDestExist(task.source, task.destination)
|
79 | .then(() => done())
|
80 | .catch((err) => {
|
81 | expect(err).to.match(/source elasticsearch/);
|
82 | done();
|
83 | });
|
84 | });
|
85 |
|
86 | it('should fail if source doesnt exist and destination does', (done) => {
|
87 | const task = {
|
88 | source: {host: 'badhost', port: 9200},
|
89 | destination: TestConfig.elasticsearch.destination,
|
90 | transfer: {
|
91 | documents: {
|
92 | fromIndices: '*'
|
93 | }
|
94 | }
|
95 | };
|
96 |
|
97 | tasks.ensureSourceAndDestExist(task.source, task.destination)
|
98 | .then(() => done('fail'))
|
99 | .catch((err) => {
|
100 | expect(err).to.match(/source elasticsearch/);
|
101 | done();
|
102 | });
|
103 | });
|
104 |
|
105 | it('should fail if destination doesnt exist and source does', (done) => {
|
106 | const task = {
|
107 | source: TestConfig.elasticsearch.source,
|
108 | destination: {host: 'badhost', port: 9200},
|
109 | transfer: {
|
110 | documents: {
|
111 | fromIndices: '*'
|
112 | }
|
113 | }
|
114 | };
|
115 |
|
116 | tasks.ensureSourceAndDestExist(task.source, task.destination)
|
117 | .then(() => done('fail'))
|
118 | .catch((err) => {
|
119 | expect(err).to.match(/destination elasticsearch/);
|
120 | done();
|
121 | });
|
122 | });
|
123 |
|
124 | it('should add task and create subtasks in backlog', (done) => {
|
125 | const task = {
|
126 | source: TestConfig.elasticsearch.source,
|
127 | destination: TestConfig.elasticsearch.destination,
|
128 | transfer: {
|
129 | documents: {
|
130 | fromIndices: '*'
|
131 | }
|
132 | }
|
133 | };
|
134 |
|
135 | utils.addData(source)
|
136 | .then(() => tasks.add(TASK_NAME, task))
|
137 | .then(() => subtasks.getBacklog(TASK_NAME))
|
138 | .then((backlogSubtasks) => expect(backlogSubtasks.length).to.be.equals(3))
|
139 | .then(() => tasks.getAll())
|
140 | .then((allTasks) => {
|
141 | expect(_.size(allTasks)).to.be.equals(1);
|
142 | expect(allTasks[0]).to.be.equals(TASK_NAME);
|
143 | })
|
144 | .then(() => done())
|
145 | .catch(done);
|
146 | });
|
147 |
|
148 | it('should return list of tasks', (done) => {
|
149 | const task = {
|
150 | source: TestConfig.elasticsearch.source,
|
151 | destination: TestConfig.elasticsearch.destination,
|
152 | transfer: {
|
153 | documents: {
|
154 | fromIndices: '*'
|
155 | }
|
156 | }
|
157 | };
|
158 |
|
159 | tasks.add(TASK_NAME, task)
|
160 | .then(() => tasks.getAll())
|
161 | .then((taskNames) => expect(taskNames).to.eql([TASK_NAME]))
|
162 | .then(() => done())
|
163 | .catch(done);
|
164 | });
|
165 |
|
166 | it('should return empty list when there are no tasks', (done) => {
|
167 | tasks.getAll()
|
168 | .then((taskNames) => expect(taskNames).to.be.empty)
|
169 | .then(() => done())
|
170 | .catch(done);
|
171 | });
|
172 |
|
173 | it('should log and return errors', (done) => {
|
174 | const subtask = {
|
175 | source: TestConfig.elasticsearch.source,
|
176 | destination: TestConfig.elasticsearch.destination,
|
177 | transfer: {
|
178 | documents: {
|
179 | index: 'myindex1',
|
180 | type: 'mytype1',
|
181 | minSize: 10,
|
182 | maxSize: 100,
|
183 | }
|
184 | },
|
185 | count: 10
|
186 | };
|
187 |
|
188 | tasks.logError(TASK_NAME, subtask, 'something broke').delay(5)
|
189 | .then(() => tasks.logError(TASK_NAME, subtask, 'something else broke'))
|
190 | .then(() => tasks.errors(TASK_NAME))
|
191 | .then((errors) => {
|
192 | expect(errors.length).to.be.equals(2);
|
193 | expect(errors[0].subtask).to.be.an.instanceof(Subtask);
|
194 | expect(errors[0].subtask.source).to.eql(subtask.source);
|
195 | expect(errors[0].subtask.destination).to.eql(subtask.destination);
|
196 | expect(errors[0].subtask.transfer).to.eql(subtask.transfer);
|
197 | expect(errors[0].subtask.count).to.be.equals(subtask.count);
|
198 | expect(errors[0].message).to.be.equals('something broke');
|
199 |
|
200 | expect(errors[1].subtask).to.be.an.instanceof(Subtask);
|
201 | expect(errors[1].subtask.source).to.eql(subtask.source);
|
202 | expect(errors[1].subtask.destination).to.eql(subtask.destination);
|
203 | expect(errors[1].subtask.transfer).to.eql(subtask.transfer);
|
204 | expect(errors[1].subtask.count).to.be.equals(subtask.count);
|
205 | expect(errors[1].message).to.be.equals('something else broke');
|
206 | })
|
207 | .then(() => done())
|
208 | .catch(done);
|
209 | });
|
210 |
|
211 | });
|