Merge lp:~elopio/snappy/resize_test into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by Leo Arias
Status: Superseded
Proposed branch: lp:~elopio/snappy/resize_test
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Prerequisite: lp:~elopio/snappy/parted_script
Diff against target: 233 lines (+182/-5)
4 files modified
_integration-tests/data/tpl/control (+1/-1)
_integration-tests/scripts/install-test-initramfs (+19/-0)
_integration-tests/scripts/resize-writable-test (+118/-0)
_integration-tests/tests/initramfs_test.go (+44/-4)
To merge this branch: bzr merge lp:~elopio/snappy/resize_test
Reviewer Review Type Date Requested Status
Oliver Grawert Needs Fixing
Review via email: mp+270718@code.launchpad.net

This proposal supersedes a proposal from 2015-09-10.

This proposal has been superseded by a proposal from 2015-09-13.

Commit message

Added tests for the resize partition forcing a resize and a no-op.

Description of the change

This is not the test I wanted to make because it's far from the initramfs-tools-ubuntu-core source code. Also it's not as fast as it could be if we just test the init script without fully booting snappy.
But this is the easiest approach of all the ones I tried. We can build the initramfs on the same testbed, and use it with a simple reboot. I made notes to keep the research about building the init and making a simple boot. Now it's time to move on.

This is a kind of a complementary test, we add two conditions that are highly unlikely to be true at the same time.

To post a comment you must log in.
lp:~elopio/snappy/resize_test updated
670. By Leo Arias

Fixed the expected free size.

671. By Leo Arias

Fixed the expected free size.

672. By Leo Arias

Fixed format.

673. By Leo Arias

Fixed format.

Revision history for this message
Oliver Grawert (ogra) wrote :

note that our initrd allows to be compressed by different tools, there is no guarantee we always use xz. so before copying the initrd.img file to initrd.xz there should be a check that picks the right suffix and the right decompression tool in the install-test-initramfs script.

see my desktop initrd vs a current snappy rpi2 initrd:

ogra@anubis:~/Devel/packages$ file /boot/initrd.img-3.13.0-63-generic
/boot/initrd.img-3.13.0-63-generic: gzip compressed data, from Unix, last modified: Sun Sep 6 10:58:31 2015
ogra@anubis:~/Devel/packages$ file /datengrab/rpi/kernel/device/assets/initrd.img
/datengrab/rpi/kernel/device/assets/initrd.img: LZMA compressed data, streamed

I have a script snippet I use in such cases, see http://paste.ubuntu.com/12334035/ (indeed you can trim that to only match xz, gzip and lzma, I don't think we use any of the others for initrd's)

also note that once Bug 1490608 is fixed, the "do_gpt()" function will be dropped and this test needs to be adjusted accordingly.

review: Needs Fixing
Revision history for this message
Leo Arias (elopio) wrote :

TODO, rename CurrentPartition to NextBootPartition. Add a proper CurrentPartition to make the test pass in bbb. Check that rpi also uses xz for the initrd.img

lp:~elopio/snappy/resize_test updated
674. By Leo Arias

Merged with prerequisite.

675. By Leo Arias

First remove the original initrd.img.

676. By Leo Arias

For mbr, resize the partition with sfdisk and using sectors.

677. By Leo Arias

Added comments.

678. By Leo Arias

Added setup and teardown.

679. By Leo Arias

Merged with trunk.

680. By Leo Arias

Fixed the name of the initrd.img

681. By Leo Arias

Fixed the call to ExecCommand.

682. By Leo Arias

