def test_delete_availability_set(self):
        ctx = self.mock_ctx('testdeleteavailabilityset')
        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN delete availability_set test")

        ctx.logger.info("create availability_set")
        self.assertEqual(200, availability_set.create(ctx=ctx))

        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, "availability_set",constants.SUCCEEDED, 600)

        ctx.logger.info("trying to delete an non-deletable availability_set")
        ctx.node.properties[constants.DELETABLE_KEY] = False
        current_ctx.set(ctx=ctx)
        self.assertEqual(0, availability_set.delete(ctx=ctx))

        ctx.logger.info("delete availability_set")
        ctx.node.properties[constants.DELETABLE_KEY] = True
        current_ctx.set(ctx=ctx)
        self.assertEqual(200, availability_set.delete(ctx=ctx))

        try:
            current_ctx.set(ctx=ctx)
            utils.wait_status(ctx, "availability_set","deleting", 600)
        except utils.WindowsAzureError:
            pass

        ctx.logger.info("END delete availability_set test")
    def test_conflict_availability_set(self):
        ctx = self.mock_ctx('testconflictavailabilityset')
        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN conflict availability_set test")

        ctx.logger.info("create availability_set")
        self.assertEqual(200, availability_set.create(ctx=ctx))

        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, "availability_set",constants.SUCCEEDED, 600)

        ctx.logger.info("create conflict availability_set")
        self.assertEqual(200, availability_set.create(ctx=ctx))

        ctx.logger.info("delete availability_set")
        self.assertEqual(200, availability_set.delete(ctx=ctx))

        try:
            current_ctx.set(ctx=ctx)
            utils.wait_status(ctx, "availability_set","deleting", 600)
        except utils.WindowsAzureError:
            pass

        ctx.logger.info("delete conflict availability_set")
        self.assertEqual(204, availability_set.delete(ctx=ctx))

        ctx.logger.info("END conflict availability_set test")
    def test_add_availability_set_instance(self):
        ctx = self.mock_ctx('testaddavailabilityinstance')
        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN add availability set VM test: {}".format(ctx.instance.id))

        ctx.logger.info("create availability_set")
        self.assertEqual(200, availability_set.create(ctx=ctx))
        ctx.logger.debug("availability_set_id = {}".format(
            ctx.instance.runtime_properties[constants.AVAILABILITY_ID_KEY]))

        ctx.instance.relationships.append(test_mockcontext.MockRelationshipContext(node_id='test',
            runtime_properties={
                constants.AVAILABILITY_ID_KEY:\
                     ctx.instance.runtime_properties[constants.AVAILABILITY_ID_KEY]
            },
            type=constants.INSTANCE_CONTAINED_IN_AVAILABILITY_SET)
        )

        ctx.logger.info("create VM")
        instance.create(ctx=ctx)

        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, "instance",constants.SUCCEEDED, 600)

        ctx.logger.info("test instance is in availability_set")
        current_ctx.set(ctx=ctx)
        json = instance.get_json_from_azure(ctx=ctx)
        self.assertIsNotNone(json['properties']['availabilitySet'])
        self.assertEqual(str(json['properties']['availabilitySet']['id']).lower(),
            str(ctx.instance.runtime_properties[constants.AVAILABILITY_ID_KEY]).lower()
        )

        ctx.logger.info("delete VM")
        self.assertEqual(202, instance.delete(ctx=ctx))

        ctx.logger.info("delete availability_set")
        self.assertEqual(200, availability_set.delete(ctx=ctx))

        ctx.logger.info("END create VM test")