t_plat.py revision 3177
1N/A#!/usr/bin/python
1N/A#
1N/A# CDDL HEADER START
1N/A#
1N/A# The contents of this file are subject to the terms of the
1N/A# Common Development and Distribution License (the "License").
1N/A# You may not use this file except in compliance with the License.
1N/A#
1N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1N/A# or http://www.opensolaris.org/os/licensing.
1N/A# See the License for the specific language governing permissions
1N/A# and limitations under the License.
1N/A#
1N/A# When distributing Covered Code, include this CDDL HEADER in each
1N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1N/A# If applicable, add the following below this CDDL HEADER, with the
1N/A# fields enclosed by brackets "[]" replaced with your own identifying
1N/A# information: Portions Copyright [yyyy] [name of copyright owner]
1N/A#
1N/A# CDDL HEADER END
1N/A#
1N/A
1N/A# Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
1N/A
1N/Aimport testutils
1N/Aif __name__ == "__main__":
1N/A testutils.setup_environment("../../../proto")
1N/Aimport pkg5unittest
1N/A
1N/Aimport unittest
1N/Aimport os
1N/Aimport shutil
1N/Aimport sys
1N/Aimport tempfile
1N/Aimport pkg.pkgsubprocess as subprocess
1N/Aimport pkg.fmri as fmri
1N/Aimport pkg.client.image as image
1N/Aimport pkg.portable.util as util
1N/Aimport pkg.portable as portable
1N/A
1N/Aclass TestPlat(pkg5unittest.Pkg5TestCase):
1N/A
1N/A def testbasic(self):
1N/A portable.get_isainfo()
1N/A portable.get_release()
1N/A portable.get_platform()
1N/A
1N/A
1N/A def testAdmin(self):
1N/A if os.name == 'posix' and os.getuid() == 0:
1N/A self.assert_(portable.is_admin())
1N/A if os.name == 'posix' and os.getuid() != 0:
1N/A self.assert_(not portable.is_admin())
1N/A
1N/A def testUtils(self):
1N/A self.assertNotEqual("unknown", util.get_canonical_os_type())
1N/A self.assertNotEqual("unknown", util.get_canonical_os_name())
1N/A
1N/A def testRelease(self):
1N/A rel = util.get_os_release()
1N/A # make sure it can be used in an fmri
1N/A test_fmri = fmri.PkgFmri("testpkg", build_release = rel)
1N/A
1N/A def testForcibleRename(self):
1N/A # rename a file on top of another file which already exists
1N/A (fd1, path1) = tempfile.mkstemp()
1N/A os.write(fd1, "foo")
1N/A (fd2, path2) = tempfile.mkstemp()
1N/A os.write(fd2, "bar")
1N/A os.close(fd1)
1N/A os.close(fd2)
1N/A portable.rename(path1, path2)
1N/A self.assertFalse(os.path.exists(path1))
1N/A self.assertTrue(os.path.exists(path2))
1N/A fd2 = os.open(path2, os.O_RDONLY)
1N/A self.assertEquals(os.read(fd2, 3), "foo")
1N/A os.close(fd2)
1N/A os.unlink(path2)
1N/A
1N/A def testRenameOfRunningExecutable(self):
1N/A if util.get_canonical_os_type() != 'windows':
1N/A return
1N/A import pkg.portable.os_windows as os_windows
1N/A cwd = os.getcwdu()
1N/A exefilesrc = 'C:\\Windows\\system32\\more.com'
1N/A self.assert_(os.path.exists(exefilesrc))
1N/A
1N/A # create an image, copy an executable into it,
1N/A # run the executable, replace the executable
1N/A tdir1 = tempfile.mkdtemp()
1N/A img1 = image.Image(tdir1, imgtype=image.IMG_USER,
1N/A should_exist=False, user_provided_dir=True)
1N/A img1.history.client_name = "pkg-test"
1N/A img1.set_attrs(False, "test",
1N/A origins=["http://localhost:10000"], refresh_allowed=False)
1N/A exefile = os.path.join(tdir1, 'less.com')
1N/A shutil.copyfile(exefilesrc, exefile)
1N/A proc = subprocess.Popen([exefile], stdin = subprocess.PIPE)
1N/A self.assertRaises(OSError, os.unlink, exefile)
1N/A fd1, path1 = tempfile.mkstemp(dir = tdir1)
1N/A os.write(fd1, "foo")
1N/A os.close(fd1)
1N/A portable.rename(path1, exefile)
1N/A fd2 = os.open(exefile, os.O_RDONLY)
1N/A self.assertEquals(os.read(fd2, 3), "foo")
1N/A os.close(fd2)
1N/A proc.communicate()
1N/A
1N/A # Make sure that the moved executable gets deleted
1N/A # This is a white-box test
1N/A # To simulate running another process, we delete the cache
1N/A # and call get_trashdir as if another file was being moved
1N/A # to the trash.
1N/A os_windows.cached_image_info = []
1N/A os_windows.get_trashdir(exefile)
1N/A self.assert_(not os.path.exists(os.path.join(img1.imgdir,
1N/A os_windows.trashname)))
1N/A
1N/A # cleanup
1N/A os.chdir(cwd)
1N/A shutil.rmtree(tdir1)
1N/A
1N/A def testRemoveOfRunningExecutable(self):
1N/A if util.get_canonical_os_type() != 'windows':
1N/A return
1N/A import pkg.portable.os_windows as os_windows
1N/A cwd = os.getcwdu()
1N/A exefilesrc = 'C:\\Windows\\system32\\more.com'
1N/A self.assert_(os.path.exists(exefilesrc))
1N/A
1N/A # create an image, copy an executable into it,
1N/A # run the executable, remove the executable
1N/A tdir1 = tempfile.mkdtemp()
1N/A img1 = image.Image(tdir1, imgtype=image.IMG_USER,
1N/A should_exist=False, user_provided_dir=True)
1N/A img1.history.client_name = "pkg-test"
1N/A img1.set_attrs(False, "test",
1N/A origins=["http://localhost:10000"], refresh_allowed=False)
1N/A exefile = os.path.join(tdir1, 'less.com')
1N/A shutil.copyfile(exefilesrc, exefile)
1N/A proc = subprocess.Popen([exefile], stdin = subprocess.PIPE)
1N/A self.assertRaises(OSError, os.unlink, exefile)
1N/A portable.remove(exefile)
1N/A self.assert_(not os.path.exists(exefile))
1N/A proc.communicate()
1N/A
1N/A # Make sure that the moved executable gets deleted
1N/A # This is a white-box test
1N/A # To simulate running another process, we delete the cache
1N/A # and call get_trashdir as if another file was being moved
1N/A # to the trash.
1N/A os_windows.cached_image_info = []
1N/A os_windows.get_trashdir(exefile)
1N/A self.assert_(not os.path.exists(os.path.join(img1.imgdir,
1N/A os_windows.trashname)))
1N/A
1N/A # cleanup
1N/A os.chdir(cwd)
1N/A shutil.rmtree(tdir1)
1N/A
1N/Aif __name__ == "__main__":
1N/A unittest.main()
1N/A