pre-upload-check.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. # some class wide data
  13. apt = "apt-get"
  14. pkg = "libglib2.0-data"
  15. pkgver = "2.13.6-1ubuntu1"
  16. pkgpath = "/var/cache/apt/archives/libglib2.0-data_2.13.6-1ubuntu1_all.deb"
  17. def setUp(self):
  18. for f in glob.glob("testkeys/*,key"):
  19. call(["apt-key", "add", f], stdout=stdout, stderr=stderr)
  20. def _cleanup(self):
  21. " make sure we get new lists and no i-m-s "
  22. call(["rm","-f", "/var/lib/apt/lists/*"])
  23. if os.path.exists(self.pkgpath):
  24. os.unlink(self.pkgpath)
  25. def _expectedRes(self, resultstr):
  26. if resultstr == 'ok':
  27. return 0
  28. elif resultstr == 'broken':
  29. return 100
  30. def testPackages(self):
  31. for f in glob.glob("testsources.list/sources.list*package*"):
  32. self._cleanup()
  33. (prefix, testtype, result) = f.split("-")
  34. expected_res = self._expectedRes(result)
  35. # update first
  36. call([self.apt,"update",
  37. "-o","Dir::Etc::sourcelist=./%s" % f]+apt_args,
  38. stdout=stdout, stderr=stderr)
  39. # then get the pkg
  40. cmd = ["install", "-y", "-d", "--reinstall",
  41. "%s=%s" % (self.pkg, self.pkgver),
  42. "-o","Dir::state::Status=./fake-status"]
  43. res = call([self.apt, "-o","Dir::Etc::sourcelist=./%s" % f]+cmd+apt_args,
  44. stdout=stdout, stderr=stderr)
  45. self.assert_(res == expected_res,
  46. "test '%s' failed (got %s expected %s" % (f,res,expected_res))
  47. def testGPG(self):
  48. for f in glob.glob("testsources.list/sources.list*gpg*"):
  49. self._cleanup()
  50. (prefix, testtype, result) = f.split("-")
  51. expected_res = self._expectedRes(result)
  52. # update first
  53. call([self.apt,"update",
  54. "-o","Dir::Etc::sourcelist=./%s" % f]+apt_args,
  55. stdout=stdout, stderr=stderr)
  56. # then get the pkg
  57. cmd = ["install", "-y", "-d", "--reinstall",
  58. "%s=%s" % (self.pkg, self.pkgver),
  59. "-o","Dir::state::Status=./fake-status"]
  60. res = call([self.apt, "-o","Dir::Etc::sourcelist=./%s" % f]+
  61. cmd+apt_args,
  62. stdout=stdout, stderr=stderr)
  63. self.assert_(res == expected_res,
  64. "test '%s' failed (got %s expected %s" % (f,res,expected_res))
  65. def testRelease(self):
  66. for f in glob.glob("testsources.list/sources.list*release*"):
  67. self._cleanup()
  68. (prefix, testtype, result) = f.split("-")
  69. expected_res = self._expectedRes(result)
  70. cmd = ["update"]
  71. res = call([self.apt,"-o","Dir::Etc::sourcelist=./%s" % f]+cmd+apt_args,
  72. stdout=stdout, stderr=stderr)
  73. self.assert_(res == expected_res,
  74. "test '%s' failed (got %s expected %s" % (f,res,expected_res))
  75. class testLocalRepositories(unittest.TestCase):
  76. " test local repository regressions "
  77. repo_dir = "local-repo"
  78. apt = "apt-get"
  79. pkg = "gdebi-test4"
  80. def setUp(self):
  81. self.repo = os.path.abspath(os.path.join(os.getcwd(), self.repo_dir))
  82. self.sources = os.path.join(self.repo, "sources.list")
  83. s = open(self.sources,"w")
  84. s.write("deb file://%s/ /\n" % self.repo)
  85. s.close()
  86. def testLocalRepoAuth(self):
  87. # two times to get at least one i-m-s hit
  88. for i in range(2):
  89. self.assert_(os.path.exists(self.sources))
  90. cmd = [self.apt,"update","-o", "Dir::Etc::sourcelist=%s" % self.sources]+apt_args
  91. res = call(cmd, stdout=stdout, stderr=stderr)
  92. self.assertEqual(res, 0, "local repo test failed")
  93. self.assert_(os.path.exists(os.path.join(self.repo,"Packages.gz")),
  94. "Packages.gz vanished from local repo")
  95. def testInstallFromLocalRepo(self):
  96. apt = [self.apt,"-o", "Dir::Etc::sourcelist=%s"% self.sources]+apt_args
  97. cmd = apt+["update"]
  98. res = call(cmd, stdout=stdout, stderr=stderr)
  99. self.assertEqual(res, 0)
  100. res = call(apt+["-y","install","--reinstall",self.pkg],
  101. stdout=stdout, stderr=stderr)
  102. self.assert_(res == 0,
  103. "installing %s failed (got %s)" % (self.pkg, res))
  104. res = call(apt+["-y","remove",self.pkg],
  105. stdout=stdout, stderr=stderr)
  106. self.assert_(res == 0,
  107. "removing %s failed (got %s)" % (self.pkg, res))
  108. def testPythonAptInLocalRepo(self):
  109. import apt, apt_pkg
  110. apt_pkg.Config.Set("Dir::Etc::sourcelist",self.sources)
  111. cache = apt.Cache()
  112. cache.update()
  113. pkg = cache["apt"]
  114. self.assert_(pkg.name == 'apt')
  115. if __name__ == "__main__":
  116. print "Runing simple testsuit on current apt-get and libapt"
  117. if len(sys.argv) > 1 and sys.argv[1] == "-v":
  118. stdout = sys.stdout
  119. stderr = sys.stderr
  120. unittest.main()