pre-upload-check.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. class testLocalRepositories(unittest.TestCase):
  83. " test local repository regressions "
  84. repo_dir = "local-repo"
  85. apt = "apt-get"
  86. pkg = "gdebi-test4"
  87. def setUp(self):
  88. self.repo = os.path.abspath(os.path.join(os.getcwd(), self.repo_dir))
  89. self.sources = os.path.join(self.repo, "sources.list")
  90. s = open(self.sources,"w")
  91. s.write("deb file://%s/ /\n" % self.repo)
  92. s.close()
  93. def testLocalRepoAuth(self):
  94. # two times to get at least one i-m-s hit
  95. for i in range(2):
  96. self.assert_(os.path.exists(self.sources))
  97. cmd = [self.apt,"update","-o", "Dir::Etc::sourcelist=%s" % self.sources]+apt_args
  98. res = call(cmd, stdout=stdout, stderr=stderr)
  99. self.assertEqual(res, 0, "local repo test failed")
  100. self.assert_(os.path.exists(os.path.join(self.repo,"Packages.gz")),
  101. "Packages.gz vanished from local repo")
  102. def testInstallFromLocalRepo(self):
  103. apt = [self.apt,"-o", "Dir::Etc::sourcelist=%s"% self.sources]+apt_args
  104. cmd = apt+["update"]
  105. res = call(cmd, stdout=stdout, stderr=stderr)
  106. self.assertEqual(res, 0)
  107. res = call(apt+["-y","install","--reinstall",self.pkg],
  108. stdout=stdout, stderr=stderr)
  109. self.assert_(res == 0,
  110. "installing %s failed (got %s)" % (self.pkg, res))
  111. res = call(apt+["-y","remove",self.pkg],
  112. stdout=stdout, stderr=stderr)
  113. self.assert_(res == 0,
  114. "removing %s failed (got %s)" % (self.pkg, res))
  115. def testPythonAptInLocalRepo(self):
  116. import apt, apt_pkg
  117. apt_pkg.Config.Set("Dir::Etc::sourcelist",self.sources)
  118. cache = apt.Cache()
  119. cache.update()
  120. pkg = cache["apt"]
  121. self.assert_(pkg.name == 'apt')
  122. if __name__ == "__main__":
  123. print "Runing simple testsuit on current apt-get and libapt"
  124. if len(sys.argv) > 1 and sys.argv[1] == "-v":
  125. stdout = sys.stdout
  126. stderr = sys.stderr
  127. unittest.main()