pre-upload-check.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. # then get the pkg
  62. cmd = ["install", "-y", "-d", "--reinstall",
  63. "%s=%s" % (self.pkg, self.pkgver),
  64. "-o","Dir::state::Status=./fake-status"]
  65. res = call([self.apt, "-o","Dir::Etc::sourcelist=./%s" % f]+
  66. cmd+apt_args,
  67. stdout=stdout, stderr=stderr)
  68. self.assert_(res == expected_res,
  69. "test '%s' failed (got %s expected %s" % (f,res,expected_res))
  70. def testRelease(self):
  71. for f in glob.glob("testsources.list/sources.list*release*"):
  72. self._cleanup()
  73. (prefix, testtype, result) = f.split("-")
  74. expected_res = self._expectedRes(result)
  75. cmd = ["update"]
  76. res = call([self.apt,"-o","Dir::Etc::sourcelist=./%s" % f]+cmd+apt_args,
  77. stdout=stdout, stderr=stderr)
  78. self.assert_(res == expected_res,
  79. "test '%s' failed (got %s expected %s" % (f,res,expected_res))
  80. class testLocalRepositories(unittest.TestCase):
  81. " test local repository regressions "
  82. repo_dir = "local-repo"
  83. apt = "apt-get"
  84. pkg = "gdebi-test4"
  85. def setUp(self):
  86. self.repo = os.path.abspath(os.path.join(os.getcwd(), self.repo_dir))
  87. self.sources = os.path.join(self.repo, "sources.list")
  88. s = open(self.sources,"w")
  89. s.write("deb file://%s/ /\n" % self.repo)
  90. s.close()
  91. def testLocalRepoAuth(self):
  92. # two times to get at least one i-m-s hit
  93. for i in range(2):
  94. self.assert_(os.path.exists(self.sources))
  95. cmd = [self.apt,"update","-o", "Dir::Etc::sourcelist=%s" % self.sources]+apt_args
  96. res = call(cmd, stdout=stdout, stderr=stderr)
  97. self.assertEqual(res, 0, "local repo test failed")
  98. self.assert_(os.path.exists(os.path.join(self.repo,"Packages.gz")),
  99. "Packages.gz vanished from local repo")
  100. def testInstallFromLocalRepo(self):
  101. apt = [self.apt,"-o", "Dir::Etc::sourcelist=%s"% self.sources]+apt_args
  102. cmd = apt+["update"]
  103. res = call(cmd, stdout=stdout, stderr=stderr)
  104. self.assertEqual(res, 0)
  105. res = call(apt+["-y","install","--reinstall",self.pkg],
  106. stdout=stdout, stderr=stderr)
  107. self.assert_(res == 0,
  108. "installing %s failed (got %s)" % (self.pkg, res))
  109. res = call(apt+["-y","remove",self.pkg],
  110. stdout=stdout, stderr=stderr)
  111. self.assert_(res == 0,
  112. "removing %s failed (got %s)" % (self.pkg, res))
  113. def testPythonAptInLocalRepo(self):
  114. import apt, apt_pkg
  115. apt_pkg.Config.Set("Dir::Etc::sourcelist",self.sources)
  116. cache = apt.Cache()
  117. cache.update()
  118. pkg = cache["apt"]
  119. self.assert_(pkg.name == 'apt')
  120. if __name__ == "__main__":
  121. print "Runing simple testsuit on current apt-get and libapt"
  122. if len(sys.argv) > 1 and sys.argv[1] == "-v":
  123. stdout = sys.stdout
  124. stderr = sys.stderr
  125. unittest.main()