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

Proposed by Leo Arias
Status: Merged
Approved by: Leo Arias
Approved revision: 682
Merged at revision: 787
Proposed branch: lp:~elopio/snappy/resize_test
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Prerequisite: lp:~snappy-dev/snappy/current_partition
Diff against target: 286 lines (+215/-7)
5 files modified
_integration-tests/data/tpl/control (+1/-1)
_integration-tests/scripts/install-test-initramfs (+25/-0)
_integration-tests/scripts/resize-writable-test (+123/-0)
_integration-tests/tests/initramfs_test.go (+63/-4)
_integration-tests/testutils/common/common.go (+3/-2)
To merge this branch: bzr merge lp:~elopio/snappy/resize_test
Reviewer Review Type Date Requested Status
Leo Arias (community) Approve
Federico Gimenez (community) Approve
Oliver Grawert Pending
Review via email: mp+270892@code.launchpad.net

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

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.
Revision history for this message
Oliver Grawert (ogra) wrote : Posted in a previous version of this proposal

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 : Posted in a previous version of this proposal

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

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

once the prerequisite lands, this is ready to land too.

Revision history for this message
Federico Gimenez (fgimenez) wrote :

LGTM, and has been heavily tested during the past few days :)

I'm getting this in bbb 15.04/edge r155

** Resuming initRAMFSSuite.TestFreeSpaceWithoutResize after reboot
PASS: <autogenerated>:27: initRAMFSSuite.SetUpTest 0.008s

/home/fgimenez/workspace/snappy/1504_validation/_integration-tests/tests/initramfs_test.go:71:
...open /home/fgimenez/workspace/snappy/1504_validation/_integration-tests/tests/initramfs_test.go: no such file or directory
... obtained float64 = 0.12999999523162842
... expected float64 = 5
... The writable partition was resized

Is this expected?

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

No, there's a problem in here.
When we try to resize on mbr, it prints a warning and asks for confirmation.
I don't understand how it worked before. On the morning I ran in bbb with rolling edge and it worked. Now it doesn't work anymore.

review: Needs Fixing
lp:~elopio/snappy/resize_test updated
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.

Revision history for this message
Federico Gimenez (fgimenez) wrote :

It's failing because of the initramfs.img filename in SetUpTest and TearDownTest. Fixing that I'm still getting errors in TestFreeSpaceWithoutResize for amd64 rolling edge #191 [1] and bbb rolling edge #187 [2]

[1] http://paste.ubuntu.com/12637246/
[2] http://paste.ubuntu.com/12637539/

review: Needs Fixing
lp:~elopio/snappy/resize_test updated
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.

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

Of course, I try to be extra smart at the end and break it all. The problem was that the setup and tear down were reverting the initrd changes.

Please, one more review. And sorry about that.

Revision history for this message
Federico Gimenez (fgimenez) wrote :

It works fine on kvm, on bbb I'm still getting one error at the end [1]. This is in rolling/edge #193, does it work for you?

Thanks,

[1] http://paste.ubuntu.com/12713787/

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

@Federico, I've been running this on friday and today and can't reproduce. Latest run was rolling/edge #203.

command1 PASS

Please, if you can still reproduce it, connect through the serial port and record the serial log with ctrl+a H.

Revision history for this message
Federico Gimenez (fgimenez) wrote :

Thanks Leo, it's passing on rolling/edge #203

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

I'll merge it before you break it :p

Thanks a lot Federico.

