forked from uyuni-project/uyuni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SystemHandlerTest.java
3040 lines (2459 loc) · 121 KB
/
SystemHandlerTest.java
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
/**
* Copyright (c) 2009--2017 Red Hat, Inc.
*
* This software is licensed to you under the GNU General Public License,
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
* along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* Red Hat trademarks are not licensed under GPLv2. No permission is
* granted to use or replicate Red Hat trademarks that are incorporated
* in this software or its documentation.
*/
package com.redhat.rhn.frontend.xmlrpc.system.test;
import com.redhat.rhn.FaultException;
import com.redhat.rhn.common.client.ClientCertificate;
import com.redhat.rhn.common.db.datasource.DataResult;
import com.redhat.rhn.common.db.datasource.ModeFactory;
import com.redhat.rhn.common.db.datasource.WriteMode;
import com.redhat.rhn.common.hibernate.HibernateFactory;
import com.redhat.rhn.common.localization.LocalizationService;
import com.redhat.rhn.domain.action.Action;
import com.redhat.rhn.domain.action.ActionFactory;
import com.redhat.rhn.domain.action.channel.SubscribeChannelsAction;
import com.redhat.rhn.domain.action.salt.ApplyStatesAction;
import com.redhat.rhn.domain.action.salt.ApplyStatesActionDetails;
import com.redhat.rhn.domain.action.script.ScriptActionDetails;
import com.redhat.rhn.domain.action.script.ScriptResult;
import com.redhat.rhn.domain.action.script.ScriptRunAction;
import com.redhat.rhn.domain.action.virtualization.VirtualizationSetMemoryGuestAction;
import com.redhat.rhn.domain.action.virtualization.VirtualizationSetVcpusGuestAction;
import com.redhat.rhn.domain.channel.Channel;
import com.redhat.rhn.domain.channel.ChannelFactory;
import com.redhat.rhn.domain.channel.test.ChannelFactoryTest;
import com.redhat.rhn.domain.entitlement.Entitlement;
import com.redhat.rhn.domain.errata.Errata;
import com.redhat.rhn.domain.errata.ErrataFactory;
import com.redhat.rhn.domain.errata.test.ErrataFactoryTest;
import com.redhat.rhn.domain.kickstart.KickstartData;
import com.redhat.rhn.domain.kickstart.KickstartFactory;
import com.redhat.rhn.domain.kickstart.test.KickstartDataTest;
import com.redhat.rhn.domain.org.CustomDataKey;
import com.redhat.rhn.domain.org.Org;
import com.redhat.rhn.domain.org.test.CustomDataKeyTest;
import com.redhat.rhn.domain.product.SUSEProductFactory;
import com.redhat.rhn.domain.product.test.SUSEProductTestUtils;
import com.redhat.rhn.domain.rhnpackage.Package;
import com.redhat.rhn.domain.rhnpackage.PackageArch;
import com.redhat.rhn.domain.rhnpackage.PackageFactory;
import com.redhat.rhn.domain.rhnpackage.profile.Profile;
import com.redhat.rhn.domain.rhnpackage.profile.ProfileFactory;
import com.redhat.rhn.domain.rhnpackage.test.PackageTest;
import com.redhat.rhn.domain.role.RoleFactory;
import com.redhat.rhn.domain.server.CPU;
import com.redhat.rhn.domain.server.CustomDataValue;
import com.redhat.rhn.domain.server.Device;
import com.redhat.rhn.domain.server.Dmi;
import com.redhat.rhn.domain.server.InstalledPackage;
import com.redhat.rhn.domain.server.InstalledProduct;
import com.redhat.rhn.domain.server.ManagedServerGroup;
import com.redhat.rhn.domain.server.MinionServer;
import com.redhat.rhn.domain.server.MinionServerFactory;
import com.redhat.rhn.domain.server.NetworkInterface;
import com.redhat.rhn.domain.server.NetworkInterfaceFactory;
import com.redhat.rhn.domain.server.Note;
import com.redhat.rhn.domain.server.Server;
import com.redhat.rhn.domain.server.ServerConstants;
import com.redhat.rhn.domain.server.ServerFactory;
import com.redhat.rhn.domain.server.ServerGroup;
import com.redhat.rhn.domain.server.ServerGroupFactory;
import com.redhat.rhn.domain.server.ServerHistoryEvent;
import com.redhat.rhn.domain.server.VirtualInstance;
import com.redhat.rhn.domain.server.VirtualInstanceFactory;
import com.redhat.rhn.domain.server.test.CPUTest;
import com.redhat.rhn.domain.server.test.GuestBuilder;
import com.redhat.rhn.domain.server.test.MinionServerFactoryTest;
import com.redhat.rhn.domain.server.test.NetworkInterfaceTest;
import com.redhat.rhn.domain.server.test.ServerFactoryTest;
import com.redhat.rhn.domain.state.PackageState;
import com.redhat.rhn.domain.state.PackageStates;
import com.redhat.rhn.domain.state.ServerStateRevision;
import com.redhat.rhn.domain.state.StateFactory;
import com.redhat.rhn.domain.state.VersionConstraints;
import com.redhat.rhn.domain.token.ActivationKey;
import com.redhat.rhn.domain.token.test.ActivationKeyTest;
import com.redhat.rhn.domain.user.User;
import com.redhat.rhn.domain.user.UserFactory;
import com.redhat.rhn.frontend.dto.ErrataOverview;
import com.redhat.rhn.frontend.dto.HistoryEvent;
import com.redhat.rhn.frontend.dto.OperationDetailsDto;
import com.redhat.rhn.frontend.dto.PackageMetadata;
import com.redhat.rhn.frontend.dto.ScheduledAction;
import com.redhat.rhn.frontend.dto.ServerPath;
import com.redhat.rhn.frontend.dto.ShortSystemInfo;
import com.redhat.rhn.frontend.dto.SystemOverview;
import com.redhat.rhn.frontend.events.SsmDeleteServersAction;
import com.redhat.rhn.frontend.xmlrpc.ChannelSubscriptionException;
import com.redhat.rhn.frontend.xmlrpc.InvalidActionTypeException;
import com.redhat.rhn.frontend.xmlrpc.InvalidChannelException;
import com.redhat.rhn.frontend.xmlrpc.InvalidChannelLabelException;
import com.redhat.rhn.frontend.xmlrpc.InvalidEntitlementException;
import com.redhat.rhn.frontend.xmlrpc.InvalidErrataException;
import com.redhat.rhn.frontend.xmlrpc.InvalidPackageException;
import com.redhat.rhn.frontend.xmlrpc.InvalidParameterException;
import com.redhat.rhn.frontend.xmlrpc.MissingCapabilityException;
import com.redhat.rhn.frontend.xmlrpc.NoSuchActionException;
import com.redhat.rhn.frontend.xmlrpc.NoSuchPackageException;
import com.redhat.rhn.frontend.xmlrpc.NoSuchSystemException;
import com.redhat.rhn.frontend.xmlrpc.PermissionCheckFailureException;
import com.redhat.rhn.frontend.xmlrpc.UndefinedCustomFieldsException;
import com.redhat.rhn.frontend.xmlrpc.UnsupportedOperationException;
import com.redhat.rhn.frontend.xmlrpc.system.SUSEInstalledProduct;
import com.redhat.rhn.frontend.xmlrpc.system.SystemHandler;
import com.redhat.rhn.frontend.xmlrpc.system.XmlRpcSystemHelper;
import com.redhat.rhn.frontend.xmlrpc.test.BaseHandlerTestCase;
import com.redhat.rhn.manager.action.ActionChainManager;
import com.redhat.rhn.manager.action.ActionManager;
import com.redhat.rhn.manager.channel.ChannelManager;
import com.redhat.rhn.manager.entitlement.EntitlementManager;
import com.redhat.rhn.manager.errata.cache.ErrataCacheManager;
import com.redhat.rhn.manager.formula.FormulaMonitoringManager;
import com.redhat.rhn.manager.profile.ProfileManager;
import com.redhat.rhn.manager.rhnpackage.test.PackageManagerTest;
import com.redhat.rhn.manager.ssm.SsmOperationManager;
import com.redhat.rhn.manager.system.ServerGroupManager;
import com.redhat.rhn.manager.system.SystemManager;
import com.redhat.rhn.manager.system.entitling.SystemEntitlementManager;
import com.redhat.rhn.manager.system.entitling.SystemEntitler;
import com.redhat.rhn.manager.system.entitling.SystemUnentitler;
import com.redhat.rhn.manager.system.test.SystemManagerTest;
import com.redhat.rhn.manager.user.UserManager;
import com.redhat.rhn.taskomatic.TaskomaticApi;
import com.redhat.rhn.testing.ChannelTestUtils;
import com.redhat.rhn.testing.ServerGroupTestUtils;
import com.redhat.rhn.testing.ServerTestUtils;
import com.redhat.rhn.testing.TestUtils;
import com.redhat.rhn.testing.UserTestUtils;
import com.suse.manager.virtualization.VirtManagerSalt;
import com.suse.manager.webui.controllers.utils.RegularMinionBootstrapper;
import com.suse.manager.webui.controllers.utils.SSHMinionBootstrapper;
import com.suse.manager.webui.services.iface.*;
import com.suse.manager.webui.services.test.TestSaltApi;
import com.suse.manager.webui.services.test.TestSystemQuery;
import org.apache.commons.lang3.StringUtils;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.imposters.ByteBuddyClassImposteriser;
import org.jmock.integration.junit3.JUnit3Mockery;
import org.jmock.lib.concurrent.Synchroniser;
import org.yaml.snakeyaml.Yaml;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static com.suse.manager.webui.services.SaltConstants.PILLAR_DATA_FILE_PREFIX;
import static com.suse.manager.webui.services.SaltConstants.PILLAR_DATA_FILE_EXT;
public class SystemHandlerTest extends BaseHandlerTestCase {
private TaskomaticApi taskomaticApi = new TaskomaticApi();
private final SystemQuery systemQuery = new TestSystemQuery();
private final SaltApi saltApi = new TestSaltApi();
private RegularMinionBootstrapper regularMinionBootstrapper = new RegularMinionBootstrapper(systemQuery, saltApi);
private SSHMinionBootstrapper sshMinionBootstrapper = new SSHMinionBootstrapper(systemQuery, saltApi);
private XmlRpcSystemHelper xmlRpcSystemHelper = new XmlRpcSystemHelper(
regularMinionBootstrapper,
sshMinionBootstrapper
);
private final ServerGroupManager serverGroupManager = new ServerGroupManager();
private final VirtManager virtManager = new VirtManagerSalt(saltApi);
private final MonitoringManager monitoringManager = new FormulaMonitoringManager();
private final SystemEntitlementManager systemEntitlementManager = new SystemEntitlementManager(
new SystemUnentitler(virtManager, monitoringManager, serverGroupManager),
new SystemEntitler(saltApi, virtManager, monitoringManager, serverGroupManager)
);
private SystemManager systemManager = new SystemManager(ServerFactory.SINGLETON, ServerGroupFactory.SINGLETON);
private SystemHandler handler =
new SystemHandler(taskomaticApi, xmlRpcSystemHelper, systemEntitlementManager, systemManager,
new ServerGroupManager());
private final Mockery MOCK_CONTEXT = new JUnit3Mockery() {{
setThreadingPolicy(new Synchroniser());
setImposteriser(ByteBuddyClassImposteriser.INSTANCE);
}};
@Override
public void setUp() throws Exception {
super.setUp();
}
public void testGetNetworkDevices() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true);
server.getNetworkInterfaces().clear();
NetworkInterface device = NetworkInterfaceTest.createTestNetworkInterface(server);
server.addNetworkInterface(device);
assertEquals(1, server.getNetworkInterfaces().size());
TestUtils.saveAndFlush(server);
List<NetworkInterface> results = handler.getNetworkDevices(admin, server.getId().intValue());
assertEquals(1, results.size());
NetworkInterface dev = results.get(0);
assertEquals(device.getName(), dev.getName());
assertEquals(device.getIPv4Addresses(), dev.getIPv4Addresses());
}
public void testGetNetworkForSystems() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true);
server.addFqdn("domain1.test.local");
server.addFqdn("domain2.test.local");
ServerFactory.save(server);
List<Map<String, Object>> returned = handler.getNetworkForSystems(admin,
Collections.singletonList(server.getId().intValue()));
assertEquals(1, returned.size());
assertEquals("domain1.test.local", returned.get(0).get("primary_fqdn"));
}
public void testObtainReactivationKey() throws Exception {
Server server = ServerFactoryTest.createUnentitledTestServer(admin, true,
ServerFactoryTest.TYPE_SERVER_NORMAL, new Date());
//since we can't really test this without giving the server entitlements, just
//make sure we get a permission exception
try {
handler.obtainReactivationKey(admin, server.getId().intValue());
fail("SystemHandler.obtainReactivationKey allowed key to be generated for " +
"system without agent smith feature.");
}
catch (PermissionCheckFailureException e) {
//success
}
}
public void testObtainReactivationKeyWithCert() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true);
ClientCertificate cert = SystemManager.createClientCertificate(server);
cert.validate(server.getSecret());
assertFalse(StringUtils.isBlank(handler.obtainReactivationKey(cert.toString())));
}
public void xxxtestUpgradeEntitlement() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true,
ServerConstants.getServerGroupTypeEnterpriseEntitled());
Entitlement ent = EntitlementManager.VIRTUALIZATION;
assertTrue(systemEntitlementManager.canEntitleServer(server, ent));
int result = handler.upgradeEntitlement(admin,
server.getId().intValue(),
ent.getLabel());
assertEquals(1, result);
assertFalse(systemEntitlementManager.canEntitleServer(server, ent));
//Cause PermissionCheckFailureException
try {
result = handler.upgradeEntitlement(admin,
server.getId().intValue(),
ent.getLabel());
fail("SystemHandler.upgradeEntitlement allowed upgrade when canEntitleServer " +
"evalueated to false.");
}
catch (PermissionCheckFailureException e) {
//success
}
//Cause InvalidEntitlementException
try {
result = handler.upgradeEntitlement(admin,
server.getId().intValue(),
TestUtils.randomString());
fail("SystemHandler.upgradeEntitlement allowed upgrade to phoney entitlement");
}
catch (InvalidEntitlementException e) {
//success
}
}
public void testSetChildChannelsDeprecated() throws Exception {
// the usage of setChildChannels API as tested by this junit where
// channel ids are passed as arguments is being deprecated...
Server server = ServerFactoryTest.createTestServer(admin, true);
assertNull(server.getBaseChannel());
Integer sid = server.getId().intValue();
Channel base = ChannelFactoryTest.createTestChannel(admin);
base.setParentChannel(null);
Channel child1 = ChannelFactoryTest.createTestChannel(admin);
child1.setParentChannel(base);
Channel child2 = ChannelFactoryTest.createTestChannel(admin);
child2.setParentChannel(base);
//subscribe to base channel.
SystemManager.subscribeServerToChannel(admin, server, base);
server = reload(server);
assertNotNull(server.getBaseChannel());
List cids = new ArrayList();
cids.add(child1.getId().intValue());
cids.add(child2.getId().intValue());
int result = handler.setChildChannels(admin, sid, cids);
server = TestUtils.reload(server);
assertEquals(1, result);
assertEquals(3, server.getChannels().size());
//Try 'unsubscribing' from child1...
cids = new ArrayList();
cids.add(child2.getId().intValue());
assertEquals(1, cids.size());
result = handler.setChildChannels(admin, sid, cids);
server = TestUtils.reload(server);
assertEquals(1, result);
assertEquals(2, server.getChannels().size());
//Try putting an invalid channel in there
cids = new ArrayList();
cids.add(-32339);
assertEquals(1, cids.size());
try {
result = handler.setChildChannels(admin, sid, cids);
fail("SystemHandler.setChildChannels allowed invalid child channel to be set.");
}
catch (InvalidChannelException e) {
//success
}
assertEquals(2, server.getChannels().size());
Channel base2 = ChannelFactoryTest.createTestChannel(admin);
base2.setParentChannel(null);
cids = new ArrayList();
cids.add(base2.getId().intValue());
assertEquals(1, cids.size());
try {
result = handler.setChildChannels(admin, sid, cids);
fail("SystemHandler.setChildChannels allowed invalid child channel to be set.");
}
catch (InvalidChannelException e) {
//success
}
catch (ChannelSubscriptionException e) {
//success
}
server = reload(server);
assertEquals(2, server.getChannels().size());
// try setting the base channel of an s390 server to
// IA-32.
try {
List ia32Children = new ArrayList();
ia32Children.add(child1.getId().intValue());
ia32Children.add(child2.getId().intValue());
// change the arch of the server
server.setServerArch(
ServerFactory.lookupServerArchByLabel("s390-redhat-linux"));
ServerFactory.save(server);
result = handler.setChildChannels(admin, sid, ia32Children);
fail("allowed invalid child channel to be set.");
}
catch (InvalidChannelException e) {
// success
}
}
public void testSetChildChannels() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true);
assertNull(server.getBaseChannel());
Integer sid = server.getId().intValue();
Channel base = ChannelFactoryTest.createTestChannel(admin);
base.setParentChannel(null);
Channel child1 = ChannelFactoryTest.createTestChannel(admin);
child1.setParentChannel(base);
Channel child2 = ChannelFactoryTest.createTestChannel(admin);
child2.setParentChannel(base);
//subscribe to base channel.
SystemManager.subscribeServerToChannel(admin, server, base);
server = reload(server);
assertNotNull(server.getBaseChannel());
List channelLabels = new ArrayList();
channelLabels.add(new String(child1.getLabel()));
channelLabels.add(new String(child2.getLabel()));
int result = handler.setChildChannels(admin, sid, channelLabels);
server = TestUtils.reload(server);
assertEquals(1, result);
assertEquals(3, server.getChannels().size());
//Try 'unsubscribing' from child1...
channelLabels = new ArrayList();
channelLabels.add(new String(child2.getLabel()));
assertEquals(1, channelLabels.size());
result = handler.setChildChannels(admin, sid, channelLabels);
server = TestUtils.reload(server);
assertEquals(1, result);
assertEquals(2, server.getChannels().size());
//Try putting an invalid channel in there
channelLabels = new ArrayList();
channelLabels.add(new String("invalid-unknown-channel-label"));
assertEquals(1, channelLabels.size());
try {
result = handler.setChildChannels(admin, sid, channelLabels);
fail("SystemHandler.setChildChannels allowed invalid child channel to be set.");
}
catch (InvalidChannelLabelException e) {
//success
}
assertEquals(2, server.getChannels().size());
Channel base2 = ChannelFactoryTest.createTestChannel(admin);
base2.setParentChannel(null);
channelLabels = new ArrayList();
channelLabels.add(new String(base2.getLabel()));
assertEquals(1, channelLabels.size());
try {
result = handler.setChildChannels(admin, sid, channelLabels);
fail("SystemHandler.setChildChannels allowed invalid child channel to be set.");
}
catch (InvalidChannelException e) {
//success
}
catch (ChannelSubscriptionException e) {
//success
}
server = reload(server);
assertEquals(2, server.getChannels().size());
// try setting the base channel of an s390 server to
// IA-32.
try {
List ia32Children = new ArrayList();
ia32Children.add(new String(child1.getLabel()));
ia32Children.add(new String(child2.getLabel()));
// change the arch of the server
server.setServerArch(
ServerFactory.lookupServerArchByLabel("s390-redhat-linux"));
ServerFactory.save(server);
result = handler.setChildChannels(admin, sid, ia32Children);
fail("allowed invalid child channel to be set.");
}
catch (InvalidChannelException e) {
// success
}
}
public void testSetBaseChannelDeprecated() throws Exception {
// the setBaseChannel API tested by this junit is being deprecated
Server server = ServerFactoryTest.createTestServer(admin, true);
assertNull(server.getBaseChannel());
Integer sid = server.getId().intValue();
Channel base1 = ChannelFactoryTest.createTestChannel(admin);
base1.setParentChannel(null);
Channel base2 = ChannelFactoryTest.createTestChannel(admin);
base2.setParentChannel(null);
Channel child1 = ChannelFactoryTest.createTestChannel(admin);
child1.setParentChannel(base1);
// Set base channel to base1
int result = handler.setBaseChannel(admin, sid,
base1.getId().intValue());
server = reload(server);
assertEquals(1, result);
assertNotNull(server.getBaseChannel());
assertEquals(server.getBaseChannel().getLabel(), base1.getLabel());
// Set base channel to base2
result = handler.setBaseChannel(admin, sid,
base2.getId().intValue());
server = TestUtils.reload(server);
assertEquals(1, result);
assertNotNull(server.getBaseChannel());
assertEquals(server.getBaseChannel().getLabel(), base2.getLabel());
// Try setting base channel to child
try {
result = handler.setBaseChannel(admin, sid,
child1.getId().intValue());
fail("SystemHandler.setBaseChannel allowed invalid base channel to be set.");
}
catch (InvalidChannelException e) {
// success
}
assertEquals(server.getBaseChannel().getLabel(), base2.getLabel());
// try setting the base channel of an s390 server to
// IA-32.
try {
// change the arch of the server
server.setServerArch(
ServerFactory.lookupServerArchByLabel("s390-redhat-linux"));
ServerFactory.save(server);
result = handler.setBaseChannel(admin, sid,
base1.getId().intValue());
fail("allowed channel with incompatible arch to be set");
}
catch (InvalidChannelException e) {
// success
}
}
public void testSetBaseChannel() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true);
assertNull(server.getBaseChannel());
Integer sid = server.getId().intValue();
Channel base1 = ChannelFactoryTest.createTestChannel(admin);
base1.setParentChannel(null);
Channel base2 = ChannelFactoryTest.createTestChannel(admin);
base2.setParentChannel(null);
Channel child1 = ChannelFactoryTest.createTestChannel(admin);
child1.setParentChannel(base1);
// Set base channel to base1
int result = handler.setBaseChannel(admin, sid, base1.getLabel());
server = reload(server);
assertEquals(1, result);
assertNotNull(server.getBaseChannel());
assertEquals(server.getBaseChannel().getLabel(), base1.getLabel());
// Set base channel to base2
result = handler.setBaseChannel(admin, sid, base2.getLabel());
server = TestUtils.reload(server);
assertEquals(1, result);
assertNotNull(server.getBaseChannel());
assertEquals(server.getBaseChannel().getLabel(), base2.getLabel());
// Try setting base channel to child
try {
result = handler.setBaseChannel(admin, sid, child1.getLabel());
fail("SystemHandler.setBaseChannel allowed invalid base channel to be set.");
}
catch (InvalidChannelException e) {
// success
}
assertEquals(server.getBaseChannel().getLabel(), base2.getLabel());
// try setting the base channel of an s390 server to
// IA-32.
try {
// change the arch of the server
server.setServerArch(
ServerFactory.lookupServerArchByLabel("s390-redhat-linux"));
ServerFactory.save(server);
result = handler.setBaseChannel(admin, sid, base1.getLabel());
fail("allowed channel with incompatible arch to be set");
}
catch (InvalidChannelException e) {
// success
}
}
public void testScheduleChangeChannels() throws Exception {
SystemHandler handler = getMockedHandler();
ActionChainManager.setTaskomaticApi(handler.getTaskomaticApi());
Server server1 = ServerFactoryTest.createTestServer(admin, true);
Server server2 = ServerFactoryTest.createTestServer(admin, true);
Channel child1 = ChannelFactoryTest.createTestChannel(admin);
Channel child2 = ChannelFactoryTest.createTestChannel(admin);
Channel parent = ChannelFactoryTest.createTestChannel(admin);
child1.setParentChannel(parent);
child2.setParentChannel(parent);
server1.addChannel(parent);
server1.addChannel(child1);
server1.addChannel(child2);
server2.addChannel(parent);
server2.addChannel(child1);
server2.addChannel(child2);
Integer sid1 = server1.getId().intValue();
Integer sid2 = server2.getId().intValue();
Channel base1 = ChannelFactoryTest.createTestChannel(admin);
base1.setParentChannel(null);
Channel child3 = ChannelFactoryTest.createTestChannel(admin);
child3.setParentChannel(base1);
Date earliest = new Date();
var sids = List.of(sid1, sid2);
List<Long> actionIds = handler.scheduleChangeChannels(admin, sids, base1.getLabel(),
Arrays.asList(child3.getLabel()), earliest);
assertEquals(1, actionIds.size());
var actionId = actionIds.get(0);
Action action = ActionFactory.lookupById(actionId);
assertEquals(earliest, action.getEarliestAction());
assertTrue(action instanceof SubscribeChannelsAction);
SubscribeChannelsAction sca = (SubscribeChannelsAction)action;
assertEquals(base1.getId(), sca.getDetails().getBaseChannel().getId());
assertEquals(1, sca.getDetails().getChannels().size());
assertTrue(sca.getDetails().getChannels()
.stream().filter(cc -> cc.getId().equals(child3.getId())).findFirst().isPresent());
var sas = MinionServerFactory.findTradClientServerActions(actionId);
assertEquals(2, sas.size());
sas.stream().allMatch(sa -> sids.contains(sa.getServerId()));
}
public void testScheduleChangeChannelsNoChildren() throws Exception {
SystemHandler handler = getMockedHandler();
ActionChainManager.setTaskomaticApi(handler.getTaskomaticApi());
Server server = ServerFactoryTest.createTestServer(admin, true);
Channel child1 = ChannelFactoryTest.createTestChannel(admin);
Channel child2 = ChannelFactoryTest.createTestChannel(admin);
Channel parent = ChannelFactoryTest.createTestChannel(admin);
child1.setParentChannel(parent);
child2.setParentChannel(parent);
server.addChannel(parent);
server.addChannel(child1);
server.addChannel(child2);
Integer sid = server.getId().intValue();
Channel base1 = ChannelFactoryTest.createTestChannel(admin);
base1.setParentChannel(null);
Date earliest = new Date();
long actionId = handler.scheduleChangeChannels(admin, sid, base1.getLabel(),
Collections.emptyList(), earliest);
Action action = ActionFactory.lookupById(actionId);
assertEquals(earliest, action.getEarliestAction());
assertTrue(action instanceof SubscribeChannelsAction);
SubscribeChannelsAction sca = (SubscribeChannelsAction)action;
assertEquals(base1.getId(), sca.getDetails().getBaseChannel().getId());
assertTrue(sca.getDetails().getChannels().isEmpty());
}
public void testListSubscribableBaseChannels() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true);
Channel base = ChannelFactoryTest.createTestChannel(admin);
base.setParentChannel(null);
SystemManager.subscribeServerToChannel(admin, server, base);
Object[] results = handler.listSubscribableBaseChannels(admin,
server.getId().intValue());
assertTrue(results.length > 0);
//make sure that every channel returned has null for parent_channel
for (int i = 0; i < results.length; i++) {
Map map = (Map) results[i];
Number id = (Number) map.get("id");
Long cid = id.longValue();
Channel c = ChannelManager.lookupByIdAndUser(cid, admin);
assertNull(c.getParentChannel());
}
}
public void testListSubscribableChildChannels() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true);
Channel parent = ChannelFactoryTest.createTestChannel(admin);
parent.setParentChannel(null);
Channel child = ChannelFactoryTest.createTestChannel(admin);
child.setParentChannel(parent);
Object[] result = handler.listSubscribableChildChannels(admin,
server.getId().intValue());
//server shouldn't have any channels yet
assertEquals(0, result.length);
SystemManager.subscribeServerToChannel(admin, server, parent);
server = reload(server);
result = handler.listSubscribableChildChannels(admin,
server.getId().intValue());
//server should have 1 child channel
assertEquals(1, result.length);
}
public void testListBaseChannels() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true);
Channel base = ChannelFactoryTest.createTestChannel(admin);
base.setParentChannel(null);
SystemManager.subscribeServerToChannel(admin, server, base);
Object[] results = handler.listSubscribableBaseChannels(admin, server.getId().intValue());
assertTrue(results.length > 0);
//make sure that every channel returned has null for parent_channel
for (int i = 0; i < results.length; i++) {
Map map = (Map) results[i];
Number id = (Number) map.get("id");
Long cid = id.longValue();
Channel c = ChannelManager.lookupByIdAndUser(cid, admin);
assertNull(c.getParentChannel());
}
}
public void testListChildChannels() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true);
Channel parent = ChannelFactoryTest.createTestChannel(admin);
parent.setParentChannel(null);
Channel child = ChannelFactoryTest.createTestChannel(admin);
child.setParentChannel(parent);
Object[] result = handler.listSubscribableChildChannels(admin, server.getId().intValue());
//server shouldn't have any channels yet
assertEquals(0, result.length);
SystemManager.subscribeServerToChannel(admin, server, parent);
server = reload(server);
result = handler.listSubscribableChildChannels(admin, server.getId().intValue());
//server should have 1 child channel
assertEquals(1, result.length);
}
public void testListNewerOlderInstalledPackages() throws Exception {
//TODO: not really sure how to test this guy. For now, send in a foobared nvre and
//make sure we get back a fault exception
Server server = ServerFactoryTest.createTestServer(admin, true);
try {
handler.listNewerInstalledPackages(admin,
server.getId().intValue(),
TestUtils.randomString(), "3", "4", "");
fail("listNewerInstalledPackages did not throw fault exception.");
}
catch (NoSuchPackageException e) {
//success
}
try {
handler.listOlderInstalledPackages(admin,
server.getId().intValue(),
"" + System.currentTimeMillis(), "3", "4", "");
fail("listOlderInstalledPackages did not throw fault exception.");
}
catch (NoSuchPackageException e) {
//success
}
}
public void testListLatestUpgradablePackages() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true);
int numPackages = SystemManager.latestUpgradablePackages(server.getId()).size();
List<Map<String, Object>> results =
handler.listLatestUpgradablePackages(admin,
server.getId().intValue());
//make sure the handler returns the same number of packages as systemmanger...
assertEquals(numPackages, results.size());
}
public void testListLatestInstallablePackages() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true);
int numPackages = SystemManager.latestInstallablePackages(server.getId()).size();
List<Map<String, Object>> results =
handler.listLatestInstallablePackages(admin,
server.getId().intValue());
//make sure the handler returns the same number of packages as systemmanger...
assertEquals(numPackages, results.size());
}
public void testGetEntitlements() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true);
int numEntitlements = server.getEntitlements().size();
Object[] results = handler.getEntitlements(admin,
server.getId().intValue());
assertEquals(numEntitlements, results.length);
assertTrue(results.length > 0);
String entLabel = (String) results[0];
Entitlement e = EntitlementManager.getByName(entLabel);
assertTrue(server.hasEntitlement(e));
}
public void testDownloadSystemId() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true);
String sysid = handler.downloadSystemId(admin,
server.getId().intValue());
assertNotNull(sysid);
}
public void testListPackages() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true);
int numPackages = SystemManager.installedPackages(server.getId()).size();
List<Map<String, Object>> result =
handler.listPackages(admin,
server.getId().intValue());
int numPackages2 = result.size();
assertEquals(numPackages, numPackages2);
//TODO: when we can add packages to servers via a test method, make revisit this.
}
public void testListExtraPackages() throws Exception {
Package testPackage = PackageTest.createTestPackage(admin.getOrg());
Server server = ServerFactoryTest.createTestServer(admin, true);
PackageManagerTest.associateSystemToPackage(server, testPackage);
int numPackages = SystemManager.listExtraPackages(server.getId()).size();
List<Map<String, Object>> result =
handler.listExtraPackages(admin,
server.getId().intValue());
int numPackages2 = result.size();
assertTrue(result.stream().anyMatch(m -> m.get("name").equals(testPackage.getPackageName().getName())));
assertEquals(numPackages, numPackages2);
}
public void testIsNvreInstalled() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true);
int result = handler.isNvreInstalled(admin,
server.getId().intValue(),
"foo", "-4", "1.b2", "bar");
assertEquals(0, result);
result = handler.isNvreInstalled(admin,
server.getId().intValue(),
"foo", "-4", "1.b2");
assertEquals(0, result);
}
public void testDeleteSystemWithCert() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true);
Long sid = server.getId();
ClientCertificate cert = SystemManager.createClientCertificate(server);
cert.validate(server.getSecret());
KickstartDataTest.setupTestConfiguration(admin);
assertEquals(1, handler.deleteSystem(cert.toString()));
assertNull(ServerFactory.lookupById(sid));
}
public void testDeleteSystems() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin, true);
Long sid = server.getId();
Integer id = sid.intValue();
Server test = SystemManager.lookupByIdAndUser(sid, admin);
assertNotNull(test);
//ok, we have an admin with a server he has access to
List sids = new ArrayList();
sids.add(id);
try {
handler.deleteSystems(regular, sids);
fail("SystemHandler.deleteSystems allowed unauthorized deletion");
}
catch (FaultException e) {
//success
}
assertEquals(1, handler.deleteSystems(admin, sids));
List<OperationDetailsDto> ops = SsmOperationManager.allOperations(admin);
for (OperationDetailsDto op : ops) {
assertEquals(op.getDescription(), LocalizationService.getInstance().
getMessage(SsmDeleteServersAction.OPERATION_NAME));
}
}
public void testScheduleVirtProvision() throws Exception {
Server server = ServerTestUtils.createTestSystem(admin);
systemEntitlementManager.setBaseEntitlement(server, EntitlementManager.MANAGEMENT);
TestUtils.saveAndFlush(server);
server = reload(server);
KickstartDataTest.setupTestConfiguration(admin);
KickstartData k = KickstartDataTest.createKickstartWithProfile(admin);
KickstartDataTest.addCommand(admin, k, "url", "--url http://cascade.sfbay.redhat." +
"com/rhn/kickstart/ks-rhel-i386-server-5");
k.getKickstartDefaults().getKstree().setChannel(server.getBaseChannel());
ChannelTestUtils.setupBaseChannelForVirtualization(admin, server.getBaseChannel());
String guestName = "vguest-" + TestUtils.randomString();
String profileName = k.getLabel();
int result = handler.provisionVirtualGuest(admin,
server.getId().intValue(),
guestName, profileName,
256, 1, 2048);
assertEquals(1, result);
assertNotNull(KickstartFactory.lookupAllKickstartSessionsByServer(server.getId()));
}
public void testAddNote() throws Exception {
Server server = ServerFactoryTest.createTestServer(admin);
int sizeBefore = server.getNotes().size();
int result = handler.addNote(admin, server.getId().intValue(),
"TestNote", "TestNote body");
int sizeAfter = server.getNotes().size();
assertEquals(1, result);
assertTrue(sizeAfter > sizeBefore);
assertEquals(1, sizeAfter - sizeBefore);
}
public void testDeleteNote() throws Exception {
// Setup
Server server = ServerFactoryTest.createTestServer(admin);
int sizeBefore = server.getNotes().size();
int result = handler.addNote(admin, server.getId().intValue(),
"TestNote", "TestNote body");
int sizeAfter = server.getNotes().size();
assertEquals(1, result);
assertTrue(sizeAfter > sizeBefore);
// Test
Note deleteMe = server.getNotes().iterator().next();
result = handler.deleteNote(admin, server.getId().intValue(),
deleteMe.getId().intValue());
// Verify
assertEquals(1, result);
}
public void testDeleteAllNotes() throws Exception {
// Setup
Server server = ServerFactoryTest.createTestServer(admin);
int sizeBefore = server.getNotes().size();
int result = handler.addNote(admin, server.getId().intValue(),
"TestNote", "TestNote body");
int sizeAfter = server.getNotes().size();
assertEquals(1, result);
assertTrue(sizeAfter > sizeBefore);
// Test
result = handler.deleteNotes(admin, server.getId().intValue());
// Verify
assertEquals(1, result);