pre-upload-check.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/python
  2. import sys
  3. import os
  4. import glob
  5. import os.path
  6. from subprocess import call, PIPE
  7. import unittest
  8. stdout = os.open("/dev/null",0) #sys.stdout
  9. stderr = os.open("/dev/null",0) # sys.stderr
  10. apt_args = [] # ["-o","Debug::pkgAcquire::Auth=true"]
  11. class testAuthentication(unittest.TestCase):
  12. """
  13. test if the authentication is working, the repository
  14. of the test-data can be found here:
  15. bzr get http://people.ubuntu.com/~mvo/bzr/apt/apt-auth-test-suit/
  16. """
  17. # some class wide data
  18. apt = "apt-get"
  19. pkg = "libglib2.0-data"
  20. pkgver = "2.13.6-1ubuntu1"
  21. pkgpath = "/var/cache/apt/archives/libglib2.0-data_2.13.6-1ubuntu1_all.deb"
  22. def setUp(self):
  23. for f in glob.glob("testkeys/*,key"):
  24. call(["apt-key", "add", f], stdout=stdout, stderr=stderr)
  25. def _cleanup(self):
  26. " make sure we get new lists and no i-m-s "
  27. call(["rm","-f", "/var/lib/apt/lists/*"])
  28. if os.path.exists(self.pkgpath):
  29. os.unlink(self.pkgpath)
  30. def _expectedRes(self, resultstr):
  31. if resultstr == 'ok':
  32. return 0
  33. elif resultstr == 'broken':
  34. return 100
  35. def testPackages(self):
  36. for f in glob.glob("testsources.list/sources.list*package*"):
  37. self._cleanup()
  38. (prefix, testtype, result) = f.split("-")
  39. expected_res = self._expectedRes(result)
  40. # update first
  41. call([self.apt,"update",
  42. "-o","Dir::Etc::sourcelist=./%s" % f]+apt_args,
  43. stdout=stdout, stderr=stderr)
  44. # then get the pkg
  45. cmd = ["install", "-y", "-d", "--reinstall",
  46. "%s=%s" % (self.pkg, self.pkgver),
  47. "-o","Dir::state::Status=./fake-status"]
  48. res = call([self.apt, "-o","Dir::Etc::sourcelist=./%s" % f]+cmd+apt_args,
  49. stdout=stdout, stderr=stderr)
  50. self.assert_(res == expected_res,
  51. "test '%s' failed (got %s expected %s" % (f,res,expected_res))
  52. def testGPG(self):
  53. for f in glob.glob("testsources.list/sources.list*gpg*"):
  54. self._cleanup()
  55. (prefix, testtype, result) = f.split("-")
  56. expected_res = self._expectedRes(result)
  57. # update first
  58. call([self.apt,"update",
  59. "-o","Dir::Etc::sourcelist=./%s" % f]+apt_args,
  60. stdout=stdout, stderr=stderr)
  61. cmd = ["install", "-y", "-d", "--reinstall",
  62. "%s=%s" % (self.pkg, self.pkgver),
  63. "-o","Dir::state::Status=./fake-status"]
  64. res = call([self.apt, "-o","Dir::Etc::sourcelist=./%s" % f]+
  65. cmd+apt_args,
  66. stdout=stdout, stderr=stderr)
  67. self.assert_(res == expected_res,
  68. "test '%s' failed (got %s expected %s" % (f,res,expected_res))
  69. def testRelease(self):
  70. for f in glob.glob("testsources.list/sources.list*release*"):
  71. self._cleanup()
  72. (prefix, testtype, result) = f.split("-")
  73. expected_res = self._expectedRes(result)
  74. cmd = ["update"]
  75. res = call([self.apt,"-o","Dir::Etc::sourcelist=./%s" % f]+cmd+apt_args,
  76. stdout=stdout, stderr=stderr)
  77. self.assert_(res == expected_res,
  78. "test '%s' failed (got %s expected %s" % (f,res,expected_res))
  79. if expected_res == 0:
  80. self.assert_(len(glob.glob("/var/lib/apt/lists/partial/*")) == 0,
  81. "partial/ dir has leftover files: %s" % glob.glob("/var/lib/apt/lists/partial/*"))
  82. def testValid(self):
  83. for f in glob.glob("testsources.list/sources.list*validuntil*"):
  84. self._cleanup()
  85. (prefix, testtype, result) = f.split("-")
  86. expected_res = self._expectedRes(result)
  87. cmd = ["update"]
  88. res = call([self.apt,"-o","Dir::Etc::sourcelist=./%s" % f]+cmd+apt_args,
  89. stdout=stdout, stderr=stderr)
  90. self.assert_(res == expected_res,
  91. "test '%s' failed (got %s expected %s" % (f,res,expected_res))
  92. if expected_res == 0:
  93. self.assert_(len(glob.glob("/var/lib/apt/lists/partial/*")) == 0,
  94. "partial/ dir has leftover files: %s" % glob.glob("/var/lib/apt/lists/partial/*"))
  95. class testLocalRepositories(unittest.TestCase):
  96. " test local repository regressions "
  97. repo_dir = "local-repo"
  98. apt = "apt-get"
  99. pkg = "gdebi-test4"
  100. def setUp(self):
  101. self.repo = os.path.abspath(os.path.join(os.getcwd(), self.repo_dir))
  102. self.sources = os.path.join(self.repo, "sources.list")
  103. s = open(self.sources,"w")
  104. s.write("deb file://%s/ /\n" % self.repo)
  105. s.close()
  106. def testLocalRepoAuth(self):
  107. # two times to get at least one i-m-s hit
  108. for i in range(2):
  109. self.assert_(os.path.exists(self.sources))
  110. cmd = [self.apt,"update","-o", "Dir::Etc::sourcelist=%s" % self.sources]+apt_args
  111. res = call(cmd, stdout=stdout, stderr=stderr)
  112. self.assertEqual(res, 0, "local repo test failed")
  113. self.assert_(os.path.exists(os.path.join(self.repo,"Packages.gz")),
  114. "Packages.gz vanished from local repo")
  115. def testInstallFromLocalRepo(self):
  116. apt = [self.apt,"-o", "Dir::Etc::sourcelist=%s"% self.sources]+apt_args
  117. cmd = apt+["update"]
  118. res = call(cmd, stdout=stdout, stderr=stderr)
  119. self.assertEqual(res, 0)
  120. res = call(apt+["-y","install","--reinstall",self.pkg],
  121. stdout=stdout, stderr=stderr)
  122. self.assert_(res == 0,
  123. "installing %s failed (got %s)" % (self.pkg, res))
  124. res = call(apt+["-y","remove",self.pkg],
  125. stdout=stdout, stderr=stderr)
  126. self.assert_(res == 0,
  127. "removing %s failed (got %s)" % (self.pkg, res))
  128. def testPythonAptInLocalRepo(self):
  129. import apt, apt_pkg
  130. apt_pkg.Config.Set("Dir::Etc::sourcelist",self.sources)
  131. cache = apt.Cache()
  132. cache.update()
  133. pkg = cache["apt"]
  134. self.assert_(pkg.name == 'apt')
  135. if __name__ == "__main__":
  136. print "Runing simple testsuit on current apt-get and libapt"
  137. if len(sys.argv) > 1 and sys.argv[1] == "-v":
  138. stdout = sys.stdout
  139. stderr = sys.stderr
  140. unittest.main()