def test_validation(self):
        self.ctx.node.properties.update(
            {'floatingip': self.test_config['floatingip']})
        with mock.patch('network_plugin.floatingip.ctx', self.ctx):
            floatingip.creation_validation()

        self.ctx.node.properties.update(
            {'floatingip': self.test_config['floatingip_auto']})
        with mock.patch('network_plugin.floatingip.ctx', self.ctx):
            floatingip.creation_validation()

        self.ctx.node.properties.update(
            {'private_key_path': os.path.realpath(__file__)})
        with mock.patch('network_plugin.keypair.ctx', self.ctx):
            keypair.creation_validation()

        self.ctx.node.properties.update({
            "resource_id":
            self.test_config['network']['name'],
            "network":
            self.test_config['network'],
            "use_external_resource":
            False
        })
        with mock.patch('network_plugin.network.ctx', self.ctx):
            network.creation_validation()

        self.ctx.node.properties.update({
            'port': {
                'network': self.test_config['management_network'],
                'ip_allocation_mode': 'dhcp',
                'primary_interface': True
            }
        })
        with mock.patch('network_plugin.port.ctx', self.ctx):
            port.creation_validation()

        self.ctx.node.properties.update({
            "nat":
            self.test_config['public_nat']['nat'],
            "rules":
            self.test_config['public_nat']['rules_net']
        })
        with mock.patch('network_plugin.public_nat.ctx', self.ctx):
            public_nat.creation_validation()

        self.ctx.node.properties.update(self.test_config['security_group'])
        with mock.patch('network_plugin.security_group.ctx', self.ctx):
            security_group.creation_validation()
    def test_validation(self):
        self.ctx.node.properties.update(
            {'floatingip': self.test_config['floatingip']})
        with mock.patch('network_plugin.floatingip.ctx', self.ctx):
            floatingip.creation_validation()

        self.ctx.node.properties.update(
            {'floatingip': self.test_config['floatingip_auto']})
        with mock.patch('network_plugin.floatingip.ctx', self.ctx):
            floatingip.creation_validation()

        self.ctx.node.properties.update(
            {'private_key_path': os.path.realpath(__file__)})
        with mock.patch('network_plugin.keypair.ctx', self.ctx):
            keypair.creation_validation()

        self.ctx.node.properties.update(
            {"resource_id": self.test_config['network']['name'],
             "network": self.test_config['network'],
             "use_external_resource": False})
        with mock.patch('network_plugin.network.ctx', self.ctx):
            network.creation_validation()

        self.ctx.node.properties.update(
            {'port': {
                'network': self.test_config['management_network'],
                'ip_allocation_mode': 'dhcp',
                'primary_interface': True}})
        with mock.patch('network_plugin.port.ctx', self.ctx):
            port.creation_validation()

        self.ctx.node.properties.update(
            {"nat": self.test_config['public_nat']['nat'],
             "rules": self.test_config['public_nat']['rules_net']})
        with mock.patch('network_plugin.public_nat.ctx', self.ctx):
            public_nat.creation_validation()

        self.ctx.node.properties.update(self.test_config['security_group'])
        with mock.patch('network_plugin.security_group.ctx', self.ctx):
            security_group.creation_validation()
    def test_creation_validation(self):
        fake_client = self.generate_client()
        # no nat
        fake_ctx = self.generate_node_context(
            properties={
                'vcloud_config': {
                    'vdc': 'vdc_name'
                }
            }
        )
        with mock.patch(
            'vcloud_plugin_common.VcloudAirClient.get',
            mock.MagicMock(return_value=fake_client)
        ):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                public_nat.creation_validation(ctx=fake_ctx)
        # no gateway
        fake_ctx = self.generate_node_context(
            properties={
                'vcloud_config': {
                    'vdc': 'vdc_name'
                },
                'nat': {
                    'some_field': 'something'
                }
            }
        )
        with mock.patch(
            'vcloud_plugin_common.VcloudAirClient.get',
            mock.MagicMock(return_value=fake_client)
        ):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                public_nat.creation_validation(ctx=fake_ctx)
        # wrong ip
        fake_ctx = self.generate_node_context(
            properties={
                'vcloud_config': {
                    'vdc': 'vdc_name',
                    'service_type': vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
                },
                'nat': {
                    'edge_gateway': 'gateway',
                    network_plugin.PUBLIC_IP: 'any'
                }
            }
        )
        with mock.patch(
            'vcloud_plugin_common.VcloudAirClient.get',
            mock.MagicMock(return_value=fake_client)
        ):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                public_nat.creation_validation(ctx=fake_ctx)
        # no free ip
        fake_ctx = self.generate_node_context(
            properties={
                'vcloud_config': {
                    'vdc': 'vdc_name',
                    'service_type': vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
                },
                'nat': {
                    'edge_gateway': 'gateway'
                }
            }
        )
        with mock.patch(
            'vcloud_plugin_common.VcloudAirClient.get',
            mock.MagicMock(return_value=fake_client)
        ):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                public_nat.creation_validation(ctx=fake_ctx)
        # no rules
        fake_ctx = self.generate_node_context(
            properties={
                'vcloud_config': {
                    'vdc': 'vdc_name',
                    'service_type': vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
                },
                'nat': {
                    'edge_gateway': 'gateway',
                    network_plugin.PUBLIC_IP: '10.12.2.1'
                }
            }
        )
        with mock.patch(
            'vcloud_plugin_common.VcloudAirClient.get',
            mock.MagicMock(return_value=fake_client)
        ):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                public_nat.creation_validation(ctx=fake_ctx)
        # wrong protocol
        fake_ctx = self.generate_node_context(
            properties={
                'vcloud_config': {
                    'vdc': 'vdc_name',
                    'service_type': vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
                },
                'nat': {
                    'edge_gateway': 'gateway',
                    network_plugin.PUBLIC_IP: '10.12.2.1'
                },
                'rules': [{
                    'type': 'DNAT',
                    'protocol': "some"
                }]
            }
        )
        with mock.patch(
            'vcloud_plugin_common.VcloudAirClient.get',
            mock.MagicMock(return_value=fake_client)
        ):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                public_nat.creation_validation(ctx=fake_ctx)
        # wrong original_port
        fake_ctx = self.generate_node_context(
            properties={
                'vcloud_config': {
                    'vdc': 'vdc_name',
                    'service_type': vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
                },
                'nat': {
                    'edge_gateway': 'gateway',
                    network_plugin.PUBLIC_IP: '10.12.2.1'
                },
                'rules': [{
                    'type': 'DNAT',
                    'protocol': "TCP",
                    'original_port': 'some'
                }]
            }
        )
        with mock.patch(
            'vcloud_plugin_common.VcloudAirClient.get',
            mock.MagicMock(return_value=fake_client)
        ):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                public_nat.creation_validation(ctx=fake_ctx)

        # wrong original_port
        fake_ctx = self.generate_node_context(
            properties={
                'vcloud_config': {
                    'vdc': 'vdc_name',
                    'service_type': vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
                },
                'nat': {
                    'edge_gateway': 'gateway',
                    network_plugin.PUBLIC_IP: '10.12.2.1'
                },
                'rules': [{
                    'type': 'DNAT',
                    'protocol': "TCP",
                    'original_port': 11,
                    'translated_port': 'some'
                }]
            }
        )
        with mock.patch(
            'vcloud_plugin_common.VcloudAirClient.get',
            mock.MagicMock(return_value=fake_client)
        ):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                public_nat.creation_validation(ctx=fake_ctx)
        # fine
        fake_ctx = self.generate_node_context(
            properties={
                'vcloud_config': {
                    'vdc': 'vdc_name',
                    'service_type': vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
                },
                'nat': {
                    'edge_gateway': 'gateway',
                    network_plugin.PUBLIC_IP: '10.12.2.1'
                },
                'rules': [{
                    'type': 'DNAT',
                    'protocol': "TCP",
                    'original_port': 11,
                    'translated_port': 12
                }]
            }
        )
        with mock.patch(
            'vcloud_plugin_common.VcloudAirClient.get',
            mock.MagicMock(return_value=fake_client)
        ):
            public_nat.creation_validation(ctx=fake_ctx)
    def test_creation_validation(self):
        fake_client = self.generate_client()
        # no nat
        fake_ctx = self.generate_node_context(
            properties={'vcloud_config': {
                'vdc': 'vdc_name'
            }})
        with mock.patch('vcloud_plugin_common.VcloudAirClient.get',
                        mock.MagicMock(return_value=fake_client)):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                public_nat.creation_validation(ctx=fake_ctx)
        # no gateway
        fake_ctx = self.generate_node_context(
            properties={
                'vcloud_config': {
                    'vdc': 'vdc_name'
                },
                'nat': {
                    'some_field': 'something'
                }
            })
        with mock.patch('vcloud_plugin_common.VcloudAirClient.get',
                        mock.MagicMock(return_value=fake_client)):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                public_nat.creation_validation(ctx=fake_ctx)
        # wrong ip
        fake_ctx = self.generate_node_context(
            properties={
                'vcloud_config': {
                    'vdc': 'vdc_name',
                    'service_type':
                    vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
                },
                'nat': {
                    'edge_gateway': 'gateway',
                    network_plugin.PUBLIC_IP: 'any'
                }
            })
        with mock.patch('vcloud_plugin_common.VcloudAirClient.get',
                        mock.MagicMock(return_value=fake_client)):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                public_nat.creation_validation(ctx=fake_ctx)
        # no free ip
        fake_ctx = self.generate_node_context(
            properties={
                'vcloud_config': {
                    'vdc': 'vdc_name',
                    'service_type':
                    vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
                },
                'nat': {
                    'edge_gateway': 'gateway'
                }
            })
        with mock.patch('vcloud_plugin_common.VcloudAirClient.get',
                        mock.MagicMock(return_value=fake_client)):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                public_nat.creation_validation(ctx=fake_ctx)
        # no rules
        fake_ctx = self.generate_node_context(
            properties={
                'vcloud_config': {
                    'vdc': 'vdc_name',
                    'service_type':
                    vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
                },
                'nat': {
                    'edge_gateway': 'gateway',
                    network_plugin.PUBLIC_IP: '10.12.2.1'
                }
            })
        with mock.patch('vcloud_plugin_common.VcloudAirClient.get',
                        mock.MagicMock(return_value=fake_client)):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                public_nat.creation_validation(ctx=fake_ctx)
        # wrong protocol
        fake_ctx = self.generate_node_context(
            properties={
                'vcloud_config': {
                    'vdc': 'vdc_name',
                    'service_type':
                    vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
                },
                'nat': {
                    'edge_gateway': 'gateway',
                    network_plugin.PUBLIC_IP: '10.12.2.1'
                },
                'rules': [{
                    'type': 'DNAT',
                    'protocol': "some"
                }]
            })
        with mock.patch('vcloud_plugin_common.VcloudAirClient.get',
                        mock.MagicMock(return_value=fake_client)):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                public_nat.creation_validation(ctx=fake_ctx)
        # wrong original_port
        fake_ctx = self.generate_node_context(
            properties={
                'vcloud_config': {
                    'vdc': 'vdc_name',
                    'service_type':
                    vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
                },
                'nat': {
                    'edge_gateway': 'gateway',
                    network_plugin.PUBLIC_IP: '10.12.2.1'
                },
                'rules': [{
                    'type': 'DNAT',
                    'protocol': "TCP",
                    'original_port': 'some'
                }]
            })
        with mock.patch('vcloud_plugin_common.VcloudAirClient.get',
                        mock.MagicMock(return_value=fake_client)):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                public_nat.creation_validation(ctx=fake_ctx)

        # wrong original_port
        fake_ctx = self.generate_node_context(
            properties={
                'vcloud_config': {
                    'vdc': 'vdc_name',
                    'service_type':
                    vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
                },
                'nat': {
                    'edge_gateway': 'gateway',
                    network_plugin.PUBLIC_IP: '10.12.2.1'
                },
                'rules': [{
                    'type': 'DNAT',
                    'protocol': "TCP",
                    'original_port': 11,
                    'translated_port': 'some'
                }]
            })
        with mock.patch('vcloud_plugin_common.VcloudAirClient.get',
                        mock.MagicMock(return_value=fake_client)):
            with self.assertRaises(cfy_exc.NonRecoverableError):
                public_nat.creation_validation(ctx=fake_ctx)
        # fine
        fake_ctx = self.generate_node_context(
            properties={
                'vcloud_config': {
                    'vdc': 'vdc_name',
                    'service_type':
                    vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE
                },
                'nat': {
                    'edge_gateway': 'gateway',
                    network_plugin.PUBLIC_IP: '10.12.2.1'
                },
                'rules': [{
                    'type': 'DNAT',
                    'protocol': "TCP",
                    'original_port': 11,
                    'translated_port': 12
                }]
            })
        with mock.patch('vcloud_plugin_common.VcloudAirClient.get',
                        mock.MagicMock(return_value=fake_client)):
            public_nat.creation_validation(ctx=fake_ctx)