def test_modify_vlans_idempotent(self, *args):
        set_module_args(
            dict(name="my-virtual-server",
                 partition="Common",
                 password="******",
                 enabled_vlans=["net1"],
                 server="localhost",
                 state="present",
                 user="******",
                 validate_certs="no"))

        # Configure the parameters that would be returned by querying the
        # remote device
        current = VirtualServerParameters(
            load_fixture('load_ltm_virtual_2.json'))

        client = AnsibleF5Client(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode,
            f5_product_name=self.spec.f5_product_name)

        # Override methods to force specific logic in the module to happen
        vsm = VirtualServerManager(client)
        vsm.exists = Mock(return_value=True)
        vsm.read_current_from_device = Mock(return_value=current)

        mm = ModuleManager(client)
        mm.get_manager = Mock(return_value=vsm)

        results = mm.exec_module()

        assert results['changed'] is False
    def test_enable_vs_that_is_already_enabled(self, *args):
        set_module_args(
            dict(all_profiles=['http', 'clientssl'],
                 description="Test Virtual Server",
                 destination="10.10.10.10",
                 name="my-snat-pool",
                 partition="Common",
                 password="******",
                 port="443",
                 server="localhost",
                 snat="Automap",
                 state="absent",
                 user="******",
                 validate_certs="no"))

        # Configure the parameters that would be returned by querying the
        # remote device
        current = VirtualServerParameters(dict(agent_status_traps='disabled'))

        client = AnsibleF5Client(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode,
            f5_product_name=self.spec.f5_product_name)

        # Override methods to force specific logic in the module to happen
        vsm = VirtualServerManager(client)
        vsm.exists = Mock(return_value=False)
        vsm.update_on_device = Mock(return_value=True)
        vsm.read_current_from_device = Mock(return_value=current)

        mm = ModuleManager(client)
        mm.get_manager = Mock(return_value=vsm)
        results = mm.exec_module()

        assert results['changed'] is False
 def test_module_partition_prefix_parameters(self):
     args = dict(server='localhost',
                 user='******',
                 password='******',
                 state='present',
                 partition='Common',
                 name='my-virtual-server',
                 destination='10.10.10.10',
                 port=443,
                 pool='/Common/my-pool',
                 snat='Automap',
                 description='Test Virtual Server',
                 profiles=[dict(name='fix', context='all')],
                 enabled_vlans=['/Common/vlan2'])
     p = VirtualServerParameters(args)
     assert p.name == 'my-virtual-server'
     assert p.partition == 'Common'
     assert p.port == 443
     assert p.server == 'localhost'
     assert p.user == 'admin'
     assert p.password == 'secret'
     assert p.destination == '/Common/10.10.10.10:443'
     assert p.pool == '/Common/my-pool'
     assert p.snat == {'type': 'automap'}
     assert p.description == 'Test Virtual Server'
     assert len(p.profiles) == 1
     assert 'context' in p.profiles[0]
     assert 'name' in p.profiles[0]
     assert '/Common/vlan2' in p.enabled_vlans
    def test_modify_profiles(self, *args):
        set_module_args(
            dict(name="my-virtual-server",
                 partition="Common",
                 password="******",
                 profiles=['http', 'clientssl'],
                 server="localhost",
                 state="present",
                 user="******",
                 validate_certs="no"))

        # Configure the parameters that would be returned by querying the
        # remote device
        current = VirtualServerParameters(
            load_fixture('load_ltm_virtual_2.json'))

        client = AnsibleF5Client(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode,
            f5_product_name=self.spec.f5_product_name)

        # Override methods to force specific logic in the module to happen
        vsm = VirtualServerManager(client)
        vsm.exists = Mock(return_value=True)
        vsm.read_current_from_device = Mock(return_value=current)
        vsm.update_on_device = Mock(return_value=True)

        mm = ModuleManager(client)
        mm.get_manager = Mock(return_value=vsm)

        results = mm.exec_module()

        assert results['changed'] is True
        assert len(results['profiles']) == 2
        assert 'name' in results['profiles'][0]
        assert 'context' in results['profiles'][0]
        assert results['profiles'][0]['name'] == 'http'
        assert results['profiles'][0]['context'] == 'all'
        assert 'name' in results['profiles'][1]
        assert 'context' in results['profiles'][1]
        assert results['profiles'][1]['name'] == 'clientssl'
        assert results['profiles'][1]['context'] == 'clientside'
    def test_modify_port_idempotent(self, *args):
        set_module_args(
            dict(destination="10.10.10.10",
                 name="my-virtual-server",
                 route_advertisement_state="enabled",
                 partition="Common",
                 password="******",
                 port="443",
                 server="localhost",
                 state="present",
                 user="******",
                 validate_certs="no"))

        client = AnsibleF5Client(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode,
            f5_product_name=self.spec.f5_product_name)

        vsm_current = VirtualServerParameters(
            load_fixture('load_ltm_virtual_1.json'))
        vam_current = VirtualAddressParameters(
            load_fixture('load_ltm_virtual_1_address.json'))

        vsm = VirtualServerManager(client)
        vsm.exists = Mock(return_value=True)
        vsm.read_current_from_device = Mock(return_value=vsm_current)
        vam = VirtualAddressManager(client)
        vam.exists = Mock(return_value=True)
        vam.read_current_from_device = Mock(return_value=vam_current)

        mm = ModuleManager(client)
        mm.get_manager = Mock(side_effect=[vsm, vam])

        results = mm.exec_module()

        assert results['changed'] is False
 def test_destination_mutex_6(self):
     args = dict(destination='/Common/1.1.1.1%2')
     p = VirtualServerParameters(args)
     assert p.destination_tuple.ip == '1.1.1.1'
     assert p.destination_tuple.route_domain == 2
 def test_destination_mutex_5(self):
     args = dict(destination='/Common/1.1.1.1')
     p = VirtualServerParameters(args)
     assert p.destination_tuple.ip == '1.1.1.1'
 def test_destination_mutex_4(self):
     args = dict(destination='1.1.1.1%2:80')
     p = VirtualServerParameters(args)
     assert p.destination_tuple.ip == '1.1.1.1'
     assert p.destination_tuple.port == 80
     assert p.destination_tuple.route_domain == 2
 def test_destination_mutex_3(self):
     args = dict(destination='1.1.1.1:80')
     p = VirtualServerParameters(args)
     assert p.destination_tuple.ip == '1.1.1.1'
     assert p.destination_tuple.port == 80
 def test_api_parameters_variables(self):
     args = {
         "kind":
         "tm:ltm:virtual:virtualstate",
         "name":
         "my-virtual-server",
         "partition":
         "Common",
         "fullPath":
         "/Common/my-virtual-server",
         "generation":
         54,
         "selfLink":
         "https://localhost/mgmt/tm/ltm/virtual/~Common~my-virtual-server?expandSubcollections=true&ver=12.1.2",
         "addressStatus":
         "yes",
         "autoLasthop":
         "default",
         "cmpEnabled":
         "yes",
         "connectionLimit":
         0,
         "description":
         "Test Virtual Server",
         "destination":
         "/Common/10.10.10.10:443",
         "enabled":
         True,
         "gtmScore":
         0,
         "ipProtocol":
         "tcp",
         "mask":
         "255.255.255.255",
         "mirror":
         "disabled",
         "mobileAppTunnel":
         "disabled",
         "nat64":
         "disabled",
         "rateLimit":
         "disabled",
         "rateLimitDstMask":
         0,
         "rateLimitMode":
         "object",
         "rateLimitSrcMask":
         0,
         "serviceDownImmediateAction":
         "none",
         "source":
         "0.0.0.0/0",
         "sourceAddressTranslation": {
             "type": "automap"
         },
         "sourcePort":
         "preserve",
         "synCookieStatus":
         "not-activated",
         "translateAddress":
         "enabled",
         "translatePort":
         "enabled",
         "vlansEnabled":
         True,
         "vsIndex":
         3,
         "vlans": ["/Common/net1"],
         "vlansReference": [{
             "link":
             "https://localhost/mgmt/tm/net/vlan/~Common~net1?ver=12.1.2"
         }],
         "policiesReference": {
             "link":
             "https://localhost/mgmt/tm/ltm/virtual/~Common~my-virtual-server/policies?ver=12.1.2",
             "isSubcollection": True
         },
         "profilesReference": {
             "link":
             "https://localhost/mgmt/tm/ltm/virtual/~Common~my-virtual-server/profiles?ver=12.1.2",
             "isSubcollection":
             True,
             "items": [{
                 "kind": "tm:ltm:virtual:profiles:profilesstate",
                 "name": "http",
                 "partition": "Common",
                 "fullPath": "/Common/http",
                 "generation": 54,
                 "selfLink":
                 "https://localhost/mgmt/tm/ltm/virtual/~Common~my-virtual-server/profiles/~Common~http?ver=12.1.2",
                 "context": "all",
                 "nameReference": {
                     "link":
                     "https://localhost/mgmt/tm/ltm/profile/http/~Common~http?ver=12.1.2"
                 }
             }, {
                 "kind": "tm:ltm:virtual:profiles:profilesstate",
                 "name": "serverssl",
                 "partition": "Common",
                 "fullPath": "/Common/serverssl",
                 "generation": 54,
                 "selfLink":
                 "https://localhost/mgmt/tm/ltm/virtual/~Common~my-virtual-server/profiles/~Common~serverssl?ver=12.1.2",
                 "context": "serverside",
                 "nameReference": {
                     "link":
                     "https://localhost/mgmt/tm/ltm/profile/server-ssl/~Common~serverssl?ver=12.1.2"
                 }
             }, {
                 "kind": "tm:ltm:virtual:profiles:profilesstate",
                 "name": "tcp",
                 "partition": "Common",
                 "fullPath": "/Common/tcp",
                 "generation": 54,
                 "selfLink":
                 "https://localhost/mgmt/tm/ltm/virtual/~Common~my-virtual-server/profiles/~Common~tcp?ver=12.1.2",
                 "context": "all",
                 "nameReference": {
                     "link":
                     "https://localhost/mgmt/tm/ltm/profile/tcp/~Common~tcp?ver=12.1.2"
                 }
             }]
         }
     }
     p = VirtualServerParameters(args)
     assert p.name == 'my-virtual-server'
     assert p.partition == 'Common'
     assert p.port == 443
     assert p.destination == '/Common/10.10.10.10:443'
     assert p.snat == {'type': 'automap'}
     assert p.description == 'Test Virtual Server'
     assert {'context': 'all', 'name': 'http'} in p.profiles
     assert '/Common/net1' in p.enabled_vlans
 def test_destination_mutex_12(self):
     args = dict(destination='2700:bc00:1f10:101::6%2.80')
     p = VirtualServerParameters(args)
     assert p.destination_tuple.ip == '2700:bc00:1f10:101::6'
     assert p.destination_tuple.port == 80
     assert p.destination_tuple.route_domain == 2
 def test_destination_mutex_9(self):
     args = dict(destination='2700:bc00:1f10:101::6')
     p = VirtualServerParameters(args)
     assert p.destination_tuple.ip == '2700:bc00:1f10:101::6'