~ubuntu-core-dev/update-manager/main

« back to all changes in this revision

Viewing changes to AutoUpgradeTester/automatic-upgrade-testing

  • Committer: Michael Vogt
  • Date: 2008-11-14 19:03:53 UTC
  • Revision ID: michael.vogt@ubuntu.com-20081114190353-tnrwn4pvarb9gxyb
* debian/rules 
  - remove the arch-build target
* DistUpgrade/DistUpgradeController.py:
  - improvements to the sources.list rewriting, better tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python -u
 
2
# "-u": Force stdin, stdout and stderr to be  totally  unbuffered.
 
3
#       To get more accurate log files
 
4
 
5
# see also 
 
6
# http://www.faqts.com/knowledge_base/entry/versions/index.phtml?aid=4419
 
7
 
 
8
from optparse import OptionParser
 
9
import os
 
10
import sys
 
11
sys.path.insert(0, "../DistUpgrade")
 
12
 
 
13
import glob
 
14
import shutil
 
15
import datetime
 
16
 
 
17
from UpgradeTestBackend import UpgradeTestBackend
 
18
 
 
19
# FIXME: move this into the generic backend code
 
20
def login(backend):
 
21
    backend.bootstrap()
 
22
    d = backend._unpackToTmpdir(backend.tarball)
 
23
    print "logging into: '%s'" % d
 
24
    backend._runInChroot(d, ["/bin/sh"])
 
25
    print "Cleaning up"
 
26
    if d:
 
27
        shutil.rmtree(d)
 
28
 
 
29
def flushBuffer(fd):
 
30
    sys.stdout.flush()
 
31
    sys.stderr.flush()
 
32
    os.fsync(fd)
 
33
 
 
34
def createBackend(backend_name, profile, basedir):
 
35
    try:
 
36
        backend_modul = __import__(backend_name)
 
37
        backend_class = getattr(backend_modul, backend_name)
 
38
    except (ImportError, AttributeError, TypeError), e:
 
39
        print "Can not import: %s (%s) " % (backend_name, e)
 
40
        return None
 
41
    return backend_class(profile, basedir)
 
42
    
 
43
def testUpgrade(backend_name, profile, basedir, add_pkgs, to_stdout=False):
 
44
    backend = createBackend(backend_name, profile, basedir)
 
45
    assert(backend != None)
 
46
    os.chdir(basedir)
 
47
    if not os.path.exists(profile):
 
48
        print "ERROR: Can't find '%s' " % profile
 
49
        return False
 
50
    # make sure we actually have a resultdir
 
51
    resultdir = os.path.join(os.path.dirname(profile),"result")
 
52
    if not os.path.exists(resultdir):
 
53
        os.makedirs(resultdir)
 
54
    fd = os.open(os.path.join(resultdir,"bootstrap.log"),
 
55
                              os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0644)
 
56
    os.dup2(fd,1)
 
57
    os.dup2(fd,2)
 
58
    #fd2 = os.open("/dev/null",os.O_RDONLY)
 
59
    #os.dup2(fd2,0)
 
60
    print "%s log started" % datetime.datetime.now()
 
61
 
 
62
    try:
 
63
        # init the chroot
 
64
        if not backend.bootstrap():
 
65
            print "FAILED: bootstrap for '%s'" % profile
 
66
            flushBuffer(fd)
 
67
            return False
 
68
        if add_pkgs:
 
69
            if not backend.installPackages(add_pkgs):
 
70
                print "FAILED: installPacakges '%s'" % add_pkgs
 
71
                return False
 
72
        if not backend.upgrade():
 
73
            print "FAILED: upgrade for '%s'" % profile
 
74
            flushBuffer(fd)
 
75
            return False
 
76
        if not backend.test():
 
77
            print "FAILED: test for '%s'" % profile
 
78
            return False
 
79
        print "profile: %s worked" % profile
 
80
    except Exception, e:
 
81
        import traceback
 
82
        traceback.print_exc()
 
83
        print "Caught exception in testUpgrade for '%s' (%s)" % (profile, e)
 
84
        flushBuffer(fd)
 
85
        return False
 
86
    return True
 
87
 
 
88
if __name__ == "__main__":
 
89
 
 
90
    parser = OptionParser()
 
91
    parser.add_option("-p", "--profile", dest="profile", 
 
92
                      default="profile/ubuntu/DistUpgrade.cfg",
 
93
                      help="write report to FILE")
 
94
    parser.add_option("--additinoal", "--additional-pkgs", dest="add_pkgs", 
 
95
                      default="",
 
96
                      help="add additional pkgs before running the upgrade")
 
97
    parser.add_option("-a", "--auto", dest="auto", default=False,
 
98
                      action="store_true",
 
99
                      help="run all tests in profile/ dir")
 
100
    parser.add_option("--bootstrap-only", "--bootstrap-only",dest="bootstrap_only", 
 
101
                      default=False,
 
102
                      action="store_true",
 
103
                      help="only bootstrap the image")
 
104
    parser.add_option("-l", "--login", dest="login", default=False,
 
105
                      action="store_true",
 
106
                      help="log into the a profile")
 
107
    parser.add_option("-b", "--backend", dest="backend",
 
108
                      default="UpgradeTestBackendQemu",
 
109
                      help="UpgradeTestBackend to use: UpgradeTestBackendChroot, UpgradeTestBackendQemu")
 
110
    parser.add_option("-d", "--diff", dest="gen_diff",
 
111
                      default=False, action="store_true",
 
112
                      help="generate a diff of the upgraded image versus a fresh installed image")
 
113
    parser.add_option("--stdout", "--stdout", dest="to_stdout",
 
114
                      default=False, action="store_true",
 
115
                      help="all output goes to stdout")
 
116
    
 
117
    
 
118
    (options, args) = parser.parse_args()
 
119
    basedir = os.getcwd()
 
120
 
 
121
    # save for later
 
122
    fd1 = os.dup(1)
 
123
    fd2 = os.dup(2)
 
124
 
 
125
    res = True
 
126
    try:
 
127
        if options.auto:
 
128
            # we have some profiles that have DistUpgrade.cfg.dapper
 
129
            for d in glob.glob("./profile/*/DistUpgrade.cfg*"):
 
130
                os.dup2(fd1, 1)
 
131
                os.dup2(fd2, 2)
 
132
                print "Testing '%s'" % d
 
133
                res &= testUpgrade(options.backend, d, basedir)
 
134
        elif options.login:
 
135
            backend = createBackend(options.backend, options.profile, basedir)
 
136
            login(backend)
 
137
        elif options.bootstrap_only:
 
138
            backend = createBackend(options.backend, options.profile, basedir)  
 
139
            backend.bootstrap(force=True)
 
140
        elif options.gen_diff:
 
141
            backend = createBackend(options.backend, options.profile, basedir)
 
142
            backend.genDiff()
 
143
        else:
 
144
            res &= testUpgrade(options.backend, options.profile, basedir, options.add_pkgs.split(","), options.to_stdout)
 
145
    except Exception, e:
 
146
        print "ERROR: exception caught '%s' " % e
 
147
        import traceback
 
148
        traceback.print_exc()
 
149
        res = False
 
150
 
 
151
    # return error
 
152
    sys.exit(not res)