View Issue Details

IDProjectCategoryView StatusLast Update
0001739GNUnetobsoletepublic2024-05-03 13:51
ReporterLRN Assigned ToChristian Grothoff  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Product VersionGit master 
Summary0001739: Portable peerinfo-tool testcase
DescriptionCurrent test_gnunet_peerinfo.py.in implementation relies on pexpect, which is not portable.
Attached is a version of test_gnunet_peerinfo.py that uses portable Python APIs.

I've also adjusted the tests to match the output of current peerinfo-tool from SVN HEAD (i.e. '\t' instead of ' ', multiple address lines per peer, possible '\n's on W32, empty line after the last address line).
TagsNo tags attached.
Attached Files
test_gnunet_peerinfo.py (5,297 bytes)   
#!/bin/python
#    This file is part of GNUnet.
#    (C) 2010 Christian Grothoff (and other contributing authors)
#
#    GNUnet is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published
#    by the Free Software Foundation; either version 2, or (at your
#    option) any later version.
#
#    GNUnet is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with GNUnet; see the file COPYING.  If not, write to the
#    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#    Boston, MA 02111-1307, USA.
#
# Testcase for gnunet-peerinfo
#import pexpect
from __future__ import print_function
import os
#import signal
import re
import subprocess
import sys
import shutil
import time

class pexpect (object):
  def __init__ (self):
    super (pexpect, self).__init__ ()

  def spawn (self, stdin, arglist, *pargs, **kwargs):
    self.proc = subprocess.Popen (arglist, *pargs, **kwargs)
    if self.proc is None:
      print ("Failed to spawn a process {0}".format (arglist))
      sys.exit (1)
    if stdin is not None:
      self.stdo, self.stde = self.proc.communicate (stdin)
    else:
      self.stdo, self.stde = self.proc.communicate ()
    return self.proc

  def expect (self, s, r, flags=0):
    stream = self.stdo if s == 'stdout' else self.stde
    if isinstance (r, str):
      if r == "EOF":
        if len (stream) == 0:
          return True
        else:
          print ("Failed to match {0} with `{1}'. {0} is `{2}'".format (s, r, stream))
          sys.exit (2)
      raise ValueError ("Argument `r' should be an instance of re.RegexObject or a special string, but is `{0}'".format (r))
    m = r.match (stream, flags)
    if not m:
      print ("Failed to match {0} with `{1}'. {0} is `{2}'".format (s, r.pattern, stream))
      sys.exit (2)
    stream = stream[m.end ():]
    if s == 'stdout':
      self.stdo = stream
    else:
      self.stde = stream
    return m

  def read (self, s, size=-1):
    stream = self.stdo if s == 'stdout' else self.stde
    result = ""
    if size < 0:
      result = stream
      new_stream = ""
    else:
      result = stream[0:size]
      new_stream = stream[size:]
    if s == 'stdout':
      self.stdo = new_stream
    else:
      self.stde = new_stream
    return result

if os.name == 'posix':
  peerinfo = 'gnunet-peerinfo'
  gnunetarm = 'gnunet-arm'
elif os.name == 'nt':
  peerinfo = 'gnunet-peerinfo.exe'
  gnunetarm = 'gnunet-arm.exe'



pinfo = pexpect ()
pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
pinfo.expect ("stdout", re.compile (r'Error in communication with PEERINFO service\r\n?'))
pinfo.expect ("stdout", "EOF")

shutil.rmtree (os.path.join (os.getenv ("TEMP"), "tmp", "gnunet-test-peerinfo"), True)
arm = subprocess.Popen ([gnunetarm, '-sq', '-c', 'test_gnunet_peerinfo_data.conf'])
arm.communicate ()

