File: automatic-upgrade-testing

package info (click to toggle)
update-manager 0.68.debian-7
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 6,796 kB
  • ctags: 814
  • sloc: python: 5,646; xml: 1,571; sh: 433; makefile: 356; ansic: 264
file content (124 lines) | stat: -rwxr-xr-x 4,068 bytes parent folder | download
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
#!/usr/bin/python

from optparse import OptionParser
import os
import sys
import glob
import shutil

import datetime

sys.path = [os.path.normpath(os.path.join(os.getcwd(),"../DistUpgrade"))] + sys.path
from UpgradeTestBackend import UpgradeTestBackend

# TODO:
# - show held-back packages 
# - if no terminal activity for a extended time, send a '\n' on the
#   terminal (assuming its a stupid maintainer script asking questions)

# FIXME: move this into the generic backend code
def login(backend):
    backend.bootstrap()
    d = backend._unpackToTmpdir(backend.tarball)
    print "logging into: '%s'" % d
    backend._runInChroot(d, ["/bin/sh"])
    print "Cleaning up"
    if d:
        shutil.rmtree(d)

def flushBuffer(fd):
    sys.stdout.flush()
    sys.stderr.flush()
    os.fsync(fd)

def createBackend(backend_name, profile, basedir):
    try:
        backend_modul = __import__(backend_name)
        backend_class = getattr(backend_modul, backend_name)
    except (ImportError, AttributeError, TypeError), e:
        print "Can not import: %s (%s) " % (backend_name, e)
        return None
    return backend_class(profile, basedir)
    
def testUpgrade(backend_name, profile, basedir):
    backend = createBackend(backend_name, profile, basedir)
    assert(backend != None)
    os.chdir(basedir)
    if not os.path.exists(profile):
        print "ERROR: Can't find '%s' " % profile
        return False
    # make sure we actually have a resultdir
    resultdir = os.path.join(os.path.dirname(profile),"result")
    if not os.path.exists(resultdir):
        os.makedirs(resultdir)
    fd = os.open(os.path.join(resultdir,"bootstrap.log"),
                              os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0644)
    os.dup2(fd,1)
    os.dup2(fd,2)
    #fd2 = os.open("/dev/null",os.O_RDONLY)
    #os.dup2(fd2,0)
    print "%s log started" % datetime.datetime.now()

    try:
        # init the chroot
        if not backend.bootstrap():
            print "FAILED: bootstrap for '%s'" % profile
            flushBuffer(fd)
            return False
        if not backend.upgrade():
            print "FAILED: upgrade for '%s'" % profile
            flushBuffer(fd)
            return False
        if not backend.test():
            print "FAILED: test for '%s'" % profile
            return False
    except Exception, e:
        import traceback
        traceback.print_exc()
        print "Caught exception in testUpgrade for '%s' (%s)" % (profile, e)
        flushBuffer(fd)
        return False
    return True

if __name__ == "__main__":

    parser = OptionParser()
    parser.add_option("-p", "--profile", dest="profile", default="profile/ubuntu/DistUpgrade.cfg",
                      help="write report to FILE")
    parser.add_option("-a", "--auto", dest="auto", default=False,
                      action="store_true",
                      help="run all tests in profile/ dir")
    parser.add_option("-l", "--login", dest="login", default=False,
                      action="store_true",
                      help="log into the a profile")
    parser.add_option("-b", "--backend", dest="backend",
                      default="UpgradeTestBackendChroot",
                      help="UpgradeTestBackend to use: UpgradeTestBackendChroot, UpgradeTestBackendQemu")
    
    
    (options, args) = parser.parse_args()
    basedir = os.getcwd()

    # save for later
    fd1 = os.dup(1)
    fd2 = os.dup(2)

    res = True
    try:
        if options.auto:
            for d in glob.glob("./profile/*/DistUpgrade.cfg"):
                os.dup2(fd1, 1)
                os.dup2(fd2, 2)
                print "Testing '%s'" % d
                res &= testUpgrade(options.backend, d, basedir)
        elif options.login:
            backend = createBackend(options.backend, options.profile, basedir)
            login(backend)
        else:
            res &= testUpgrade(options.backend, options.profile, basedir)
    except Exception, e:
        print "ERROR: expection caught '%s' " % e
        res = False

    # return error
    sys.exit(not res)