Merge lp:~lool/linaro-image-tools/testsuite-when-installed into lp:linaro-image-tools/11.11

Proposed by James Westby
Status: Merged
Merged at revision: 284
Proposed branch: lp:~lool/linaro-image-tools/testsuite-when-installed
Merge into: lp:linaro-image-tools/11.11
Prerequisite: lp:~lool/linaro-image-tools/optional-sudo
Diff against target: 228 lines (+104/-13) (has conflicts)
4 files modified
linaro-media-create (+22/-1)
linaro_media_create/hwpack.py (+11/-9)
linaro_media_create/tests/test_media_create.py (+39/-3)
linaro_media_create/utils.py (+32/-0)
Text conflict in linaro-media-create
To merge this branch: bzr merge lp:~lool/linaro-image-tools/testsuite-when-installed
Reviewer Review Type Date Requested Status
James Westby (community) Approve
Review via email: mp+48193@code.launchpad.net

This proposal supersedes a proposal from 2011-02-01.

To post a comment you must log in.
Revision history for this message
James Westby (james-w) wrote :

I'm not sure that the

116 + if os.path.isabs(__file__):

check is really what we want.

I think that if there is a linaro-hwpack-install alongside the linaro-media-create,
then we should always use it, regardless of $PATH, or how it is being executed.

That's probably not true of all commands though.

It's probably worth looking back over the discussion around using the correct
linaro-hwpack-install from when the code to use one in the current dir was
added.

Thanks,

James

review: Needs Information
Revision history for this message
Loïc Minier (lool) wrote :

(Thanks for adding the dep on the sudo branch; I failed to push the sudo branch after merging it locally -- it's merged in trunk now)

The isbas(__file__) test is just a test for whether or not we're running from bzr.

Note that __file__ is the possibly relative pathname of the current module, so of hwpack.py or utils.py.

The assumptions are:
* if you're running from a checkout, __file__ is relative, you're in the top directory and commands are in the current directory
* if you're not running from a checkout, __file__ is absolute, commands are in the PATH

Revision history for this message
Loïc Minier (lool) wrote :

Followup IRC chat:

15:09 < james_w> lool, We have to be very careful to use a matching
                 linaro-hwpack-create for our linaro-image-tools.

15:10 < james_w> lool, so when looking for that command, I think we should
                 always take one from next to the linaro-image-tools script,
                 before searching $PATH
15:10 < james_w> I think your change will approximate that in most cases, but
                 I'm not sure if it goes far enough to be robust
15:11 < lool> james_w: So you're saying, additionally to all things I've
              listed, we should first look for linaro-hwpack-* in the directory
              where linaro-media-create is, then fallback to PATH?
15:13 < james_w> lool, that's what I think
15:13 < lool> james_w: It makes sense
15:14 < lool> james_w: albeit I can think that if you have multiple
              linaro-hwpack-*, then you probably also have multiple python
              modules, and you're asking for trouble if PYTHONPATH and PATH
              don't point at the same versions in the same order
15:14 < james_w> lool, indeed
15:15 < lool> james_w: Do you still want this covered? We could do it either
              by passing directories across calls, or by prepending PATH
              immediately
15:15 < lool> But prepending PATH might have side effects when looking for
              other commands

15:16 < james_w> lool, right, I imagine it should just be for
                 linaro-hwpack-install

Revision history for this message
Loïc Minier (lool) wrote :

Ok; I pushed up to r280 to implement support for preferring the dir with which linaro-media-create is run; it was a bit more intrusive than I wished for

278. By Loïc Minier

Add an optional prefer_dir arg to find_command to allow searching a specific
directory first.

279. By Loïc Minier

Pass a tools dir (preferred directory) to install_hwpacks; should be the
directory of linaro-media-create when it's run with an explicit directory, or
"." when running from a bzr checkout.

280. By Loïc Minier

More explicit handling of empty PATH components as "." in find_command.

Revision history for this message
Loïc Minier (lool) wrote :

I tried supporting hwpack.tests as well, and had to add r281 for this; with r281 and this diff:

=== modified file 'hwpack/tests/test_script.py'
--- hwpack/tests/test_script.py 2011-01-28 19:50:48 +0000
+++ hwpack/tests/test_script.py 2011-02-02 16:40:28 +0000
@@ -31,6 +31,7 @@
     IsHardwarePack,
     TestCaseWithFixtures,
     )
+from linaro_media_create.utils import find_command

 class ScriptTests(TestCaseWithFixtures):
@@ -38,26 +39,16 @@

     def setUp(self):
         super(ScriptTests, self).setUp()
