generator.py 15.4 KB
Newer Older
Rafael Monnerat's avatar
Rafael Monnerat committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
""" Script:  generator

Generate a Selenium testcase using request / response logiles from tcpwatch.

$Id$
"""

import sys
import re
import getopt
import glob
import cgi
import mimetools
import urllib
import urlparse
import multifile
import StringIO

_TEST_CASE_HEADER = """\
<html>
<head>
<title>%(TEST_CASE_TITLE)s</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
 <tbody>
  <tr>
   <td rowspan="1" colspan="3">%(TEST_CASE_TITLE)s<br>
  </td>
 </tr>
"""

_TEST_CASE_FOOTER = """
 </tbody>
</table>
</body>
</html>
"""

_GET_REQUEST_SKELETON = """
  <tr>
   <td>open</td>
   <td>%(REQUEST_URI)s</td>
   <td>&nbsp;</td>
  </tr>
"""

_POST_REQUEST_SKELETON = """
  <tr>
   <td>click</td>
   <td>submit</td>
   <td>&nbsp;</td>
  </tr>
"""

_REDIRECT_SKELETON = """
  <tr>
   <td>verifyLocation</td>
   <td>%(REDIRECTED_URL)s</td>
   <td>&nbsp;</td>
  </tr>
"""

_INPUT_FIELD_SKELETON = """
  <tr>
   <td>type</td>
   <td>%(FIELD_NAME)s</td>
   <td>%(FIELD_VALUE)s</td>
  </tr>
"""

_SELECT_FIELD_SKELETON = """
  <tr>
   <td>select</td>
   <td>%(FIELD_NAME)s</td>
   <td>%(FIELD_VALUE)s</td>
  </tr>
"""

