This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
/
test_machine_view_panel.js
1294 lines (1208 loc) · 50.2 KB
/
test_machine_view_panel.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of the Juju GUI, which lets users view and manage Juju
environments within a graphical interface (https://launchpad.net/juju-gui).
Copyright (C) 2012-2013 Canonical Ltd.
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License version 3, as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
General Public License for more details.
You should have received a copy of the GNU Affero General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
describe('machine view panel view', function() {
var Y, container, machines, machine, models, notifications, scaleUpView,
scaleUpViewRender, services, utils, units, views, view, View;
function createViewNoUnits() {
// Create a test-specific view that has no units to start
return new View({
container: container,
db: {
services: new models.ServiceList(),
machines: machines,
units: new models.ServiceUnitList()
}
});
}
before(function(done) {
Y = YUI(GlobalConfig).use(['machine-view-panel',
'juju-models',
'juju-views',
'juju-tests-utils',
'event-simulate',
'node-event-simulate',
'drop-target-view-extension',
'node'], function(Y) {
models = Y.namespace('juju.models');
utils = Y.namespace('juju-tests.utils');
views = Y.namespace('juju.views');
View = views.MachineViewPanelView;
done();
});
});
beforeEach(function() {
container = utils.makeContainer(this, 'machine-view-panel');
// setup machines
machine = {
id: '0',
hardware: {
disk: 1024,
mem: 1024,
cpuPower: 1024,
cpuCores: 1
}
};
machines = new models.MachineList();
machines.add([machine]);
// displayName is set on the machine object in the list; we also need to
// set it on our standalone model.
var displayName = machines.createDisplayName(machine.id);
machine.displayName = displayName;
// setup unplaced service units
units = new models.ServiceUnitList();
units.add([
{id: 'test/1'}
]);
// setup test services
services = new models.ServiceList();
services.add([
{id: 'test', icon: 'test.svg'},
{id: 'baz', icon: 'baz.svg'}
]);
// Set up test notifications.
notifications = new models.NotificationList();
// add everything to the view
view = new View({
container: container,
db: {
machines: machines,
notifications: notifications,
services: services,
units: units
}
});
});
afterEach(function() {
view.destroy();
machines.destroy();
notifications.destroy();
services.destroy();
units.destroy();
container.remove(true);
});
it('should apply the wrapping class to the container', function() {
view.render();
assert.equal(container.hasClass('machine-view-panel'), true);
});
it('should render the header widgets', function() {
view.render();
assert.equal(container.one('.column .head .title').get('text'),
'Unplaced units');
});
it('should set the initial container header label', function() {
var labels = ['0 containers', '0 units'];
view.render();
labels.forEach(function(l, index) {
assert.equal(view._containersHeader.get(
'container').all('.label').item(index).get('text'), l);
});
});
describe('_onMachineCreated', function() {
beforeEach(function() {
machines.reset();
view.render();
});
it('adds a notification if a global error occurred', function() {
var machine = machines.addGhost();
view._onMachineCreated(machine, {err: 'bad wolf'});
assert.strictEqual(notifications.size(), 1);
var notification = notifications.item(0);
assert.strictEqual(
notification.get('title'), 'Error creating the new machine');
assert.strictEqual(
notification.get('message'),
'Could not add the requested machine. ' +
'Server responded with: bad wolf');
assert.strictEqual(notification.get('level'), 'error');
});
it('adds a notification if a machine error occurred', function() {
var machine = machines.addGhost();
var response = {machines: [{name: '42', err: 'exterminate'}]};
view._onMachineCreated(machine, response);
assert.strictEqual(notifications.size(), 1);
var notification = notifications.item(0);
assert.strictEqual(
notification.get('title'), 'Error creating machine 42');
assert.strictEqual(
notification.get('message'),
'Could not add the requested machine. ' +
'Server responded with: exterminate');
assert.strictEqual(notification.get('level'), 'error');
});
it('removes the ghost machine', function() {
var machine = machines.addGhost();
assert.strictEqual(machines.size(), 1);
var response = {machines: [{name: '42'}]};
view._onMachineCreated(machine, response);
assert.strictEqual(machines.size(), 0);
});
it('removes the ghost machine even when an error occurs', function() {
var machine = machines.addGhost();
assert.strictEqual(machines.size(), 1);
var response = {err: 'bad wolf'};
view._onMachineCreated(machine, response);
assert.strictEqual(machines.size(), 0);
});
});
describe('_onMachineRemove', function() {
it('clears the container column when the active machine is removed',
function() {
var clearStub = utils.makeStubMethod(view, '_clearContainerColumn');
this._cleanups.push(clearStub.reset);
view._machinesHeader = {};
var labelStub = utils.makeStubMethod(view._machinesHeader,
'updateLabelCount');
this._cleanups.push(labelStub.reset);
view.set('selectedMachine', '0');
machines.remove(machines.getById('0'));
assert.equal(clearStub.calledOnce(), true);
});
it('does not clear the container column if the machine is not active',
function() {
var clearStub = utils.makeStubMethod(view, '_clearContainerColumn');
this._cleanups.push(clearStub.reset);
view._machinesHeader = {};
var labelStub = utils.makeStubMethod(view._machinesHeader,
'updateLabelCount');
this._cleanups.push(labelStub.reset);
view.set('selectedMachine', '9');
machines.remove(machines.getById('0'));
assert.equal(clearStub.calledOnce(), false);
});
});
describe('create machine view', function() {
beforeEach(function() {
view.set('env', {
addMachines: utils.makeStubFunction('add-machine-record-key'),
placeUnit: utils.makeStubFunction()
});
});
it('displays when the machine header action is clicked', function() {
view.render();
var createMachine = container.one('.create-machine');
container.one('.machines .head .action').simulate('click');
assert.equal(createMachine.hasClass('create-machine-view'), true,
'expected class is not present');
assert.equal(createMachine.getHTML() === '', false,
'HTML is not present');
assert.equal(createMachine.hasClass('state-constraints'), true,
'the constraints should be visible');
});
it('displays when the container header action is clicked', function() {
view.render();
view.set('selectedMachine', '0');
var createContainer = container.one('.create-container');
container.one('.containers .head .action').simulate('click');
assert.equal(createContainer.hasClass('create-machine-view'), true,
'expected class is not present');
assert.equal(createContainer.getHTML() === '', false,
'HTML is not present');
assert.equal(createContainer.hasClass('state-containers'), true,
'the container types should be visible');
});
it('creates an unplaced unit when cancelled with a unit', function() {
var unitTokens = view.get('unitTokens');
var toggleStub = utils.makeStubMethod(view, '_toggleAllPlacedMessage');
this._cleanups.push(toggleStub.reset);
view.render();
view._unitTokenDropHandler({
dropAction: 'machine',
unit: 'test/1'
});
assert.equal(Object.keys(unitTokens).length, 0);
// Confirm the machine creation.
container.one('.create-machine-view .cancel').simulate('click');
assert.equal(Object.keys(unitTokens).length, 1);
});
});
describe('token drag and drop', function() {
beforeEach(function() {
view.set('env', {
addMachines: utils.makeStubFunction('add-machine-record-key'),
placeUnit: utils.makeStubFunction()
});
});
it('listens for the drag start, end, drop events', function() {
var onStub = utils.makeStubMethod(view, 'on');
this._cleanups.push(onStub.reset);
view._bindEvents();
var onStubArgs = onStub.allArguments();
assert.equal(onStubArgs[0][0], '*:unit-token-drag-start');
assert.deepEqual(onStubArgs[0][1], view._showDraggingUI);
assert.equal(onStubArgs[1][0], '*:unit-token-drag-end');
assert.deepEqual(onStubArgs[1][1], view._hideDraggingUI);
assert.equal(onStubArgs[2][0], '*:unit-token-drop');
assert.deepEqual(onStubArgs[2][1], view._unitTokenDropHandler);
});
it('converts the headers and tokens to drop targets when dragging',
function() {
// This tests assumes the previous test passed.
// 'listens for the drag start, end, drop events'
var onStub = utils.makeStubMethod(view, 'on');
this._cleanups.push(onStub.reset);
var machineToken = view.get('machineTokens')['0'];
view._bindEvents();
view._machinesHeader = { setDroppable: utils.makeStubFunction() };
view._containersHeader = { setDroppable: utils.makeStubFunction() };
machineToken.setDroppable = utils.makeStubFunction();
// unit-drag start handler _showDraggingUI
onStub.allArguments()[0][1].call(view);
assert.equal(view._machinesHeader.setDroppable.calledOnce(), true);
assert.equal(machineToken.setDroppable.calledOnce(), true);
// The user hasn't selected a machine so this header should not be
// a drop target.
assert.equal(view._containersHeader.setDroppable.calledOnce(), false);
});
it('converts headers and tokens to drop targets when machine selected',
function() {
// This tests assumes the previous test passed.
// 'listens for the drag start, end, drop events'
var onStub = utils.makeStubMethod(view, 'on');
this._cleanups.push(onStub.reset);
container.append(Y.Node.create('<div class="containers">' +
'<div class="content"><div class="items"></div></div></div>'));
// Add a container.
machines.add([{id: '0/lxc/3'}]);
var machineToken = view.get('machineTokens')['0'];
var containerToken = view.get('containerTokens')['0/lxc/3'];
view._bindEvents();
view._machinesHeader = { setDroppable: utils.makeStubFunction() };
view._containersHeader = { setDroppable: utils.makeStubFunction() };
machineToken.setDroppable = utils.makeStubFunction();
containerToken.setDroppable = utils.makeStubFunction();
view.set('selectedMachine', 1);
// unit-drag start handler _showDraggingUI
onStub.allArguments()[0][1].call(view);
assert.equal(view._machinesHeader.setDroppable.calledOnce(), true);
assert.equal(machineToken.setDroppable.calledOnce(), true);
// The user selected a machine so this header should be a drop target.
assert.equal(view._containersHeader.setDroppable.calledOnce(), true);
assert.equal(containerToken.setDroppable.calledOnce(), true);
});
it('converts headers to non-drop targets when drag stopped', function() {
// This tests assumes the previous test passed.
// 'listens for the drag start, end, drop events'
var onStub = utils.makeStubMethod(view, 'on');
this._cleanups.push(onStub.reset);
container.append(Y.Node.create('<div class="containers">' +
'<div class="content"><div class="items"></div></div></div>'));
machines.add([{id: '0/lxc/3'}]);
var machineToken = view.get('machineTokens')['0'];
var containerToken = view.get('containerTokens')['0/lxc/3'];
view._bindEvents();
view._machinesHeader = { setNotDroppable: utils.makeStubFunction() };
view._containersHeader = { setNotDroppable: utils.makeStubFunction() };
machineToken.setNotDroppable = utils.makeStubFunction();
containerToken.setNotDroppable = utils.makeStubFunction();
// unit-drag end handler _hideDraggingUI
onStub.allArguments()[1][1].call(view);
assert.equal(view._machinesHeader.setNotDroppable.calledOnce(), true);
assert.equal(view._containersHeader.setNotDroppable.calledOnce(), true);
assert.equal(machineToken.setNotDroppable.calledOnce(), true);
assert.equal(containerToken.setNotDroppable.calledOnce(), true);
});
it('converts headers to non-drop targets when dropped on a header',
function() {
view.render();
machines.add([{id: '0/lxc/3'}]);
var machineToken = view.get('machineTokens')['0'];
var containerToken = view.get('containerTokens')['0/lxc/3'];
machineToken.setNotDroppable = utils.makeStubFunction();
containerToken.setNotDroppable = utils.makeStubFunction();
view._machinesHeader = {
setNotDroppable: utils.makeStubFunction(),
updateLabelCount: utils.makeStubFunction()
};
view._containersHeader = {
setNotDroppable: utils.makeStubFunction()
};
view._unitTokenDropHandler({
dropAction: 'machine',
unit: 'test/1'
});
assert.equal(view._machinesHeader.setNotDroppable.calledOnce(), true);
assert.equal(view._containersHeader.setNotDroppable.calledOnce(),
true);
assert.equal(machineToken.setNotDroppable.calledOnce(), true);
assert.equal(containerToken.setNotDroppable.calledOnce(), true);
});
it('creates a new machine when dropped on machine header', function() {
var toggleStub = utils.makeStubMethod(view, '_toggleAllPlacedMessage');
this._cleanups.push(toggleStub.reset);
view.render();
view._unitTokenDropHandler({
dropAction: 'machine',
unit: 'test/1'
});
var env = view.get('env');
// The create machine options should be visible.
var createMachine = container.one('.create-machine');
assert.equal(createMachine.hasClass('create-machine-view'), true);
assert.equal(createMachine.getHTML() === '', false);
// Confirm the machine creation.
container.one('.create-machine-view .create').simulate('click');
assert.deepEqual(env.addMachines.lastArguments()[0], [{
containerType: undefined,
parentId: undefined,
constraints: {
'cpu-power': '',
mem: '',
'root-disk': ''
}
}]);
// A new ghost machine has been added to the database.
assert.isNotNull(machines.getById('new0'));
var placeArgs = env.placeUnit.lastArguments();
assert.strictEqual(placeArgs[0].id, 'test/1');
assert.equal(placeArgs[1], 'new0');
});
it('creates new container when dropped on container header', function() {
view.render();
var toggleStub = utils.makeStubMethod(view, '_toggleAllPlacedMessage');
this._cleanups.push(toggleStub.reset);
view.set('selectedMachine', '5');
view._unitTokenDropHandler({
dropAction: 'container',
unit: 'test/1'
});
var env = view.get('env');
// The create container options should be visible
var createView = container.one('.create-machine-view');
assert.equal(createView.hasClass('create-machine-view'), true,
'expected class is not present');
assert.equal(createView.getHTML() === '', false,
'HTML is not present');
// Make sure the correct container type is selected
var select = createView.one('select');
select.all('option').each(function(option, index) {
var value = option.get('value');
if (value === 'lxc') {
select.getDOMNode().selectedIndex = index;
}
});
// Confirm the container creation
createView.one('select').simulate('change');
createView.one('.create').simulate('click');
assert.deepEqual(env.addMachines.lastArguments()[0], [{
containerType: 'lxc',
parentId: '5',
constraints: {
'cpu-power': '',
mem: '',
'root-disk': ''
}
}], 'Args passed to addMachines are incorrect');
// A new ghost machine has been added to the database.
assert.notEqual(machines.getById('5/lxc/new0'), null,
'new container is not in the database');
var placeArgs = env.placeUnit.lastArguments();
assert.strictEqual(placeArgs[0].id, 'test/1',
'the unit ID passed to placeUnit is incorrect');
assert.equal(placeArgs[1], '5/lxc/new0',
'the container ID passed to placeUnit is incorrect');
});
it('displays new container form when dropped on a machine', function() {
view.render();
var toggleStub = utils.makeStubMethod(view, '_toggleAllPlacedMessage');
this._cleanups.push(toggleStub.reset);
view._unitTokenDropHandler({
dropAction: 'container',
targetId: '0',
unit: 'test/1'
});
// The create container options should be visible
var createView = container.one('.create-machine-view');
assert.equal(createView.hasClass('create-machine-view'), true,
'expected class is not present');
assert.equal(createView.getHTML() === '', false,
'HTML is not present');
});
it('places the unit on an already existing container', function() {
var toggleStub = utils.makeStubMethod(view, '_toggleAllPlacedMessage');
this._cleanups.push(toggleStub.reset);
view._machinesHeader = {
setNotDroppable: utils.makeStubFunction(),
updateLabelCount: utils.makeStubFunction()
};
view._containersHeader = {
setNotDroppable: utils.makeStubFunction()
};
view._unitTokenDropHandler({
dropAction: 'container',
targetId: '0/lxc/1',
unit: 'test/1'
});
var env = view.get('env');
// The machine is already created so we don't need to create a new one.
assert.equal(env.addMachines.callCount(), 0);
var placeArgs = env.placeUnit.lastArguments();
assert.strictEqual(placeArgs[0].id, 'test/1');
assert.equal(placeArgs[1], '0/lxc/1');
});
});
describe('unplaced units column', function() {
beforeEach(function() {
view.set('env', {
addMachines: utils.makeStubFunction('add-machines-record-key'),
placeUnit: utils.makeStubFunction()
});
});
it('should render a list of units', function() {
view.render();
var list = container.all('.unplaced .unplaced-unit');
assert.equal(list.size(), units.size(),
'models are out of sync with displayed list');
list.each(function(item, index) {
var u = units.item(index),
id = item.getAttribute('data-id');
assert.equal(id, u.id, 'displayed item does not match model');
});
});
it('displays a message when there are no unplaced units', function() {
var view = createViewNoUnits();
view.render();
var message = view.get('container').one('.column.unplaced .all-placed');
assert.equal(message.getStyle('display'), 'block');
});
it('doesn\'t show a message when there are no unplaced units', function() {
view.render();
var message = view.get('container').one('.column.unplaced .all-placed');
assert.equal(message.getStyle('display'), 'none');
});
it('should add new tokens when units are added', function() {
view.render();
var selector = '.unplaced .unplaced-unit',
list = container.all(selector),
id = 'test/2';
assert.equal(list.size(), units.size(),
'initial displayed list is out of sync with unplaced units');
units.add([{ id: id }]);
list = container.all(selector);
assert.equal(list.size(), units.size(),
'final displayed list is out of sync with unplaced units');
var addedItem = container.one(selector + '[data-id="' + id + '"]');
assert.notEqual(addedItem, null,
'unable to find added unit in the displayed list');
});
it('should remove tokens when units are deleted', function() {
view.render();
var selector = '.unplaced .unplaced-unit',
list = container.all(selector);
assert.equal(list.size(), units.size(),
'initial displayed list is out of sync with unplaced units');
units.remove(0);
list = container.all(selector);
assert.equal(list.size(), units.size(),
'final displayed list is out of sync with unplaced units');
var deletedSelector = selector + '[data-id="test/1"]';
var deletedItem = container.one(deletedSelector);
assert.equal(deletedItem, null,
'found the deleted unit still in the list');
});
it('should re-render token when a unit is updated', function() {
view.render();
var name = 'scooby',
unitModel = units.revive(0),
id = unitModel.get('id'),
selector = '.unplaced .unplaced-unit[data-id="{id}"]',
item;
selector = Y.Lang.sub(selector, {id: id});
item = container.one(selector);
assert.notEqual(item, null, 'unit was not initially displayed');
unitModel.set('displayName', name);
item = container.one(selector);
assert.notEqual(item, null, 'unit was not displayed post-update');
assert.equal(name, item.one('.title').get('text').trim(),
'unit did not have the updated name');
});
it('can move the unit to a new machine', function(done) {
view.render();
var env = view.get('env');
var unplacedUnit = container.one('.unplaced .unplaced-unit');
var constraints = {
'cpu-power': '',
mem: '',
'root-disk': ''
};
unplacedUnit.on('moveToken', function(e) {
view._placeServiceUnit(e);
assert.deepEqual(env.addMachines.lastArguments()[0], [{
containerType: undefined,
parentId: null,
constraints: constraints
}], 'A new machine should have been created');
var placeArgs = env.placeUnit.lastArguments();
assert.strictEqual(placeArgs[0].id, 'test/1',
'The correct unit should be placed');
assert.equal(placeArgs[1], 'new0',
'The unit should be placed on the new machine');
done();
});
// Move the unit.
unplacedUnit.fire('moveToken', {
unit: {id: 'test/1'},
machine: 'new',
container: undefined,
constraints: constraints
});
});
it('can move the unit to a new machine with constraints', function(done) {
view.render();
var env = view.get('env');
var unplacedUnit = container.one('.unplaced .unplaced-unit');
var constraints = {
'cpu-power': '2',
mem: '4',
'root-disk': '6'
};
unplacedUnit.on('moveToken', function(e) {
view._placeServiceUnit(e);
assert.deepEqual(env.addMachines.lastArguments()[0], [{
containerType: undefined,
parentId: null,
constraints: constraints
}], 'A new machine should have been created');
var placeArgs = env.placeUnit.lastArguments();
assert.strictEqual(placeArgs[0].id, 'test/1',
'The correct unit should be placed');
assert.equal(placeArgs[1], 'new0',
'The unit should be placed on the new machine');
done();
});
// Move the unit.
unplacedUnit.fire('moveToken', {
unit: {id: 'test/1'},
machine: 'new',
container: undefined,
constraints: constraints
});
});
it('can move the unit to a new kvm container', function(done) {
view.render();
var env = view.get('env');
var unplacedUnit = container.one('.unplaced .unplaced-unit');
var constraints = {
'cpu-power': '4',
mem: '5',
'root-disk': '6'
};
unplacedUnit.on('moveToken', function(e) {
view._placeServiceUnit(e);
assert.deepEqual(env.addMachines.lastArguments()[0], [{
containerType: 'kvm',
parentId: '4',
constraints: constraints
}], 'A new container should have been created');
var placeArgs = env.placeUnit.lastArguments();
assert.strictEqual(placeArgs[0].id, 'test/1',
'The correct unit should be placed');
assert.equal(placeArgs[1], '4/kvm/new0',
'The unit should be placed on the new container');
done();
});
// Move the unit.
unplacedUnit.fire('moveToken', {
unit: {id: 'test/1'},
machine: '4',
container: 'kvm',
constraints: constraints
});
});
it('can move the unit to a new lxc container', function(done) {
view.render();
var env = view.get('env');
var unplacedUnit = container.one('.unplaced .unplaced-unit');
unplacedUnit.on('moveToken', function(e) {
view._placeServiceUnit(e);
assert.deepEqual(env.addMachines.lastArguments()[0], [{
containerType: 'lxc',
parentId: '42',
constraints: {}
}], 'A new container should have been created');
var placeArgs = env.placeUnit.lastArguments();
assert.strictEqual(placeArgs[0].id, 'test/1',
'The correct unit should be placed');
assert.equal(placeArgs[1], '42/lxc/new0',
'The unit should be placed on the new container');
done();
});
// Move the unit.
unplacedUnit.fire('moveToken', {
unit: {id: 'test/1'},
machine: '42',
container: 'lxc',
constraints: {}
});
});
it('can move the unit to the root container', function(done) {
view.render();
var env = view.get('env');
var unplacedUnit = container.one('.unplaced .unplaced-unit');
var constraints = {
'cpu-power': '',
mem: '',
'root-disk': ''
};
unplacedUnit.on('moveToken', function(e) {
view._placeServiceUnit(e);
assert.equal(env.addMachines.lastArguments(), undefined,
'No machines or containers should have been created');
var placeArgs = env.placeUnit.lastArguments();
assert.strictEqual(placeArgs[0].id, 'test/1',
'The correct unit should be placed');
assert.equal(placeArgs[1], '4',
'The unit should be placed on the root container');
done();
});
// Move the unit.
unplacedUnit.fire('moveToken', {
unit: {id: 'test/1'},
machine: '4',
container: 'root-container',
constraints: constraints
});
});
it('can move the unit to a container', function(done) {
view.render();
var env = view.get('env');
var unplacedUnit = container.one('.unplaced .unplaced-unit');
var constraints = {
'cpu-power': '',
mem: '',
'root-disk': ''
};
unplacedUnit.on('moveToken', function(e) {
view._placeServiceUnit(e);
assert.equal(env.addMachines.lastArguments(), undefined,
'No machines or containers should have been created');
var placeArgs = env.placeUnit.lastArguments();
assert.strictEqual(placeArgs[0].id, 'test/1',
'The correct unit should be placed');
assert.equal(placeArgs[1], '4/lxc/2',
'The unit should be placed on the container');
done();
});
// Move the unit.
unplacedUnit.fire('moveToken', {
unit: {id: 'test/1'},
machine: '4',
container: '4/lxc/2',
constraints: constraints
});
});
it('removes the unit token when it has been placed', function(done) {
view.render();
var unplacedUnit = container.one('.unplaced .unplaced-unit');
var unitTokens = view.get('unitTokens');
assert.equal(Object.keys(unitTokens).length, 1);
unplacedUnit.on('moveToken', function(e) {
view._placeServiceUnit(e);
assert.equal(Object.keys(unitTokens).length, 0);
assert.equal(container.one('.unplaced .unplaced-unit'), null);
done();
});
// Move the unit.
unplacedUnit.fire('moveToken', {
unit: {id: 'test/1'},
machine: 'new',
container: undefined,
constraints: {}
});
});
it('does not show more than one placement form at once', function() {
view.render();
units.add([{id: 'test/5'}]);
var firstToken = container.all('.serviceunit-token').item(0);
var secondToken = container.all('.serviceunit-token').item(1);
firstToken.one('.token-move').simulate('click');
assert.equal(firstToken.hasClass('state-select-machine'), true);
assert.equal(secondToken.hasClass('state-select-machine'), false);
// Clicking on a different token should make the second token
// active, and reset the first token.
secondToken.one('.token-move').simulate('click');
assert.equal(firstToken.hasClass('state-select-machine'), false);
assert.equal(secondToken.hasClass('state-select-machine'), true);
});
});
describe('machine column', function() {
it('should render a list of machines', function() {
view.render();
var list = container.all('.machines .content li');
assert.equal(list.size(), machines.size(),
'models are out of sync with displayed list');
list.each(function(item, index) {
var m = machines.item(index);
assert.equal(item.one('.title').get('text').replace(/\s+/g, ' ').trim(),
m.displayName + ' °', 'displayed item does not match model');
});
});
it('should set the correct machine count in the header', function() {
var label = '1 machine';
view.render();
assert.equal(view._machinesHeader.get(
'container').one('.label').get('text'), label);
});
it('should select the first machine by default', function() {
view.render();
assert.equal(container.one(
'.machines .content li:first-child .token').hasClass('active'),
true, 'the first machine token should be selected');
});
it('should add new tokens when machines are added', function() {
view.render();
var selector = '.machines .token',
list = container.all(selector),
id = '1';
assert.equal(list.size(), machines.size(),
'initial displayed list is out of sync with machines');
machines.add([{
id: id,
parentId: null,
hardware: {
disk: 1024,
mem: 1024,
cpuPower: 1024,
cpuCores: 1
}
}]);
list = container.all(selector);
assert.equal(list.size(), machines.size(),
'final displayed list is out of sync with machines');
var addedItem = container.one(selector + '[data-id="' + id + '"]');
assert.notEqual(addedItem, null,
'unable to find added machine in the displayed list');
});
it('should remove tokens when machines are deleted', function() {
view.render();
var selector = '.machines .token',
list = container.all(selector);
assert.equal(list.size(), machines.size(),
'initial displayed list is out of sync with machines');
machines.remove(0);
list = container.all(selector);
assert.equal(list.size(), machines.size(),
'final displayed list is out of sync with machines');
var deletedSelector = selector + '[data-id="' + machine.id + '"]';
var deletedItem = container.one(deletedSelector);
assert.equal(deletedItem, null,
'found the deleted machine still in the list');
});
it('should re-render token when machine is updated', function() {
view.render();
var id = 999,
machineModel = machines.revive(0),
selector = '.machines .token',
item = container.one(
selector + '[data-id="' + machineModel.get('id') + '"]');
assert.notEqual(item, null, 'machine was not initially displayed');
assert.equal(
item.one('.title').get('text').replace(/\s+/g, ' ').trim(),
machineModel.get('displayName') + ' °',
'initial machine names do not match');
machineModel.set('id', id);
item = container.one(selector + '[data-id="' + id + '"]');
assert.notEqual(item, null, 'machine was not displayed post-update');
assert.equal(
item.one('.title').get('text').replace(/\s+/g, ' ').trim(),
machineModel.get('displayName') + ' °',
'machine names do not match post-update');
});
it('should set the correct counts in the container header', function(done) {
var labels = ['2 containers', '1 unit'];
view.render();
var machineToken = container.one('.machines li .token');
machines.add([
{id: '0/lxc/1'},
{id: '0/lxc/2'}
]);
// Add a unit to the machine.
view.get('db').units.add([{id: 'test/2', machine: '0'}]);
machineToken.on('click', function(e) {
// Need to explicitly fire the click handler as we are catching
// the click event before it can be fired.
view.handleMachineTokenSelect(e);
labels.forEach(function(l, index) {
assert.equal(view._containersHeader.get(
'container').all('.label').item(index).get('text'), l);
});
done();
});
machineToken.simulate('click');
});
it('should select a token when clicked', function() {
view.render();
machines.add([
{id: '0/lxc/1'}
]);
// Click on a machine so that we have a list of containers.
container.one('.machines li .token').simulate('click');
// Now select a container.
var containerToken = container.one('.containers li .token');
containerToken.simulate('click');
assert.equal(containerToken.hasClass('active'), true);
});
it('should annotate units with icons for its tokens', function() {
var db = view.get('db'),
unit = {id: 'foo/0', service: 'foo' };
db.services.add({ id: 'foo', icon: 'http://example.com/foo.png' });
unit = view._addIconsToUnits([unit])[0];
assert.equal(unit.icon, 'http://example.com/foo.png');
});
it('should log an error when it cannot find the service', function() {
var unit = {id: 'foo/0', service: 'foo' },
errorStub = utils.makeStubMethod(console, 'error');
this._cleanups.push(errorStub.reset);
unit = view._addIconsToUnits([unit])[0];
assert.equal(errorStub.calledOnce(), true);
assert.equal(errorStub.lastArguments(), 'Unit foo/0 has no service.');
});
it('should update a machine when a new unit is placed', function() {
view.render();
var machineModel = machines.revive(0),
unitModel = units.revive(0),
id = machineModel.get('id'),
selector = Y.Lang.sub('.machines .token[data-id="{id}"]', {id: id}),
item = container.one(selector);
assert.notEqual(item, null, 'machine was not initially displayed');
assert.equal(item.one('img.unit'), null,
'machine should not have any unit icons initially');
unitModel.set('machine', id);
item = container.one(selector);
assert.notEqual(item, null, 'machine was not displayed post-update');
var icon = item.one('img.unit');
assert.equal(icon.getAttribute('src'), 'test.svg',
'machine should display icons post-update');
});
it('should add tokens when containers are added', function() {
view.render();
var selector = '.containers .token',
list = container.all(selector),
id = '2/foo/999',
initialSize = list.size();
assert.equal(initialSize, 1,
'check the initial size');
machines.add([
{ id: id }
]);
list = container.all(selector);
assert.equal(list.size(), 2, 'list should have updated');
assert.equal(container.all(selector).item(1).getData('id'), '2/foo/999',
'the container should be in the displayed list');
});
it('should remove tokens when containers are removed', function() {
var id = '2/foo/999';
view.render();
machines.add([{ id: id }]);
var selector = '.containers .token',
list = container.all(selector),
initialSize = list.size(),
m = machines.getById(id);
assert.equal(initialSize, 2,
'check the initial size');
machines.remove(m);
list = container.all(selector);
assert.equal(list.size(), 1,
'list should have changed in size');
var deletedSelector = selector + '[data-id="' + id + '"]';
var deletedItem = container.one(deletedSelector);
assert.equal(deletedItem, null,
'found the deleted container in the list');
});
it('should update tokens when containers are changed', function() {
var id = '2/foo/999';
view.render();
machines.add([{ id: id }]);
var selector = '.containers .token',
list = container.all(selector),
initialSize = list.size(),
m = machines.getById(id);
var oldItem = container.one(selector + '[data-id="' + id + '"]');
assert.equal(initialSize, 2,
'check the initial size');