try:
  pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-s'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  pinfo.expect ("stdout", re.compile (r'I am peer `.*\'.\r\n?'))
  pinfo.expect ("stdout", "EOF")

  pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  pinfo.expect ("stdout", re.compile (r'.......................................................................................................\r\n?'))
  pinfo.expect ("stdout", "EOF")

  pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', 'invalid'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  pinfo.expect ("stdout", re.compile (r'Invalid command line argument `invalid\'\r\n?'))
  pinfo.expect ("stdout", "EOF")

  arm = subprocess.Popen ([gnunetarm, '-q', '-i', 'transport', '-c', 'test_gnunet_peerinfo_data.conf'])
  arm.communicate ()
  time.sleep (1)

  pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  pinfo.expect ("stdout", re.compile ("Peer `.*'\r\n?"))
  m = pinfo.expect ("stdout", re.compile ("\s.*:24357\r\n?"))
  while len (m.group (0)) > 0:
    m = pinfo.expect ("stdout", re.compile ("(\s.*:24357\r\n?|\r\n?|)"))
  pinfo.expect ("stdout", "EOF")

  pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-n'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  pinfo.expect ("stdout", re.compile ("Peer `.*'\r\n?"))
  m = pinfo.expect ("stdout", re.compile ("\s.*:24357\r\n?"))
  while len (m.group (0)) > 0:
    m = pinfo.expect ("stdout", re.compile ("(\s.*:24357\r\n?|\r\n?|)"))
  pinfo.expect ("stdout", "EOF")

  pinfo.spawn (None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  pid = pinfo.read ("stdout")
  pid.strip ()
  
finally:
  arm = subprocess.Popen ([gnunetarm, '-eq', '-c', 'test_gnunet_peerinfo_data.conf'])
  arm.communicate ()
  shutil.rmtree (os.path.join (os.getenv ("TEMP"), "tmp", "gnunet-test-peerinfo"), True)

test_gnunet_peerinfo.py (5,297 bytes)   

Activities

Christian Grothoff

2011-07-27 20:02

manager   ~0004534

Having lines end with '\r\n' breaks stuff on GNU:

Failed to match stdout with `Error in communication with PEERINFO service\r\n?'. stdout is `Error in communication with PEERINFO service
'

Christian Grothoff

2011-07-27 20:03

manager   ~0004535

If I replace \r\n with \n, I then get this:

Traceback (most recent call last):
  File "./test_gnunet_peerinfo.py", line 96, in <module>
    shutil.rmtree (os.path.join (os.getenv ("TEMP"), "tmp", "gnunet-test-peerinfo"), True)
  File "/usr/lib/python2.7/posixpath.py", line 68, in join
    elif path == '' or path.endswith('/'):
AttributeError: 'NoneType' object has no attribute 'endswith'
FAIL: test_gnunet_peerinfo.py

LRN

2011-07-27 20:27

reporter   ~0004536

Well, "TEMP" is my quick hack to be able to address a temporary directory without using an absolute path, like "/tmp/...".
GNUnet on W32 uses plibc to substitute absolute POSIX-style paths with W32-specific paths, but this is Python...
I think something like this:

if os.name == "nt":
  shutil.rmtree (os.path.join (os.getenv ("TEMP"), "tmp", "gnunet-test-peerinfo"), True)
else:
  shutil.rmtree ("/tmp/gnunet-test-peerinfo"), True)

is in order here.

LRN

2011-07-27 20:30

reporter   ~0004537

As for \r\n and \r...it SHOULD work, since \n is followed by a '?', which should make it optional...right?
Ah, wait...\n is 0x0A, and \r is 0x0D, right?
Why did the original script use \r then?
On *nix you are supposed to have \n-only line ends, not \r-only. Or is this pexpect-specific (maybe you get different line ends from a tty?).

Christian Grothoff

2011-07-28 18:41

manager   ~0004538

In SVN 16258.

Issue History

Date Modified Username Field Change
2011-07-27 01:31 LRN New Issue
2011-07-27 01:31 LRN Status new => assigned
2011-07-27 01:31 LRN Assigned To => NDurner
2011-07-27 01:31 LRN File Added: test_gnunet_peerinfo.py
2011-07-27 20:02 Christian Grothoff Note Added: 0004534
2011-07-27 20:03 Christian Grothoff Note Added: 0004535
2011-07-27 20:27 LRN Note Added: 0004536
2011-07-27 20:30 LRN Note Added: 0004537
2011-07-27 20:33 NDurner Assigned To NDurner => Christian Grothoff
2011-07-28 18:41 Christian Grothoff Note Added: 0004538
2011-07-28 18:42 Christian Grothoff Status assigned => resolved
2011-07-28 18:42 Christian Grothoff Resolution open => fixed
2011-08-01 09:52 Christian Grothoff Status resolved => closed
2024-01-12 14:28 schanzen Category Win32 port => Win32 port (deprecated)
2024-05-03 13:51 Christian Grothoff Category Win32 port (deprecated) => obsolete