[PATCH] osmo-bsc[master]: Make extended tests work with any python version

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Max gerrit-no-reply at lists.osmocom.org
Sun Jan 7 16:23:57 UTC 2018


Review at  https://gerrit.osmocom.org/5661

Make extended tests work with any python version

Previously vty and ctrl tests were only compatible with python2. Make it
work with either v2 or v3 and adjust ./configure check accordingly.

Change-Id: Ib6f845420c0b23f81b076ef6a403d5969260811a
Related: OS#2819
---
M configure.ac
M tests/ctrl_test_runner.py
M tests/vty_test_runner.py
3 files changed, 45 insertions(+), 49 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/osmo-bsc refs/changes/61/5661/1

diff --git a/configure.ac b/configure.ac
index d756970..f618c0d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -127,10 +127,7 @@
 				[Include the VTY/CTRL tests in make check [default=no]]),
 		[enable_ext_tests="$enableval"],[enable_ext_tests="no"])
 if test "x$enable_ext_tests" = "xyes" ; then
-	AC_CHECK_PROG(PYTHON2_AVAIL,python2,yes)
-	 if test "x$PYTHON2_AVAIL" != "xyes" ; then
-		AC_MSG_ERROR([Please install python2 to run the VTY/CTRL tests.])
-	fi
+   	AM_PATH_PYTHON
 	AC_CHECK_PROG(OSMOTESTEXT_CHECK,osmotestvty.py,yes)
 	 if test "x$OSMOTESTEXT_CHECK" != "xyes" ; then
 		AC_MSG_ERROR([Please install git://osmocom.org/python/osmo-python-tests to run the VTY/CTRL tests.])
diff --git a/tests/ctrl_test_runner.py b/tests/ctrl_test_runner.py
index 4f5df39..6382fa5 100755
--- a/tests/ctrl_test_runner.py
+++ b/tests/ctrl_test_runner.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 
 # (C) 2013 by Jacob Erlbeck <jerlbeck at sysmocom.de>
 # (C) 2014 by Holger Hans Peter Freyther
@@ -19,7 +19,7 @@
 
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
+from __future__ import print_function
 import os
 import time
 import unittest
@@ -53,8 +53,8 @@
         try:
             self.proc = osmoutil.popen_devnull(osmo_ctrl_cmd)
         except OSError:
-            print >> sys.stderr, "Current directory: %s" % os.getcwd()
-            print >> sys.stderr, "Consider setting -b"
+            print("Current directory: %s" % os.getcwd(), file=sys.stderr)
+            print("Consider setting -b", file=sys.stderr)
         time.sleep(2)
 
         appstring = self.ctrl_app()[2]
@@ -72,7 +72,7 @@
 
     def connect(self, host, port):
         if verbose:
-            print "Connecting to host %s:%i" % (host, port)
+            print("Connecting to host %s:%i" % (host, port))
 
         retries = 30
         while True:
@@ -92,7 +92,7 @@
 
     def send(self, data):
         if verbose:
-            print "Sending \"%s\"" %(data)
+            print("Sending \"%s\"" %(data))
         data = Ctrl().add_header(data)
         return self.sock.send(data) == len(data)
 
@@ -123,7 +123,7 @@
             (head, data) = IPA().split_combined(data)
             answer = Ctrl().rem_header(head)
             if verbose:
-                print "Got message:", answer
+                print("Got message:", answer)
             (mtype, id, msg) = answer.split(None, 2)
             id = int(id)
             rsp = {'mtype': mtype, 'id': id}
@@ -139,7 +139,7 @@
             responses[id] = rsp
 
         if verbose:
-            print "Decoded replies: ", responses
+            print("Decoded replies: ", responses)
 
         return responses
 
@@ -560,9 +560,9 @@
     if args.p:
         confpath = args.p
 
-    print "confpath %s, workdir %s" % (confpath, workdir)
+    print("confpath %s, workdir %s" % (confpath, workdir))
     os.chdir(workdir)
-    print "Running tests for specific control commands"
+    print("Running tests for specific control commands")
     suite = unittest.TestSuite()
     add_bsc_test(suite, workdir)
     add_nat_test(suite, workdir)
diff --git a/tests/vty_test_runner.py b/tests/vty_test_runner.py
index 387ea70..ac6f1de 100755
--- a/tests/vty_test_runner.py
+++ b/tests/vty_test_runner.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 
 # (C) 2013 by Katerina Barone-Adesi <kat.obsc at gmail.com>
 # (C) 2013 by Holger Hans Peter Freyther
@@ -14,7 +14,7 @@
 
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
+from __future__ import print_function
 import os, sys
 import time
 import unittest
@@ -54,8 +54,8 @@
         try:
             self.proc = osmoutil.popen_devnull(osmo_vty_cmd)
         except OSError:
-            print >> sys.stderr, "Current directory: %s" % os.getcwd()
-            print >> sys.stderr, "Consider setting -b"
+            print("Current directory: %s" % os.getcwd(), file=sys.stderr)
+            print("Consider setting -b", file=sys.stderr)
 
         appstring = self.vty_app()[2]
         appport = self.vty_app()[0]
@@ -444,33 +444,33 @@
         ussdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         ussdSocket.connect(('127.0.0.1', 5001))
         ussdSocket.settimeout(2.0)
-        print "Connected to %s:%d" % ussdSocket.getpeername()
+        print("Connected to %s:%d" % ussdSocket.getpeername())
 
-        print "Expecting ID_GET request"
+        print("Expecting ID_GET request")
         data = ussdSocket.recv(4)
         self.assertEqual(data, "\x00\x01\xfe\x04")
 
-        print "Going to send ID_RESP response"
+        print("Going to send ID_RESP response")
         res = ussdSocket.send(IPA().id_resp(IPA().tag_name('key')))
         self.assertEqual(res, 10)
 
         # initiating PING/PONG cycle to know, that the ID_RESP message has been processed
 
-        print "Going to send PING request"
+        print("Going to send PING request")
         res = ussdSocket.send(IPA().ping())
         self.assertEqual(res, 4)
 
-        print "Expecting PONG response"
+        print("Expecting PONG response")
         data = ussdSocket.recv(4)
         self.assertEqual(data, "\x00\x01\xfe\x01")
 
         res = self.vty.verify("show ussd-connection", ['The USSD side channel provider is connected and authorized.'])
         self.assertTrue(res)
 
-        print "Going to shut down connection"
+        print("Going to shut down connection")
         ussdSocket.shutdown(socket.SHUT_WR)
 
-        print "Expecting EOF"
+        print("Expecting EOF")
         data = ussdSocket.recv(4)
         self.assertEqual(data, "")
 
@@ -548,11 +548,11 @@
     msc.bind((ip, port))
     msc.listen(5)
     if (verbose):
-        print "MSC is ready at " + ip
+        print("MSC is ready at " + ip)
     conn = None
     while True:
         vty_response = x.vty.command("show msc connection")
-        print "'show msc connection' says: %r" % vty_response
+        print("'show msc connection' says: %r" % vty_response)
         if vty_response == "MSC is connected: 1":
             # success
             break;
@@ -564,10 +564,10 @@
         while timeout_retries > 0:
             try:
                 conn, addr = msc.accept()
-                print "MSC got connection from ", addr
+                print("MSC got connection from ", addr)
                 break
             except socket.timeout:
-                print "socket timed out."
+                print("socket timed out.")
                 timeout_retries -= 1
                 continue
 
@@ -582,42 +582,41 @@
       raise Exception("expected to receive 4 bytes, but got %d (%r)" % (len(s)/2, s))
     if "0001fe00" == s:
         if (verbose):
-            print "\tBSC <- NAT: PING?"
+            print("\tBSC <- NAT: PING?")
         x.send(IPA().pong())
     elif "0001fe06" == s:
         if (verbose):
-            print "\tBSC <- NAT: IPA ID ACK"
+            print("\tBSC <- NAT: IPA ID ACK")
         x.send(IPA().id_ack())
     elif "0001fe00" == s:
         if (verbose):
-            print "\tBSC <- NAT: PONG!"
+            print("\tBSC <- NAT: PONG!")
     else:
         if (verbose):
-            print "\tBSC <- NAT: ", s
+            print("\tBSC <- NAT: ", s)
 
 def ipa_handle_resp(x, tk, verbose = False, proc=None):
     s = data2str(x.recv(38))
     if "0023fe040108010701020103010401050101010011" in s:
         retries = 3
         while True:
-            print "\tsending IPA identity(%s) at %s" % (tk, time.strftime("%T"))
+            print("\tsending IPA identity(%s) at %s" % (tk, time.strftime("%T")))
             try:
                 x.send(IPA().id_resp(IPA().identity(name = tk.encode('utf-8'))))
-                print "\tdone sending IPA identity(%s) at %s" % (tk,
-                                                            time.strftime("%T"))
+                print("\tdone sending IPA identity(%s) at %s" % (tk, time.strftime("%T")))
                 break
             except:
-                print "\tfailed sending IPA identity at", time.strftime("%T")
+                print("\tfailed sending IPA identity at", time.strftime("%T"))
                 if proc:
-                  print "\tproc.poll() = %r" % proc.poll()
+                  print("\tproc.poll() = %r" % proc.poll())
                 if retries < 1:
-                    print "\tgiving up"
+                    print("\tgiving up")
                     raise
-                print "\tretrying (%d attempts left)" % retries
+                print("\tretrying (%d attempts left)" % retries)
                 retries -= 1
     else:
         if (verbose):
-            print "\tBSC <- NAT: ", s
+            print("\tBSC <- NAT: ", s)
 
 def nat_bsc_num_con(x):
     return len(x.vty.command("show bsc connections").split('\n'))
@@ -627,18 +626,18 @@
     bsc.bind(('127.0.0.1', 0))
     bsc.connect(('127.0.0.1', 5000))
     if (verbose):
-        print "BSC%d " %nr
-        print "\tconnected to %s:%d" % bsc.getpeername()
+        print("BSC%d " %nr)
+        print("\tconnected to %s:%d" % bsc.getpeername())
     if proc:
-      print "\tproc.poll() = %r" % proc.poll()
-      print "\tproc.pid = %r" % proc.pid
+      print("\tproc.poll() = %r" % proc.poll())
+      print("\tproc.pid = %r" % proc.pid)
     ipa_handle_small(bsc, verbose)
     ipa_handle_resp(bsc, tk, verbose, proc=proc)
     if proc:
-      print "\tproc.poll() = %r" % proc.poll()
+      print("\tproc.poll() = %r" % proc.poll())
     bsc.recv(27) # MGCP msg
     if proc:
-      print "\tproc.poll() = %r" % proc.poll()
+      print("\tproc.poll() = %r" % proc.poll())
     ipa_handle_small(bsc, verbose)
     return bsc
 
@@ -675,9 +674,9 @@
     if args.p:
         confpath = args.p
 
-    print "confpath %s, workdir %s" % (confpath, workdir)
+    print("confpath %s, workdir %s" % (confpath, workdir))
     os.chdir(workdir)
-    print "Running tests for specific VTY commands"
+    print("Running tests for specific VTY commands")
     suite = unittest.TestSuite()
     add_bsc_test(suite, workdir)
     add_nat_test(suite, workdir)

-- 
To view, visit https://gerrit.osmocom.org/5661
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib6f845420c0b23f81b076ef6a403d5969260811a
Gerrit-PatchSet: 1
Gerrit-Project: osmo-bsc
Gerrit-Branch: master
Gerrit-Owner: Max <msuraev at sysmocom.de>



More information about the gerrit-log mailing list