Skip to content

Commit 44813a6

Browse files
committed
Added activate hook to activate a revisionKey
1 parent 62f99f4 commit 44813a6

File tree

3 files changed

+239
-22
lines changed

3 files changed

+239
-22
lines changed

index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,22 @@ module.exports = {
9393
.catch(_errorMessage.bind(this, ui));
9494
},
9595

96+
activate: function(context) {
97+
var deployment = context.deployment;
98+
var ui = deployment.ui;
99+
var config = deployment.config[this.name] || {};
100+
var redis = context.redisClient || new Redis(config);
101+
var revisionKey = this._resolveConfigValue('revisionKey', config, context);
102+
103+
return _beginActivateMessage(ui, revisionKey)
104+
.then(redis.activate.bind(redis, config.keyPrefix, revisionKey))
105+
.then(_activationSuccessMessage.bind(this, ui, revisionKey))
106+
.catch(_errorMessage.bind(this, ui));
107+
},
108+
96109
_resolvePipelineData: function(config, context) {
97110
config.revisionKey = config.revisionKey || function(context) {
98-
return context.deployment.commandLineArgs.revisionKey || context.deployment.revisionKey;
111+
return context.deployment.commandLineArgs.revisionKey || context.revisionKey;
99112
};
100113

101114
return Promise.resolve();

tests/unit/index-nodetest.js

Lines changed: 181 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ var Promise = require('ember-cli/lib/ext/promise');
44

55
var assert = require('ember-cli/tests/helpers/assert');
66

7+
function hooks(plugin) {
8+
return Object.keys(plugin).filter(function(key) {
9+
return (key !== 'name') && (key.charAt(0) !== '_') && (typeof plugin[key] === 'function');
10+
});
11+
}
12+
13+
var stubUi = { write: function() {}, writeLine: function() {} };
14+
var stubProject = {
15+
name: function(){
16+
return 'my-project';
17+
}
18+
};
19+
720
describe('redis plugin', function() {
821
var subject;
922

@@ -20,28 +33,24 @@ describe('redis plugin', function() {
2033
});
2134

2235
it('implements the correct hooks', function() {
23-
var result = subject.createDeployPlugin({
36+
var plugin = subject.createDeployPlugin({
2437
name: 'test-plugin'
2538
});
2639

27-
assert.equal(typeof result.configure, 'function');
28-
assert.equal(typeof result.upload, 'function');
40+
assert.equal(hooks(plugin).length, 3);
41+
assert.sameMembers(hooks(plugin), ['configure', 'upload', 'activate']);
2942
});
3043

31-
describe('willDeploy hook', function() {
44+
describe('configure hook', function() {
3245
it('resolves if config is ok', function() {
3346
var plugin = subject.createDeployPlugin({
3447
name: 'redis'
3548
});
3649

3750
var context = {
3851
deployment: {
39-
ui: { write: function() {}, writeLine: function() {} },
40-
project: {
41-
name: function(){
42-
return 'my-project';
43-
}
44-
},
52+
ui: stubUi,
53+
project: stubProject,
4554
config: {
4655
redis: {
4756
host: 'somehost',
@@ -53,6 +62,96 @@ describe('redis plugin', function() {
5362

5463
return assert.isFulfilled(plugin.configure.call(plugin, context))
5564
});
65+
66+
describe('resolving data from the pipeline', function() {
67+
it('uses the config data if it already exists', function() {
68+
var plugin = subject.createDeployPlugin({
69+
name: 'redis'
70+
});
71+
72+
var config = {
73+
host: 'somehost',
74+
port: 1234,
75+
revisionKey: '12345'
76+
};
77+
var context = {
78+
deployment: {
79+
ui: stubUi,
80+
project: stubProject,
81+
config: {
82+
redis: config
83+
}
84+
},
85+
86+
revisionKey: 'something-else'
87+
};
88+
89+
return assert.isFulfilled(plugin.configure.call(plugin, context))
90+
.then(function() {
91+
assert.equal(config.revisionKey, '12345');
92+
});
93+
});
94+
95+
it('uses the commandLineArgs value if it exists', function() {
96+
var plugin = subject.createDeployPlugin({
97+
name: 'redis'
98+
});
99+
100+
var config = {
101+
host: 'somehost',
102+
port: 1234
103+
};
104+
var context = {
105+
deployment: {
106+
ui: stubUi,
107+
project: stubProject,
108+
config: {
109+
redis: config
110+
},
111+
commandLineArgs: {
112+
revisionKey: 'abcd'
113+
}
114+
},
115+
116+
revisionKey: 'something-else'
117+
};
118+
119+
return assert.isFulfilled(plugin.configure.call(plugin, context))
120+
.then(function() {
121+
assert.typeOf(config.revisionKey, 'function');
122+
assert.equal(config.revisionKey(context), 'abcd');
123+
});
124+
})
125+
126+
it('uses the context value if it exists and commandLineArgs don\'t', function() {
127+
var plugin = subject.createDeployPlugin({
128+
name: 'redis'
129+
});
130+
131+
var config = {
132+
host: 'somehost',
133+
port: 1234
134+
};
135+
var context = {
136+
deployment: {
137+
ui: stubUi,
138+
project: stubProject,
139+
config: {
140+
redis: config
141+
},
142+
commandLineArgs: { }
143+
},
144+
145+
revisionKey: 'something-else'
146+
};
147+
148+
return assert.isFulfilled(plugin.configure.call(plugin, context))
149+
.then(function() {
150+
assert.typeOf(config.revisionKey, 'function');
151+
assert.equal(config.revisionKey(context), 'something-else');
152+
});
153+
})
154+
});
56155
});
57156

58157
describe('upload hook', function() {
@@ -66,34 +165,95 @@ describe('redis plugin', function() {
66165

67166
context = {
68167
redisClient: {
69-
upload: function() {
70-
return Promise.resolve('redis-key');
168+
upload: function(keyPrefix, revisionKey) {
169+
return Promise.resolve(keyPrefix + ':' + revisionKey);
71170
}
72171
},
73172
tag: 'some-tag',
74173
deployment: {
75-
ui: { write: function() {} },
76-
project: { name: function() { return 'test-project'; } },
174+
ui: stubUi,
175+
project: stubProject,
77176
config: {
78177
redis: {
178+
keyPrefix: 'test-prefix',
79179
filePattern: 'tests/index.html',
180+
revisionKey: '123abc'
80181
}
81182
}
82-
}
183+
},
83184
};
84185
});
85186

86-
it('uploads the index to redis', function() {
187+
it('uploads the index', function() {
87188
return assert.isFulfilled(plugin.upload.call(plugin, context))
88189
.then(function(result) {
89-
assert.deepEqual(result, { redisKey: 'redis-key' });
190+
assert.deepEqual(result, { redisKey: 'test-prefix:123abc' });
90191
});
91192
});
193+
});
92194

93-
it('returns the uploaded key', function() {
94-
return assert.isFulfilled(plugin.upload.call(plugin, context))
95-
.then(function(result) {
96-
assert.deepEqual(result.redisKey, 'redis-key');
195+
describe('activate hook', function() {
196+
it('activates revision', function() {
197+
var activateCalled = false;
198+
199+
var plugin = subject.createDeployPlugin({
200+
name: 'redis'
201+
});
202+
203+
var context = {
204+
redisClient: {
205+
activate: function() {
206+
activateCalled = true;
207+
}
208+
},
209+
tag: 'some-tag',
210+
deployment: {
211+
ui: stubUi,
212+
project: stubProject,
213+
config: {
214+
redis: {
215+
keyPrefix: 'test-prefix',
216+
filePattern: 'tests/index.html',
217+
revisionKey: '123abc'
218+
}
219+
}
220+
}
221+
};
222+
223+
return assert.isFulfilled(plugin.activate.call(plugin, context))
224+
.then(function() {
225+
assert.ok(activateCalled);
226+
});
227+
});
228+
229+
it('rejects if an error is thrown when activating', function() {
230+
var plugin = subject.createDeployPlugin({
231+
name: 'redis'
232+
});
233+
234+
var context = {
235+
redisClient: {
236+
activate: function() {
237+
return Promise.reject('some-error');
238+
}
239+
},
240+
tag: 'some-tag',
241+
deployment: {
242+
ui: stubUi,
243+
project: stubProject,
244+
config: {
245+
redis: {
246+
keyPrefix: 'test-prefix',
247+
filePattern: 'tests/index.html',
248+
revisionKey: '123abc'
249+
}
250+
}
251+
}
252+
};
253+
254+
return assert.isRejected(plugin.activate.call(plugin, context))
255+
.then(function(error) {
256+
assert.equal(error, 'some-error');
97257
});
98258
});
99259
});

tests/unit/lib/redis-nodetest.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,48 @@ describe('redis', function() {
169169
});
170170
});
171171
});
172+
173+
describe('#activate', function() {
174+
it('rejects if the revisionKey doesn\t exist in list of uploaded revisions', function() {
175+
var recentRevisions = ['a', 'b', 'c'];
176+
177+
var redis = new Redis({
178+
redisClient: {
179+
lrange: function() {
180+
return recentRevisions;
181+
}
182+
}
183+
});
184+
185+
var promise = redis.activate('key-prefix', 'revision-key');
186+
return assert.isRejected(promise)
187+
.then(function(error) {
188+
assert.equal(error, '`revision-key` is not a valid revision key');
189+
});
190+
});
191+
192+
it('resolves and sets the current revision to the revision key provided', function() {
193+
var recentRevisions = ['a', 'b', 'c'];
194+
var redisKey, redisValue;
195+
196+
var redis = new Redis({
197+
redisClient: {
198+
lrange: function() {
199+
return recentRevisions;
200+
},
201+
set: function(key, value) {
202+
redisKey = key;
203+
redisValue = value;
204+
}
205+
}
206+
});
207+
208+
var promise = redis.activate('key-prefix', 'c');
209+
return assert.isFulfilled(promise)
210+
.then(function() {
211+
assert.equal(redisKey, 'key-prefix:current');
212+
assert.equal(redisValue, 'c');
213+
});
214+
});
215+
});
172216
});

0 commit comments

Comments
 (0)