def test_conflict_security_group(self):
        ctx = self.mock_ctx('testconflictsecuritygroup')
        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN conflict security_group test")

        ctx.logger.info("create security_group")
        self.assertEqual(201, security_group.create(ctx=ctx))

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

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

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

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

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

        ctx.logger.info("END conflict security_group test")
    def test_delete_security_group(self):
        ctx = self.mock_ctx('testdeletesecuritygroup')
        current_ctx.set(ctx=ctx)
        ctx.logger.info("BEGIN delete security_group test")

        ctx.logger.info("create security_group")
        self.assertEqual(201, security_group.create(ctx=ctx))

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

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

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

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

        ctx.logger.info("END delete security_group test")
    def test_connect_security_group_subnet(self):
        ctx = self.mock_ctx("testaddsecugroupsubnet", cdir="10.0.5.0/24")
        ctx.logger.info("BEGIN test_connect_security_group_subnet")

        ctx.logger.info("create security_group")
        self.assertEqual(201, security_group.create(ctx=ctx))
        ctx.logger.debug(
            "security_group_id = {}".format(ctx.instance.runtime_properties[constants.SECURITY_GROUP_ID_KEY])
        )
        current_ctx.set(ctx=ctx)
        utils.wait_status(ctx, "security_group", constants.SUCCEEDED, timeout=900)

        ctx.instance.relationships.append(
            test_mockcontext.MockRelationshipContext(
                node_id="test",
                runtime_properties={
                    constants.SECURITY_GROUP_ID_KEY: ctx.instance.runtime_properties[constants.SECURITY_GROUP_ID_KEY]
                },
                type=constants.SUBNET_CONNECTED_TO_SECURITY_GROUP,
            )
        )

        current_ctx.set(ctx=ctx)
        status_code = subnet.create(ctx=ctx)
        ctx.logger.debug("status_code = " + str(status_code))
        self.assertTrue(bool((status_code == 200) or (status_code == 201)))

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

        ctx.logger.info("test subnet is connected to a security_group")
        current_ctx.set(ctx=ctx)
        json = subnet.get_json_from_azure(ctx=ctx)
        self.assertIsNotNone(json["properties"]["networkSecurityGroup"])
        self.assertEqual(
            str(json["properties"]["networkSecurityGroup"]["id"]).lower(),
            str(ctx.instance.runtime_properties[constants.SECURITY_GROUP_ID_KEY]).lower(),
        )

        current_ctx.set(ctx=ctx)
        status_code = subnet.delete(ctx=ctx)
        ctx.logger.debug("status_code = " + str(status_code))
        self.assertTrue(bool((status_code == 202) or (status_code == 204)))

        try:
            current_ctx.set(ctx=ctx)
            utils.wait_status(ctx, "subnet", "waiting for exception", timeout=600)
        except utils.WindowsAzureError:
            pass

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

        ctx.logger.info("END test_connect_security_group_subnet")