- self.script_path = self.find_script()
+
+ prefer_dir = None
+ # running from bzr checkout?
+ if not os.path.isabs(__file__):
+ prefer_dir = os.getcwd()
+
+ self.script_path = find_command(
+ 'linaro-hwpack-create', prefer_dir=prefer_dir)
         self.useFixture(ChdirToTempdirFixture())

- def find_script(self):
- script_name = "linaro-hwpack-create"
- this_path = os.path.abspath(__file__)
- parent_path = this_path
- for i in range(3):
- parent_path = os.path.dirname(parent_path)
- possible_paths = [
- os.path.join(parent_path, script_name),
- os.path.join("usr", "local", "bin", script_name),
- os.path.join("usr", "bin", script_name),
- ]
- for script_path in possible_paths:
- if os.path.exists(script_path):
- return script_path
- raise AssertionError(
- "Could not find linaro-hwpack-create script to test.")
-
     def run_script(self, args, expected_returncode=0):
         cmdline = [self.script_path] + args
         proc = subprocess.Popen(

all tests pass from a bzr checkout and when installed.

281. By Loïc Minier

Default to os.getcwd() instead of "." as "." breaks with some
ChdirToTempdirFixture based tests.

Revision history for this message
James Westby (james-w) wrote :

Looks good, aside from the merge conflicts.

Thanks,

James

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'linaro-media-create'
--- linaro-media-create 2011-02-02 12:08:05 +0000
+++ linaro-media-create 2011-02-02 16:43:03 +0000
@@ -62,6 +62,7 @@
62 Before doing so, make sure BOOT_DISK and ROOT_DISK are not mounted.62 Before doing so, make sure BOOT_DISK and ROOT_DISK are not mounted.
63 """63 """
64 devnull = open('/dev/null', 'w')64 devnull = open('/dev/null', 'w')
65<<<<<<< TREE
65 # ignore non-zero return codes66 # ignore non-zero return codes
66 for disk in BOOT_DISK, ROOT_DISK:67 for disk in BOOT_DISK, ROOT_DISK:
67 if disk is not None:68 if disk is not None:
@@ -70,10 +71,26 @@
70 stdout=devnull, stderr=devnull, as_root=True).wait()71 stdout=devnull, stderr=devnull, as_root=True).wait()
71 except cmd_runner.SubcommandNonZeroReturnValue:72 except cmd_runner.SubcommandNonZeroReturnValue:
72 pass73 pass
74=======
75 # ignore non-zero return codes
76 try:
77 if BOOT_DISK is not None:
78 cmd_runner.Popen(['umount', BOOT_DISK],
79 stdout=devnull, stderr=devnull, as_root=True).wait()
80 if ROOT_DISK is not None:
81 cmd_runner.Popen(['umount', ROOT_DISK],
82 stdout=devnull, stderr=devnull, as_root=True).wait()
83 except SubcommandNonZeroReturnValue:
84 pass
85>>>>>>> MERGE-SOURCE
73 # Remove TMP_DIR as root because some files written there are86 # Remove TMP_DIR as root because some files written there are
74 # owned by root.87 # owned by root.
75 if TMP_DIR is not None:88 if TMP_DIR is not None:
89<<<<<<< TREE
76 cmd_runner.run(['rm', '-rf', TMP_DIR], as_root=True).wait()90 cmd_runner.run(['rm', '-rf', TMP_DIR], as_root=True).wait()
91=======
92 cmd_runner.Popen(['rm', '-rf', TMP_DIR], as_root=True).wait()
93>>>>>>> MERGE-SOURCE
7794
7895
79def ensure_required_commands(args):96def ensure_required_commands(args):
@@ -121,7 +138,11 @@
121 unpack_binary_tarball(args.binary, TMP_DIR)138 unpack_binary_tarball(args.binary, TMP_DIR)
122139
123 hwpacks = args.hwpacks140 hwpacks = args.hwpacks
124 install_hwpacks(ROOTFS_DIR, TMP_DIR, args.hwpack_force_yes, *hwpacks)141 lmc_dir = os.path.dirname(__file__)
142 if lmc_dir == '':
143 lmc_dir = None
144 install_hwpacks(
145 ROOTFS_DIR, TMP_DIR, lmc_dir, args.hwpack_force_yes, *hwpacks)
125146
126 boot_partition, root_partition = setup_partitions(147 boot_partition, root_partition = setup_partitions(
127 board_config, media, args.image_size, args.boot_label, args.rfs_label,148 board_config, media, args.image_size, args.boot_label, args.rfs_label,
128149
=== modified file 'linaro_media_create/hwpack.py'
--- linaro_media_create/hwpack.py 2011-02-01 17:07:04 +0000
+++ linaro_media_create/hwpack.py 2011-02-02 16:43:03 +0000
@@ -21,7 +21,10 @@
21import sys21import sys
2222
23from linaro_media_create import cmd_runner23from linaro_media_create import cmd_runner
24from linaro_media_create.utils import is_arm_host24from linaro_media_create.utils import (
25 is_arm_host,
26 find_command,
27 )
2528
2629
27# It'd be nice if we could use atexit here, but all the things we need to undo30# It'd be nice if we could use atexit here, but all the things we need to undo
@@ -29,7 +32,8 @@
29# functions would only be called after l-m-c.py exits.32# functions would only be called after l-m-c.py exits.
30local_atexit = []33local_atexit = []
3134
32def install_hwpacks(chroot_dir, tmp_dir, hwpack_force_yes, *hwpack_files):35def install_hwpacks(
36 chroot_dir, tmp_dir, tools_dir, hwpack_force_yes, *hwpack_files):
33 """Install the given hwpacks onto the given chroot."""37 """Install the given hwpacks onto the given chroot."""
3438
35 chroot_etc = os.path.join(chroot_dir, 'etc')39 chroot_etc = os.path.join(chroot_dir, 'etc')
@@ -40,13 +44,11 @@
40 copy_file('/usr/bin/qemu-arm-static',44 copy_file('/usr/bin/qemu-arm-static',
41 os.path.join(chroot_dir, 'usr', 'bin'))45 os.path.join(chroot_dir, 'usr', 'bin'))
4246
43 # FIXME: This is an ugly hack to make sure we use the l-h-i script from47 linaro_hwpack_install_path = find_command(
44 # the current development tree when possible.48 'linaro-hwpack-install', prefer_dir=tools_dir)
45 here = os.path.dirname(__file__)49 # FIXME: shouldn't use chroot/usr/bin as this might conflict with installed
46 linaro_hwpack_install_path = os.path.join(50 # packages; would be best to use some custom directory like
47 here, '..', 'linaro-hwpack-install')51 # chroot/linaro-image-tools/bin
48 if not os.path.exists(linaro_hwpack_install_path):
49 linaro_hwpack_install_path = '/usr/bin/linaro-hwpack-install'
50 copy_file(linaro_hwpack_install_path,52 copy_file(linaro_hwpack_install_path,
51 os.path.join(chroot_dir, 'usr', 'bin'))53 os.path.join(chroot_dir, 'usr', 'bin'))
5254
5355
=== modified file 'linaro_media_create/tests/test_media_create.py'
--- linaro_media_create/tests/test_media_create.py 2011-02-01 17:29:25 +0000
+++ linaro_media_create/tests/test_media_create.py 2011-02-02 16:43:03 +0000
@@ -21,6 +21,7 @@
21import glob21import glob
22import os22import os
23import random23import random
24import stat
24import string25import string
25import subprocess26import subprocess
26import sys27import sys
@@ -82,6 +83,7 @@
82from linaro_media_create.unpack_binary_tarball import unpack_binary_tarball83from linaro_media_create.unpack_binary_tarball import unpack_binary_tarball
83from linaro_media_create.utils import (84from linaro_media_create.utils import (
84 ensure_command,85 ensure_command,
86 find_command,
85 install_package_providing,87 install_package_providing,
86 UnableToFindPackageProvidingCommand,88 UnableToFindPackageProvidingCommand,
87 )89 )
@@ -121,6 +123,32 @@
121 utils, 'install_package_providing', mock_func))123 utils, 'install_package_providing', mock_func))
122124
123125
126class TestFindCommand(TestCaseWithFixtures):
127
128 def test_preferred_dir(self):
129 tempdir = self.useFixture(CreateTempDirFixture()).get_temp_dir()
130 lmc = 'linaro-media-create'
131 path = os.path.join(tempdir, lmc)
132 open(path, 'w').close()
133 os.chmod(path, stat.S_IXUSR)
134 self.assertEquals(path, find_command(lmc, tempdir))
135
136 def test_existing_command(self):
137 lmc = 'linaro-media-create'
138 # running from bzr checkout?
139 if os.path.isabs(__file__):
140 expected, _ = cmd_runner.run(
141 ['which', lmc, ],
142 stdout=subprocess.PIPE).communicate()
143 expected = expected.strip()
144 else:
145 expected = os.path.join(os.getcwd(), lmc)
146 self.assertEquals(expected, find_command(lmc))
147
148 def test_nonexisting_command(self):
149 self.assertEquals(find_command('linaro-moo'), None)
150
151
124class TestInstallPackageProviding(TestCaseWithFixtures):152class TestInstallPackageProviding(TestCaseWithFixtures):
125153
126 def test_found_package(self):154 def test_found_package(self):
@@ -1083,8 +1111,17 @@
1083 sys, 'stdout', open('/dev/null', 'w')))1111 sys, 'stdout', open('/dev/null', 'w')))
1084 fixture = self.useFixture(MockCmdRunnerPopenFixture())1112 fixture = self.useFixture(MockCmdRunnerPopenFixture())
1085 force_yes = True1113 force_yes = True
1114
1115 prefer_dir = None
1116 # running from bzr checkout?
1117 if not os.path.isabs(__file__):
1118 prefer_dir = os.getcwd()
1119
1086 install_hwpacks(1120 install_hwpacks(
1087 'chroot', '/tmp/dir', force_yes, 'hwpack1.tgz', 'hwpack2.tgz')1121 'chroot', '/tmp/dir', prefer_dir, force_yes, 'hwpack1.tgz',
1122 'hwpack2.tgz')
1123 linaro_hwpack_install = find_command(
1124 'linaro-hwpack-install', prefer_dir=prefer_dir)
1088 self.assertEquals(1125 self.assertEquals(
1089 [['sudo', 'mv', '-f', 'chroot/etc/resolv.conf',1126 [['sudo', 'mv', '-f', 'chroot/etc/resolv.conf',
1090 '/tmp/dir/resolv.conf'],1127 '/tmp/dir/resolv.conf'],
@@ -1092,8 +1129,7 @@
1092 ['sudo', 'mv', '-f', 'chroot/etc/hosts', '/tmp/dir/hosts'],1129 ['sudo', 'mv', '-f', 'chroot/etc/hosts', '/tmp/dir/hosts'],
1093 ['sudo', 'cp', '/etc/hosts', 'chroot/etc'],1130 ['sudo', 'cp', '/etc/hosts', 'chroot/etc'],
1094 ['sudo', 'cp', '/usr/bin/qemu-arm-static', 'chroot/usr/bin'],1131 ['sudo', 'cp', '/usr/bin/qemu-arm-static', 'chroot/usr/bin'],
1095 ['sudo', 'cp', 'linaro_media_create/../linaro-hwpack-install',1132 ['sudo', 'cp', linaro_hwpack_install, 'chroot/usr/bin'],
1096 'chroot/usr/bin'],
1097 ['sudo', 'mount', 'proc', 'chroot/proc', '-t', 'proc'],1133 ['sudo', 'mount', 'proc', 'chroot/proc', '-t', 'proc'],
1098 ['sudo', 'cp', 'hwpack1.tgz', 'chroot'],1134 ['sudo', 'cp', 'hwpack1.tgz', 'chroot'],
1099 ['sudo', 'chroot', 'chroot', 'linaro-hwpack-install',1135 ['sudo', 'chroot', 'chroot', 'linaro-hwpack-install',
11001136
=== modified file 'linaro_media_create/utils.py'
--- linaro_media_create/utils.py 2011-01-28 19:50:48 +0000
+++ linaro_media_create/utils.py 2011-02-02 16:43:03 +0000
@@ -17,6 +17,7 @@
17# You should have received a copy of the GNU General Public License17# You should have received a copy of the GNU General Public License
18# along with Linaro Image Tools. If not, see <http://www.gnu.org/licenses/>.18# along with Linaro Image Tools. If not, see <http://www.gnu.org/licenses/>.
1919
20import os
20import platform21import platform
2122
22try:23try:
@@ -61,6 +62,37 @@
61 except cmd_runner.SubcommandNonZeroReturnValue:62 except cmd_runner.SubcommandNonZeroReturnValue:
62 install_package_providing(command)63 install_package_providing(command)
6364
65def find_command(name, prefer_dir=None):
66 """Finds a linaro-image-tools command.
67
68 Prefers specified directory, otherwise searches only the current directory
69 when running from a checkout, or only PATH when running from an installed
70 version.
71 """
72 assert name != ""
73 assert os.path.dirname(name) == ""
74
75 if not os.environ.has_key("PATH"):
76 os.environ["PATH"] = ":/bin:usr/bin"
77
78 # default to searching in current directory when running from a bzr
79 # checkout
80 dirs = [os.getcwd(),]
81 if os.path.isabs(__file__):
82 dirs = os.environ["PATH"].split(os.pathsep)
83 # empty dir in PATH means current directory
84 dirs = map(lambda x: x == '' and '.' or x, dirs)
85
86 if prefer_dir is not None:
87 dirs.insert(0, prefer_dir)
88
89 for dir in dirs:
90 path = os.path.join(dir, name)
91 if os.path.exists(path) and os.access(path, os.X_OK):
92 return path
93
94 return None
95
6496
65def is_arm_host():97def is_arm_host():
66 return platform.machine().startswith('arm')98 return platform.machine().startswith('arm')

Subscribers

People subscribed via source and target branches