src/tests/pkg5unittest.py
changeset 2026 d1b30615bc99
parent 1970 60471574c487
child 2028 b2c674e6ee28
equal deleted inserted replaced
2025:4ba5092f1b06 2026:d1b30615bc99
    25 import baseline
    25 import baseline
    26 import copy
    26 import copy
    27 import difflib
    27 import difflib
    28 import errno
    28 import errno
    29 import gettext
    29 import gettext
       
    30 import hashlib
    30 import os
    31 import os
    31 import pprint
    32 import pprint
    32 import shutil
    33 import shutil
    33 import signal
    34 import signal
    34 import stat
    35 import stat
    43 import re
    44 import re
    44 import textwrap
    45 import textwrap
    45 
    46 
    46 EmptyI = tuple()
    47 EmptyI = tuple()
    47 EmptyDict = dict()
    48 EmptyDict = dict()
       
    49 
       
    50 path_to_pub_util = "../util/publish"
    48 
    51 
    49 #
    52 #
    50 # These are initialized by pkg5testenv.setup_environment.
    53 # These are initialized by pkg5testenv.setup_environment.
    51 #
    54 #
    52 g_proto_area = "TOXIC"
    55 g_proto_area = "TOXIC"
   184         #
   187         #
   185         # Uses property() to implements test_root as a read-only attribute.
   188         # Uses property() to implements test_root as a read-only attribute.
   186         #
   189         #
   187         test_root = property(fget=lambda self: self.__test_root)
   190         test_root = property(fget=lambda self: self.__test_root)
   188 
   191 
       
   192         def __get_ro_data_root(self):
       
   193                 if not self.__test_root:
       
   194                         return None
       
   195                 return os.path.join(self.__test_root, "ro_data")
       
   196         
       
   197         ro_data_root = property(fget=__get_ro_data_root)
       
   198 
   189         def cmdline_run(self, cmdline, comment="", coverage=True, exit=0,
   199         def cmdline_run(self, cmdline, comment="", coverage=True, exit=0,
   190             handle=False, out=False, prefix="", raise_error=True, su_wrap=None):
   200             handle=False, out=False, prefix="", raise_error=True, su_wrap=None):
   191                 wrapper = ""
   201                 wrapper = ""
   192                 if coverage:
   202                 if coverage:
   193                         wrapper = self.coverage_cmd
   203                         wrapper = self.coverage_cmd
   208                     stderr=subprocess.PIPE)
   218                     stderr=subprocess.PIPE)
   209 
   219 
   210                 if handle:
   220                 if handle:
   211                         # Do nothing more.
   221                         # Do nothing more.
   212                         return p
   222                         return p
   213 
       
   214                 self.output, self.errout = p.communicate()
   223                 self.output, self.errout = p.communicate()
   215                 retcode = p.returncode
   224                 retcode = p.returncode
   216                 self.debugresult(retcode, exit, self.output)
   225                 self.debugresult(retcode, exit, self.output)
   217                 if self.errout != "":
   226                 if self.errout != "":
   218                         self.debug(self.errout)
   227                         self.debug(self.errout)
   299                 try:
   308                 try:
   300                         os.makedirs(self.__test_root, 0755)
   309                         os.makedirs(self.__test_root, 0755)
   301                 except OSError, e:
   310                 except OSError, e:
   302                         if e.errno != errno.EEXIST:
   311                         if e.errno != errno.EEXIST:
   303                                 raise e
   312                                 raise e
       
   313                 shutil.copytree(os.path.join(self.__pwd, "ro_data"),
       
   314                     self.ro_data_root)
   304                 #
   315                 #
   305                 # TMPDIR affects the behavior of mkdtemp and mkstemp.
   316                 # TMPDIR affects the behavior of mkdtemp and mkstemp.
   306                 # Setting this here should ensure that tests will make temp
   317                 # Setting this here should ensure that tests will make temp
   307                 # files and dirs inside the test directory rather than
   318                 # files and dirs inside the test directory rather than
   308                 # polluting /tmp.
   319                 # polluting /tmp.
   550                 t_fh.write(content)
   561                 t_fh.write(content)
   551                 t_fh.close()
   562                 t_fh.close()
   552                 self.debugfilecreate(content, t_path)
   563                 self.debugfilecreate(content, t_path)
   553                 return t_path
   564                 return t_path
   554 
   565 
       
   566         @staticmethod
       
   567         def calc_file_hash(pth):
       
   568                 # Find the hash of the file.
       
   569                 fh = open(pth, "rb")
       
   570                 s = fh.read()
       
   571                 fh.close()
       
   572                 hsh = hashlib.sha1()
       
   573                 hsh.update(s)
       
   574                 return hsh.hexdigest()
       
   575 
   555         def reduceSpaces(self, string):
   576         def reduceSpaces(self, string):
   556                 """Reduce runs of spaces down to a single space."""
   577                 """Reduce runs of spaces down to a single space."""
   557                 return re.sub(" +", " ", string)
   578                 return re.sub(" +", " ", string)
   558 
   579 
   559         def assertEqualDiff(self, expected, actual):
   580         def assertEqualDiff(self, expected, actual):
  1355         def pkgrepo(self, command, comment="", exit=0, su_wrap=False):
  1376         def pkgrepo(self, command, comment="", exit=0, su_wrap=False):
  1356                 cmdline = "%s/usr/bin/pkgrepo %s" % (g_proto_area, command)
  1377                 cmdline = "%s/usr/bin/pkgrepo %s" % (g_proto_area, command)
  1357                 return self.cmdline_run(cmdline, comment=comment, exit=exit,
  1378                 return self.cmdline_run(cmdline, comment=comment, exit=exit,
  1358                     su_wrap=su_wrap)
  1379                     su_wrap=su_wrap)
  1359 
  1380 
       
  1381         def pkgsign(self, depot_url, command, exit=0, comment=""):
       
  1382                 args = []
       
  1383                 if depot_url:
       
  1384                         args.append("-s %s" % depot_url)
       
  1385 
       
  1386                 if command:
       
  1387                         args.append(command)
       
  1388 
       
  1389                 cmdline = "%s/usr/bin/pkgsign %s" % (g_proto_area,
       
  1390                     " ".join(args))
       
  1391                 return self.cmdline_run(cmdline, comment=comment, exit=exit)
       
  1392 
  1360         def pkgsend(self, depot_url="", command="", exit=0, comment="",
  1393         def pkgsend(self, depot_url="", command="", exit=0, comment="",
  1361             retry400=True):
  1394             retry400=True):
  1362                 args = []
  1395                 args = []
  1363                 if depot_url:
  1396                 if depot_url:
  1364                         args.append("-s " + depot_url)
  1397                         args.append("-s " + depot_url)
  1373                 retcode, out = self.cmdline_run(cmdline, comment=comment,
  1406                 retcode, out = self.cmdline_run(cmdline, comment=comment,
  1374                     exit=exit, out=True, prefix=prefix, raise_error=False)
  1407                     exit=exit, out=True, prefix=prefix, raise_error=False)
  1375                 errout = self.errout
  1408                 errout = self.errout
  1376 
  1409 
  1377                 cmdop = command.split(' ')[0]
  1410                 cmdop = command.split(' ')[0]
  1378                 if cmdop == "open" and retcode == 0:
  1411                 if cmdop in ("open", "append") and retcode == 0:
  1379                         out = out.rstrip()
  1412                         out = out.rstrip()
  1380                         assert out.startswith("export PKG_TRANS_ID=")
  1413                         assert out.startswith("export PKG_TRANS_ID=")
  1381                         arr = out.split("=")
  1414                         arr = out.split("=")
  1382                         assert arr
  1415                         assert arr
  1383                         out = arr[1]
  1416                         out = arr[1]
  1495                 if retcode != exit:
  1528                 if retcode != exit:
  1496                         raise UnexpectedExitCodeException(line, exit, retcode)
  1529                         raise UnexpectedExitCodeException(line, exit, retcode)
  1497 
  1530 
  1498                 return plist
  1531                 return plist
  1499 
  1532 
       
  1533         def merge(self, args=EmptyI, exit=0):
       
  1534                 prog = os.path.realpath(os.path.join(path_to_pub_util,
       
  1535                     "merge.py"))
       
  1536                 cmd = "%s %s" % (prog, " ".join(args))
       
  1537                 self.cmdline_run(cmd, exit=exit)
       
  1538 
  1500         def copy_repository(self, src, src_pub, dest, dest_pub):
  1539         def copy_repository(self, src, src_pub, dest, dest_pub):
  1501                 """Copies the packages from the src repository to a new
  1540                 """Copies the packages from the src repository to a new
  1502                 destination repository that will be created at dest.  In
  1541                 destination repository that will be created at dest.  In
  1503                 addition, any packages from the src_pub will be assigned
  1542                 addition, any packages from the src_pub will be assigned
  1504                 to the dest_pub during the copy.  The new repository will
  1543                 to the dest_pub during the copy.  The new repository will
  1551                                         else:
  1590                                         else:
  1552                                                 mdest.write(l)
  1591                                                 mdest.write(l)
  1553                                 msrc.close()
  1592                                 msrc.close()
  1554                                 mdest.close()
  1593                                 mdest.close()
  1555 
  1594 
       
  1595         def get_img_manifest_path(self, pfmri, img_path=None):
       
  1596                 """Returns the path to the manifest for the fiven fmri."""
       
  1597 
       
  1598                 if not img_path:
       
  1599                         img_path = self.get_img_path()
       
  1600 
       
  1601                 return os.path.join(img_path, "var", "pkg", "pkg",
       
  1602                     pfmri.get_dir_path(), "manifest")
       
  1603 
  1556         def get_img_manifest(self, pfmri, img_path=None):
  1604         def get_img_manifest(self, pfmri, img_path=None):
  1557                 """Retrieves the client's cached copy of the manifest for the
  1605                 """Retrieves the client's cached copy of the manifest for the
  1558                 given package FMRI and returns it as a string.  Callers are
  1606                 given package FMRI and returns it as a string.  Callers are
  1559                 responsible for all error handling."""
  1607                 responsible for all error handling."""
  1560 
  1608 
  1561                 if not img_path:
  1609                 mpath = self.get_img_manifest_path(pfmri, img_path)
  1562                         img_path = self.get_img_path()
       
  1563 
       
  1564                 mpath = os.path.join(img_path, "var", "pkg", "pkg",
       
  1565                     pfmri.get_dir_path(), "manifest")
       
  1566                 with open(mpath, "rb") as f:
  1610                 with open(mpath, "rb") as f:
  1567                         return f.read()
  1611                         return f.read()
  1568 
  1612 
  1569         def write_img_manifest(self, pfmri, mdata, img_path=None):
  1613         def write_img_manifest(self, pfmri, mdata, img_path=None):
  1570                 """Overwrites the client's cached copy of the manifest for the
  1614                 """Overwrites the client's cached copy of the manifest for the
  1621                 self.assertEqual(expected, actual)
  1665                 self.assertEqual(expected, actual)
  1622 
  1666 
  1623         def validate_html_file(self, fname, exit=0, comment="",
  1667         def validate_html_file(self, fname, exit=0, comment="",
  1624             options="-quiet -utf8"):
  1668             options="-quiet -utf8"):
  1625                 cmdline = "tidy %s %s" % (options, fname)
  1669                 cmdline = "tidy %s %s" % (options, fname)
  1626                 return self.cmdline_run(cmdline, comment=comment, exit=exit)
  1670                 return self.cmdline_run(cmdline, comment=comment,
       
  1671                     coverage=False, exit=exit)
  1627 
  1672 
  1628         def create_repo(self, repodir, properties=EmptyI):
  1673         def create_repo(self, repodir, properties=EmptyI):
  1629                 """ Convenience routine to help subclasses create a package
  1674                 """ Convenience routine to help subclasses create a package
  1630                     repository.  Returns a pkg.server.repository.Repository
  1675                     repository.  Returns a pkg.server.repository.Repository
  1631                     object. """
  1676                     object. """