#!/opt/csw/bin/python2.6

import pprint
import unittest2 as unittest

from lib.python import representations
from lib.python import fake_pkgstats_composer

EXAMPLE_PKGSTATS_LIST = [
 {'bad_paths': {},
  'basic_stats': {'catalogname': 'csw_bar',
                  'md5_sum': '0b8a07d49138011b0f3d74d2af2e84e5',
                  'parsed_basename': {'arch': 'sparc',
                                      'catalogname': None,
                                      'full_version_string': 'fakeversion,REV=0000.00.00',
                                      'osrel': 'SunOS5.9',
                                      'revision_info': {'REV': 'REV=0000.00.00'},
                                      'vendortag': None,
                                      'version': 'fakeversion',
                                      'version_info': {'major version': '0',
                                                       'minor version': '0',
                                                       'patchlevel': '0'}},
                  'pkg_basename': 'csw_bar-fakeversion,REV=0000.00.00-SunOS5.9-sparc-FAKE.pkg.gz',
                  'pkg_path': None,
                  'pkgname': 'CSWbar',
                  'size': 0,
                  'stats_version': 10L},
  'binaries': ['opt/csw/lib/libfoo.so.1', 'opt/csw/bin/bar'],
  'binaries_dump_info': [
    ('opt/csw/bin/bar', 'bar', None, ['libfoo.so.1'], ('/opt/csw/lib/$ISALIST',), True, True, True),
    ('opt/csw/lib/libfoo.so.1', 'libfoo.so.1', None, (), ('/opt/csw/lib/$ISALIST',), True, True, True),
  ],
  'binary_md5_sums': [('opt/csw/bin/bar',
                       '710de5ad3353b4747776a7a6babb7fb0'),
                      ('opt/csw/lib/libfoo.so.1',
                       '00685f8d00958bff01952a206b6ef636')],
  'depends': [],
  'elfdump_info': {'00685f8d00958bff01952a206b6ef636': {'symbol table': [],
                                                        'version definition': [],
                                                        'version needed': []},
                   '710de5ad3353b4747776a7a6babb7fb0': {'symbol table': [{'bind': 'GLOB',
                                                                          'flags': 'DBL',
                                                                          'shndx': 'UNDEF',
                                                                          'soname': 'libfoo.so.1',
                                                                          'symbol': 'foo'}],
                                                        'version definition': [],
                                                        'version needed': []}},
  'files_metadata': [('opt/csw/lib/libfoo.so.1',
                      'application/x-sharedlib',
                      3),
                     ('opt/csw/bin/bar', 'application/x-executable', 3)],
  'i_depends': [],
  'isalist': ['sparcv8plus+vis',
              'sparcv8',
              'sparcv8plus',
              'sparcv9',
              'sparcv8plus+vis2',
              'sparcv7',
              'sparcv8-fsmuld',
              'sparc',
              'sparcv9+vis2',
              'sparcv9+vis'],
  'mtime': '2010-07-05T23:48:10',
  'overrides': [],
  'pkgchk': {'return_code': 0,
             'stderr_lines': [],
             'stdout_lines': ['This is a fake package']},
  'pkginfo': {'ARCH': 'sparc',
              'CATEGORY': 'fake-package',
              'CLASSES': 'none',
              'EMAIL': 'fake-maintainer@opencsw.org',
              'HOTLINE': 'No hotline',
              'NAME': 'A fake, autogenerated package',
              'OPENCSW_CATALOGNAME': 'csw_bar',
              'PKG': 'CSWbar',
              'PSTAMP': 'fake@unknown-00000000000000',
              'VENDOR': 'A fake package packaged for CSW by The Buildfarm',
              'VERSION': 'fake_version'},
  'pkgmap': [representations.PkgmapEntry(
                 line=None, class_=None, mode=None, owner=None, group=None,
                 path='/opt/csw/lib/libfoo.so.1', target=None, type_=None,
                 major=None, minor=None, size=None, cksum=None, modtime=None, pkgnames=[]),
             representations.PkgmapEntry(
                 line=None, class_=None, mode=None, owner=None, group=None,
                 path='/opt/csw/bin/bar', target=None, type_=None, major=None,
                 minor=None, size=None, cksum=None, modtime=None, pkgnames=[])]}]


class TestSharedLibsOnlyIsalist(unittest.TestCase):
  """/opt/csw/lib/$ISALIST in RPATH without the bare /opt/csw/lib."""
  FUNCTION_NAME = 'SetCheckLibraries'
  # Contains only necessary bits.  The data listed in full.

  elfinfo_1 = {
      'version definition': [],
      'version needed': [],
      'symbol table': [
        {'soname': 'libfoo.so.1',
         'symbol': 'foo',
         'flags': 'DBL',
         'shndx': 'UNDEF',
         'bind': 'GLOB'},
      ]
  }
  elfinfo_2 = {
      'version definition': [],
      'version needed': [],
      'symbol table': [],
  }

  def setUp(self):
    super(TestSharedLibsOnlyIsalist, self).setUp()
    self.plc = fake_pkgstats_composer.PkgstatsListComposer('SunOS5.9', 'sparc')
    self.plc.AddPkgname('CSWbar')
    metadata_by_file_name = {
        '/opt/csw/lib/libfoo.so.1': representations.FileMetadata(
          path='/opt/csw/lib/libfoo.so.1',
          mime_type='application/x-sharedlib',
          machine_id=3,
        ),
        '/opt/csw/bin/bar': representations.FileMetadata(
          path='/opt/csw/bin/bar',
          mime_type='application/x-executable',
          machine_id=3,
        ),
    }
    pkgmap_entry_1 = representations.PkgmapEntry(
            line=None, class_=None, mode=None, owner=None, group=None,
            path='/opt/csw/lib/libfoo.so.1',
            target=None, type_=None, major=None, minor=None, size=None,
            cksum=None, modtime=None, pkgnames=[],
    )
    pkgmap_entry_2 = representations.PkgmapEntry(
            line=None, class_=None, mode=None, owner=None, group=None,
            path='/opt/csw/bin/bar',
            target=None, type_=None, major=None, minor=None, size=None,
            cksum=None, modtime=None, pkgnames=[],
    )
    binary_dump_info_1 = representations.BinaryDumpInfo(
        base_name='bar',
        needed_sonames=['libfoo.so.1'],
        path='opt/csw/bin/bar',
        rpath_set=True,
        runpath_rpath_the_same=True,
        runpath_set=True,
        runpath=('/opt/csw/lib/$ISALIST',),
        soname=None,
    )
    binary_dump_info_2 = representations.BinaryDumpInfo(
        base_name='libfoo.so.1',
        needed_sonames=(),
        path='opt/csw/lib/libfoo.so.1',
        rpath_set=True,
        runpath_rpath_the_same=True,
        runpath_set=True,
        runpath=('/opt/csw/lib/$ISALIST',),
        soname=None,
    )
    self.plc.AddFile('CSWbar', pkgmap_entry_1,
                     metadata_by_file_name[pkgmap_entry_1.path],
                     binary_dump_info_1, self.elfinfo_1)
    self.plc.AddFile('CSWbar', pkgmap_entry_2,
                     metadata_by_file_name[pkgmap_entry_2.path],
                     binary_dump_info_2, self.elfinfo_2)

  def testBasic(self):
    self.maxDiff = None
    self.assertEqual(EXAMPLE_PKGSTATS_LIST, self.plc.GetPkgstats())


if __name__ == '__main__':
  unittest.main()
