From patchwork Mon Aug 23 08:53:44 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amos Kong X-Patchwork-Id: 123581 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.4/8.14.3) with ESMTP id o7N9QZRF027645 for ; Mon, 23 Aug 2010 09:39:52 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753341Ab0HWIus (ORCPT ); Mon, 23 Aug 2010 04:50:48 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58081 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753078Ab0HWIus (ORCPT ); Mon, 23 Aug 2010 04:50:48 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o7N8olB5029359 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 23 Aug 2010 04:50:47 -0400 Received: from [0.0.0.190] (dhcp-91-190.nay.redhat.com [10.66.91.190]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o7N8ojs9009304; Mon, 23 Aug 2010 04:50:46 -0400 Subject: [PATCH v2 14/17] Add support of check if remote port free To: autotest@vger.kernel.org From: Amos Kong Cc: lmr@redhat.com, kvm@vger.kernel.org Date: Mon, 23 Aug 2010 16:53:44 +0800 Message-ID: <20100823085344.19173.97999.stgit@190> In-Reply-To: <20100823084745.19173.61655.stgit@190> References: <20100823084745.19173.61655.stgit@190> User-Agent: StGit/0.15 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Mon, 23 Aug 2010 09:39:52 +0000 (UTC) diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py index b019fc5..a24585b 100644 --- a/client/tests/kvm/kvm_utils.py +++ b/client/tests/kvm/kvm_utils.py @@ -789,7 +789,7 @@ def scp_from_remote(host, port, username, password, remote_path, local_path, # The following are utility functions related to ports. -def is_port_free(port): +def is_port_free(port, address): """ Return True if the given port is available for use. @@ -798,15 +798,22 @@ def is_port_free(port): try: s = socket.socket() #s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - s.bind(("localhost", port)) - free = True + if address == "localhost": + s.bind(("localhost", port)) + free = True + else: + s.connect((address, port)) + free = False except socket.error: - free = False + if address == "localhost": + free = False + else: + free = True s.close() return free -def find_free_port(start_port, end_port): +def find_free_port(start_port, end_port, address="localhost"): """ Return a host free port in the range [start_port, end_port]. @@ -814,12 +821,12 @@ def find_free_port(start_port, end_port): @param end_port: Port immediately after the last one that will be checked. """ for i in range(start_port, end_port): - if is_port_free(i): + if is_port_free(i, address): return i return None -def find_free_ports(start_port, end_port, count): +def find_free_ports(start_port, end_port, count, address="localhost"): """ Return count of host free ports in the range [start_port, end_port]. @@ -830,7 +837,7 @@ def find_free_ports(start_port, end_port, count): ports = [] i = start_port while i < end_port and count > 0: - if is_port_free(i): + if is_port_free(i, address): ports.append(i) count -= 1 i += 1