cronicle 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,365 @@
1
+ describe 'Cronicle::Client#apply (update)' do
2
+ before do
3
+ on TARGET_HOSTS do |ssh_options|
4
+ user = ssh_options[:user]
5
+
6
+ set_crontab user, <<-CRON.undent
7
+ FOO=bar
8
+ ZOO=baz
9
+ 1 1 1 1 1 echo #{user} > /dev/null
10
+ CRON
11
+
12
+ set_crontab :root, <<-CRON.undent
13
+ FOO=bar
14
+ ZOO=baz
15
+ 1 1 1 1 1 echo root > /dev/null
16
+ CRON
17
+ end
18
+
19
+ cronicle(:apply) { <<-RUBY.undent }
20
+ on servers: /.*/ do
21
+ job :foo, user: :root, schedule: '1 2 * * *' do
22
+ puts `uname`
23
+ puts `whoami`
24
+ end
25
+ end
26
+
27
+ on servers: /.*/ do
28
+ job :bar, user: :root, schedule: :@hourly, content: <<-SH.undent
29
+ #!/bin/sh
30
+ echo hello
31
+ SH
32
+ end
33
+
34
+ on servers: /amazon_linux/ do
35
+ job :foo, user: 'ec2-user', schedule: '1 * * * *' do
36
+ puts 100
37
+ end
38
+ end
39
+
40
+ on servers: /ubuntu/ do
41
+ job :foo, user: :ubuntu, schedule: :@daily do
42
+ puts 200
43
+ end
44
+ end
45
+ RUBY
46
+ end
47
+
48
+ let(:amzn_crontab_orig) do
49
+ {
50
+ "/var/spool/cron/ec2-user" =>
51
+ "FOO=bar
52
+ ZOO=baz
53
+ 1 1 1 1 1 echo ec2-user > /dev/null
54
+ 1 * * * *\t/var/lib/cronicle/libexec/ec2-user/foo 2>&1 | logger -t cronicle/ec2-user/foo
55
+ ",
56
+ "/var/spool/cron/root" =>
57
+ "FOO=bar
58
+ ZOO=baz
59
+ 1 1 1 1 1 echo root > /dev/null
60
+ 1 2 * * *\t/var/lib/cronicle/libexec/root/foo 2>&1 | logger -t cronicle/root/foo
61
+ @hourly\t/var/lib/cronicle/libexec/root/bar 2>&1 | logger -t cronicle/root/bar
62
+ "
63
+ }
64
+ end
65
+
66
+ let(:ubuntu_crontab_orig) do
67
+ {
68
+ "/var/spool/cron/crontabs/root" =>
69
+ "FOO=bar
70
+ ZOO=baz
71
+ 1 1 1 1 1 echo root > /dev/null
72
+ 1 2 * * *\t/var/lib/cronicle/libexec/root/foo 2>&1 | logger -t cronicle/root/foo
73
+ @hourly\t/var/lib/cronicle/libexec/root/bar 2>&1 | logger -t cronicle/root/bar
74
+ ",
75
+ "/var/spool/cron/crontabs/ubuntu" =>
76
+ "FOO=bar
77
+ ZOO=baz
78
+ 1 1 1 1 1 echo ubuntu > /dev/null
79
+ @daily\t/var/lib/cronicle/libexec/ubuntu/foo 2>&1 | logger -t cronicle/ubuntu/foo
80
+ "
81
+ }
82
+ end
83
+
84
+ let(:jobfile) do
85
+ <<-RUBY.undent
86
+ on servers: /.*/ do
87
+ job :foo, user: :root, schedule: '1 2 * * *' do
88
+ puts `uname`
89
+ #puts `whoami`
90
+ end
91
+ end
92
+
93
+ on servers: /.*/ do
94
+ job :bar, user: :root, schedule: :@hourly, content: <<-SH.undent
95
+ #!/bin/sh
96
+ echo hello
97
+ SH
98
+ end
99
+
100
+ on servers: /amazon_linux/ do
101
+ job :foo, user: 'ec2-user', schedule: '2 * * * *' do
102
+ puts 100
103
+ end
104
+ end
105
+
106
+ on servers: /ubuntu/ do
107
+ job :foo2, user: :ubuntu, schedule: :@daily do
108
+ puts 200
109
+ end
110
+ end
111
+ RUBY
112
+ end
113
+
114
+ context 'when cron is updated' do
115
+ let(:amzn_crontab) do
116
+ {
117
+ "/var/spool/cron/ec2-user" =>
118
+ "FOO=bar
119
+ ZOO=baz
120
+ 1 1 1 1 1 echo ec2-user > /dev/null
121
+ 2 * * * *\t/var/lib/cronicle/libexec/ec2-user/foo 2>&1 | logger -t cronicle/ec2-user/foo
122
+ ",
123
+ "/var/spool/cron/root" =>
124
+ "FOO=bar
125
+ ZOO=baz
126
+ 1 1 1 1 1 echo root > /dev/null
127
+ @hourly\t/var/lib/cronicle/libexec/root/bar 2>&1 | logger -t cronicle/root/bar
128
+ 1 2 * * *\t/var/lib/cronicle/libexec/root/foo 2>&1 | logger -t cronicle/root/foo
129
+ "
130
+ }
131
+ end
132
+
133
+ let(:ubuntu_crontab) do
134
+ {
135
+ "/var/spool/cron/crontabs/root" =>
136
+ "FOO=bar
137
+ ZOO=baz
138
+ 1 1 1 1 1 echo root > /dev/null
139
+ @hourly\t/var/lib/cronicle/libexec/root/bar 2>&1 | logger -t cronicle/root/bar
140
+ 1 2 * * *\t/var/lib/cronicle/libexec/root/foo 2>&1 | logger -t cronicle/root/foo
141
+ ",
142
+ "/var/spool/cron/crontabs/ubuntu" =>
143
+ "FOO=bar
144
+ ZOO=baz
145
+ 1 1 1 1 1 echo ubuntu > /dev/null
146
+ @daily\t/var/lib/cronicle/libexec/ubuntu/foo2 2>&1 | logger -t cronicle/ubuntu/foo2
147
+ "
148
+ }
149
+ end
150
+
151
+ it do
152
+ on :amazon_linux do
153
+ expect(get_uname).to match /amzn/
154
+ expect(get_crontabs).to eq amzn_crontab_orig
155
+
156
+ expect(get_file('/var/lib/cronicle/libexec/root/foo')).to eq <<-EOS.undent
157
+ #!/usr/bin/env ruby
158
+ puts `uname`
159
+ puts `whoami`
160
+ EOS
161
+
162
+ expect(get_file('/var/lib/cronicle/libexec/root/bar')).to eq <<-EOS.undent
163
+ #!/bin/sh
164
+ echo hello
165
+ EOS
166
+
167
+ expect(get_file('/var/lib/cronicle/libexec/ec2-user/foo')).to eq <<-EOS.undent
168
+ #!/usr/bin/env ruby
169
+ puts 100
170
+ EOS
171
+ end
172
+
173
+ on :ubuntu do
174
+ expect(get_uname).to match /Ubuntu/
175
+ expect(get_crontabs).to eq ubuntu_crontab_orig
176
+
177
+ expect(get_file('/var/lib/cronicle/libexec/root/foo')).to eq <<-EOS.undent
178
+ #!/usr/bin/env ruby
179
+ puts `uname`
180
+ puts `whoami`
181
+ EOS
182
+
183
+ expect(get_file('/var/lib/cronicle/libexec/root/bar')).to eq <<-EOS.undent
184
+ #!/bin/sh
185
+ echo hello
186
+ EOS
187
+
188
+ expect(get_file('/var/lib/cronicle/libexec/ubuntu/foo')).to eq <<-EOS.undent
189
+ #!/usr/bin/env ruby
190
+ puts 200
191
+ EOS
192
+ end
193
+
194
+ cronicle(:apply) { jobfile }
195
+
196
+ on :amazon_linux do
197
+ expect(get_uname).to match /amzn/
198
+ expect(get_crontabs).to eq amzn_crontab
199
+
200
+ expect(get_file('/var/lib/cronicle/libexec/root/foo')).to eq <<-EOS.undent
201
+ #!/usr/bin/env ruby
202
+ puts `uname`
203
+ #puts `whoami`
204
+ EOS
205
+
206
+ expect(get_file('/var/lib/cronicle/libexec/root/bar')).to eq <<-EOS.undent
207
+ #!/bin/sh
208
+ echo hello
209
+ EOS
210
+
211
+ expect(get_file('/var/lib/cronicle/libexec/ec2-user/foo')).to eq <<-EOS.undent
212
+ #!/usr/bin/env ruby
213
+ puts 100
214
+ EOS
215
+ end
216
+
217
+ on :ubuntu do
218
+ expect(get_uname).to match /Ubuntu/
219
+ expect(get_crontabs).to eq ubuntu_crontab
220
+
221
+ expect(get_file('/var/lib/cronicle/libexec/root/foo')).to eq <<-EOS.undent
222
+ #!/usr/bin/env ruby
223
+ puts `uname`
224
+ #puts `whoami`
225
+ EOS
226
+
227
+ expect(get_file('/var/lib/cronicle/libexec/root/bar')).to eq <<-EOS.undent
228
+ #!/bin/sh
229
+ echo hello
230
+ EOS
231
+
232
+ expect(get_file('/var/lib/cronicle/libexec/ubuntu/foo2')).to eq <<-EOS.undent
233
+ #!/usr/bin/env ruby
234
+ puts 200
235
+ EOS
236
+ end
237
+ end
238
+ end
239
+
240
+ context 'when cron is updated (dry-run)' do
241
+ let(:amzn_crontab) do
242
+ {
243
+ "/var/spool/cron/ec2-user" =>
244
+ "FOO=bar
245
+ ZOO=baz
246
+ 1 1 1 1 1 echo ec2-user > /dev/null
247
+ 2 * * * *\t/var/lib/cronicle/libexec/ec2-user/foo 2>&1 | logger -t cronicle/ec2-user/foo
248
+ ",
249
+ "/var/spool/cron/root" =>
250
+ "FOO=bar
251
+ ZOO=baz
252
+ 1 1 1 1 1 echo root > /dev/null
253
+ @hourly\t/var/lib/cronicle/libexec/root/bar 2>&1 | logger -t cronicle/root/bar
254
+ 1 2 * * *\t/var/lib/cronicle/libexec/root/foo 2>&1 | logger -t cronicle/root/foo
255
+ "
256
+ }
257
+ end
258
+
259
+ let(:ubuntu_crontab) do
260
+ {
261
+ "/var/spool/cron/crontabs/root" =>
262
+ "FOO=bar
263
+ ZOO=baz
264
+ 1 1 1 1 1 echo root > /dev/null
265
+ @hourly\t/var/lib/cronicle/libexec/root/bar 2>&1 | logger -t cronicle/root/bar
266
+ 1 2 * * *\t/var/lib/cronicle/libexec/root/foo 2>&1 | logger -t cronicle/root/foo
267
+ ",
268
+ "/var/spool/cron/crontabs/ubuntu" =>
269
+ "FOO=bar
270
+ ZOO=baz
271
+ 1 1 1 1 1 echo ubuntu > /dev/null
272
+ @daily\t/var/lib/cronicle/libexec/ubuntu/foo2 2>&1 | logger -t cronicle/ubuntu/foo2
273
+ "
274
+ }
275
+ end
276
+
277
+ it do
278
+ on :amazon_linux do
279
+ expect(get_uname).to match /amzn/
280
+ expect(get_crontabs).to eq amzn_crontab_orig
281
+
282
+ expect(get_file('/var/lib/cronicle/libexec/root/foo')).to eq <<-EOS.undent
283
+ #!/usr/bin/env ruby
284
+ puts `uname`
285
+ puts `whoami`
286
+ EOS
287
+
288
+ expect(get_file('/var/lib/cronicle/libexec/root/bar')).to eq <<-EOS.undent
289
+ #!/bin/sh
290
+ echo hello
291
+ EOS
292
+
293
+ expect(get_file('/var/lib/cronicle/libexec/ec2-user/foo')).to eq <<-EOS.undent
294
+ #!/usr/bin/env ruby
295
+ puts 100
296
+ EOS
297
+ end
298
+
299
+ on :ubuntu do
300
+ expect(get_uname).to match /Ubuntu/
301
+ expect(get_crontabs).to eq ubuntu_crontab_orig
302
+
303
+ expect(get_file('/var/lib/cronicle/libexec/root/foo')).to eq <<-EOS.undent
304
+ #!/usr/bin/env ruby
305
+ puts `uname`
306
+ puts `whoami`
307
+ EOS
308
+
309
+ expect(get_file('/var/lib/cronicle/libexec/root/bar')).to eq <<-EOS.undent
310
+ #!/bin/sh
311
+ echo hello
312
+ EOS
313
+
314
+ expect(get_file('/var/lib/cronicle/libexec/ubuntu/foo')).to eq <<-EOS.undent
315
+ #!/usr/bin/env ruby
316
+ puts 200
317
+ EOS
318
+ end
319
+
320
+ cronicle(:apply, dry_run: true) { jobfile }
321
+
322
+ on :amazon_linux do
323
+ expect(get_uname).to match /amzn/
324
+ expect(get_crontabs).to eq amzn_crontab_orig
325
+
326
+ expect(get_file('/var/lib/cronicle/libexec/root/foo')).to eq <<-EOS.undent
327
+ #!/usr/bin/env ruby
328
+ puts `uname`
329
+ puts `whoami`
330
+ EOS
331
+
332
+ expect(get_file('/var/lib/cronicle/libexec/root/bar')).to eq <<-EOS.undent
333
+ #!/bin/sh
334
+ echo hello
335
+ EOS
336
+
337
+ expect(get_file('/var/lib/cronicle/libexec/ec2-user/foo')).to eq <<-EOS.undent
338
+ #!/usr/bin/env ruby
339
+ puts 100
340
+ EOS
341
+ end
342
+
343
+ on :ubuntu do
344
+ expect(get_uname).to match /Ubuntu/
345
+ expect(get_crontabs).to eq ubuntu_crontab_orig
346
+
347
+ expect(get_file('/var/lib/cronicle/libexec/root/foo')).to eq <<-EOS.undent
348
+ #!/usr/bin/env ruby
349
+ puts `uname`
350
+ puts `whoami`
351
+ EOS
352
+
353
+ expect(get_file('/var/lib/cronicle/libexec/root/bar')).to eq <<-EOS.undent
354
+ #!/bin/sh
355
+ echo hello
356
+ EOS
357
+
358
+ expect(get_file('/var/lib/cronicle/libexec/ubuntu/foo')).to eq <<-EOS.undent
359
+ #!/usr/bin/env ruby
360
+ puts 200
361
+ EOS
362
+ end
363
+ end
364
+ end
365
+ end
@@ -0,0 +1,162 @@
1
+ describe Cronicle::HostList do
2
+ let(:host_list) { described_class.new(source) }
3
+
4
+ context 'when csv is passed' do
5
+ let(:source) { "foo,bar" }
6
+
7
+ it do
8
+ expect(host_list.all).to match_array ["foo", "bar"]
9
+ end
10
+
11
+ it do
12
+ expect(host_list.select(servers: /foo/)).to match_array ["foo"]
13
+ end
14
+
15
+ it do
16
+ expect(host_list.select(roles: /web/)).to match_array []
17
+ end
18
+ end
19
+
20
+ context 'when list is passed' do
21
+ let(:source) do
22
+ <<-EOS.undent
23
+ foo
24
+ bar
25
+ EOS
26
+ end
27
+
28
+ it do
29
+ expect(host_list.all).to match_array ["foo", "bar"]
30
+ end
31
+
32
+ it do
33
+ expect(host_list.select(servers: /foo/)).to match_array ["foo"]
34
+ end
35
+
36
+ it do
37
+ expect(host_list.select(roles: /web/)).to match_array []
38
+ end
39
+ end
40
+
41
+ context 'when JSON(servers) is passed' do
42
+ context 'when single server' do
43
+ let(:source) do
44
+ <<-EOS.undent
45
+ {
46
+ "servers": "foo"
47
+ }
48
+ EOS
49
+ end
50
+
51
+ it do
52
+ expect(host_list.all).to match_array ["foo"]
53
+ end
54
+
55
+ it do
56
+ expect(host_list.select(servers: /foo/)).to match_array ["foo"]
57
+ end
58
+
59
+ it do
60
+ expect(host_list.select(roles: /web/)).to match_array []
61
+ end
62
+ end
63
+
64
+ context 'when multiple servers' do
65
+ let(:source) do
66
+ <<-EOS.undent
67
+ {
68
+ "servers": ["foo", "bar"]
69
+ }
70
+ EOS
71
+ end
72
+
73
+ it do
74
+ expect(host_list.all).to match_array ["foo", "bar"]
75
+ end
76
+
77
+ it do
78
+ expect(host_list.select(servers: /foo/)).to match_array ["foo"]
79
+ end
80
+
81
+ it do
82
+ expect(host_list.select(roles: /web/)).to match_array []
83
+ end
84
+ end
85
+
86
+ context 'when multiple servers with role' do
87
+ let(:source) do
88
+ <<-EOS.undent
89
+ {
90
+ "servers": {
91
+ "foo": "db",
92
+ "bar": "web"
93
+ }
94
+ }
95
+ EOS
96
+ end
97
+
98
+ it do
99
+ expect(host_list.all).to match_array ["foo", "bar"]
100
+ end
101
+
102
+ it do
103
+ expect(host_list.select(servers: /foo/)).to match_array ["foo"]
104
+ end
105
+
106
+ it do
107
+ expect(host_list.select(roles: /web/)).to match_array ["bar"]
108
+ end
109
+ end
110
+ end
111
+
112
+ context 'when JSON(roles) is passed' do
113
+ context 'when single server' do
114
+ let(:source) do
115
+ <<-EOS.undent
116
+ {
117
+ "roles": {
118
+ "web": "bar"
119
+ }
120
+ }
121
+ EOS
122
+ end
123
+
124
+ it do
125
+ expect(host_list.all).to match_array ["bar"]
126
+ end
127
+
128
+ it do
129
+ expect(host_list.select(servers: /foo/)).to match_array []
130
+ end
131
+
132
+ it do
133
+ expect(host_list.select(roles: /web/)).to match_array ["bar"]
134
+ end
135
+ end
136
+
137
+ context 'when multiple servers' do
138
+ let(:source) do
139
+ <<-EOS.undent
140
+ {
141
+ "roles": {
142
+ "db": ["foo", "bar"],
143
+ "web": ["zoo", "baz"]
144
+ }
145
+ }
146
+ EOS
147
+ end
148
+
149
+ it do
150
+ expect(host_list.all).to match_array ["foo", "bar", "zoo", "baz"]
151
+ end
152
+
153
+ it do
154
+ expect(host_list.select(servers: /oo/)).to match_array ["foo", "zoo"]
155
+ end
156
+
157
+ it do
158
+ expect(host_list.select(roles: /web/)).to match_array ["zoo", "baz"]
159
+ end
160
+ end
161
+ end
162
+ end