class ScenarioGenerator:
    """
        Convert a series of HTTP requests files (as generated by
        LoggingProxy) into a scenario file (to be consumed by
        FT_Runner).
    """
    _verbosity              = 1
    _logfile_directory      = '/tmp'
    _logfile_prefix         = 'watch'
    _logfile_extension_req  = 'request'
    _logfile_extension_resp = 'response'
    _output_file            = None
    _exclude_patterns       = []
    _exclude_file           = None
    _exclude_regex          = None
    _site_host              = None
    _site_path              = None
    _test_case_title        = None

    def __init__( self, args ):

        self.parseOptions( args )

    def printUsage( self, msg=None ):
        """
            Dump a help message, and bail out.
        """
        sys.stderr.write( """
  %(GENERATOR_EXE)s [-?vq] \\
                     [-l log_dir] [-f log_prefix] [-e log_extension] \\
                     [-o file] [-x pattern] [-X file] \\
                     [-h site_host] [-r site_path]

    -?, --help                      Print this help message

    -v, --verbose                   Increment verbosity (default is '1')

    -q, --quiet                     Set verbosity to '0'

    -l, --logfile-directory         Directory from which to read log files
                                    (default '%(LOGFILE_DIRECTORY)s')

    -f, --logfile-prefix            Prefix for log file names
                                    (default '%(LOGFILE_PREFIX)s')

    -e, --logfile-extension         Extension for log file request names
                                    (default '%(LOGFILE_EXTENSION_REQ)s')

    -E, --logfile-response          Extension for log file response names
                                    (default '%(LOGFILE_EXTENSION_RESP)s')

    -o, --output-file               Write to 'file', instead of default
                                    (%(LOGFILE_PREFIX)s.zft). 
                                    Use '-' to write to stdout.

    -x, --exclude-pattern           Exclude requests which match 'pattern'
                                    (e.g., to suppress stylesheet or images).

    -X, --exclude-file              Exclude requests which match any pattern
                                    read from 'file' (one pattern per line).

    -h, --site-host                 Specify the host / port of the site being
                                    tested.

    -p, --site-path                 Specify the path to the "base" of the
                                    site being tested.

%(MESSAGE)s\n""" % { 'GENERATOR_EXE'        : sys.argv[0]
                   , 'LOGFILE_DIRECTORY'    : self._logfile_directory
                   , 'LOGFILE_PREFIX'       : self._logfile_prefix
                   , 'LOGFILE_EXTENSION_REQ'    : self._logfile_extension_req
                   , 'LOGFILE_EXTENSION_RESP'    : self._logfile_extension_resp
                   , 'MESSAGE'              : msg or ''
                   } )
        sys.exit( 1 )

    def parseOptions( self, args ):
        """
            Parse command-line options.
        """
        verbosity = self._verbosity
        logfile_directory = logfile_prefix = None
        logfile_extension_req = logfile_extension_resp = None
        output_file = exclude_file = site_host = site_path = None
        test_case_title = None

        try:
            opts, ignored = getopt.getopt( args
                                         , "?vql:f:e:E:o:x:X:h:p:T:"
                                         , [ 'help'
                                           , 'verbose'
                                           , 'quiet'
                                           , 'logfile-directory='
                                           , 'logfile-prefix='
                                           , 'logfile-extension='
                                           , 'logfile-extension-response='
                                           , 'output-file='
                                           , 'exclude-pattern'
                                           , 'exclude-file'
                                           , 'site-host='
                                           , 'site-path='
                                           , 'test-case-title='
                                           ]
                                         )
        except getopt.GetoptError, msg:
            self.printUsage( msg=msg)

        for o, v in opts:

            if o == '-?' or o == '--help':
                self.printUsage()

            if o == '-v' or o == '--verbose':
                verbosity = verbosity + 1

            if o == '-q' or o == '--quiet':
                verbosity = 0

            if o == '-l' or o == '--logfile-directory':
                logfile_directory = v

            if o == '-f' or o == '--logfile-prefix':
                logfile_prefix = v

            if o == '-e' or o == '--logfile-extension':
                logfile_extension_req = v

            if o == '-E' or o == '--logfile-extension-response':
                logfile_extension_resp = v

            if o == '-o' or o == '--output-file':
                output_file = v

            if o == '-x' or o == '--exclude-pattern':
                self._addExcludePattern( v )

            if o == '-X' or o == '--exclude-file':
                exclude_file = v

            if o == '-h' or o == '--site-host':
                site_host = v

            if o == '-p' or o == '--site-path':
                site_path = v

            if o == '-T' or o == '--test-case-title':
                test_case_title = v

        self._verbosity = verbosity

        if logfile_directory is not None:
            self._logfile_directory = logfile_directory

        if logfile_prefix is not None:
            self._logfile_prefix = logfile_prefix

        if logfile_extension_req is not None:
            self._logfile_extension_req = logfile_extension_req

        if logfile_extension_resp is not None:
            self._logfile_extension_resp = logfile_extension_resp

        if site_host is not None:
            self._site_host = site_host

        if site_path is not None:
            self._site_path = site_path

        if test_case_title is not None:
            self._test_case_title = test_case_title

        if output_file == '-':
            self._output_file = sys.stdout
        elif output_file is not None:
            self._output_file = open( output_file, 'w' )
        else:
            self._output_file = sys.stdout

        if exclude_file is not None:
            self._exclude_file = exclude_file

    def _log( self, msg, level ):
        """
            Write a note to stderr (if verbosity enabled).
        """
        if level <= self._verbosity:
            sys.stderr.write( "%s\n" % msg )

    def _print( self, fmt, **kw ):
        """
            Dump the appropriately-formatted values to our output
            file.
        """
        self._output_file.write( fmt % kw )


    def _addExcludePattern( self, pattern ):
        """
            Add a pattern to our list of excluded patterns.
        """
        self._exclude_patterns.append( r'(%s)' % pattern )
        self._exclude_regex = None

    def _getExcludeRegex( self
                        ):
        """
            Return a regex which, if matched, indicates that we should
            skip the file.
        """
        if self._exclude_regex is None:

            if self._exclude_file:
                f = open( self._exclude_file )
                for line in f.readlines():
                    line = line.strip()
                    self._addExcludePattern( line )

            if self._exclude_patterns:
                self._exclude_regex = re.compile(
                                        '|'.join( self._exclude_patterns ) )
        return self._exclude_regex

    def _stripSitePath(self, uri, parms):
        """
            Strip off our site-host and site-path from 'uri'.
        """
        ( scheme
        , netloc
        , path
        , url_parm
        , query
        , fragment
        ) = urlparse.urlparse( uri )

        site_host = urlparse.urlunparse( ( scheme, netloc, '', '', '', '' ) )

        if scheme and parms.get( 'site_host' ) is None:
            parms[ 'site_host' ] = site_host

        if site_host != parms[ 'site_host' ]: # XXX foreign site!  Punt!
            return None, None

        if self._site_path and path.startswith( self._site_path ):
            path = path[ len( self._site_path ) : ]

        uri = urlparse.urlunparse(
                            ( '', '', path, url_parm, query, fragment ) )

        return uri, query

    def processFile( self
                   , infilename
                   , outfilename
                   , parms={}
                   , REQUEST_LINE=re.compile( r'^([^\s]+)\s+'
                                              r'([^\s]+)\s+'
                                              r'([^\s]+)$' )
                   , RESPONSE_LINE=re.compile( r'^([^\s]+)\s+'
                                               r'([0-9][0-9][0-9])\s+'
                                               r'(.*)$' )
                   ):
        """
            Process a single request file;  record global context
            in parms.
        """
        self._log( 'Scanning request file: %s' % infilename, 1 )

        parms[ 'content_type' ] = None

        f = open( infilename )
        all_text = f.read()
        body_end = f.tell()
        f.seek( 0 )

        exclude = self._getExcludeRegex()
        if exclude is not None and exclude.search( all_text ):
            self._log( '** matches exclude regex, skipping', 1 )
            return 

        request = f.readline().rstrip()
        match = REQUEST_LINE.match( request )

        if not match:
            self._log( 'Invalid request line: %s' % request, 0 )
            return

        http_verb, uri, http_version = match.groups()

        uri, query = self._stripSitePath( uri, parms )

        if uri is None:
            return # XXX foreign site

        headers = mimetools.Message( f )

        body_start = f.tell()
        content_length = body_end - body_start

        content_type = parms[ 'content_type' ] = headers.gettype()
        parms[ 'encoding' ] = headers.getencoding()

        cgi_environ = { 'REQUEST_METHOD' : http_verb
                      , 'QUERY_STRING'   : query
                      , 'CONTENT_TYPE'   : headers.typeheader
                      , 'CONTENT_LENGTH' : content_length
                      }

        if http_verb == 'POST':

            if content_type == 'text/xml':   # XXX XML-RPC, punt!
                return

            form_data = cgi.FieldStorage( fp=f
                                        , environ=cgi_environ
                                        , keep_blank_values=1
                                      # , headers=headers.dict  XXX
                                        )
            for k in form_data.keys():

                v = form_data.getvalue( k )

                # TODO: handle uploaded files.

                self._print( _INPUT_FIELD_SKELETON
                           , FIELD_NAME=k
                           , FIELD_VALUE=v
                           )

            payload = f.read()
            f.close()

            self._print( _POST_REQUEST_SKELETON
                       , REQUEST_URI=uri
                       )
        else:
            self._print( _GET_REQUEST_SKELETON
                       , REQUEST_URI=uri
                       )

        if outfilename:
            self._log( 'Scanning response file: %s' % outfilename, 1 )
            response_file = open( outfilename )
            # could exclude here as well
            status = response_file.readline().rstrip()
            match = RESPONSE_LINE.match( status )
            http_verb, code, reason = match.groups()        
            response_headers = mimetools.Message( response_file )
            response_file.close()
        else:
            code = 200

        if code in ('301', '302', '307'):
            response_location = response_headers[ 'Location' ]
            response_uri, query = self._stripSitePath( response_location
                                                     , parms
                                                     )
            self._print( _REDIRECT_SKELETON
                       , REDIRECTED_URL=response_uri
                       )


        return 

    def processScenario( self ):
        """
            Read all files in '_logfile_directory' whose prefix
            matches '_logfile_prefix', '_logfile_extension_req', and
            '_logfile_extension_resp';
            create a scenario file with one section per request file,
            dumping to specified output file.
        """
        self._print( _TEST_CASE_HEADER
                   , TEST_CASE_TITLE=self._test_case_title or 'TEST CASE'
                   )

        glob__in_pattern = '%s/%s*.%s' % ( self._logfile_directory
                                         , self._logfile_prefix
                                         , self._logfile_extension_req
                                         )

        glob__out_pattern = '%s/%s*.%s' % ( self._logfile_directory
                                          , self._logfile_prefix
                                          , self._logfile_extension_resp
                                          )

        parms = { 'site_host' : self._site_host
                , 'site_path' : self._site_path
                }

        infilenames = glob.glob( glob__in_pattern )
        infilenames.sort()
        outfilenames = glob.glob( glob__out_pattern )
        outfilenames.sort()
        for infilename in infilenames:
            # find the response file name that matches this request file
            outfilename = re.sub( self._logfile_extension_req + '$'
                                , self._logfile_extension_resp
                                , infilename
                                )
            # XXX error if missing?  Optional outfile processing?
            if outfilename in outfilenames:
                self.processFile( infilename
                                , outfilename
                                , parms
                                )
            else:
                self.processFile( infilename
                                , None
                                , parms
                                )
                
        self._print( _TEST_CASE_FOOTER )

if __name__ == '__main__':

    ScenarioGenerator( sys.argv[1:] ).processScenario()