ztk 0.3.1 → 1.0.0.rc.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,635 @@
1
+ ################################################################################
2
+ #
3
+ # Author: Zachary Patten <zachary@jovelabs.net>
4
+ # Copyright: Copyright (c) Jove Labs
5
+ # License: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ ################################################################################
20
+
21
+ require "spec_helper"
22
+
23
+ describe ZTK::DSL do
24
+
25
+ subject {
26
+ class DSLTest < ZTK::DSL::Base
27
+ end
28
+
29
+ DSLTest.new
30
+ }
31
+
32
+ before(:all) do
33
+ $stdout = File.open("/dev/null", "w")
34
+ $stderr = File.open("/dev/null", "w")
35
+ $stdin = File.open("/dev/null", "r")
36
+ end
37
+
38
+
39
+ describe "class" do
40
+
41
+ it "should be an instance of ZTK::DSL" do
42
+ subject.should be_an_instance_of DSLTest
43
+ end
44
+
45
+ end
46
+
47
+ describe "attributes" do
48
+
49
+ it "should allow setting of an attribute via a block" do
50
+ data = "Hello World @ #{Time.now.utc}"
51
+ class DSLTest < ZTK::DSL::Base
52
+ attribute :name
53
+ end
54
+
55
+ dsl_test = DSLTest.new do
56
+ name "#{data}"
57
+ end
58
+
59
+ dsl_test.name.should == data
60
+ end
61
+
62
+ it "should allow setting of an attribute directly" do
63
+ data = "Hello World @ #{Time.now.utc}"
64
+ class DSLTest < ZTK::DSL::Base
65
+ attribute :name
66
+ end
67
+
68
+ dsl_test = DSLTest.new
69
+ dsl_test.name ="#{data}"
70
+
71
+ dsl_test.name.should == data
72
+ end
73
+
74
+ it "should throw an exception when setting an invalid attribute" do
75
+ data = "Hello World @ #{Time.now.utc}"
76
+ class DSLTest < ZTK::DSL::Base
77
+ attribute :name
78
+ end
79
+
80
+ lambda {
81
+ dsl_test = DSLTest.new do
82
+ thing "#{data}"
83
+ end
84
+ }.should raise_error
85
+ end
86
+
87
+ end
88
+
89
+ describe "relations" do
90
+
91
+ before(:each) do
92
+ class Environment < ZTK::DSL::Base
93
+ has_many :containers
94
+
95
+ attribute :name
96
+ end
97
+ Environment.purge
98
+
99
+ class Container < ZTK::DSL::Base
100
+ belongs_to :environment
101
+
102
+ attribute :name
103
+ end
104
+ Container.purge
105
+ end
106
+
107
+ describe "has_many" do
108
+
109
+ it "can has_many via nesting" do
110
+ env = Environment.new do
111
+ name "environment"
112
+ container do
113
+ name "container0"
114
+ end
115
+ container do
116
+ name "container1"
117
+ end
118
+ container do
119
+ name "container2"
120
+ end
121
+ end
122
+
123
+ env.name.should == "environment"
124
+ env.containers.count.should == 3
125
+ env.containers.each do |container|
126
+ %w(container0 container1 container2).include?(container.name).should == true
127
+ container.environment.should == env
128
+ container.environment_id.should == env.id
129
+ end
130
+ end
131
+
132
+ it "can has_many via direct object assignment" do
133
+ env = Environment.new do
134
+ name "environment"
135
+ end
136
+ con0 = Container.new do
137
+ name "container0"
138
+ end
139
+ con1 = Container.new do
140
+ name "container1"
141
+ end
142
+ con2 = Container.new do
143
+ name "container2"
144
+ end
145
+ con0.environment = env
146
+ con1.environment = env
147
+ con2.environment = env
148
+
149
+ env.name.should == "environment"
150
+ env.containers.count.should == 3
151
+ env.containers.each do |container|
152
+ %w(container0 container1 container2).include?(container.name).should == true
153
+ container.environment.should == env
154
+ container.environment_id.should == env.id
155
+ end
156
+ end
157
+
158
+ it "can has_many via direct object id assignment" do
159
+ env = Environment.new do
160
+ name "environment"
161
+ end
162
+ con0 = Container.new do
163
+ name "container0"
164
+ end
165
+ con1 = Container.new do
166
+ name "container1"
167
+ end
168
+ con2 = Container.new do
169
+ name "container2"
170
+ end
171
+ con0.environment_id = env.id
172
+ con1.environment_id = env.id
173
+ con2.environment_id = env.id
174
+
175
+ env.name.should == "environment"
176
+ env.containers.count.should == 3
177
+ env.containers.each do |container|
178
+ %w(container0 container1 container2).include?(container.name).should == true
179
+ container.environment.should == env
180
+ container.environment_id.should == env.id
181
+ end
182
+ end
183
+
184
+ # FIXME
185
+ # it "can has_many via direct object addition" do
186
+ # env = Environment.new do
187
+ # name "environment"
188
+ # end
189
+ # con0 = Container.new do
190
+ # name "container0"
191
+ # end
192
+ # con1 = Container.new do
193
+ # name "container1"
194
+ # end
195
+ # con2 = Container.new do
196
+ # name "container2"
197
+ # end
198
+ # env.containers << con0
199
+ # env.containers << con1
200
+ # env.containers << con2
201
+
202
+ # env.name.should == "environment"
203
+ # env.containers.count.should == 3
204
+ # env.containers.each do |container|
205
+ # %w(container0 container1 container2).include?(container.name).should == true
206
+ # container.environment.should == env
207
+ # container.environment_id.should == env.id
208
+ # end
209
+ # end
210
+
211
+ end
212
+
213
+ describe "belongs_to" do
214
+ it "can belong_to via nesting" do
215
+ env = Environment.new do
216
+ name "environment"
217
+ container do
218
+ name "container0"
219
+ end
220
+ container do
221
+ name "container1"
222
+ end
223
+ container do
224
+ name "container2"
225
+ end
226
+ end
227
+
228
+ env.name.should == "environment"
229
+ env.containers.count.should == 3
230
+ env.containers.each do |container|
231
+ %w(container0 container1 container2).include?(container.name).should == true
232
+ container.environment.should == env
233
+ container.environment_id.should == env.id
234
+ end
235
+ end
236
+
237
+ it "can belong_to via direct assignment" do
238
+ env = Environment.new do
239
+ name "environment"
240
+ end
241
+ con0 = Container.new do
242
+ name "container0"
243
+ environment env
244
+ end
245
+ con1 = Container.new do
246
+ name "container1"
247
+ environment env
248
+ end
249
+ con2 = Container.new do
250
+ name "container2"
251
+ environment env
252
+ end
253
+
254
+ env.name.should == "environment"
255
+ env.containers.count.should == 3
256
+ env.containers.each do |container|
257
+ %w(container0 container1 container2).include?(container.name).should == true
258
+ container.environment.should == env
259
+ container.environment_id.should == env.id
260
+ end
261
+ end
262
+
263
+ it "can belong_to via direct object id assignment" do
264
+ env = Environment.new do
265
+ name "environment"
266
+ end
267
+ con0 = Container.new do
268
+ name "container0"
269
+ environment_id env.id
270
+ end
271
+ con1 = Container.new do
272
+ name "container1"
273
+ environment_id env.id
274
+ end
275
+ con2 = Container.new do
276
+ name "container2"
277
+ environment_id env.id
278
+ end
279
+
280
+ env.name.should == "environment"
281
+ env.containers.count.should == 3
282
+ env.containers.each do |container|
283
+ %w(container0 container1 container2).include?(container.name).should == true
284
+ container.environment.should == env
285
+ container.environment_id.should == env.id
286
+ end
287
+ end
288
+
289
+ end
290
+
291
+ end
292
+
293
+ describe "find" do
294
+
295
+ before(:each) do
296
+ class Environment < ZTK::DSL::Base
297
+ has_many :containers
298
+
299
+ attribute :name
300
+ end
301
+ Environment.purge
302
+
303
+ class Container < ZTK::DSL::Base
304
+ belongs_to :environment
305
+
306
+ attribute :name
307
+ end
308
+ Container.purge
309
+ end
310
+
311
+ it "single id" do
312
+ env = Environment.new do
313
+ id :env0
314
+ name "environment"
315
+
316
+ $con0 = container do
317
+ id :con0
318
+ name "container0"
319
+ end
320
+ $con1 = container do
321
+ id :con1
322
+ name "container1"
323
+ end
324
+ $con2 = container do
325
+ id :con2
326
+ name "container2"
327
+ end
328
+ end
329
+ Environment.find(:env0).first.should == env
330
+ Container.find(:con0).first.should == $con0
331
+ Container.find(:con1).first.should == $con1
332
+ Container.find(:con2).first.should == $con2
333
+ end
334
+
335
+ it "multiple ids" do
336
+ env = Environment.new do
337
+ id :env0
338
+ name "environment"
339
+
340
+ $con0 = container do
341
+ id :con0
342
+ name "container0"
343
+ end
344
+ $con1 = container do
345
+ id :con1
346
+ name "container1"
347
+ end
348
+ $con2 = container do
349
+ id :con2
350
+ name "container2"
351
+ end
352
+ end
353
+ Environment.find(:env0).first.should == env
354
+ containers = Container.find([:con0, :con1, :con2])
355
+ containers.count.should == 3
356
+ containers.all? do |container|
357
+ [:con0, :con1, :con2].include?(container.id)
358
+ end
359
+ end
360
+
361
+ end
362
+
363
+ describe "complex relations" do
364
+
365
+ before(:each) do
366
+ class Environment < ZTK::DSL::Base
367
+ has_many :containers
368
+ has_many :networks
369
+
370
+ attribute :name
371
+ end
372
+ Environment.purge
373
+
374
+ class Container < ZTK::DSL::Base
375
+ belongs_to :environment
376
+ belongs_to :network
377
+
378
+ attribute :name
379
+ end
380
+ Container.purge
381
+
382
+ class Network < ZTK::DSL::Base
383
+ has_many :containers
384
+ belongs_to :environment
385
+
386
+ attribute :name
387
+ end
388
+ Network.purge
389
+ end
390
+
391
+ it "one parent, two children nested with interdependency, id assignment" do
392
+ env = Environment.new do
393
+ name "environment"
394
+
395
+ network do
396
+ id :net0
397
+ name "network0"
398
+ end
399
+ network do
400
+ id :net1
401
+ name "network1"
402
+ end
403
+
404
+ container do
405
+ id :con0
406
+ name "container0"
407
+ network_id :net0
408
+ end
409
+ container do
410
+ id :con1
411
+ name "container1"
412
+ network_id :net0
413
+ end
414
+ container do
415
+ id :con2
416
+ name "container2"
417
+ network_id :net1
418
+ end
419
+ end
420
+
421
+ env.name.should == "environment"
422
+ env.containers.count.should == 3
423
+ env.containers.each do |container|
424
+ %w(container0 container1 container2).include?(container.name).should == true
425
+ container.environment.should == env
426
+ container.environment_id.should == env.id
427
+ end
428
+ env.networks.each do |network|
429
+ %w(network0 network1).include?(network.name).should == true
430
+ network.environment.should == env
431
+ network.environment_id.should == env.id
432
+ end
433
+
434
+ n0 = Network.find(:net0).first
435
+ n0.containers.each do |container|
436
+ %w(container0 container1).include?(container.name).should == true
437
+ container.environment.should == env
438
+ container.environment_id.should == env.id
439
+ end
440
+
441
+ n1 = Network.find(:net1).first
442
+ n1.containers.each do |container|
443
+ %w(container2).include?(container.name).should == true
444
+ container.environment.should == env
445
+ container.environment_id.should == env.id
446
+ end
447
+ end
448
+
449
+ it "one parent, two children with interdependency, id assignment" do
450
+ env = Environment.new do
451
+ id :env0
452
+
453
+ name "environment"
454
+ end
455
+
456
+ Network.new do
457
+ id :net0
458
+ name "network0"
459
+ end
460
+ Network.new do
461
+ id :net1
462
+ name "network1"
463
+ end
464
+
465
+ Container.new do
466
+ id :con0
467
+ name "container0"
468
+ network_id :net0
469
+ environment_id :env0
470
+ end
471
+ Container.new do
472
+ id :con1
473
+ name "container1"
474
+ network_id :net0
475
+ environment_id :env0
476
+ end
477
+ Container.new do
478
+ id :con2
479
+ name "container2"
480
+ network_id :net1
481
+ environment_id :env0
482
+ end
483
+
484
+ env.name.should == "environment"
485
+ env.containers.count.should == 3
486
+ env.containers.each do |container|
487
+ %w(container0 container1 container2).include?(container.name).should == true
488
+ container.environment.should == env
489
+ container.environment_id.should == env.id
490
+ end
491
+ env.networks.each do |network|
492
+ %w(network0 network1).include?(network.name).should == true
493
+ network.environment.should == env
494
+ network.environment_id.should == env.id
495
+ end
496
+
497
+ n0 = Network.find(:net0).first
498
+ n0.containers.each do |container|
499
+ %w(container0 container1).include?(container.name).should == true
500
+ container.environment.should == env
501
+ container.environment_id.should == env.id
502
+ end
503
+
504
+ n1 = Network.find(:net1).first
505
+ n1.containers.each do |container|
506
+ %w(container2).include?(container.name).should == true
507
+ container.environment.should == env
508
+ container.environment_id.should == env.id
509
+ end
510
+ end
511
+
512
+ it "one parent, two children nested with interdependency, direct assignment" do
513
+ env = Environment.new do
514
+ name "environment"
515
+
516
+ net0 = network do
517
+ id :net0
518
+ name "network0"
519
+ end
520
+ net1 = network do
521
+ id :net1
522
+ name "network1"
523
+ end
524
+
525
+ container do
526
+ id :con0
527
+ name "container0"
528
+ network net0
529
+ end
530
+ container do
531
+ id :con1
532
+ name "container1"
533
+ network net0
534
+ end
535
+ container do
536
+ id :con2
537
+ name "container2"
538
+ network net1
539
+ end
540
+ end
541
+
542
+ env.name.should == "environment"
543
+ env.containers.count.should == 3
544
+ env.containers.each do |container|
545
+ %w(container0 container1 container2).include?(container.name).should == true
546
+ container.environment.should == env
547
+ container.environment_id.should == env.id
548
+ end
549
+ env.networks.each do |network|
550
+ %w(network0 network1).include?(network.name).should == true
551
+ network.environment.should == env
552
+ network.environment_id.should == env.id
553
+ end
554
+
555
+ n0 = Network.find(:net0).first
556
+ n0.containers.each do |container|
557
+ %w(container0 container1).include?(container.name).should == true
558
+ container.environment.should == env
559
+ container.environment_id.should == env.id
560
+ end
561
+
562
+ n1 = Network.find(:net1).first
563
+ n1.containers.each do |container|
564
+ %w(container2).include?(container.name).should == true
565
+ container.environment.should == env
566
+ container.environment_id.should == env.id
567
+ end
568
+ end
569
+
570
+ it "one parent, two children with interdependency, direct assignment" do
571
+ env = Environment.new do
572
+ id :env0
573
+
574
+ name "environment"
575
+ end
576
+
577
+ net0 = Network.new do
578
+ id :net0
579
+ name "network0"
580
+ end
581
+ net1 = Network.new do
582
+ id :net1
583
+ name "network1"
584
+ end
585
+
586
+ Container.new do
587
+ id :con0
588
+ name "container0"
589
+ network net0
590
+ environment env
591
+ end
592
+ Container.new do
593
+ id :con1
594
+ name "container1"
595
+ network net0
596
+ environment env
597
+ end
598
+ Container.new do
599
+ id :con2
600
+ name "container2"
601
+ network net1
602
+ environment env
603
+ end
604
+
605
+ env.name.should == "environment"
606
+ env.containers.count.should == 3
607
+ env.containers.each do |container|
608
+ %w(container0 container1 container2).include?(container.name).should == true
609
+ container.environment.should == env
610
+ container.environment_id.should == env.id
611
+ end
612
+ env.networks.each do |network|
613
+ %w(network0 network1).include?(network.name).should == true
614
+ network.environment.should == env
615
+ network.environment_id.should == env.id
616
+ end
617
+
618
+ n0 = Network.find(:net0).first
619
+ n0.containers.each do |container|
620
+ %w(container0 container1).include?(container.name).should == true
621
+ container.environment.should == env
622
+ container.environment_id.should == env.id
623
+ end
624
+
625
+ n1 = Network.find(:net1).first
626
+ n1.containers.each do |container|
627
+ %w(container2).include?(container.name).should == true
628
+ container.environment.should == env
629
+ container.environment_id.should == env.id
630
+ end
631
+ end
632
+
633
+ end
634
+
635
+ end
data/ztk.gemspec CHANGED
@@ -1,5 +1,6 @@
1
- # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/ztk/version', __FILE__)
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'ztk/version'
3
4
 
4
5
  Gem::Specification.new do |gem|
5
6
  gem.authors = ["Zachary Patten"]
@@ -18,6 +19,7 @@ Gem::Specification.new do |gem|
18
19
  gem.add_dependency("erubis", ">= 0")
19
20
  gem.add_dependency("net-ssh", ">= 0")
20
21
  gem.add_dependency("net-sftp", ">= 0")
22
+ gem.add_dependency("activesupport", ">= 0")
21
23
 
22
24
  gem.add_development_dependency("pry", ">= 0")
23
25
  gem.add_development_dependency("rake", ">= 0")