def test_dpkg_manager_identify_packages(): files = ["/sbin/iptables"] tracer = DebTracer() (packages, unknown_files) = \ tracer.identify_packages_from_files(files) # Make sure that iptables was identified assert (not unknown_files), "/sbin/iptables should be identified" assert len(packages) == 1 pkg = packages[0] assert pkg.name == 'iptables' # Make sure apt_sources are identified, but then we should ask the entire # distribution distributions = list(tracer.identify_distributions(files)) assert len(distributions) == 1 distribution, unknown_files = distributions[0] print(json.dumps(attr.asdict(distribution), indent=4)) assert distribution.apt_sources # Make sure both a non-local origin was found for o in distribution.apt_sources: if o.site: # Loop over mandatory attributes for a in [ "name", "component", "origin", "label", "site", "archive_uri" ]: assert getattr(o, a), "A non-local origin needs a " + a # Note: date and architecture are not mandatory (and not found on # travis) break else: assert False, "A non-local origin must be found"
def test_get_packagefields_for_files(): manager = DebTracer() # TODO: mock! and bring back afni and fail2ban files = [ '/bin/sh', # the tricky one with alternatives etc, on my system - provided by dash '/lib/i386-linux-gnu/libz.so.1.2.8', '/lib/x86_64-linux-gnu/libz.so.1.2.8', # multiarch '/usr/lib/afni/bin/afni', # from contrib '/usr/bin/fail2ban-server', '/usr/bin/fail2ban-server', # arch all and multiple '/bogus' ] def exec_cmd_batch_mock(session, cmd, subfiles, exc_classes): assert subfiles == files # we get all of the passed in assert cmd == ['dpkg-query', '-S'] yield ("""\ diversion by dash from: /bin/sh diversion by dash to: /bin/sh.distrib dash: /bin/sh zlib1g:i386: /lib/i386-linux-gnu/libz.so.1.2.8 zlib1g:amd64: /lib/x86_64-linux-gnu/libz.so.1.2.8 afni: /usr/lib/afni/bin/afni fail2ban: /usr/bin/fail2ban-server fail2ban: /usr/bin/fail2ban-server """, None, None) with mock.patch('niceman.distributions.debian.execute_command_batch', exec_cmd_batch_mock): out = manager._get_packagefields_for_files(files) assert out == { '/lib/i386-linux-gnu/libz.so.1.2.8': { 'name': u'zlib1g', 'architecture': u'i386' }, '/lib/x86_64-linux-gnu/libz.so.1.2.8': { 'name': u'zlib1g', 'architecture': u'amd64' }, '/usr/bin/fail2ban-server': { 'name': u'fail2ban' }, '/usr/lib/afni/bin/afni': { 'name': u'afni' }, '/bin/sh': { 'name': u'dash' } }
def test_get_packagefields_for_files(): manager = DebTracer() # TODO: mock! and bring back afni and fail2ban files = [ '/bin/sh', # the tricky one with alternatives etc, on my system - provided by dash '/lib/i386-linux-gnu/libz.so.1.2.8', '/lib/x86_64-linux-gnu/libz.so.1.2.8', # multiarch '/usr/lib/afni/bin/afni', # from contrib '/usr/bin/fail2ban-server', '/usr/bin/fail2ban-server', # arch all and multiple '/bogus' ] def _run_dpkg_query(subfiles): assert subfiles == files # we get all of the passed in return """\ diversion by dash from: /bin/sh diversion by dash to: /bin/sh.distrib dash: /bin/sh zlib1g:i386: /lib/i386-linux-gnu/libz.so.1.2.8 zlib1g:amd64: /lib/x86_64-linux-gnu/libz.so.1.2.8 afni: /usr/lib/afni/bin/afni fail2ban: /usr/bin/fail2ban-server fail2ban: /usr/bin/fail2ban-server """ with mock.patch.object(manager, "_run_dpkg_query", _run_dpkg_query): out = manager._get_packagefields_for_files(files) assert out == { '/lib/i386-linux-gnu/libz.so.1.2.8': { 'name': u'zlib1g', 'architecture': u'i386' }, '/lib/x86_64-linux-gnu/libz.so.1.2.8': { 'name': u'zlib1g', 'architecture': u'amd64' }, '/usr/bin/fail2ban-server': { 'name': u'fail2ban' }, '/usr/lib/afni/bin/afni': { 'name': u'afni' }, '/bin/sh': { 'name': u'dash' } }
def test_check_bin_packages(): # Gather files in /usr/bin and /usr/lib files = list_all_files("/usr/bin") + list_all_files("/usr/lib") tracer = DebTracer() distributions = list(tracer.identify_distributions(files)) assert len(distributions) == 1 distribution, unknown_files = distributions[0] print(json.dumps(attr.asdict(distribution), indent=4)) non_local_origins = [o for o in distribution.apt_sources if o.site] assert len(non_local_origins) > 0, "A non-local origin must be found" for o in non_local_origins: # Loop over mandatory attributes for a in [ "name", "component", "origin", "label", "site", "archive_uri" ]: assert getattr(o, a), "A non-local origin needs a " + a
def test_parse_dpkgquery_line(): parse = DebTracer()._parse_dpkgquery_line mock_values = { "unique": { "name": "pkg", "path": "/path/to/file", "pkgs_rest": None }, "multi_dir": { "name": "pkg", "path": os.getcwd(), "pkgs_rest": ", more, packages" }, "multi_file": { "name": "pkg", "path": __file__, "pkgs_rest": ", more, packages" } } with mock.patch("niceman.distributions.debian.parse_dpkgquery_line", mock_values.get): assert parse("unique") == {"name": "pkg", "path": "/path/to/file"} assert parse("multi_dir") is None with swallow_logs(new_level=logging.WARNING) as log: assert parse("multi_file") == {"name": "pkg", "path": __file__} assert any("multiple packages " in ln for ln in log.lines)
def test_utf8_file(): files = [ u"/usr/share/ca-certificates/mozilla/" u"TÜBİTAK_UEKAE_Kök_Sertifika_Hizmet_Sağlayıcısı_-_Sürüm_3.crt" ] manager = DebTracer() # Simple sanity check that the pipeline works with utf-8 (packages, unknown_files) = \ manager.identify_packages_from_files(files) # Print for manual debugging pprint(unknown_files) pprint(packages) # If the file exists, it should be in ca-certificates if os.path.isfile(files[0]): assert packages[0].name == "ca-certificates" else: # Otherwise just make sure we didn't throw an exception assert True