~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to tests/lib/sys_mgmt/system_management.py

  • Committer: Mark Atwood
  • Date: 2011-10-11 22:37:47 UTC
  • mfrom: (2429.2.1 dbqp_revamp)
  • Revision ID: me@mark.atwood.name-20111011223747-vomebzgvnxxysqrm
mergeĀ lp:~patrick-crews/drizzle/dbqp_revamp

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
# -*- mode: python; indent-tabs-mode: nil; -*-
3
3
# vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
4
#
5
 
# Copyright (C) 2010 Patrick Crews
 
5
# Copyright (C) 2010, 2011 Patrick Crews
6
6
#
7
7
# This program is free software; you can redistribute it and/or modify
8
8
# it under the terms of the GNU General Public License as published by
33
33
# imports
34
34
import os
35
35
import sys
36
 
import copy
37
36
import shutil
38
37
import getpass
39
38
import commands
40
39
 
41
 
from lib.uuid import uuid4
 
40
from uuid import uuid4
 
41
 
 
42
from lib.sys_mgmt.code_management import codeManager
 
43
from lib.sys_mgmt.environment_management import environmentManager
 
44
from lib.sys_mgmt.logging_management import loggingManager
42
45
from lib.sys_mgmt.port_management import portManager
43
 
from lib.sys_mgmt.logging_management import loggingManager
44
46
from lib.sys_mgmt.time_management import timeManager
45
47
 
46
48
class systemManager:
55
57
        if variables['verbose']:
56
58
            self.logging.verbose("Initializing system manager...")
57
59
 
58
 
        self.skip_keys = [ 'code_tree'
59
 
                         , 'ld_lib_paths'
60
 
                         , 'port_manager'
 
60
        self.skip_keys = [ 'port_manager'
61
61
                         , 'logging_manager'
 
62
                         , 'code_manager'
 
63
                         , 'env_manager'
62
64
                         , 'environment_reqs'
63
 
                         , 'env_var_delimiter']
 
65
                         ]
64
66
        self.debug = variables['debug']
65
67
        self.verbose = variables['verbose']
66
 
        self.env_var_delimiter = ':'
67
68
        self.no_shm = variables['noshm']
68
69
        self.shm_path = self.find_path(["/dev/shm", "/tmp"], required=0)
69
70
        self.cur_os = os.uname()[0]
75
76
        self.top_builddir = os.path.abspath(variables['topbuilddir'])
76
77
        self.start_dirty = variables['startdirty']
77
78
        self.valgrind = variables['valgrind']
 
79
        self.valgrind_suppress_file = variables['valgrindsuppressions']
78
80
        self.gdb = variables['gdb']
79
81
        self.manual_gdb = variables['manualgdb']
80
82
        self.randgen_path = variables['randgenpath']
84
86
        
85
87
        self.port_manager = portManager(self,variables['debug'])
86
88
        self.time_manager = timeManager(self)
87
 
                   
88
 
        # Make sure the tree we are testing looks good
89
 
        self.code_tree = self.get_code_tree(variables, tree_type)
90
 
 
91
 
        self.ld_lib_paths = self.join_env_var_values(self.code_tree.ld_lib_paths)
 
89
 
 
90
        # use our code_manager to handle the various basedirs 
 
91
        # we have been passed
 
92
        self.code_manager = codeManager(self, variables)
 
93
 
 
94
        # environment manager handles updates / whatever to testing environments
 
95
        self.env_manager = environmentManager(self, variables)
92
96
 
93
97
        # Some ENV vars are system-standard
94
98
        # We describe and set them here and now
101
105
                                , 'USE_RUNNING_SERVER' : "0"
102
106
                                , 'TOP_SRCDIR' : self.top_srcdir
103
107
                                , 'TOP_BUILDDIR' : self.top_builddir
104
 
                                , 'DRIZZLE_TEST_DIR' : self.code_tree.testdir
 
108
                                , 'DRIZZLE_TEST_DIR' : self.testdir
105
109
                                , 'DTR_BUILD_THREAD' : "-69.5"
106
 
                                , 'LD_LIBRARY_PATH' : self.append_env_var( 'LD_LIBRARY_PATH'
107
 
                                                                         , self.ld_lib_paths
108
 
                                                                         , suffix = 0
109
 
                                                                         , quiet = 1
110
 
                                                                         )
111
 
                                , 'DYLD_LIBRARY_PATH' : self.append_env_var( 'DYLD_LIBRARY_PATH'
112
 
                                                                         , self.ld_lib_paths
113
 
                                                                         , suffix = 0
114
 
                                                                         , quiet = 1
115
 
                                                                         )
116
110
                                }
117
111
        # set the env vars we need
118
112
        # self.process_environment_reqs(self.environment_reqs)
