t_plat.py revision 691
409N/A#!/usr/bin/python2.4
290N/A#
290N/A# CDDL HEADER START
290N/A#
290N/A# The contents of this file are subject to the terms of the
290N/A# Common Development and Distribution License (the "License").
290N/A# You may not use this file except in compliance with the License.
290N/A#
290N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
290N/A# or http://www.opensolaris.org/os/licensing.
290N/A# See the License for the specific language governing permissions
290N/A# and limitations under the License.
290N/A#
290N/A# When distributing Covered Code, include this CDDL HEADER in each
290N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
290N/A# If applicable, add the following below this CDDL HEADER, with the
290N/A# fields enclosed by brackets "[]" replaced with your own identifying
290N/A# information: Portions Copyright [yyyy] [name of copyright owner]
290N/A#
290N/A# CDDL HEADER END
290N/A#
290N/A
290N/A# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
290N/A# Use is subject to license terms.
290N/A
526N/Aimport unittest
290N/Aimport os
402N/Aimport shutil
430N/Aimport sys
290N/Aimport tempfile
691N/Aimport pkg.pkgsubprocess as subprocess
290N/Aimport pkg.fmri as fmri
402N/Aimport pkg.client.image as image
290N/Aimport pkg.portable.util as util
290N/Aimport pkg.portable as portable
290N/A
430N/A# Set the path so that modules above can be found
430N/Apath_to_parent = os.path.join(os.path.dirname(__file__), "..")
430N/Asys.path.insert(0, path_to_parent)
430N/Aimport pkg5unittest
430N/A
430N/Aclass TestPlat(pkg5unittest.Pkg5TestCase):
402N/A def setUp(self):
402N/A pass
402N/A
402N/A def testbasic(self):
402N/A portable.get_isainfo()
402N/A portable.get_release()
402N/A portable.get_platform()
290N/A
290N/A
402N/A def testAdmin(self):
402N/A if os.name == 'posix' and os.getuid() == 0:
402N/A self.assert_(portable.is_admin())
402N/A if os.name == 'posix' and os.getuid() != 0:
402N/A self.assert_(not portable.is_admin())
402N/A
402N/A def testUtils(self):
402N/A self.assertNotEqual("unknown", util.get_canonical_os_type())
402N/A self.assertNotEqual("unknown", util.get_canonical_os_name())
402N/A
402N/A def testRelease(self):
402N/A rel = util.get_os_release()
402N/A # make sure it can be used in an fmri
402N/A test_fmri = fmri.PkgFmri("testpkg", build_release = rel)
290N/A
402N/A def testForcibleRename(self):
402N/A # rename a file on top of another file which already exists
402N/A (fd1, path1) = tempfile.mkstemp()
402N/A os.write(fd1, "foo")
402N/A (fd2, path2) = tempfile.mkstemp()
402N/A os.write(fd2, "bar")
402N/A os.close(fd1)
402N/A os.close(fd2)
402N/A portable.rename(path1, path2)
402N/A self.failIf(os.path.exists(path1))
402N/A self.failUnless(os.path.exists(path2))
402N/A fd2 = os.open(path2, os.O_RDONLY)
402N/A self.assertEquals(os.read(fd2, 3), "foo")
402N/A os.close(fd2)
402N/A os.unlink(path2)
290N/A
402N/A def testRenameOfRunningExecutable(self):
402N/A if util.get_canonical_os_type() != 'windows':
402N/A return
402N/A import pkg.portable.os_windows as os_windows
402N/A exefilesrc = 'C:\\Windows\\system32\\more.com'
402N/A self.assert_(os.path.exists(exefilesrc))
290N/A
402N/A # create an image, copy an executable into it,
402N/A # run the executable, replace the executable
402N/A tdir1 = tempfile.mkdtemp()
402N/A img1 = image.Image()
539N/A img1.history.client_name = "pkg-test"
402N/A img1.set_attrs(image.IMG_USER, tdir1, False, "test",
402N/A "http://localhost:10000")
402N/A exefile = os.path.join(tdir1, 'less.com')
402N/A shutil.copyfile(exefilesrc, exefile)
402N/A proc = subprocess.Popen([exefile], stdin = subprocess.PIPE)
402N/A self.assertRaises(OSError, os.unlink, exefile)
402N/A fd1, path1 = tempfile.mkstemp(dir = tdir1)
402N/A os.write(fd1, "foo")
402N/A os.close(fd1)
402N/A portable.rename(path1, exefile)
402N/A fd2 = os.open(exefile, os.O_RDONLY)
402N/A self.assertEquals(os.read(fd2, 3), "foo")
402N/A os.close(fd2)
402N/A proc.communicate()
290N/A
402N/A # Make sure that the moved executable gets deleted
402N/A # First do a rename in another image
402N/A tdir2 = tempfile.mkdtemp()
402N/A img2 = image.Image()
539N/A img2.history.client_name = "pkg-test"
402N/A img2.set_attrs(image.IMG_USER, tdir2, False, "test",
402N/A "http://localhost:10000")
402N/A fd2, path2 = tempfile.mkstemp(dir = tdir2)
402N/A os.write(fd2, "bar")
402N/A os.close(fd2)
402N/A portable.rename(path2, os.path.join(tdir2, "bar"))
402N/A # Now do another rename in the original image
402N/A # This should cause the executable to deleted from the trash
402N/A portable.rename(exefile, os.path.join(tdir1, "foo"))
402N/A self.assert_(not os.path.exists(os.path.join(img1.imgdir,
402N/A os_windows.trashname)))
402N/A
402N/A # cleanup
402N/A shutil.rmtree(img1.get_root())
402N/A shutil.rmtree(img2.get_root())
481N/A
481N/A def testRemoveOfRunningExecutable(self):
481N/A if util.get_canonical_os_type() != 'windows':
481N/A return
481N/A import pkg.portable.os_windows as os_windows
481N/A exefilesrc = 'C:\\Windows\\system32\\more.com'
481N/A self.assert_(os.path.exists(exefilesrc))
481N/A
481N/A # create an image, copy an executable into it,
481N/A # run the executable, remove the executable
481N/A tdir1 = tempfile.mkdtemp()
481N/A img1 = image.Image()
539N/A img1.history.client_name = "pkg-test"
481N/A img1.set_attrs(image.IMG_USER, tdir1, False, "test",
481N/A "http://localhost:10000")
481N/A exefile = os.path.join(tdir1, 'less.com')
481N/A shutil.copyfile(exefilesrc, exefile)
481N/A proc = subprocess.Popen([exefile], stdin = subprocess.PIPE)
481N/A self.assertRaises(OSError, os.unlink, exefile)
481N/A portable.remove(exefile)
481N/A self.assert_(not os.path.exists(exefile))
481N/A proc.communicate()
481N/A
481N/A # Make sure that the removed executable gets deleted
481N/A # First do a rename in another image
481N/A tdir2 = tempfile.mkdtemp()
481N/A img2 = image.Image()
539N/A img2.history.client_name = "pkg-test"
481N/A img2.set_attrs(image.IMG_USER, tdir2, False, "test",
481N/A "http://localhost:10000")
481N/A fd2, path2 = tempfile.mkstemp(dir = tdir2)
481N/A os.write(fd2, "bar")
481N/A os.close(fd2)
481N/A portable.rename(path2, os.path.join(tdir2, "bar"))
481N/A # Now do another rename in the original image
481N/A # This should cause the executable to deleted from the trash
481N/A fd3, path3 = tempfile.mkstemp(dir = tdir1)
481N/A os.write(fd3, "baz")
481N/A os.close(fd3)
481N/A portable.rename(path3, os.path.join(tdir1, "foo"))
481N/A self.assert_(not os.path.exists(os.path.join(img1.imgdir,
481N/A os_windows.trashname)))
481N/A
481N/A # cleanup
481N/A shutil.rmtree(img1.get_root())
481N/A shutil.rmtree(img2.get_root())
402N/A
290N/Aif __name__ == "__main__":
290N/A unittest.main()