3 # Copyright (c) 2006 by Aurelien Foret <orelien@chez.com>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
29 from util
import vprint
35 def __init__(self
, name
, root
):
37 self
.testname
= os
.path
.basename(name
).replace('.py', '')
42 return "name = %s\n" \
44 "root = %s" % (self
.name
, self
.testname
, self
.root
)
46 def addpkg2db(self
, treename
, pkg
):
49 if not treename
in self
.db
:
50 self
.db
[treename
] = pmdb
.pmdb(treename
, self
.root
)
51 self
.db
[treename
].pkgs
.append(pkg
)
53 def addpkg(self
, pkg
):
56 self
.localpkgs
.append(pkg
)
58 def findpkg(self
, name
, version
, allow_local
=False):
59 """Find a package object matching the name and version specified in
60 either sync databases or the local package collection. The local database
61 is allowed to match if allow_local is True."""
62 for db
in self
.db
.itervalues():
63 if db
.treename
== "local" and not allow_local
:
66 if pkg
and pkg
.version
== version
:
68 for pkg
in self
.localpkgs
:
69 if pkg
.name
== name
and pkg
.version
== version
:
74 def addrule(self
, rulename
):
77 rule
= pmrule
.pmrule(rulename
)
78 self
.rules
.append(rule
)
84 # Reset test parameters
92 "local": pmdb
.pmdb("local", self
.root
)
95 self
.createlocalpkgs
= False
104 self
.expectfailure
= False
106 if os
.path
.isfile(self
.name
):
107 # all tests expect this to be available
108 from pmpkg
import pmpkg
111 raise IOError("file %s does not exist!" % self
.name
)
117 print "==> Generating test environment"
119 # Cleanup leftover files from a previous test session
120 if os
.path
.isdir(self
.root
):
121 shutil
.rmtree(self
.root
)
122 vprint("\t%s" % self
.root
)
124 # Create directory structure
125 vprint(" Creating directory structure:")
126 dbdir
= os
.path
.join(self
.root
, util
.PM_DBPATH
)
127 cachedir
= os
.path
.join(self
.root
, util
.PM_CACHEDIR
)
128 syncdir
= os
.path
.join(self
.root
, util
.SYNCREPO
)
129 tmpdir
= os
.path
.join(self
.root
, util
.TMPDIR
)
130 logdir
= os
.path
.join(self
.root
, os
.path
.dirname(util
.LOGFILE
))
131 etcdir
= os
.path
.join(self
.root
, os
.path
.dirname(util
.PACCONF
))
132 bindir
= os
.path
.join(self
.root
, "bin")
133 sys_dirs
= [dbdir
, cachedir
, syncdir
, tmpdir
, logdir
, etcdir
, bindir
]
134 for sys_dir
in sys_dirs
:
135 if not os
.path
.isdir(sys_dir
):
136 vprint("\t%s" % sys_dir
[len(self
.root
)+1:])
137 os
.makedirs(sys_dir
, 0755)
138 # Only the dynamically linked binary is needed for fakechroot
139 shutil
.copy("/bin/sh", bindir
)
142 vprint(" Creating configuration file")
143 util
.mkcfgfile(util
.PACCONF
, self
.root
, self
.option
, self
.db
)
146 vprint(" Creating package archives")
147 for pkg
in self
.localpkgs
:
148 vprint("\t%s" % os
.path
.join(util
.TMPDIR
, pkg
.filename()))
150 for key
, value
in self
.db
.iteritems():
151 if key
== "local" and not self
.createlocalpkgs
:
153 for pkg
in value
.pkgs
:
154 vprint("\t%s" % os
.path
.join(util
.PM_CACHEDIR
, pkg
.filename()))
156 pkg
.makepkg(cachedir
)
158 pkg
.makepkg(os
.path
.join(syncdir
, value
.treename
))
159 pkg
.md5sum
= util
.getmd5sum(pkg
.path
)
160 pkg
.csize
= os
.stat(pkg
.path
)[stat
.ST_SIZE
]
162 # Populating databases
163 vprint(" Populating databases")
164 for key
, value
in self
.db
.iteritems():
165 for pkg
in value
.pkgs
:
166 vprint("\t%s/%s" % (key
, pkg
.fullname()))
168 pkg
.installdate
= time
.ctime()
171 # Creating sync database archives
172 vprint(" Creating sync database archives")
173 for key
, value
in self
.db
.iteritems():
176 vprint("\t" + value
.treename
)
178 serverpath
= os
.path
.join(syncdir
, value
.treename
)
179 util
.mkdir(serverpath
)
180 shutil
.copy(value
.dbfile
, serverpath
)
183 vprint(" Populating file system")
184 for pkg
in self
.db
["local"].pkgs
:
185 vprint("\tinstalling %s" % pkg
.fullname())
188 path
= os
.path
.join(self
.root
, f
)
190 if os
.path
.isfile(path
):
191 os
.utime(path
, (355, 355))
192 for f
in self
.filesystem
:
194 path
= os
.path
.join(self
.root
, f
)
196 if os
.path
.isfile(path
):
197 os
.utime(path
, (355, 355))
200 vprint(" Taking a snapshot of the file system")
201 for roots
, dirs
, files
in os
.walk(self
.root
):
203 filename
= os
.path
.join(roots
, i
)
204 f
= pmfile
.pmfile(self
.root
, filename
.replace(self
.root
+ "/", ""))
206 vprint("\t%s" % f
.name
)
208 def run(self
, pacman
):
212 if os
.path
.isfile(util
.PM_LOCK
):
213 print "\tERROR: another pacman session is on-going -- skipping"
216 print "==> Running test"
217 vprint("\tpacman %s" % self
.args
)
220 if os
.geteuid() != 0:
221 fakeroot
= util
.which("fakeroot")
223 print "WARNING: fakeroot not found!"
225 cmd
.append("fakeroot")
227 fakechroot
= util
.which("fakechroot")
229 cmd
.append("fakechroot")
232 cmd
.append("libtool execute gdb --args")
233 if pacman
["valgrind"]:
234 cmd
.append("valgrind -q --tool=memcheck --leak-check=full --show-reachable=yes --suppressions=%s/valgrind.supp" % os
.getcwd())
235 cmd
.append("\"%s\" --config=\"%s\" --root=\"%s\" --dbpath=\"%s\" --cachedir=\"%s\"" \
237 os
.path
.join(self
.root
, util
.PACCONF
),
239 os
.path
.join(self
.root
, util
.PM_DBPATH
),
240 os
.path
.join(self
.root
, util
.PM_CACHEDIR
)))
241 if not pacman
["manual-confirm"]:
242 cmd
.append("--noconfirm")
244 cmd
.append("--debug=%s" % pacman
["debug"])
245 cmd
.append("%s" % self
.args
)
246 if not pacman
["gdb"] and not pacman
["valgrind"] and not pacman
["nolog"]:
247 cmd
.append(">\"%s\" 2>&1" % os
.path
.join(self
.root
, util
.LOGFILE
))
248 vprint("\trunning: %s" % " ".join(cmd
))
250 # Change to the tmp dir before running pacman, so that local package
251 # archives are made available more easily.
253 tmpdir
= os
.path
.join(self
.root
, util
.TMPDIR
)
256 time_start
= time
.time()
257 self
.retcode
= os
.system(" ".join(cmd
))
258 time_end
= time
.time()
259 vprint("\ttime elapsed: %ds" % (time_end
- time_start
))
261 if self
.retcode
== None:
265 vprint("\tretcode = %s" % self
.retcode
)
268 # Check if the lock is still there
269 if os
.path
.isfile(util
.PM_LOCK
):
270 print "\tERROR: %s not removed" % util
.PM_LOCK
271 os
.unlink(util
.PM_LOCK
)
272 # Look for a core file
273 if os
.path
.isfile(os
.path
.join(self
.root
, util
.TMPDIR
, "core")):
274 print "\tERROR: pacman dumped a core file"
280 print "==> Checking rules"
283 success
= i
.check(self
)
286 self
.result
["success"] += 1
289 self
.result
["fail"] += 1
292 print "\t[%s] %s" % (msg
, i
)
294 # vim: set ts=4 sw=4 et: