test_preflight.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. import os, shutil
  5. import simplejson as json
  6. import unittest
  7. import hashlib
  8. import base64
  9. from cuddlefish import preflight
  10. from StringIO import StringIO
  11. class Util(unittest.TestCase):
  12. def get_basedir(self):
  13. return os.path.join(".test_tmp", self.id())
  14. def make_basedir(self):
  15. basedir = self.get_basedir()
  16. if os.path.isdir(basedir):
  17. here = os.path.abspath(os.getcwd())
  18. assert os.path.abspath(basedir).startswith(here) # safety
  19. shutil.rmtree(basedir)
  20. os.makedirs(basedir)
  21. return basedir
  22. def test_base62(self):
  23. for i in range(1000):
  24. h = hashlib.sha1(str(i)).digest()
  25. s1 = base64.b64encode(h, "AB").strip("=")
  26. s2 = base64.b64encode(h).strip("=").replace("+","A").replace("/","B")
  27. self.failUnlessEqual(s1, s2)
  28. def write(self, config):
  29. basedir = self.get_basedir()
  30. fn = os.path.join(basedir, "package.json")
  31. open(fn,"w").write(config)
  32. def read(self):
  33. basedir = self.get_basedir()
  34. fn = os.path.join(basedir, "package.json")
  35. return open(fn,"r").read()
  36. def get_cfg(self):
  37. cfg = json.loads(self.read())
  38. if "name" not in cfg:
  39. # the cfx parser always provides a name, even if package.json
  40. # doesn't contain one
  41. cfg["name"] = "pretend name"
  42. return cfg
  43. def parse(self, keydata):
  44. fields = {}
  45. fieldnames = []
  46. for line in keydata.split("\n"):
  47. if line.strip():
  48. k,v = line.split(":", 1)
  49. k = k.strip() ; v = v.strip()
  50. fields[k] = v
  51. fieldnames.append(k)
  52. return fields, fieldnames
  53. def test_preflight(self):
  54. basedir = self.make_basedir()
  55. fn = os.path.join(basedir, "package.json")
  56. # empty config is not ok: need id (name is automatically supplied)
  57. config_orig = "{}"
  58. self.write(config_orig)
  59. out = StringIO()
  60. cfg = self.get_cfg()
  61. config_was_ok, modified = preflight.preflight_config(cfg, fn,
  62. stderr=out)
  63. self.failUnlessEqual(config_was_ok, False)
  64. self.failUnlessEqual(modified, True)
  65. backup_fn = os.path.join(basedir, "package.json.backup")
  66. config_backup = open(backup_fn,"r").read()
  67. self.failUnlessEqual(config_backup, config_orig)
  68. config = json.loads(self.read())
  69. self.failIf("name" in config)
  70. self.failUnless("id" in config)
  71. self.failUnless(config["id"].startswith("jid1-"), config["id"])
  72. self.failUnlessEqual(out.getvalue().strip(),
  73. "No 'id' in package.json: creating a new ID for you.")
  74. os.unlink(backup_fn)
  75. # just a name? we add the id
  76. config_orig = '{"name": "my-awesome-package"}'
  77. self.write(config_orig)
  78. out = StringIO()
  79. cfg = self.get_cfg()
  80. config_was_ok, modified = preflight.preflight_config(cfg, fn,
  81. stderr=out)
  82. self.failUnlessEqual(config_was_ok, False)
  83. self.failUnlessEqual(modified, True)
  84. backup_fn = os.path.join(basedir, "package.json.backup")
  85. config_backup = open(backup_fn,"r").read()
  86. self.failUnlessEqual(config_backup, config_orig)
  87. config = json.loads(self.read())
  88. self.failUnlessEqual(config["name"], "my-awesome-package")
  89. self.failUnless("id" in config)
  90. self.failUnless(config["id"].startswith("jid1-"), config["id"])
  91. jid = str(config["id"])
  92. self.failUnlessEqual(out.getvalue().strip(),
  93. "No 'id' in package.json: creating a new ID for you.")
  94. os.unlink(backup_fn)
  95. # name and valid id? great! ship it!
  96. config2 = '{"name": "my-awesome-package", "id": "%s"}' % jid
  97. self.write(config2)
  98. out = StringIO()
  99. cfg = self.get_cfg()
  100. config_was_ok, modified = preflight.preflight_config(cfg, fn,
  101. stderr=out)
  102. self.failUnlessEqual(config_was_ok, True)
  103. self.failUnlessEqual(modified, False)
  104. config2a = self.read()
  105. self.failUnlessEqual(config2a, config2)
  106. self.failUnlessEqual(out.getvalue().strip(), "")
  107. # name and anonymous ID? without asking to see its papers, ship it
  108. config3 = '{"name": "my-old-skool-package", "id": "anonid0-deadbeef"}'
  109. self.write(config3)
  110. out = StringIO()
  111. cfg = self.get_cfg()
  112. config_was_ok, modified = preflight.preflight_config(cfg, fn,
  113. stderr=out)
  114. self.failUnlessEqual(config_was_ok, True)
  115. self.failUnlessEqual(modified, False)
  116. config3a = self.read()
  117. self.failUnlessEqual(config3a, config3)
  118. self.failUnlessEqual(out.getvalue().strip(), "")
  119. # name and old-style ID? with nostalgic trepidation, ship it
  120. config4 = '{"name": "my-old-skool-package", "id": "foo@bar.baz"}'
  121. self.write(config4)
  122. out = StringIO()
  123. cfg = self.get_cfg()
  124. config_was_ok, modified = preflight.preflight_config(cfg, fn,
  125. stderr=out)
  126. self.failUnlessEqual(config_was_ok, True)
  127. self.failUnlessEqual(modified, False)
  128. config4a = self.read()
  129. self.failUnlessEqual(config4a, config4)
  130. self.failUnlessEqual(out.getvalue().strip(), "")
  131. if __name__ == '__main__':
  132. unittest.main()