1""" 2 :codeauthor: Jayesh Kariya <jayeshk@saltstack.com> 3""" 4 5 6import salt.modules.netscaler as netscaler 7from tests.support.mixins import LoaderModuleMockMixin 8from tests.support.mock import MagicMock, patch 9from tests.support.unit import TestCase 10 11 12class MockJson(Exception): 13 """ 14 Mock Json class 15 """ 16 17 @staticmethod 18 def loads(content): 19 """ 20 Mock loads method 21 """ 22 return content 23 24 @staticmethod 25 def dumps(dumps): 26 """ 27 Mock dumps method 28 """ 29 return dumps 30 31 32class MockNSNitroError(Exception): 33 """ 34 Mock NSNitroError class 35 """ 36 37 def __init__(self, message="error"): 38 self._message = message 39 super().__init__(self.message) 40 41 def _get_message(self): 42 """ 43 get_message method 44 """ 45 return self._message 46 47 def _set_message(self, message): 48 """ 49 set_message method 50 """ 51 self._message = message 52 53 message = property(_get_message, _set_message) 54 55 56class MockNSNitro: 57 """ 58 Mock NSNitro class 59 """ 60 61 flag = None 62 63 def __init__(self, host, user, passwd, bol): 64 pass 65 66 @staticmethod 67 def login(): 68 """ 69 Mock login method 70 """ 71 return True 72 73 @staticmethod 74 def logout(): 75 """ 76 Mock logout method 77 """ 78 return True 79 80 81class MockNSServiceGroup: 82 """ 83 Mock NSServiceGroup class 84 """ 85 86 def __init__(self): 87 self.sg_name = None 88 89 def set_servicegroupname(self, sg_name): 90 """ 91 Mock set_servicegroupname method 92 """ 93 self.sg_name = sg_name 94 return MockNSServiceGroup() 95 96 @staticmethod 97 def get(obj, servicegroup): 98 """ 99 Mock get method 100 """ 101 if MockNSNitro.flag: 102 raise MockNSNitroError 103 return MockNSServiceGroup() 104 105 @staticmethod 106 def add(obj, servicegroup): 107 """ 108 Mock add method 109 """ 110 if MockNSNitro.flag: 111 raise MockNSNitroError 112 return MockNSServiceGroup() 113 114 @staticmethod 115 def delete(obj, servicegroup): 116 """ 117 Mock delete method 118 """ 119 if MockNSNitro.flag: 120 raise MockNSNitroError 121 return MockNSServiceGroup() 122 123 @staticmethod 124 def get_servers(obj, servicegroup): 125 """ 126 Mock get_servers method 127 """ 128 if MockNSNitro.flag: 129 raise MockNSNitroError 130 return [MockNSServiceGroup()] 131 132 @staticmethod 133 def enable_server(obj, servicegroup): 134 """ 135 Mock enable_server method 136 """ 137 if MockNSNitro.flag: 138 raise MockNSNitroError 139 return MockNSServiceGroup() 140 141 @staticmethod 142 def disable_server(obj, servicegroup): 143 """ 144 Mock disable_server method 145 """ 146 if MockNSNitro.flag: 147 raise MockNSNitroError 148 return MockNSServiceGroup() 149 150 @staticmethod 151 def get_servername(): 152 """ 153 Mock get_servername method 154 """ 155 return "serviceGroupName" 156 157 @staticmethod 158 def get_state(): 159 """ 160 Mock get_state method 161 """ 162 return "ENABLED" 163 164 @staticmethod 165 def get_servicetype(): 166 """ 167 Mock get_servicetype method 168 """ 169 return "" 170 171 @staticmethod 172 def set_servicetype(bol): 173 """ 174 Mock set_servicetype method 175 """ 176 return bol 177 178 179class MockNSServiceGroupServerBinding: 180 """ 181 Mock NSServiceGroupServerBinding class 182 """ 183 184 def __init__(self): 185 self.sg_name = None 186 187 def set_servername(self, sg_name): 188 """ 189 Mock set_servername method 190 """ 191 self.sg_name = sg_name 192 return MockNSServiceGroupServerBinding() 193 194 def set_servicegroupname(self, sg_name): 195 """ 196 Mock set_servicegroupname method 197 """ 198 self.sg_name = sg_name 199 return MockNSServiceGroupServerBinding() 200 201 def set_port(self, sg_name): 202 """ 203 Mock set_port method 204 """ 205 self.sg_name = sg_name 206 return MockNSServiceGroupServerBinding() 207 208 @staticmethod 209 def add(obj, servicegroup): 210 """ 211 Mock add method 212 """ 213 if MockNSNitro.flag: 214 raise MockNSNitroError 215 return MockNSServiceGroupServerBinding() 216 217 @staticmethod 218 def delete(obj, servicegroup): 219 """ 220 Mock delete method 221 """ 222 if MockNSNitro.flag: 223 raise MockNSNitroError 224 return MockNSServiceGroupServerBinding() 225 226 227class MockNSService: 228 """ 229 Mock NSService class 230 """ 231 232 def __init__(self): 233 self.sg_name = None 234 235 def set_name(self, sg_name): 236 """ 237 Mock set_name method 238 """ 239 self.sg_name = sg_name 240 return MockNSService() 241 242 @staticmethod 243 def get(obj, servicegroup): 244 """ 245 Mock get method 246 """ 247 if MockNSNitro.flag: 248 raise MockNSNitroError 249 return MockNSService() 250 251 @staticmethod 252 def enable(obj, servicegroup): 253 """ 254 Mock enable method 255 """ 256 if MockNSNitro.flag: 257 raise MockNSNitroError 258 return MockNSService() 259 260 @staticmethod 261 def disable(obj, servicegroup): 262 """ 263 Mock disable method 264 """ 265 if MockNSNitro.flag: 266 raise MockNSNitroError 267 return MockNSService() 268 269 @staticmethod 270 def get_svrstate(): 271 """ 272 Mock get_svrstate method 273 """ 274 return "UP" 275 276 277class MockNSServer: 278 """ 279 Mock NSServer class 280 """ 281 282 flag = None 283 284 def __init__(self): 285 self.sg_name = None 286 287 def set_name(self, sg_name): 288 """ 289 Mock set_name method 290 """ 291 self.sg_name = sg_name 292 return MockNSServer() 293 294 @staticmethod 295 def get(obj, servicegroup): 296 """ 297 Mock get method 298 """ 299 return MockNSServer() 300 301 @staticmethod 302 def add(obj, servicegroup): 303 """ 304 Mock add method 305 """ 306 return MockNSServer() 307 308 @staticmethod 309 def delete(obj, servicegroup): 310 """ 311 Mock delete method 312 """ 313 return MockNSServer() 314 315 @staticmethod 316 def update(obj, servicegroup): 317 """ 318 Mock update method 319 """ 320 return MockNSServer() 321 322 @staticmethod 323 def enable(obj, servicegroup): 324 """ 325 Mock enable method 326 """ 327 return MockNSServer() 328 329 @staticmethod 330 def disable(obj, servicegroup): 331 """ 332 Mock disable method 333 """ 334 return MockNSServer() 335 336 @staticmethod 337 def get_ipaddress(): 338 """ 339 Mock get_ipaddress method 340 """ 341 return "" 342 343 @staticmethod 344 def set_ipaddress(s_ip): 345 """ 346 Mock set_ipaddress method 347 """ 348 return s_ip 349 350 def get_state(self): 351 """ 352 Mock get_state method 353 """ 354 if self.flag == 1: 355 return "" 356 elif self.flag == 2: 357 return "DISABLED" 358 return "ENABLED" 359 360 361class MockNSLBVServer: 362 """ 363 Mock NSLBVServer class 364 """ 365 366 def __init__(self): 367 self.sg_name = None 368 369 def set_name(self, sg_name): 370 """ 371 Mock set_name method 372 """ 373 self.sg_name = sg_name 374 return MockNSLBVServer() 375 376 @staticmethod 377 def get(obj, servicegroup): 378 """ 379 Mock get method 380 """ 381 return MockNSLBVServer() 382 383 @staticmethod 384 def set_ipv46(v_ip): 385 """ 386 Mock set_ipv46 method 387 """ 388 return v_ip 389 390 @staticmethod 391 def set_port(v_port): 392 """ 393 Mock set_port method 394 """ 395 return v_port 396 397 @staticmethod 398 def set_servicetype(v_type): 399 """ 400 Mock set_servicetype method 401 """ 402 return v_type 403 404 @staticmethod 405 def get_ipv46(): 406 """ 407 Mock get_ipv46 method 408 """ 409 return "" 410 411 @staticmethod 412 def get_port(): 413 """ 414 Mock get_port method 415 """ 416 return "" 417 418 @staticmethod 419 def get_servicetype(): 420 """ 421 Mock get_servicetype method 422 """ 423 return "" 424 425 @staticmethod 426 def add(obj, servicegroup): 427 """ 428 Mock add method 429 """ 430 return MockNSLBVServer() 431 432 @staticmethod 433 def delete(obj, servicegroup): 434 """ 435 Mock delete method 436 """ 437 return MockNSLBVServer() 438 439 440class MockNSLBVServerServiceGroupBinding: 441 """ 442 Mock NSLBVServerServiceGroupBinding class 443 """ 444 445 flag = None 446 447 def __init__(self): 448 self.sg_name = None 449 450 def set_name(self, sg_name): 451 """ 452 Mock set_name method 453 """ 454 self.sg_name = sg_name 455 return MockNSLBVServerServiceGroupBinding() 456 457 @staticmethod 458 def get(obj, servicegroup): 459 """ 460 Mock get method 461 """ 462 if MockNSNitro.flag: 463 raise MockNSNitroError 464 return [MockNSLBVServerServiceGroupBinding()] 465 466 @staticmethod 467 def get_servicegroupname(): 468 """ 469 Mock get_servicegroupname method 470 """ 471 return "serviceGroupName" 472 473 def set_servicegroupname(self, sg_name): 474 """ 475 Mock set_servicegroupname method 476 """ 477 self.sg_name = sg_name 478 if self.flag: 479 return None 480 return MockNSLBVServerServiceGroupBinding() 481 482 @staticmethod 483 def add(obj, servicegroup): 484 """ 485 Mock add method 486 """ 487 if MockNSNitro.flag: 488 raise MockNSNitroError 489 return MockNSLBVServerServiceGroupBinding() 490 491 @staticmethod 492 def delete(obj, servicegroup): 493 """ 494 Mock delete method 495 """ 496 if MockNSNitro.flag: 497 raise MockNSNitroError 498 return MockNSLBVServerServiceGroupBinding() 499 500 501class MockNSSSLVServerSSLCertKeyBinding: 502 """ 503 Mock NSSSLVServerSSLCertKeyBinding class 504 """ 505 506 def __init__(self): 507 self.sg_name = None 508 509 def set_vservername(self, sg_name): 510 """ 511 Mock set_vservername method 512 """ 513 self.sg_name = sg_name 514 return MockNSSSLVServerSSLCertKeyBinding() 515 516 @staticmethod 517 def get(obj, servicegroup): 518 """ 519 Mock get method 520 """ 521 if MockNSNitro.flag: 522 raise MockNSNitroError 523 return [MockNSSSLVServerSSLCertKeyBinding()] 524 525 @staticmethod 526 def get_certkeyname(): 527 """ 528 Mock get_certkeyname method 529 """ 530 return "serviceGroupName" 531 532 def set_certkeyname(self, sg_name): 533 """ 534 Mock set_certkeyname method 535 """ 536 self.sg_name = sg_name 537 return MockNSSSLVServerSSLCertKeyBinding() 538 539 @staticmethod 540 def add(obj, servicegroup): 541 """ 542 Mock add method 543 """ 544 if MockNSNitro.flag: 545 raise MockNSNitroError 546 return MockNSSSLVServerSSLCertKeyBinding() 547 548 @staticmethod 549 def delete(obj, servicegroup): 550 """ 551 Mock delete method 552 """ 553 if MockNSNitro.flag: 554 raise MockNSNitroError 555 return MockNSSSLVServerSSLCertKeyBinding() 556 557 558class NetscalerTestCase(TestCase, LoaderModuleMockMixin): 559 """ 560 TestCase for salt.modules.netscaler 561 """ 562 563 def setup_loader_modules(self): 564 return { 565 netscaler: { 566 "NSNitro": MockNSNitro, 567 "NSServiceGroup": MockNSServiceGroup, 568 "NSServiceGroupServerBinding": MockNSServiceGroupServerBinding, 569 "NSLBVServerServiceGroupBinding": MockNSLBVServerServiceGroupBinding, 570 "NSService": MockNSService, 571 "NSServer": MockNSServer, 572 "NSLBVServer": MockNSLBVServer, 573 "NSNitroError": MockNSNitroError, 574 "NSSSLVServerSSLCertKeyBinding": MockNSSSLVServerSSLCertKeyBinding, 575 } 576 } 577 578 # 'servicegroup_exists' function tests: 1 579 580 def test_servicegroup_exists(self): 581 """ 582 Tests if it checks if a service group exists 583 """ 584 mock = MagicMock(return_value="") 585 with patch.dict(netscaler.__salt__, {"config.option": mock}): 586 MockNSNitro.flag = None 587 self.assertTrue(netscaler.servicegroup_exists("serviceGrpName")) 588 589 self.assertFalse( 590 netscaler.servicegroup_exists("serviceGrpName", sg_type="HTTP") 591 ) 592 593 MockNSNitro.flag = True 594 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 595 self.assertFalse(netscaler.servicegroup_exists("serGrpNme")) 596 597 # 'servicegroup_add' function tests: 1 598 599 def test_servicegroup_add(self): 600 """ 601 Tests if it add a new service group 602 """ 603 mock = MagicMock(return_value="") 604 with patch.dict(netscaler.__salt__, {"config.option": mock}): 605 self.assertFalse(netscaler.servicegroup_add("serviceGroupName")) 606 607 MockNSNitro.flag = True 608 self.assertFalse(netscaler.servicegroup_add("serviceGroupName")) 609 610 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 611 self.assertFalse(netscaler.servicegroup_add("serveGrpName")) 612 613 # 'servicegroup_delete' function tests: 1 614 615 def test_servicegroup_delete(self): 616 """ 617 Tests if it delete a new service group 618 """ 619 mock = MagicMock(return_value="") 620 with patch.dict(netscaler.__salt__, {"config.option": mock}): 621 MockNSNitro.flag = None 622 self.assertTrue(netscaler.servicegroup_delete("serviceGrpName")) 623 624 mock = MagicMock(side_effect=[None, MockNSServiceGroup()]) 625 with patch.object(netscaler, "_servicegroup_get", mock): 626 MockNSNitro.flag = True 627 self.assertFalse(netscaler.servicegroup_delete("srGrpName")) 628 629 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 630 self.assertFalse(netscaler.servicegroup_delete("sGNam")) 631 632 # 'servicegroup_server_exists' function tests: 1 633 634 def test_servicegroup_server_exists(self): 635 """ 636 Tests if it check if a server:port combination 637 is a member of a servicegroup 638 """ 639 mock = MagicMock(return_value="") 640 with patch.dict(netscaler.__salt__, {"config.option": mock}): 641 self.assertFalse( 642 netscaler.servicegroup_server_exists( 643 "serviceGrpName", "serverName", "serverPort" 644 ) 645 ) 646 647 # 'servicegroup_server_up' function tests: 1 648 649 def test_servicegroup_server_up(self): 650 """ 651 Tests if it check if a server:port combination 652 is a member of a servicegroup 653 """ 654 mock = MagicMock(return_value="") 655 with patch.dict(netscaler.__salt__, {"config.option": mock}): 656 self.assertFalse( 657 netscaler.servicegroup_server_up( 658 "serviceGrpName", "serverName", "serverPort" 659 ) 660 ) 661 662 # 'servicegroup_server_enable' function tests: 1 663 664 def test_servicegroup_server_enable(self): 665 """ 666 Tests if it enable a server:port member of a servicegroup 667 """ 668 mock = MagicMock(return_value="") 669 with patch.dict(netscaler.__salt__, {"config.option": mock}): 670 self.assertFalse( 671 netscaler.servicegroup_server_enable( 672 "serviceGrpName", "serverName", "serverPort" 673 ) 674 ) 675 676 with patch.object( 677 netscaler, 678 "_servicegroup_get_server", 679 MagicMock(return_value=MockNSServiceGroup()), 680 ): 681 MockNSNitro.flag = None 682 self.assertTrue( 683 netscaler.servicegroup_server_enable( 684 "servGrpName", "serverName", "serPort" 685 ) 686 ) 687 688 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 689 self.assertFalse( 690 netscaler.servicegroup_server_enable( 691 "serGrpName", "serverName", "sPort" 692 ) 693 ) 694 695 # 'servicegroup_server_disable' function tests: 1 696 697 def test_sergrp_server_disable(self): 698 """ 699 Tests if it disable a server:port member of a servicegroup 700 """ 701 mock = MagicMock(return_value="") 702 with patch.dict(netscaler.__salt__, {"config.option": mock}): 703 self.assertFalse( 704 netscaler.servicegroup_server_disable( 705 "serviceGrpName", "serverName", "serverPort" 706 ) 707 ) 708 709 with patch.object( 710 netscaler, 711 "_servicegroup_get_server", 712 MagicMock(return_value=MockNSServiceGroup()), 713 ): 714 MockNSNitro.flag = None 715 self.assertTrue( 716 netscaler.servicegroup_server_disable( 717 "serveGrpName", "serverName", "serPort" 718 ) 719 ) 720 721 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 722 self.assertFalse( 723 netscaler.servicegroup_server_disable( 724 "servGrpName", "serverName", "sPort" 725 ) 726 ) 727 728 # 'servicegroup_server_add' function tests: 1 729 730 def test_servicegroup_server_add(self): 731 """ 732 Tests if it add a server:port member to a servicegroup 733 """ 734 mock = MagicMock(return_value="") 735 with patch.dict(netscaler.__salt__, {"config.option": mock}): 736 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 737 self.assertFalse( 738 netscaler.servicegroup_server_add( 739 "serGrpName", "serverName", "sPort" 740 ) 741 ) 742 743 MockNSNitro.flag = None 744 self.assertTrue( 745 netscaler.servicegroup_server_add( 746 "serGrpName", "serverName", "serverPort" 747 ) 748 ) 749 750 mock = MagicMock(return_value=MockNSServiceGroupServerBinding()) 751 with patch.object(netscaler, "_servicegroup_get_server", mock): 752 MockNSNitro.flag = True 753 self.assertFalse( 754 netscaler.servicegroup_server_add( 755 "serviceGroupName", "serverName", "serPort" 756 ) 757 ) 758 759 # 'servicegroup_server_delete' function tests: 1 760 761 def test_servicegroup_server_delete(self): 762 """ 763 Tests if it remove a server:port member to a servicegroup 764 """ 765 mock = MagicMock(return_value="") 766 with patch.dict(netscaler.__salt__, {"config.option": mock}): 767 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 768 self.assertFalse( 769 netscaler.servicegroup_server_delete( 770 "servGrpName", "serverName", "sPort" 771 ) 772 ) 773 774 self.assertFalse( 775 netscaler.servicegroup_server_delete( 776 "serviceGroupName", "serverName", "serverPort" 777 ) 778 ) 779 780 mock = MagicMock(return_value=MockNSServiceGroupServerBinding()) 781 with patch.object(netscaler, "_servicegroup_get_server", mock): 782 MockNSNitro.flag = None 783 self.assertTrue( 784 netscaler.servicegroup_server_delete( 785 "serviceGroupName", "serverName", "serPort" 786 ) 787 ) 788 789 # 'service_up' function tests: 1 790 791 def test_service_up(self): 792 """ 793 Tests if it checks if a service is UP 794 """ 795 mock = MagicMock(return_value=MockNSService()) 796 with patch.object(netscaler, "_service_get", mock): 797 self.assertTrue(netscaler.service_up("serviceGrpName")) 798 799 # 'service_exists' function tests: 1 800 801 def test_service_exists(self): 802 """ 803 Tests if it checks if a service is UP 804 """ 805 mock = MagicMock(return_value=MockNSService()) 806 with patch.object(netscaler, "_service_get", mock): 807 self.assertTrue(netscaler.service_exists("serviceGrpName")) 808 809 # 'service_enable' function tests: 1 810 811 def test_service_enable(self): 812 """ 813 Tests if it enable a service 814 """ 815 mock = MagicMock(return_value="") 816 with patch.dict(netscaler.__salt__, {"config.option": mock}): 817 self.assertTrue(netscaler.service_enable("serviceGrpName")) 818 819 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 820 self.assertFalse(netscaler.service_enable("serviceGrpName")) 821 822 mock = MagicMock(return_value=MockNSService()) 823 with patch.object(netscaler, "_service_get", mock): 824 self.assertFalse(netscaler.service_enable("serGrpName")) 825 826 # 'service_disable' function tests: 1 827 828 def test_service_disable(self): 829 """ 830 Tests if it disable a service 831 """ 832 mock = MagicMock(return_value="") 833 with patch.dict(netscaler.__salt__, {"config.option": mock}): 834 self.assertTrue(netscaler.service_disable("serviceGrpName")) 835 836 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 837 self.assertFalse(netscaler.service_disable("serceGrpName")) 838 839 mock = MagicMock(return_value=MockNSService()) 840 with patch.object(netscaler, "_service_get", mock): 841 self.assertFalse(netscaler.service_disable("seGrpName")) 842 843 # 'server_exists' function tests: 1 844 845 def test_server_exists(self): 846 """ 847 Tests if it checks if a server exists 848 """ 849 mock = MagicMock(return_value="") 850 with patch.dict(netscaler.__salt__, {"config.option": mock}): 851 self.assertTrue(netscaler.server_exists("serviceGrpName")) 852 853 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 854 self.assertFalse(netscaler.server_exists("serviceGrpName")) 855 856 self.assertFalse(netscaler.server_exists("serviceGrpName", ip="1.0.0.1")) 857 858 self.assertFalse( 859 netscaler.server_exists("serviceGrpName", s_state="serverName") 860 ) 861 862 # 'server_add' function tests: 1 863 864 def test_server_add(self): 865 """ 866 Tests if it add a server 867 """ 868 mock = MagicMock(return_value="") 869 with patch.dict(netscaler.__salt__, {"config.option": mock}): 870 self.assertFalse(netscaler.server_add("servGrpName", "1.0.0.1")) 871 872 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 873 self.assertFalse(netscaler.server_add("serviceGrpName", "1.0.0.1")) 874 875 mock = MagicMock(return_value=False) 876 with patch.object(netscaler, "server_exists", mock): 877 self.assertTrue(netscaler.server_add("serviceGrpName", "1.0.0.1")) 878 879 # 'server_delete' function tests: 1 880 881 def test_server_delete(self): 882 """ 883 Tests if it delete a server 884 """ 885 mock = MagicMock(return_value="") 886 with patch.dict(netscaler.__salt__, {"config.option": mock}): 887 self.assertTrue(netscaler.server_delete("serviceGrpName")) 888 889 mock = MagicMock(side_effect=[MockNSServer(), None]) 890 with patch.object(netscaler, "_server_get", mock): 891 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 892 self.assertFalse(netscaler.server_delete("serGrpName")) 893 894 self.assertFalse(netscaler.server_delete("serviceGrpName")) 895 896 # 'server_update' function tests: 1 897 898 def test_server_update(self): 899 """ 900 Tests if it update a server's attributes 901 """ 902 mock = MagicMock( 903 side_effect=[None, MockNSServer(), MockNSServer(), MockNSServer()] 904 ) 905 with patch.object(netscaler, "_server_get", mock): 906 self.assertFalse(netscaler.server_update("seGrName", "1.0.0.1")) 907 908 self.assertFalse(netscaler.server_update("serGrpName", "")) 909 910 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 911 self.assertFalse(netscaler.server_update("serGrpName", "1.0.0.1")) 912 913 mock = MagicMock(return_value="") 914 with patch.dict(netscaler.__salt__, {"config.option": mock}): 915 self.assertTrue(netscaler.server_update("serGrpName", "1.0.0.1")) 916 917 # 'server_enabled' function tests: 1 918 919 def test_server_enabled(self): 920 """ 921 Tests if it check if a server is enabled globally 922 """ 923 mock = MagicMock(return_value=MockNSServer()) 924 with patch.object(netscaler, "_server_get", mock): 925 MockNSServer.flag = None 926 self.assertTrue(netscaler.server_enabled("serGrpName")) 927 928 # 'server_enable' function tests: 1 929 930 def test_server_enable(self): 931 """ 932 Tests if it enables a server globally 933 """ 934 mock = MagicMock(return_value="") 935 with patch.dict(netscaler.__salt__, {"config.option": mock}): 936 self.assertTrue(netscaler.server_enable("serGrpName")) 937 938 MockNSServer.flag = 1 939 self.assertTrue(netscaler.server_enable("serGrpName")) 940 941 mock = MagicMock(side_effect=[MockNSServer(), None]) 942 with patch.object(netscaler, "_server_get", mock): 943 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 944 self.assertFalse(netscaler.server_enable("serGrpName")) 945 946 self.assertFalse(netscaler.server_enable("serGrpName")) 947 948 # 'server_disable' function tests: 1 949 950 def test_server_disable(self): 951 """ 952 Tests if it disable a server globally 953 """ 954 mock = MagicMock(return_value="") 955 with patch.dict(netscaler.__salt__, {"config.option": mock}): 956 self.assertTrue(netscaler.server_disable("serGrpName")) 957 958 MockNSServer.flag = 2 959 self.assertTrue(netscaler.server_disable("serGrpName")) 960 961 MockNSServer.flag = None 962 mock = MagicMock(side_effect=[None, MockNSServer()]) 963 with patch.object(netscaler, "_server_get", mock): 964 self.assertFalse(netscaler.server_disable("serGrpName")) 965 966 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 967 self.assertFalse(netscaler.server_disable("serGrpName")) 968 969 # 'vserver_exists' function tests: 1 970 971 def test_vserver_exists(self): 972 """ 973 Tests if it checks if a vserver exists 974 """ 975 mock = MagicMock(return_value="") 976 with patch.dict(netscaler.__salt__, {"config.option": mock}): 977 self.assertTrue(netscaler.vserver_exists("vserverName")) 978 979 self.assertFalse(netscaler.vserver_exists("vserverName", v_ip="1.0.0.1")) 980 981 self.assertFalse( 982 netscaler.vserver_exists("vserrName", v_ip="", v_port="vserverPort") 983 ) 984 985 self.assertFalse( 986 netscaler.vserver_exists( 987 "vserrName", v_ip="", v_port="", v_type="vserverType" 988 ) 989 ) 990 991 mock = MagicMock(return_value=None) 992 with patch.object(netscaler, "_vserver_get", mock): 993 self.assertFalse(netscaler.vserver_exists("vserverName")) 994 995 # 'vserver_add' function tests: 1 996 997 def test_vserver_add(self): 998 """ 999 Tests if it add a new lb vserver 1000 """ 1001 mock = MagicMock(return_value="") 1002 with patch.dict(netscaler.__salt__, {"config.option": mock}): 1003 self.assertFalse( 1004 netscaler.vserver_add("alex.patate.chaude.443", "1.2.3.4", "443", "SSL") 1005 ) 1006 1007 mock = MagicMock(return_value=False) 1008 with patch.object(netscaler, "vserver_exists", mock): 1009 self.assertTrue( 1010 netscaler.vserver_add( 1011 "alex.pae.chaude.443", "1.2.3.4", "443", "SSL" 1012 ) 1013 ) 1014 1015 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 1016 self.assertFalse( 1017 netscaler.vserver_add("alex.chde.443", "1.2.3.4", "443", "SSL") 1018 ) 1019 1020 # 'vserver_delete' function tests: 1 1021 1022 def test_vserver_delete(self): 1023 """ 1024 Tests if it delete a new lb vserver 1025 """ 1026 mock = MagicMock(return_value="") 1027 with patch.dict(netscaler.__salt__, {"config.option": mock}): 1028 self.assertTrue(netscaler.vserver_delete("alex.pe.chaude.443")) 1029 1030 mock = MagicMock(side_effect=[None, MockNSLBVServer()]) 1031 with patch.object(netscaler, "_vserver_get", mock): 1032 self.assertFalse(netscaler.vserver_delete("alex.chade.443")) 1033 1034 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 1035 self.assertFalse(netscaler.vserver_delete("al.cha.443")) 1036 1037 # 'vserver_servicegroup_exists' function tests: 1 1038 1039 def test_vser_sergrp_exists(self): 1040 """ 1041 Tests if it checks if a servicegroup is tied to a vserver 1042 """ 1043 mock = MagicMock(return_value="") 1044 with patch.dict(netscaler.__salt__, {"config.option": mock}): 1045 self.assertTrue( 1046 netscaler.vserver_servicegroup_exists("vserverName", "serviceGroupName") 1047 ) 1048 1049 # 'vserver_servicegroup_add' function tests: 1 1050 1051 def test_vserver_servicegroup_add(self): 1052 """ 1053 Tests if it bind a servicegroup to a vserver 1054 """ 1055 mock = MagicMock(return_value="") 1056 with patch.dict(netscaler.__salt__, {"config.option": mock}): 1057 MockNSNitro.flag = None 1058 self.assertTrue( 1059 netscaler.vserver_servicegroup_add("vserverName", "serGroupName") 1060 ) 1061 1062 mock = MagicMock(side_effect=[MockNSLBVServerServiceGroupBinding(), None]) 1063 with patch.object(netscaler, "vserver_servicegroup_exists", mock): 1064 self.assertFalse( 1065 netscaler.vserver_servicegroup_add("vserName", "serGroupName") 1066 ) 1067 1068 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 1069 self.assertFalse( 1070 netscaler.vserver_servicegroup_add("vName", "serGroupName") 1071 ) 1072 1073 # 'vserver_servicegroup_delete' function tests: 1 1074 1075 def test_vser_sergrp_delete(self): 1076 """ 1077 Tests if it unbind a servicegroup from a vserver 1078 """ 1079 mock = MagicMock(return_value="") 1080 with patch.dict(netscaler.__salt__, {"config.option": mock}): 1081 self.assertFalse( 1082 netscaler.vserver_servicegroup_delete("vservName", "serGroupName") 1083 ) 1084 1085 mock = MagicMock(return_value=MockNSLBVServerServiceGroupBinding()) 1086 with patch.object(netscaler, "vserver_servicegroup_exists", mock): 1087 MockNSNitro.flag = None 1088 self.assertTrue( 1089 netscaler.vserver_servicegroup_delete("vName", "serGroupName") 1090 ) 1091 1092 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 1093 self.assertFalse( 1094 netscaler.vserver_servicegroup_delete( 1095 "vserverName", "serGroupName" 1096 ) 1097 ) 1098 1099 # 'vserver_sslcert_exists' function tests: 1 1100 1101 def test_vserver_sslcert_exists(self): 1102 """ 1103 Tests if it checks if a SSL certificate is tied to a vserver 1104 """ 1105 mock = MagicMock(return_value="") 1106 with patch.dict(netscaler.__salt__, {"config.option": mock}): 1107 self.assertTrue( 1108 netscaler.vserver_sslcert_exists("vserverName", "serviceGroupName") 1109 ) 1110 1111 # 'vserver_sslcert_add' function tests: 1 1112 1113 def test_vserver_sslcert_add(self): 1114 """ 1115 Tests if it binds a SSL certificate to a vserver 1116 """ 1117 mock = MagicMock(side_effect=[MockNSSSLVServerSSLCertKeyBinding(), None, None]) 1118 with patch.object(netscaler, "vserver_sslcert_exists", mock): 1119 self.assertFalse(netscaler.vserver_sslcert_add("vserName", "serGroupName")) 1120 1121 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 1122 self.assertFalse(netscaler.vserver_sslcert_add("vName", "serGrName")) 1123 1124 mock = MagicMock(return_value="") 1125 with patch.dict(netscaler.__salt__, {"config.option": mock}): 1126 MockNSNitro.flag = None 1127 self.assertTrue( 1128 netscaler.vserver_sslcert_add("vserverName", "serGroupName") 1129 ) 1130 1131 # 'vserver_sslcert_delete' function tests: 1 1132 1133 def test_vserver_sslcert_delete(self): 1134 """ 1135 Tests if it unbinds a SSL certificate from a vserver 1136 """ 1137 mock = MagicMock( 1138 side_effect=[ 1139 None, 1140 MockNSSSLVServerSSLCertKeyBinding(), 1141 MockNSSSLVServerSSLCertKeyBinding(), 1142 ] 1143 ) 1144 with patch.object(netscaler, "vserver_sslcert_exists", mock): 1145 self.assertFalse(netscaler.vserver_sslcert_delete("vName", "serGrpName")) 1146 1147 mock = MagicMock(return_value="") 1148 with patch.dict(netscaler.__salt__, {"config.option": mock}): 1149 MockNSNitro.flag = None 1150 self.assertTrue( 1151 netscaler.vserver_sslcert_delete("vservName", "serGroupName") 1152 ) 1153 1154 with patch.object(netscaler, "_connect", MagicMock(return_value=None)): 1155 self.assertFalse( 1156 netscaler.vserver_sslcert_delete("vserverName", "serGroupName") 1157 ) 1158