From patchwork Mon Sep 27 22:44:01 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas Meneghel Rodrigues X-Patchwork-Id: 213652 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id o8RMforR032214 for ; Mon, 27 Sep 2010 22:41:55 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932982Ab0I0Wld (ORCPT ); Mon, 27 Sep 2010 18:41:33 -0400 Received: from mx1.redhat.com ([209.132.183.28]:32085 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932833Ab0I0Wlb (ORCPT ); Mon, 27 Sep 2010 18:41:31 -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 o8RMfKrQ031832 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 27 Sep 2010 18:41:20 -0400 Received: from virtlab105.virt.bos.redhat.com (virtlab105.virt.bos.redhat.com [10.16.72.124]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o8RMf344003834; Mon, 27 Sep 2010 18:41:19 -0400 From: Lucas Meneghel Rodrigues To: autotest@test.kernel.org Cc: kvm@vger.kernel.org, mst@redhat.com, akong@redhat.com, jasowang@redhat.com, psuriset@linux.vnet.ibm.com, mgoldish@redhat.com Subject: [PATCH 15/18] KVM test: kvm_utils - Add support of check if remote port free Date: Mon, 27 Sep 2010 18:44:01 -0400 Message-Id: <1285627444-2732-16-git-send-email-lmr@redhat.com> In-Reply-To: <1285627444-2732-1-git-send-email-lmr@redhat.com> References: <1285627444-2732-1-git-send-email-lmr@redhat.com> 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 (demeter1.kernel.org [140.211.167.41]); Mon, 27 Sep 2010 22:41:55 +0000 (UTC) diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py index 9390393..a2b0a3f 100644 --- a/client/tests/kvm/kvm_utils.py +++ b/client/tests/kvm/kvm_utils.py @@ -831,7 +831,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. @@ -840,15 +840,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]. @@ -856,12 +863,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]. @@ -872,7 +879,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