def test_allocate_external_elasticip_external_bad_id(self):

        ctx = self.mock_cloudify_context(
            'test_allocate_external_elasticip_external')
        current_ctx.set(ctx=ctx)
        ctx.node.properties['use_external_resource'] = True
        ctx.node.properties['resource_id'] = '127.0.0.1'

        with self.assertRaisesRegexp(
                NonRecoverableError,
                'elasticip does not exist in the account'):
            elasticip._allocate_external_elasticip()
    def test_allocate_external_elasticip_external_bad_id(self):

        ctx = self.mock_cloudify_context(
            'test_allocate_external_elasticip_external')
        current_ctx.set(ctx=ctx)
        ctx.node.properties['use_external_resource'] = True
        ctx.node.properties['resource_id'] = '127.0.0.1'

        with self.assertRaisesRegexp(
                NonRecoverableError,
                'elasticip does not exist in the account'):
            elasticip._allocate_external_elasticip()
    def test_allocate_external_elasticip(self):
        """ This tests that this function returns False
        if use_external_resource is false.
        """

        ctx = self.mock_ctx(
            'test_allocate_external_elasticip')
        current_ctx.set(ctx=ctx)
        ctx.node.properties['use_external_resource'] = False

        output = \
            elasticip._allocate_external_elasticip()

        self.assertEqual(False, output)
    def test_allocate_external_elasticip_external(self):

        ctx = self.mock_cloudify_context(
            'test_allocate_external_elasticip_external')
        current_ctx.set(ctx=ctx)
        client = self._get_ec2_client()
        address_object = client.allocate_address()
        self.addCleanup(address_object.delete)
        ctx.node.properties['use_external_resource'] = True
        ctx.node.properties['resource_id'] = address_object.public_ip

        output = \
            elasticip._allocate_external_elasticip()

        self.assertEqual(True, output)
        self.assertIn(EXTERNAL_RESOURCE_ID, ctx.instance.runtime_properties)
        self.assertEqual(address_object.public_ip,
                         ctx.instance.runtime_properties[EXTERNAL_RESOURCE_ID])
    def test_allocate_external_elasticip_external(self):

        ctx = self.mock_cloudify_context(
            'test_allocate_external_elasticip_external')
        current_ctx.set(ctx=ctx)
        client = self._get_ec2_client()
        address_object = client.allocate_address()
        self.addCleanup(address_object.delete)
        ctx.node.properties['use_external_resource'] = True
        ctx.node.properties['resource_id'] = address_object.public_ip

        output = \
            elasticip._allocate_external_elasticip()

        self.assertEqual(True, output)
        self.assertIn(
            EXTERNAL_RESOURCE_ID,
            ctx.instance.runtime_properties)
        self.assertEqual(
            address_object.public_ip,
            ctx.instance.runtime_properties[EXTERNAL_RESOURCE_ID])