119
 
        self.update_environment_vars(self.environment_reqs)
 
113
        self.env_manager.update_environment_vars(self.environment_reqs)
120
114
 
121
115
        # We find or generate our id file
122
116
        # We use a uuid to identify the symlinked
137
131
        # options like valgrind and gdb
138
132
        self.handle_additional_reqs(variables)
139
133
     
140
 
        if self.debug:
141
 
            self.logging.debug_class(self)
142
 
        
143
 
    def get_code_tree(self, variables, tree_type):
144
 
        """Find out the important files, directories, and env. vars
145
 
           for a particular type of tree.  We import a definition module
146
 
           depending on the tree_type.  The module lets us know
147
 
           what to look for, etc
148
 
 
149
 
        """
150
 
        
151
 
        # Import the appropriate module that defines
152
 
        # where we find what we need depending on 
153
 
        # tree type
154
 
        test_tree = self.process_tree_type(tree_type, variables)
155
 
        return test_tree
156
 
 
157
 
    def process_tree_type(self, tree_type, variables):
158
 
        """Import the appropriate module depending on the type of tree
159
 
           we are testing. 
160
 
 
161
 
           Drizzle is the only supported type currently
162
 
 
163
 
        """
164
 
 
165
 
        if self.verbose:
166
 
            self.logging.verbose("Processing source tree under test...")
167
 
        if tree_type == 'drizzle':
168
 
            # base_case
169
 
            from lib.sys_mgmt.codeTree import drizzleTree
170
 
            test_tree = drizzleTree(variables,self)
171
 
            return test_tree
172
 
        else:
173
 
            self.logging.error("Tree_type: %s not supported yet" %(tree_type))
174
 
            sys.exit(1)        
175
 
 
176
 
    
 
134
        self.logging.debug_class(self)
 
135
 
 
136
 
177
137
    def create_dirset(self, rootdir, dirset):
178
138
        """ We produce the set of directories defined in dirset
179
139
            dirset is a set of dictionaries like
270
230
                return full_path
271
231
            else:
272
232
                shutil.rmtree(full_path)
273
 
            if self.debug:
274
 
                 self.logging.debug("Creating directory: %s" %(dirname))   
 
233
            self.logging.debug("Creating directory: %s" %(dirname))   
275
234
        os.makedirs(full_path)
276
235
        return full_path
277
236
 
282
241
            the dir must be empty to remove it
283
242
 
284
243
        """
285
 
        if self.debug:
286
 
            self.logging.debug("Removing directory: %s" %(dirname))
 
244
        self.logging.debug("Removing directory: %s" %(dirname))
287
245
        if os.path.islink(dirname):
288
246
            os.remove(dirname)
289
247
        elif require_empty:
297
255
            if overwrite == 1
298
256
 
299
257
        """
300
 
        if self.debug:
301
 
            self.logging.debug("Copying directory: %s to %s" %(srcdir, tgtdir))
 
258
 
 
259
        self.logging.debug("Copying directory: %s to %s" %(srcdir, tgtdir))
302
260
        if os.path.exists(tgtdir):
303
261
            if overwrite:
304
262
                self.remove_dir(tgtdir)
309
267
 
310
268
    def create_symlink(self, source, link_name):
311
269
        """ We create a symlink to source named link_name """
312
 
        if self.debug:
313
 
            self.logging.debug("Creating symlink from %s to %s" %(source, link_name))
 
270
 
 
271
        self.logging.debug("Creating symlink from %s to %s" %(source, link_name))
314
272
        if os.path.exists(link_name) or os.path.islink(link_name):
315
273
            os.remove(link_name)
316
274
        return os.symlink(source, link_name)
325
283
            source, link_name = needed_symlink
326
284
            self.create_symlink(source, link_name)
327
285
 
328
 
    def join_env_var_values(self, value_list):
329
 
        """ Utility to join multiple values into a nice string
330
 
            for setting an env var to
331
 
 
332
 
        """
333
 
 
334
 
        return self.env_var_delimiter.join(value_list)
335
 
 
336
 
    def set_env_var(self, var_name, var_value, quiet=0):
337
 
        """Set an environment variable.  We really just abstract
338
 
           voodoo on os.environ
339
 
 
340
 
        """
341
 
        if self.debug and not quiet:
342
 
            self.logging.debug("Setting env var: %s" %(var_name))
343
 
        try:
344
 
            os.environ[var_name]=var_value
345
 
        except Exception, e:
346
 
            self.logging.error("Issue setting environment variable %s to value %s" %(var_name, var_value))
347
 
            self.logging.error("%s" %(e))
348
 
            sys.exit(1)
349
 
 
350
 
    def update_environment_vars(self, desired_vars, working_environment=None):
351
 
        """ We update the environment vars with desired_vars