review: Approve

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-10-07 22:21:11 +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-10-07 22:21:11 +0000
16@@ -0,0 +1,25 @@
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+# We need sfdisk to shrink the partition in mbr.
32+cp /sbin/sfdisk sbin
33+# Libs required by sfdisk.
34+cp /lib/*/libfdisk.so.1 lib
35+cp /lib/*/libsmartcols.so.1 lib
36+cp $tree_dir/_integration-tests/scripts/resize-writable-test scripts/local-premount/
37+sed -i "s/writable_percent=.*/writable_percent=${writable_percent}/" scripts/local-premount/resize-writable-test
38+sed -i '1i /scripts/local-premount/resize-writable-test "$@"\n[ -e /conf/param.conf ] && . /conf/param.conf' scripts/local-premount/ORDER
39+find .|cpio -o -H newc|xz -c -7 --check=crc32 > initrd.img
40+sudo rm $current_boot_dir/initrd.img
41+sudo mv initrd.img $current_boot_dir
42
43=== renamed file '_integration-tests/reboot-wrapper' => '_integration-tests/scripts/reboot-wrapper'
44=== added file '_integration-tests/scripts/resize-writable-test'
45--- _integration-tests/scripts/resize-writable-test 1970-01-01 00:00:00 +0000
46+++ _integration-tests/scripts/resize-writable-test 2015-10-07 22:21:11 +0000
47@@ -0,0 +1,123 @@
48+#! /bin/sh -ex
49+# initramfs local-premount script to resize writable for testing
50+
51+PREREQ=""
52+
53+# Output pre-requisites
54+prereqs()
55+{
56+ echo "$PREREQ"
57+}
58+
59+case "$1" in
60+ prereqs)
61+ prereqs
62+ exit 0
63+ ;;
64+esac
65+
66+TMPFILE="/run/initramfs/old-table-test.txt"
67+LOGFILE="/run/initramfs/resize-writable-test.log"
68+
69+writable_percent=100
70+
71+wait-for-root "LABEL=writable" "${ROOTDELAY:-180}" >/dev/null || true
72+
73+writable_part="$(findfs LABEL=writable)"
74+
75+syspath="$(dirname $(realpath /sys/class/block/$(basename $writable_part)))"
76+device="$(realpath /dev/block/$(cat $syspath/dev))"
77+partition=$(cat $syspath/$(basename $writable_part)/partition)
78+
79+device_size="$(($(cat $syspath/size)/2))"
80+
81+get_end()
82+{
83+ NUM=$1
84+ lastpart="$(grep -cE ^'[0-9]{1,}': $TMPFILE)"
85+ if [ "$lastpart" = "$NUM" ]; then
86+ endsize="$(parted -ms $DEV unit B print| grep ^/ | cut -d: -f2|sed 's/B$//')"
87+ else
88+ # we are not at the end ! get the start of the next partition
89+ # (minus 1 byte) instead of using the absolute end of the disk
90+ endsize=$(($(parted -ms $DEV unit B print|grep ^$(($num+1)):|\
91+ cut -d: -f2|sed 's/B$//')-1))
92+ fi
93+ echo "endsize: ${endsize}" >/dev/kmsg || true
94+ endsize_percent=$(($endsize*$writable_percent/100))
95+ echo "endsize percent: ${endsize_percent}" >/dev/kmsg || true
96+ echo "$endsize_percent"
97+}
98+
99+do_gpt()
100+{
101+ DEV=$1
102+ # create new empty GPT
103+ parted -s $DEV mklabel gpt
104+ oIFS=$IFS
105+ IFS=:
106+ # re-create all partitions from backup table
107+ grep -E ^'[0-9]{1,}': $TMPFILE|while read -r num start end size type name flags; do
108+ if [ "$name" = "writable" ]; then
109+ endsize=$(get_end $num)B
110+ parted -s $DEV mkpart \"$name\" \"$type\" $start $endsize
111+ else
112+ flags="$(echo $flags|sed -e 's/[,;]//g' -e 's/ /:/')"
113+ parted -s $DEV mkpart \"$name\" \"$type\" $start $end
114+ if [ -n "$flags" ]; then
115+ for flag in ${flags}; do
116+ parted -s $DEV set $num $flag on
117+ done
118+ fi
119+ fi
120+ done
121+ IFS=$oIFS
122+ parted -ms $DEV unit B print >/run/initramfs/new-gpt-table.txt 2>/dev/null
123+}
124+
125+do_mbr()
126+{
127+ DEV=$1
128+ PART=$2
129+ endsize=$(get_end $PART)
130+ # We can't just resize with parted because it prints a warning when you try to shrink the
131+ # partition.
132+ part_start="$(parted -ms $DEV unit B print | grep ^$PART: | cut -d: -f2 | sed 's/B$//')"
133+ part_end=$(($endsize-$part_start))
134+ part_end_sectors=$(($part_end/512))
135+ echo ", ${part_end_sectors}" | sfdisk -f -N$PART $DEV
136+}
137+
138+
139+echo "initrd: resize ${writable_part} to ${writable_percent}% of the disk" >/dev/kmsg || true
140+echo "initrd: see ${LOGFILE} for details" >/dev/kmsg || true
141+
142+# check the filesystem before attempting re-size
143+e2fsck -fy $writable_part >>$LOGFILE 2>&1
144+# shrink the filesystem to the minimum
145+resize2fs -M $writable_part >>$LOGFILE 2>&1
146+
147+# back up the original partition table for later use or debugging
148+parted -ms $device unit B print >$TMPFILE 2>/dev/null
149+table="$(parted -ms $device print| grep ^/| cut -d: -f6)"
150+case $table in
151+ gpt)
152+ # do_gpt needs the device name
153+ do_gpt $device >>$LOGFILE 2>&1
154+ ;;
155+ mbr|msdos)
156+ # do_mbr needs the device node and partition number
157+ do_mbr $device $partition >>$LOGFILE 2>&1
158+ resizeopts="-f"
159+ ;;
160+ *)
161+ echo "unknown partition table type, not resizing" >>$LOGFILE
162+ exit 0
163+ ;;
164+esac
165+# make sure we re read the partition table in any case
166+blockdev --rereadpt $device >>$LOGFILE 2>&1
167+# check the filesystem before attempting re-size
168+e2fsck -fy $writable_part >>$LOGFILE 2>&1
169+# resize the filesystem to full size of the partition
170+resize2fs $resizeopts $writable_part >>$LOGFILE 2>&1
171
172=== modified file '_integration-tests/tests/initramfs_test.go'
173--- _integration-tests/tests/initramfs_test.go 2015-09-08 18:25:45 +0000
174+++ _integration-tests/tests/initramfs_test.go 2015-10-07 22:21:11 +0000
175@@ -20,11 +20,15 @@
176 package tests
177
178 import (
179+ "os"
180 "os/exec"
181+ "path"
182 "strconv"
183 "strings"
184
185+ "launchpad.net/snappy/_integration-tests/testutils/cli"
186 "launchpad.net/snappy/_integration-tests/testutils/common"
187+ "launchpad.net/snappy/_integration-tests/testutils/partition"
188
189 "gopkg.in/check.v1"
190 )
191@@ -35,14 +39,69 @@
192 common.SnappySuite
193 }
194
195-func (s *initRAMFSSuite) TestFreeSpace(c *check.C) {
196- cmd := exec.Command("sh", "_integration-tests/tests/get_unpartitioned_space")
197+func getFreeSpacePercent(c *check.C) float64 {
198+ cmd := exec.Command("sh", "_integration-tests/scripts/get_unpartitioned_space")
199 free, err := cmd.Output()
200 c.Assert(err, check.IsNil, check.Commentf("Error running the script to get the free space: %s", err))
201 freePercent := strings.TrimRight(strings.TrimSpace(string(free)), "%")
202 freePercentFloat, err := strconv.ParseFloat(freePercent, 32)
203 c.Assert(err, check.IsNil,
204 check.Commentf("Error converting the free space percentage to float: %s", err))
205- c.Assert(freePercentFloat < 10, check.Equals, true,
206- check.Commentf("The free space at the end of the disk is greater than 10%"))
207+ return freePercentFloat
208+}
209+
210+func getCurrentBootDir(c *check.C) string {
211+ system, err := partition.BootSystem()
212+ c.Assert(err, check.IsNil, check.Commentf("Error getting the boot system: %s", err))
213+ bootDir := partition.BootDir(system)
214+ current, err := partition.CurrentPartition()
215+ c.Assert(err, check.IsNil, check.Commentf("Error getting the current partition: %s", err))
216+ return path.Join(bootDir, current)
217+}
218+
219+func (s *initRAMFSSuite) SetUpTest(c *check.C) {
220+ s.SnappySuite.SetUpTest(c)
221+ if common.BeforeReboot() {
222+ bootDir := getCurrentBootDir(c)
223+ cli.ExecCommand(c, "cp", path.Join(bootDir, "initrd.img"), os.Getenv("ADT_ARTIFACTS"))
224+ }
225+}
226+
227+func (s *initRAMFSSuite) TearDownTest(c *check.C) {
228+ s.SnappySuite.TearDownTest(c)
229+ if !common.IsInRebootProcess() {
230+ bootDir := getCurrentBootDir(c)
231+ cli.ExecCommand(
232+ c, "sudo", "mv", path.Join(os.Getenv("ADT_ARTIFACTS"), "initrd.img"), bootDir)
233+ }
234+}
235+
236+func (s *initRAMFSSuite) TestFreeSpaceWithoutResize(c *check.C) {
237+ writablePercent := "95"
238+ if common.BeforeReboot() {
239+ bootDir := getCurrentBootDir(c)
240+ cli.ExecCommand(
241+ c, "sh", "-x", "_integration-tests/scripts/install-test-initramfs", bootDir, writablePercent)
242+ common.Reboot(c)
243+ } else if common.AfterReboot(c) {
244+ common.RemoveRebootMark(c)
245+ freeSpace := getFreeSpacePercent(c)
246+ c.Assert(freeSpace, check.Equals, float64(5),
247+ check.Commentf("The writable partition was resized"))
248+ }
249+}
250+
251+func (s *initRAMFSSuite) TestFreeSpaceWithResize(c *check.C) {
252+ if common.BeforeReboot() {
253+ bootDir := getCurrentBootDir(c)
254+ writablePercent := "85"
255+ cli.ExecCommand(
256+ c, "sh", "-x", "_integration-tests/scripts/install-test-initramfs", bootDir, writablePercent)
257+ common.Reboot(c)
258+ } else if common.AfterReboot(c) {
259+ common.RemoveRebootMark(c)
260+ freeSpace := getFreeSpacePercent(c)
261+ c.Assert(freeSpace < 10, check.Equals, true,
262+ check.Commentf("The writable partition was not resized"))
263+ }
264 }
265
266=== modified file '_integration-tests/testutils/common/common.go'
267--- _integration-tests/testutils/common/common.go 2015-10-01 09:37:04 +0000
268+++ _integration-tests/testutils/common/common.go 2015-10-07 22:21:11 +0000
269@@ -62,7 +62,7 @@
270 "_integration-tests/data/output/testconfig.json")
271 c.Assert(err, check.IsNil, check.Commentf("Error reading config: %v", err))
272
273- if !isInRebootProcess() {
274+ if !IsInRebootProcess() {
275 if Cfg.Update || Cfg.Rollback {
276 switchSystemImageConf(c, Cfg.TargetRelease, Cfg.TargetChannel, "0")
277 // Always use the installed snappy because we are updating from an old
278@@ -275,7 +275,8 @@
279 return os.Getenv("ADT_REBOOT_MARK") == mark
280 }
281
282-func isInRebootProcess() bool {
283+// IsInRebootProcess returns True if the suite needs to execute a reboot or has just rebooted.
284+func IsInRebootProcess() bool {
285 return !CheckRebootMark("") || NeedsReboot()
286 }
287

Subscribers

People subscribed via source and target branches