Skip to content

Commit

Permalink
CP-45750 get storage-init test to work when sh != bash
Browse files Browse the repository at this point in the history
* storage-init itself contains bashisms, so in the tests don't try to
  run it with sh
* bash run under its own name seems a bit resistent to attempts to
  redefine '[', so instead manipulate storage-init to subsistute uses of
  this for a mockable alternative.

Signed-off-by: Robin Newton <robin.newton@cloud.com>
  • Loading branch information
Robin Newton authored and MarkSymsCtx committed Mar 27, 2024
1 parent 5770fa7 commit b1eecb5
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions tests/test_storage_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def setUp(self):

# There are tweaks we need to make the to storage-init:
# - Change the location of various key files
# - Add an alias for the shell's built-in '[' operator to a command we
# can mock
# - Replace the uses of the shell's built-in '[' operator to a command
# we can mock
# - Ensure that the script calls a special command when it exits, so
# that the test knows when it is done.

Expand All @@ -61,7 +61,8 @@ def setUp(self):
script = re.sub(r"/var/lib/misc/ran-storage-init\b",
os.path.join(self.test_dir.name, "ran-storage-init"),
script)
script = f"trap on_exit EXIT\nalias [=mock_test\n{script}"
script = re.sub(r"(?<!\[)\[\s+([^]]*)\s+\]", r"mock_test \1", script)
script = f"trap on_exit EXIT\n{script}"

self.script_path = self.write_file("storage-init", script)

Expand Down Expand Up @@ -156,7 +157,7 @@ def test_install_with_lvm(self):
f.write("TYPE=lvm\n")
f.write("PARTITIONS='/dev/sda'")

p = subprocess.Popen(["/bin/sh", self.script_path],
p = subprocess.Popen(["/bin/bash", self.script_path],
env=self.make_env())

self.run_script_commands()
Expand Down Expand Up @@ -222,7 +223,7 @@ def test_install_with_ext(self):
f.write("TYPE=ext\n")
f.write("PARTITIONS='/dev/sda4 /dev/sdb'")

p = subprocess.Popen(["/bin/sh", self.script_path],
p = subprocess.Popen(["/bin/bash", self.script_path],
env=self.make_env())

self.run_script_commands()
Expand Down Expand Up @@ -292,7 +293,7 @@ def test_install_with_other_sr_type(self):
f.write("TYPE=wtf\n")
f.write("PARTITIONS='/dev/sda4 /dev/sdb /dev/sdc'")

p = subprocess.Popen(["/bin/sh", self.script_path],
p = subprocess.Popen(["/bin/bash", self.script_path],
env=self.make_env())

self.run_script_commands()
Expand Down Expand Up @@ -341,9 +342,7 @@ def _mock_test_command(self, args):
elif re.match(r"! -b /dev/", combined_args):
returncode = 1
else:
# Use the standard test command (excluding the final arg, which is
# a ']')
proc = subprocess.run(["test"] + args[:-1])
proc = subprocess.run(["test"] + args)
returncode = proc.returncode

return CmdResult(returncode)
Expand Down

0 comments on commit b1eecb5

Please sign in to comment.