352
 
            The expectation is that you know what you are asking for ; )
353
 
            If working_environment is provided, we will update that with
354
 
            desired_vars.  We operate directly on os.environ by default
355
 
            We return our updated environ dictionary
356
 
 
357
 
        """
358
 
 
359
 
        if not working_environment:
360
 
            working_environment = os.environ
361
 
        working_environment.update(desired_vars)
362
 
        return working_environment
363
 
 
364
 
    def create_working_environment(self, desired_vars):
365
 
        """ We return a copy of os.environ updated with desired_vars """
366
 
 
367
 
        working_copy = copy.deepcopy(os.environ)
368
 
        return self.update_environment_vars( desired_vars
369
 
                                    , working_environment = working_copy )
370
 
 
371
 
    def append_env_var(self, var_name, append_string, suffix=1, quiet=0):
372
 
        """ We add the values in var_values to the environment variable 
373
 
            var_name.  Depending on suffix value, we either append or prepend
374
 
            we return a string suitable for os.putenv
375
 
 
376
 
        """
377
 
        new_var_value = ""
378
 
        if var_name in os.environ:
379
 
            cur_var_value = os.environ[var_name]
380
 
            if suffix: # We add new values to end of existing value
381
 
                new_var_values = [ cur_var_value, append_string ]
382
 
            else:
383
 
                new_var_values = [ append_string, cur_var_value ]
384
 
            new_var_value = self.env_var_delimiter.join(new_var_values)
385
 
        else:
386
 
            # No existing variable value
387
 
            new_var_value = append_string
388
 
        return new_var_value
 
286
 
389
287
 
390
288
    def find_path(self, paths, required=1):
391
289
        """We search for the files we need / want to be aware of
402
300
        """
403
301
 
404
302
        for test_path in paths:
405
 
            if self.debug:
406
 
                self.logging.debug("Searching for path: %s" %(test_path))
 
303
            self.logging.debug("Searching for path: %s" %(test_path))
407
304
            if os.path.exists(test_path):
408
305
                return test_path
409
306
        if required:
418
315
 
419
316
        """
420
317
 
421
 
        if self.debug:
422
 
            self.logging.debug("Executing command: %s" %(cmd))
 
318
        self.logging.debug("Executing command: %s" %(cmd))
423
319
        (retcode, output)= commands.getstatusoutput(cmd)
424
320
        if not retcode == 0 and must_pass:
425
321
            self.logging.error("Command %s failed with retcode %d" %(cmd, retcode))
500
396
 
501
397
        if self.manual_gdb:
502
398
            self.logging.info("To start gdb, open another terminal and enter:")
503
 
            self.logging.info("%s/../libtool --mode=execute gdb -cd %s -x %s %s" %( self.code_tree.testdir
504
 
                                                                                  , self.code_tree.testdir
 
399
            self.logging.info("%s/../libtool --mode=execute gdb -cd %s -x %s %s" %( server.code_tree.testdir
 
400
                                                                                  , server.code_tree.testdir
505
401
                                                                                  , gdb_file_path
506
402
                                                                                  , server.server_path
507
403
                                                                                  ) )
527
423
        self.logging.info("Running valgrind with options: %s" %(" ".join(valgrind_args)))
528
424
 
529
425
        # set our environment variable
530
 
        self.set_env_var('VALGRIND_RUN', '1', quiet=0)
 
426
        self.env_manager.set_env_var('VALGRIND_RUN', '1', quiet=0)
531
427
 
532
428
        # generate command prefix to call valgrind
533
429
        cmd_prefix = ''
541
437
                   , "--num-callers=16" 
542
438
                   ]
543
439
            # look for our suppressions file and add it to the mix if found
544
 
            suppress_file = os.path.join(self.code_tree.testdir,'valgrind.supp')
545
 
            if os.path.exists(suppress_file):
 
440
            if os.path.exists(self.valgrind_suppress_file):
546
441
                args = args + [ "--suppressions=%s" %(suppress_file) ]
547
442
 
548
443
            cmd_prefix = cmd_prefix + " ".join(args + valgrind_args)
551
446
        # add debug libraries to ld_library_path
552
447
        debug_path = '/usr/lib/debug'
553
448
        if os.path.exists(debug_path):
554
 
            self.append_env_var("LD_LIBRARY_PATH", debug_path, suffix=1)
555
 
            self.append_env_var("DYLD_LIBRARY_PATH", debug_path, suffix=1)
 
449
            self.env_manager.append_env_var("LD_LIBRARY_PATH", debug_path, suffix=1)
 
450
            self.env_manager.append_env_var("DYLD_LIBRARY_PATH", debug_path, suffix=1)
556
451
 
557
452
 
558
453
    def cleanup(self, exit=False):