diff options
author | Michal Minar <miminar@redhat.com> | 2014-01-23 12:48:03 +0100 |
---|---|---|
committer | Michal Minar <miminar@redhat.com> | 2014-01-24 10:14:33 +0100 |
commit | 84f8972201a2362f966690e855453276ca7adc75 (patch) | |
tree | 4a5d794a98f780fcf5a233ad02c87cc826f1092e /src/software/test | |
parent | b95b572144f54e51df9f5322ee9e3101b73a2e1e (diff) | |
download | openlmi-providers-84f8972201a2362f966690e855453276ca7adc75.tar.gz openlmi-providers-84f8972201a2362f966690e855453276ca7adc75.tar.xz openlmi-providers-84f8972201a2362f966690e855453276ca7adc75.zip |
software: added tests for non-ascii characters
Test that unicode characters work at least for package files.
Diffstat (limited to 'src/software/test')
-rw-r--r-- | src/software/test/reposetup.py | 13 | ||||
-rw-r--r-- | src/software/test/test_software_identity_checks.py | 66 | ||||
-rw-r--r-- | src/software/test/test_software_identity_file_check.py | 147 |
3 files changed, 180 insertions, 46 deletions
diff --git a/src/software/test/reposetup.py b/src/software/test/reposetup.py index c84e32c..09d5605 100644 --- a/src/software/test/reposetup.py +++ b/src/software/test/reposetup.py @@ -123,6 +123,8 @@ PKG_FILE_ENTRIES = { } } +UTF8_TEST_FILE_NAME = u'\u011b\u0161\u010d\u0159\u017e\xfd\xe1\xed\xe9' + #: Repositories with assigned packages. REPOS = { 'openlmi-sw-test-repo-stable' : { @@ -227,6 +229,15 @@ REPOS = { } } }, + # unicode characters + 'openlmi-sw-test-unicode-chars-0:1.2.3-1%(disttag)s.noarch' : { + 'files' : { + 'usr/share/openlmi-sw-test-unicode-chars/' : { + UTF8_TEST_FILE_NAME: + (REGULAR | DOC, 0644, 'content\n') + } + } + } }, } @@ -433,6 +444,8 @@ def _make_spec_files_string(path, entries): res = [] for name, entry in entries.items(): full_path = os.path.join(path, name) + if isinstance(full_path, unicode): + full_path = full_path.encode('utf-8') if isinstance(entry, dict): if name.endswith('perms/'): res.append('%dir %attr(1777, -, -) ' + full_path) diff --git a/src/software/test/test_software_identity_checks.py b/src/software/test/test_software_identity_checks.py index b9d5fbb..1ed01d5 100644 --- a/src/software/test/test_software_identity_checks.py +++ b/src/software/test/test_software_identity_checks.py @@ -29,6 +29,7 @@ import unittest from lmi.test.lmibase import enable_lmi_exceptions import package +import reposetup import swbase import util @@ -141,6 +142,39 @@ class TestSoftwareIdentityChecks(swbase.SwTestCase): pkg_names.remove(ref.Name) self.assertEqual(len(pkg_names), 0) + @swbase.test_with_packages('misc#unicode-chars') + def test_package_file_checks_unicode(self): + """ + Try to get file checks associated with package. With utf-8 characters. + """ + pkg = self.get_repo('misc')['unicode-chars'] + filepath = '/usr/share/openlmi-sw-test-unicode-chars' + objpath = self.make_op(pkg, filepath) + inst = objpath.Element.to_instance() + self.assertNotEqual(inst, None, + "get instance on %s:%s succeeds" % (pkg, filepath)) + refs = inst.associators( + AssocClass=self.CLASS_NAME, + Role="Element", + ResultRole="Check", + ResultClass="LMI_SoftwareIdentityFileCheck") + self.assertEqual(len(refs), len(pkg)) + self.assertEqual(len(refs), 2, "there are 2 files in %s" % pkg.name) + pkg_names = set(pkg.files) + for ref in refs: + self.assertIn(ref.Name, set(r.Name for r in refs)) + self.assertEqual(sorted(ref.path.key_properties()), + ["CheckID", "Name", "SoftwareElementID", + "SoftwareElementState", + "TargetOperatingSystem", "Version"]) + self.assertEqual(ref.SoftwareElementID, pkg.nevra) + self.assertIn(ref.Name, pkg_names) + self.assertEqual(ref.FailedFlags, [], + "FailedFlags are empty for unmodified file %s:%s" + % (pkg, filepath)) + pkg_names.remove(ref.Name) + self.assertEqual(len(pkg_names), 0) + @swbase.test_with_packages('stable#pkg3') def test_package_file_check_names(self): """ @@ -198,6 +232,38 @@ class TestSoftwareIdentityChecks(swbase.SwTestCase): ResultClass="LMI_SoftwareIdentity") self.assertEqual(len(refs), 1) + @swbase.test_with_packages('misc#unicode-chars') + def test_file_check_package_unicode(self): + """ + Try to get package assocatied to file check. + """ + pkg = self.get_repo('misc')['unicode-chars'] + filepath = os.path.join('/usr/share/openlmi-sw-test-unicode-chars', + reposetup.UTF8_TEST_FILE_NAME) + objpath = self.make_op(pkg, filepath) + inst = objpath.Check.to_instance() + self.assertNotEqual(inst, None, + "get instance for %s:%s succeeds" % (pkg, filepath)) + refs = inst.associators( + AssocClass=self.CLASS_NAME, + Role="Check", + ResultRole="Element", + ResultClass="LMI_SoftwareIdentity") + self.assertEqual(len(refs), 1) + ref = refs[0] + self.assertCIMNameEqual(ref.path, objpath.Element) + self.assertEqual(ref.ElementName, pkg.nevra) + self.assertEqual(ref.VersionString, pkg.evra) + + # try with removed file + os.remove(filepath.encode('utf-8')) + refs = objpath.Check.to_instance().associators( + AssocClass=self.CLASS_NAME, + Role="Check", + ResultRole="Element", + ResultClass="LMI_SoftwareIdentity") + self.assertEqual(len(refs), 1) + def suite(): """For unittest loaders.""" return unittest.TestLoader().loadTestsFromTestCase( diff --git a/src/software/test/test_software_identity_file_check.py b/src/software/test/test_software_identity_file_check.py index 6123d8c..60c18cc 100644 --- a/src/software/test/test_software_identity_file_check.py +++ b/src/software/test/test_software_identity_file_check.py @@ -89,6 +89,17 @@ HASH_DIGEST_LENGTH = { 11 : 56 #SHA224 } +def utf8_encode(string): + """ + Encodes string, utf-8 encoding is used. + + :returns: Byte representation of string. + :rtype: str + """ + if isinstance(string, unicode): + return string.encode('utf-8') + return str(string) + class TestSoftwareIdentityFileCheck(swbase.SwTestCase): """ Basic cim operations test. @@ -120,6 +131,21 @@ class TestSoftwareIdentityFileCheck(swbase.SwTestCase): "Version" : pkg.evra }) + def assertEqual(self, fst, snd, *args): + if args: + args = args[0] % tuple(utf8_encode(a) for a in args[1:]) + swbase.SwTestCase.assertEqual(self, fst, snd, args) + + def assertNotEqual(self, fst, snd, *args): + if args: + args = args[0] % tuple(utf8_encode(a) for a in args[1:]) + swbase.SwTestCase.assertNotEqual(self, fst, snd, args) + + def assertGreater(self, fst, snd, *args): + if args: + args = args[0] % tuple(utf8_encode(a) for a in args[1:]) + swbase.SwTestCase.assertGreater(self, fst, snd, args) + def setUp(self): to_uninstall = set() for repo in self.repodb.values(): @@ -165,14 +191,11 @@ class TestSoftwareIdentityFileCheck(swbase.SwTestCase): os.lchown(filepath, stats.st_uid + 1, -1) inst.refresh() self.assertEqual(inst.UserID, inst.UserIDOriginal + 1, - "Unexpected uid of modified symlink for %s:%s"%( - pkg.name, filepath)) + "Unexpected uid of modified symlink for %s:%s", pkg.name, filepath) self.assertEqual(inst.UserID, prev_user + 1, - "Unexpected uid of modified symlink for %s:%s"%( - pkg.name, filepath)) + "Unexpected uid of modified symlink for %s:%s", pkg.name, filepath) self.assertEqual(inst.GroupID, stats.st_gid, - "Unexpected gid of modified symlink for %s:%s"%( - pkg.name, filepath)) + "Unexpected gid of modified symlink for %s:%s", pkg.name, filepath) self.assertEqual(inst.FailedFlags, [FAILED_FLAGS_UID]) # modify link_target @@ -188,22 +211,22 @@ class TestSoftwareIdentityFileCheck(swbase.SwTestCase): self.assertTrue(inst.FileExists, "File %s:%s should exist"%(pkg.name, filepath)) self.assertEqual(inst.FileType, inst.FileTypeOriginal, - "File type should match for symlink %s:%s"%(pkg.name, filepath)) + "File type should match for symlink %s:%s", pkg.name, filepath) self.assertNotEqual(inst.FileSizeOriginal, inst.FileSize, - "File size should not match for symlink %s:%s"%( - pkg.name, filepath)) + "File size should not match for symlink %s:%s", + pkg.name, filepath) self.assertEqual(inst.FileMode, inst.FileModeOriginal, - "File mode should match for symlink %s:%s"%(pkg.name, filepath)) + "File mode should match for symlink %s:%s", pkg.name, filepath) self.assertEqual(inst.LinkTarget, lt_modif, - "Link target should match modified path %s:%s"%( - pkg.name, filepath)) + "Link target should match modified path %s:%s", + pkg.name, filepath) self.assertNotEqual(inst.LinkTargetOriginal, inst.LinkTarget, - "Link target should not match for symlink %s:%s"%( - pkg.name, filepath)) + "Link target should not match for symlink %s:%s", + pkg.name, filepath) self.assertEqual(inst.UserID, inst.UserIDOriginal, - "File uid should match for symlink %s:%s"%(pkg.name, filepath)) + "File uid should match for symlink %s:%s", pkg.name, filepath) self.assertEqual(inst.GroupID, inst.GroupIDOriginal, - "File gid should match for symlink %s:%s"%(pkg.name, filepath)) + "File gid should match for symlink %s:%s", pkg.name, filepath) def do_check_directory(self, pkg, filepath, inst): """ @@ -235,24 +258,24 @@ class TestSoftwareIdentityFileCheck(swbase.SwTestCase): stats = os.lstat(filepath) self.assertEqual(inst.FileType, pywbem.Uint16(FILE_TYPE_FILE), - "Unexpected file type for %s:%s"%(pkg.name, filepath)) + "Unexpected file type for %s:%s", pkg.name, filepath) self.assertEqual(inst.UserID, stats.st_uid, - "Unexpected file uid for %s:%s"%(pkg.name, filepath)) + "Unexpected file uid for %s:%s", pkg.name, filepath) self.assertEqual(inst.GroupID, stats.st_gid, - "Unexpected gid for regular file %s:%s"%(pkg.name, filepath)) + "Unexpected gid for regular file %s:%s", pkg.name, filepath) self.assertEqual(inst.FileMode, stats.st_mode, - "Unexpected mode for regular file %s:%s"%(pkg.name, filepath)) + "Unexpected mode for regular file %s:%s", pkg.name, filepath) self.assertEqual(inst.FileSize, stats.st_size, - "Unexpected size for regular file %s:%s"%(pkg.name, filepath)) + "Unexpected size for regular file %s:%s", pkg.name, filepath) self.assertIs(inst.LinkTarget, None) csum = self.make_checksum_str(inst.ChecksumType, filepath) self.assertEqual(inst.FileChecksum.lower(), csum, - "Unexpected checksum for regular file %s:%s"%(pkg.name, filepath)) + "Unexpected checksum for regular file %s:%s", pkg.name, filepath) self.assertEqual(inst.LastModificationTime, inst.LastModificationTimeOriginal, - "Unexpected mtime for regular file %s:%s"%(pkg.name, filepath)) + "Unexpected mtime for regular file %s:%s", pkg.name, filepath) self.assertEqual(inst.LastModificationTime, int(stats.st_mtime), - "Unexpected mtime for regular file %s:%s"%(pkg.name, filepath)) + "Unexpected mtime for regular file %s:%s", pkg.name, filepath) # make it longer with open(filepath, "a+") as fobj: @@ -266,13 +289,14 @@ class TestSoftwareIdentityFileCheck(swbase.SwTestCase): self.assertGreater(inst.FileSize, inst.FileSizeOriginal, "File size should be greater, then expected for regular file" - " %s:%s"%(pkg.name, filepath)) + " %s:%s", pkg.name, filepath) self.assertGreater(inst.LastModificationTime, inst.LastModificationTimeOriginal, - "Unexpected mtime for regular file %s:%s"%(pkg.name, filepath)) + "Unexpected mtime for regular file %s:%s", pkg.name, filepath) self.assertTrue(inst.FileExists, - "Regular file should exist %s:%s"%(pkg.name, filepath)) + "Regular file should exist %s:%s"%( + utf8_encode(pkg.name), utf8_encode(filepath))) # change file type os.remove(filepath) @@ -281,28 +305,28 @@ class TestSoftwareIdentityFileCheck(swbase.SwTestCase): inst.GroupIDOriginal) inst.refresh() self.assertNotEqual(inst.LinkTargetOriginal, inst.LinkTarget, - "Link target should not match for %s:%s"%(pkg.name, filepath)) + "Link target should not match for %s:%s", pkg.name, filepath) self.assertNotEqual(inst.FileSizeOriginal, inst.FileSize, - "File size should not match for %s:%s"%(pkg.name, filepath)) + "File size should not match for %s:%s", pkg.name, filepath) self.assertGreater(inst.LastModificationTime, inst.LastModificationTimeOriginal, - "File mtime should be greater than expected for %s:%s"%( - pkg.name, filepath)) + "File mtime should be greater than expected for %s:%s", + pkg.name, filepath) self.assertNotEqual(inst.FileTypeOriginal, inst.FileType, - "File type should not match for %s:%s"%(pkg.name, filepath)) + "File type should not match for %s:%s", pkg.name, filepath) self.assertEqual(inst.FileType, pywbem.Uint16(FILE_TYPE_SYMLINK), - "File type should match for %s:%s"%(pkg.name, filepath)) + "File type should match for %s:%s", pkg.name, filepath) self.assertIn(pywbem.Uint16(FAILED_FLAGS_MODE), inst.FailedFlags) # remove it os.remove(filepath) inst.refresh() self.assertEqual(inst.LinkTarget, inst.LinkTargetOriginal, - "Link target does not match for regular file %s:%s"%( - pkg.name, filepath)) + "Link target does not match for regular file %s:%s", + pkg.name, filepath) self.assertNotEqual(inst.FileSizeOriginal, inst.FileSize, - "File size should not match for regular file %s:%s"%( - pkg.name, filepath)) + "File size should not match for regular file %s:%s", + pkg.name, filepath) self.assertIsNone(inst.LastModificationTime) self.assertIsNone(inst.FileType) self.assertIsNone(inst.FileChecksum) @@ -362,21 +386,28 @@ class TestSoftwareIdentityFileCheck(swbase.SwTestCase): inst = objpath.to_instance() self.assertNotEqual(inst, None) self.assertCIMNameEqual(inst.path, objpath, - msg="Object paths of instance must match for %s:%s"%( - pkg.name, filepath)) + "Object paths of instance must match for %s:%s" % tuple( + utf8_encode(s) for s in (pkg.name, filepath))) for key in self.KEYS: if key.lower() == "targetoperatingsystem": self.assertIsInstance(getattr(objpath, key), (int, long)) else: - self.assertEqual(getattr(objpath, key), getattr(inst, key), - "OP key %s values should match for %s:%s"%( - key, pkg.name, filepath)) + if key == "Name": + fst = utf8_encode(getattr(objpath, key)) + snd = utf8_encode(getattr(inst, key)) + else: + fst = getattr(objpath, key) + snd = getattr(inst, key) + self.assertEqual(fst, snd, + "OP key %s values should match for %s:%s", + key, pkg.name, filepath) self.assertTrue(inst.FileExists, - "File %s:%s must exist"%(pkg.name, filepath)) + "File %s:%s must exist" % tuple(utf8_encode(s) + for s in (pkg.name, filepath))) self.assertEqual(len(inst.FailedFlags), 0, - "FailedFlags must be empty for %s:%s, not: %s" % ( - str(pkg), filepath, inst.FailedFlags)) + "FailedFlags must be empty for %s:%s, not: %s", + pkg, filepath, inst.FailedFlags) for prop in ( "FileType", "UserID", "GroupID" , "FileMode", "FileSize", "LinkTarget" @@ -388,7 +419,7 @@ class TestSoftwareIdentityFileCheck(swbase.SwTestCase): continue self.assertEqual( getattr(inst, prop+"Original"), getattr(inst, prop), - "%s should match for %s:%s"%(prop, pkg.name, filepath)) + "%s should match for %s:%s", prop, pkg.name, filepath) if os.path.islink(filepath): self.do_check_symlink(pkg, filepath, inst) elif os.path.isdir(filepath): @@ -422,6 +453,30 @@ class TestSoftwareIdentityFileCheck(swbase.SwTestCase): self.assertEqual(inst.FileType, FILE_TYPE_FILE) self.check_filepath(pkg, filepath) + @swbase.test_with_packages('misc#unicode-chars') + def test_check_regular_file(self): + """ + Test ``GetInstance()`` call on ``LMI_SoftwareIdentityFileCheck`` with + config file.. + """ + pkg = self.get_repo('misc')['unicode-chars'] + filepath = ( u'/usr/share/openlmi-sw-test-unicode-chars/' \ + u'\u011b\u0161\u010d\u0159\u017e\xfd\xe1\xed\xe9') + objpath = self.make_op(pkg, filepath) + inst = objpath.to_instance() + self.assertEqual(inst.FileType, FILE_TYPE_FILE) + self.check_filepath(pkg, filepath) + + # try also with encoded string + package.remove_pkgs(pkg.name) + package.install_pkgs(pkg) + filepath = filepath.encode('utf-8') + self.assertIsInstance(filepath, str) + objpath = self.make_op(pkg, filepath) + inst = objpath.to_instance() + self.assertEqual(inst.FileType, FILE_TYPE_FILE) + self.check_filepath(pkg, filepath) + @enable_lmi_exceptions @swbase.test_with_packages('stable#pkg1', 'stable#pkg2') def test_get_instance_invalid(self): |