qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Huth <thuth@redhat.com>
To: "Daniel P. Berrangé" <berrange@redhat.com>
Cc: qemu-devel@nongnu.org, "Philippe Mathieu-Daudé" <philmd@linaro.org>
Subject: Re: [PATCH 2/5] tests/functional: Extract the find_free_ports() function into a helper file
Date: Fri, 6 Dec 2024 16:56:12 +0100	[thread overview]
Message-ID: <86b6f948-6ddf-481e-81e1-de5ee27b6dbd@redhat.com> (raw)
In-Reply-To: <Z1Ay61hpfABc6n33@redhat.com>

On 04/12/2024 11.46, Daniel P. Berrangé wrote:
> On Wed, Dec 04, 2024 at 08:19:08AM +0100, Thomas Huth wrote:
>> We'll need this functionality in other functional tests, too, so
>> let's extract it into the qemu_test module.
>> Also add  an __enter__ and __exit__ function that can be used for
>> using this functionality in a locked context, so that tests that
>> are running in parallel don't try to compete for the same ports
>> later.
>>
>> Signed-off-by: Thomas Huth <thuth@redhat.com>
>> ---
>>   tests/functional/qemu_test/ports.py | 53 +++++++++++++++++++++++++++++
>>   tests/functional/test_vnc.py        | 36 +++++---------------
>>   2 files changed, 61 insertions(+), 28 deletions(-)
>>   create mode 100644 tests/functional/qemu_test/ports.py
>>
>> diff --git a/tests/functional/qemu_test/ports.py b/tests/functional/qemu_test/ports.py
>> new file mode 100644
>> index 0000000000..d235d3432b
>> --- /dev/null
>> +++ b/tests/functional/qemu_test/ports.py
>> @@ -0,0 +1,53 @@
>> +#!/usr/bin/env python3
>> +#
>> +# Simple functional tests for VNC functionality
>> +#
>> +# Copyright 2018, 2024 Red Hat, Inc.
>> +#
>> +# This work is licensed under the terms of the GNU GPL, version 2 or
>> +# later.  See the COPYING file in the top-level directory.
>> +
>> +import fcntl
>> +import os
>> +import socket
>> +import sys
>> +import tempfile
>> +from typing import List
>> +
>> +class Ports():
>> +
>> +    PORTS_ADDR = '127.0.0.1'
>> +    PORTS_START = 32768
>> +    PORTS_END = PORTS_START + 1024
>> +
>> +    def __enter__(self):
>> +        lock_file = os.path.join(tempfile.gettempdir(), "qemu_port_lock")
>> +        self.lock_fh = os.open(lock_file, os.O_CREAT)
>> +        fcntl.flock(self.lock_fh, fcntl.LOCK_EX)
>> +        return self
>> +
>> +    def __exit__(self, exc_type, exc_value, traceback):
>> +        fcntl.flock(self.lock_fh, fcntl.LOCK_UN)
>> +        os.close(self.lock_fh)
> 
> This code will leave '/tmp/qemu_port_lock' existing forever.... which is
> correct, because if you try to unlink it after closing, you'll introduce
> a race because the 2nd __enter__ will now be locking an unlinked file,
> and a 3rd __enter__ that comes along will create & lock an entirely new
> file.
> 
> There are ways to make this safe by using stat + fstat either side of
> LOCK_EX, in a loop, to detect locking of an unlinked file. That is
> overkill though.  It is simpler to just put the lock file in the build
> directory IMHO, and thus avoid needing to care about unlinking - that'll
> be done when the user purges their build dir.

Putting the lock in the build directory is a nice idea - but it will fail if 
the user is using multiple build directory and running the test in the 
multiple directories in parallel. But maybe we could ease that situation by 
randomizing PORTS_START based on a seed calculated somehow by the build 
directory string?

  Thomas



  reply	other threads:[~2024-12-06 15:56 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-04  7:19 [PATCH for-10.0 0/5] tests/functional: Convert tests with find_free_ports() Thomas Huth
2024-12-04  7:19 ` [PATCH 1/5] tests/functional: Convert the vnc test Thomas Huth
2024-12-04  7:19 ` [PATCH 2/5] tests/functional: Extract the find_free_ports() function into a helper file Thomas Huth
2024-12-04 10:41   ` Daniel P. Berrangé
2024-12-06 15:53     ` Thomas Huth
2024-12-04 10:46   ` Daniel P. Berrangé
2024-12-06 15:56     ` Thomas Huth [this message]
2024-12-04  7:19 ` [PATCH 3/5] tests/functional/test_vnc: Do not use a hard-coded VNC port Thomas Huth
2024-12-04  7:19 ` [PATCH 4/5] tests/functional/test_vnc: Remove the test_no_vnc test Thomas Huth
2024-12-04 10:42   ` Daniel P. Berrangé
2024-12-04  7:19 ` [PATCH 5/5] tests/functional: Convert the migration avocado test Thomas Huth
2024-12-04  8:27   ` Thomas Huth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86b6f948-6ddf-481e-81e1-de5ee27b6dbd@redhat.com \
    --to=thuth@redhat.com \
    --cc=berrange@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).