Fixed set up and tear down.

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '_integration-tests/data/tpl/control'
2--- _integration-tests/data/tpl/control 2015-07-22 16:36:14 +0000
3+++ _integration-tests/data/tpl/control 2015-09-10 17:46:43 +0000
4@@ -1,4 +1,4 @@
5 {{ $filter := .Filter }}
6 {{ $test := .Test }}
7-Test-Command: ./_integration-tests/reboot-wrapper {{ $test }} {{ if $filter }}-gocheck.f {{ $filter }}{{ end }}
8+Test-Command: ./_integration-tests/scripts/reboot-wrapper {{ $test }} {{ if $filter }}-gocheck.f {{ $filter }}{{ end }}
9 Restrictions: allow-stderr
10
11=== added directory '_integration-tests/scripts'
12=== renamed file '_integration-tests/tests/get_unpartitioned_space' => '_integration-tests/scripts/get_unpartitioned_space'
13=== added file '_integration-tests/scripts/install-test-initramfs'
14--- _integration-tests/scripts/install-test-initramfs 1970-01-01 00:00:00 +0000
15+++ _integration-tests/scripts/install-test-initramfs 2015-09-10 17:46:43 +0000
16@@ -0,0 +1,19 @@
17+#! /bin/sh -e
18+# Prepare the initramfs image for the tests.
19+
20+current_boot_dir=$1
21+writable_percent=$2
22+tree_dir=$(pwd)
23+initrd_unpack_dir=$ADT_ARTIFACTS/initrd
24+
25+cp ${current_boot_dir}/initrd.img /tmp/initrd.xz
26+cd /tmp/
27+unxz /tmp/initrd.xz
28+mkdir $initrd_unpack_dir || true
29+cd $initrd_unpack_dir
30+cpio -id < /tmp/initrd
31+cp $tree_dir/_integration-tests/scripts/resize-writable-test scripts/local-premount/
32+sed -i "s/writable_percent=.*/writable_percent=${writable_percent}/" scripts/local-premount/resize-writable-test
33+sed -i '1i /scripts/local-premount/resize-writable-test "$@"\n[ -e /conf/param.conf ] && . /conf/param.conf' scripts/local-premount/ORDER
34+find .|cpio -o -H newc|xz -c -7 --check=crc32 > initrd.img
35+sudo mv initrd.img $current_boot_dir
36
37=== renamed file '_integration-tests/reboot-wrapper' => '_integration-tests/scripts/reboot-wrapper'
38=== added file '_integration-tests/scripts/resize-writable-test'
39--- _integration-tests/scripts/resize-writable-test 1970-01-01 00:00:00 +0000
40+++ _integration-tests/scripts/resize-writable-test 2015-09-10 17:46:43 +0000
41@@ -0,0 +1,118 @@
42+#! /bin/sh -e
43+# initramfs local-premount script to resize writable for testing
44+
45+PREREQ=""
46+
47+# Output pre-requisites
48+prereqs()
49+{
50+ echo "$PREREQ"
51+}
52+
53+case "$1" in
54+ prereqs)
55+ prereqs
56+ exit 0
57+ ;;
58+esac
59+
60+TMPFILE="/run/initramfs/old-table-test.txt"
61+LOGFILE="/run/initramfs/resize-writable-test.log"
62+
63+writable_percent=100
64+
65+wait-for-root "LABEL=writable" "${ROOTDELAY:-180}" >/dev/null || true
66+
67+writable_part="$(findfs LABEL=writable)"
68+
69+syspath="$(dirname $(realpath /sys/class/block/$(basename $writable_part)))"
70+device="$(realpath /dev/block/$(cat $syspath/dev))"
71+partition=$(cat $syspath/$(basename $writable_part)/partition)
72+
73+device_size="$(($(cat $syspath/size)/2))"
74+
75+get_end()
76+{
77+ NUM=$1
78+ lastpart="$(grep -cE ^'[0-9]{1,}': $TMPFILE)"
79+ if [ "$lastpart" = "$NUM" ]; then
80+ endsize="$(parted -ms $DEV unit B print| grep ^/ | cut -d: -f2|sed 's/B$//')"
81+ else
82+ # we are not at the end ! get the start of the next partition
83+ # (minus 1 byte) instead of using the absolute end of the disk
84+ endsize=$(($(parted -ms $DEV unit B print|grep ^$(($num+1)):|\
85+ cut -d: -f2|sed 's/B$//')-1))
86+ fi
87+ echo "endsize: ${endsize}" >/dev/kmsg || true
88+ endsize_percent=$(($endsize*$writable_percent/100))
89+ echo "endsize percent: ${endsize_percent}" >/dev/kmsg || true
90+ echo "$endsize_percent"B
91+}
92+
93+do_gpt()
94+{
95+ DEV=$1
96+ # create new empty GPT
97+ parted -s $DEV mklabel gpt
98+ oIFS=$IFS
99+ IFS=:
100+ # re-create all partitions from backup table
101+ grep -E ^'[0-9]{1,}': $TMPFILE|while read -r num start end size type name flags; do
102+ if [ "$name" = "writable" ]; then
103+ endsize=$(get_end $num)
104+ parted -s $DEV mkpart \"$name\" \"$type\" $start $endsize
105+ else
106+ flags="$(echo $flags|sed -e 's/[,;]//g' -e 's/ /:/')"
107+ parted -s $DEV mkpart \"$name\" \"$type\" $start $end
108+ if [ -n "$flags" ]; then
109+ for flag in ${flags}; do
110+ parted -s $DEV set $num $flag on
111+ done
112+ fi
113+ fi
114+ done
115+ IFS=$oIFS
116+ parted -ms $DEV unit B print >/run/initramfs/new-gpt-table.txt 2>/dev/null
117+}
118+
119+do_mbr()
120+{
121+ DEV=$1
122+ PART=$2
123+ endsize=$(get_end $PART)
124+ parted -s $DEV resizepart $PART $endsize
125+}
126+
127+
128+echo "initrd: resize ${writable_part} to ${writable_percent}% of the disk" >/dev/kmsg || true
129+echo "initrd: see ${LOGFILE} for details" >/dev/kmsg || true
130+
131+# check the filesystem before attempting re-size
132+e2fsck -fy $writable_part >>$LOGFILE 2>&1
133+# shrink the filesystem to the minimum
134+resize2fs -M $writable_part >>$LOGFILE 2>&1
135+
136+# back up the original partition table for later use or debugging
137+parted -ms $device unit B print >$TMPFILE 2>/dev/null
138+table="$(parted -ms $device print| grep ^/| cut -d: -f6)"
139+case $table in
140+ gpt)
141+ # do_gpt needs the device name
142+ do_gpt $device >>$LOGFILE 2>&1
143+ ;;
144+ mbr|msdos)
145+ # do_mbr needs the device node and partition number
146+ do_mbr $device $partition >>$LOGFILE 2>&1
147+ resizeopts="-f"
148+ ;;
149+ *)
150+ echo "unknown partition table type, not resizing" >>$LOGFILE
151+ exit 0
152+ ;;
153+esac
154+# make sure we re read the partition table in any case
155+blockdev --rereadpt $device >>$LOGFILE 2>&1
156+# check the filesystem before attempting re-size
157+e2fsck -fy $writable_part >>$LOGFILE 2>&1
158+# resize the filesystem to full size of the partition
159+resize2fs $resizeopts $writable_part >>$LOGFILE 2>&1
160
161=== modified file '_integration-tests/tests/initramfs_test.go'
162--- _integration-tests/tests/initramfs_test.go 2015-09-10 17:46:42 +0000
163+++ _integration-tests/tests/initramfs_test.go 2015-09-10 17:46:43 +0000
164@@ -21,10 +21,12 @@
165
166 import (
167 "os/exec"
168+ "path"
169 "strconv"
170 "strings"
171
172 "launchpad.net/snappy/_integration-tests/testutils/common"
173+ "launchpad.net/snappy/_integration-tests/testutils/partition"
174
175 "gopkg.in/check.v1"
176 )
177@@ -35,14 +37,52 @@
178 common.SnappySuite
179 }
180
181-func (s *initRAMFSSuite) TestFreeSpace(c *check.C) {
182- cmd := exec.Command("sh", "_integration-tests/tests/get_unpartitioned_space")
183+func getFreeSpacePercent(c *check.C) float64 {
184+ cmd := exec.Command("sh", "_integration-tests/scripts/get_unpartitioned_space")
185 free, err := cmd.Output()
186 c.Assert(err, check.IsNil, check.Commentf("Error running the script to get the free space: %s", err))
187 freePercent := strings.TrimRight(strings.TrimSpace(string(free)), "%")
188 freePercentFloat, err := strconv.ParseFloat(freePercent, 32)
189 c.Assert(err, check.IsNil,
190 check.Commentf("Error converting the free space percentage to float: %s", err))
191- c.Assert(freePercentFloat < 10, check.Equals, true,
192- check.Commentf("The free space at the end of the disk is greater than 10%"))
193+ return freePercentFloat
194+}
195+
196+func getCurrentBootDir(c *check.C) string {
197+ system, err := partition.BootSystem()
198+ c.Assert(err, check.IsNil, check.Commentf("Error getting the boot system: %s", err))
199+ bootDir := partition.BootDir(system)
200+ current, err := partition.CurrentPartition()
201+ c.Assert(err, check.IsNil, check.Commentf("Error getting the current partition: %s", err))
202+ return path.Join(bootDir, current)
203+}
204+
205+func (s *initRAMFSSuite) TestFreeSpaceWithoutResize(c *check.C) {
206+ writablePercent := "95"
207+ if common.BeforeReboot() {
208+ bootDir := getCurrentBootDir(c)
209+ common.ExecCommand(
210+ c, "sh", "-x", "_integration-tests/scripts/install-test-initramfs", bootDir, writablePercent)
211+ common.Reboot(c)
212+ } else if common.AfterReboot(c) {
213+ common.RemoveRebootMark(c)
214+ freeSpace := getFreeSpacePercent(c)
215+ c.Assert(freeSpace, check.Equals, float64(5),
216+ check.Commentf("The writable partition was resized"))
217+ }
218+}
219+
220+func (s *initRAMFSSuite) TestFreeSpaceWithResize(c *check.C) {
221+ if common.BeforeReboot() {
222+ bootDir := getCurrentBootDir(c)
223+ writablePercent := "85"
224+ common.ExecCommand(
225+ c, "sh", "-x", "_integration-tests/scripts/install-test-initramfs", bootDir, writablePercent)
226+ common.Reboot(c)
227+ } else if common.AfterReboot(c) {
228+ common.RemoveRebootMark(c)
229+ freeSpace := getFreeSpacePercent(c)
230+ c.Assert(freeSpace < 10, check.Equals, true,
231+ check.Commentf("The writable partition was not resized"))
232+ }
233 }

Subscribers

People subscribed via source and target branches