import mock
import unittest

from cloudify.mocks import MockCloudifyContext, MockContext,\
    MockNodeContext, MockNodeInstanceContext

from tests.integration import TestCase
from network_plugin import floatingip
from network_plugin import network
from network_plugin.floatingip import VCLOUD_VAPP_NAME


class NatRulesOperationsTestCase(TestCase):

    def setUp(self):
        super(NatRulesOperationsTestCase, self).setUp()

        name = "testnode"
        self.ctx = MockCloudifyContext(
            node_id=name,
            node_name=name,
            properties={},
            target=MockContext({
                'instance': None,
                'node': None
            }),
            source=MockContext({
                'instance': None,
                'node': None
            }))

        self.public_ip = '23.92.245.236'
        self.ctx.source.instance = MockNodeInstanceContext(
            runtime_properties={VCLOUD_VAPP_NAME: 'ilyashenko'})
        self.ctx.target.node = MockNodeContext(
            properties={'floatingip': {'public_ip': self.public_ip,
                                       'gateway': 'M966854774-4471'}})
        ctx_patch1 = mock.patch('network_plugin.floatingip.ctx', self.ctx)
        ctx_patch2 = mock.patch('vcloud_plugin_common.ctx', self.ctx)
        ctx_patch1.start()
        ctx_patch2.start()
        self.addCleanup(ctx_patch1.stop)
        self.addCleanup(ctx_patch2.stop)

    def tearDown(self):
        super(NatRulesOperationsTestCase, self).tearDown()

    def test_nat_rules_create_delete(self):
        self.assertNotIn(self.public_ip, self._collectExternalIps())
        floatingip.connect_floatingip()
        self.assertIn(self.public_ip, self._collectExternalIps())
        floatingip.disconnect_floatingip()
        self.assertNotIn(self.public_ip, self._collectExternalIps())

    def _collectExternalIps(self):
        ips = []
        gateway = self.vcd_client.get_gateway(
            self.ctx.target.node.properties['floatingip']['gateway'])
        if gateway:
            for natRule in gateway.get_nat_rules():
                rule = natRule.get_GatewayNatRule()
                rule_type = natRule.get_RuleType()
                if rule_type == "DNAT":
                    ips.append(rule.get_OriginalIp())
                else:
                    ips.append(rule.get_TranslatedIp())
        return ips


class OrgNetworkOperationsTestCase(TestCase):

    def setUp(self):
        super(OrgNetworkOperationsTestCase, self).setUp()

        self.net_name = "test_network"
        self.ctx = MockCloudifyContext(
            node_id=self.net_name,
            node_name=self.net_name,
            properties={"resource_id": self.net_name,
                        "network":
                        {"start_address": "192.168.0.100",
                         "end_address": "192.168.0.199",
                         "gateway_ip": "192.168.0.1",
                         "netmask": "255.255.255.0",
                         "dns": "10.147.115.1",
                         "dns_duffix": "example.com"},
                        "use_external_resource": False})

        ctx_patch1 = mock.patch('network_plugin.network.ctx', self.ctx)
        ctx_patch2 = mock.patch('vcloud_plugin_common.ctx', self.ctx)
        ctx_patch1.start()
        ctx_patch2.start()
        self.addCleanup(ctx_patch1.stop)
        self.addCleanup(ctx_patch2.stop)

    def tearDown(self):
        super(OrgNetworkOperationsTestCase, self).tearDown()

    def test_orgnetwork_create_delete(self):
        self.assertNotIn(self.net_name,
                         network._get_network_list(self.vcd_client))
        network.create()
        self.assertIn(self.net_name,
                      network._get_network_list(self.vcd_client))
        network.delete()
        self.assertNotIn(self.net_name,
                         network._get_network_list(self.vcd_client))


if __name__ == '__main__':
    unittest.main()

@@ -21,17 +21,16 @@
 nittest%0A
-%0A
 from clo
@@ -128,17 +128,45 @@
 Context%0A
+from cloudify import context
 %0A
-
 from tes
@@ -324,17 +324,27 @@
 PP_NAME%0A
+import pdb
 %0A
-
 %0Aclass N
@@ -659,36 +659,34 @@
     'instance': 
-None
+%7B%7D
 ,%0A              
@@ -777,20 +777,18 @@
 tance': 
-None
+%7B%7D
 ,%0A      
@@ -1452,32 +1452,41 @@
 ctx_patch2.stop)
+%0A        
 %0A%0A    def tearDo
@@ -1698,32 +1698,40 @@
 nect_floatingip(
+self.ctx
 )%0A        self.a
@@ -1823,16 +1823,24 @@
 atingip(
+self.ctx
 )%0A      
@@ -3548,32 +3548,77 @@
 lf).tearDown()%0A%0A
+    @unittest.skip(%22demonstrating skipping%22)%0A
     def test_org

Test fixes