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
=== modified file '_integration-tests/data/tpl/control'
--- _integration-tests/data/tpl/control 2015-07-22 16:36:14 +0000
+++ _integration-tests/data/tpl/control 2015-10-07 22:21:11 +0000
@@ -1,4 +1,4 @@
1{{ $filter := .Filter }}1{{ $filter := .Filter }}
2{{ $test := .Test }}2{{ $test := .Test }}
3Test-Command: ./_integration-tests/reboot-wrapper {{ $test }} {{ if $filter }}-gocheck.f {{ $filter }}{{ end }}3Test-Command: ./_integration-tests/scripts/reboot-wrapper {{ $test }} {{ if $filter }}-gocheck.f {{ $filter }}{{ end }}
4Restrictions: allow-stderr4Restrictions: allow-stderr
55
=== added directory '_integration-tests/scripts'
=== renamed file '_integration-tests/tests/get_unpartitioned_space' => '_integration-tests/scripts/get_unpartitioned_space'
=== added file '_integration-tests/scripts/install-test-initramfs'
--- _integration-tests/scripts/install-test-initramfs 1970-01-01 00:00:00 +0000
+++ _integration-tests/scripts/install-test-initramfs 2015-10-07 22:21:11 +0000
@@ -0,0 +1,25 @@
1#! /bin/sh -e
2# Prepare the initramfs image for the tests.
3
4current_boot_dir=$1
5writable_percent=$2
6tree_dir=$(pwd)
7initrd_unpack_dir=$ADT_ARTIFACTS/initrd
8
9cp ${current_boot_dir}/initrd.img /tmp/initrd.xz
10cd /tmp/
11unxz /tmp/initrd.xz
12mkdir $initrd_unpack_dir || true
13cd $initrd_unpack_dir
14cpio -id < /tmp/initrd
15# We need sfdisk to shrink the partition in mbr.
16cp /sbin/sfdisk sbin
17# Libs required by sfdisk.
18cp /lib/*/libfdisk.so.1 lib
19cp /lib/*/libsmartcols.so.1 lib
20cp $tree_dir/_integration-tests/scripts/resize-writable-test scripts/local-premount/
21sed -i "s/writable_percent=.*/writable_percent=${writable_percent}/" scripts/local-premount/resize-writable-test
22sed -i '1i /scripts/local-premount/resize-writable-test "$@"\n[ -e /conf/param.conf ] && . /conf/param.conf' scripts/local-premount/ORDER
23find .|cpio -o -H newc|xz -c -7 --check=crc32 > initrd.img
24sudo rm $current_boot_dir/initrd.img
25sudo mv initrd.img $current_boot_dir
026
=== renamed file '_integration-tests/reboot-wrapper' => '_integration-tests/scripts/reboot-wrapper'
=== added file '_integration-tests/scripts/resize-writable-test'
--- _integration-tests/scripts/resize-writable-test 1970-01-01 00:00:00 +0000
+++ _integration-tests/scripts/resize-writable-test 2015-10-07 22:21:11 +0000
@@ -0,0 +1,123 @@
1#! /bin/sh -ex
2# initramfs local-premount script to resize writable for testing
3
4PREREQ=""
5
6# Output pre-requisites
7prereqs()
8{
9 echo "$PREREQ"
10}
11
12case "$1" in
13 prereqs)
14 prereqs
15 exit 0
16 ;;
17esac
18
19TMPFILE="/run/initramfs/old-table-test.txt"
20LOGFILE="/run/initramfs/resize-writable-test.log"
21
22writable_percent=100
23
24wait-for-root "LABEL=writable" "${ROOTDELAY:-180}" >/dev/null || true
25
26writable_part="$(findfs LABEL=writable)"
27
28syspath="$(dirname $(realpath /sys/class/block/$(basename $writable_part)))"
29device="$(realpath /dev/block/$(cat $syspath/dev))"
30partition=$(cat $syspath/$(basename $writable_part)/partition)
31
32device_size="$(($(cat $syspath/size)/2))"
33
34get_end()
35{
36 NUM=$1
37 lastpart="$(grep -cE ^'[0-9]{1,}': $TMPFILE)"
38 if [ "$lastpart" = "$NUM" ]; then
39 endsize="$(parted -ms $DEV unit B print| grep ^/ | cut -d: -f2|sed 's/B$//')"
40 else
41 # we are not at the end ! get the start of the next partition
42 # (minus 1 byte) instead of using the absolute end of the disk
43 endsize=$(($(parted -ms $DEV unit B print|grep ^$(($num+1)):|\
44 cut -d: -f2|sed 's/B$//')-1))
45 fi
46 echo "endsize: ${endsize}" >/dev/kmsg || true
47 endsize_percent=$(($endsize*$writable_percent/100))
48 echo "endsize percent: ${endsize_percent}" >/dev/kmsg || true
49 echo "$endsize_percent"
50}
51
52do_gpt()
53{
54 DEV=$1
55 # create new empty GPT
56 parted -s $DEV mklabel gpt
57 oIFS=$IFS
58 IFS=:
59 # re-create all partitions from backup table
60 grep -E ^'[0-9]{1,}': $TMPFILE|while read -r num start end size type name flags; do
61 if [ "$name" = "writable" ]; then
62 endsize=$(get_end $num)B
63 parted -s $DEV mkpart \"$name\" \"$type\" $start $endsize
64 else
65 flags="$(echo $flags|sed -e 's/[,;]//g' -e 's/ /:/')"
66 parted -s $DEV mkpart \"$name\" \"$type\" $start $end
67 if [ -n "$flags" ]; then
68 for flag in ${flags}; do
69 parted -s $DEV set $num $flag on
70 done
71 fi
72 fi
73 done
74 IFS=$oIFS
75 parted -ms $DEV unit B print >/run/initramfs/new-gpt-table.txt 2>/dev/null
76}
77
78do_mbr()
79{
80 DEV=$1
81 PART=$2
82 endsize=$(get_end $PART)
83 # We can't just resize with parted because it prints a warning when you try to shrink the
84 # partition.
85 part_start="$(parted -ms $DEV unit B print | grep ^$PART: | cut -d: -f2 | sed 's/B$//')"
86 part_end=$(($endsize-$part_start))
87 part_end_sectors=$(($part_end/512))
88 echo ", ${part_end_sectors}" | sfdisk -f -N$PART $DEV
89}
90
91
92echo "initrd: resize ${writable_part} to ${writable_percent}% of the disk" >/dev/kmsg || true
93echo "initrd: see ${LOGFILE} for details" >/dev/kmsg || true
94
95# check the filesystem before attempting re-size
96e2fsck -fy $writable_part >>$LOGFILE 2>&1
97# shrink the filesystem to the minimum
98resize2fs -M $writable_part >>$LOGFILE 2>&1
99
100# back up the original partition table for later use or debugging
101parted -ms $device unit B print >$TMPFILE 2>/dev/null
102table="$(parted -ms $device print| grep ^/| cut -d: -f6)"
103case $table in
104 gpt)
105 # do_gpt needs the device name
106 do_gpt $device >>$LOGFILE 2>&1
107 ;;
108 mbr|msdos)
109 # do_mbr needs the device node and partition number
110 do_mbr $device $partition >>$LOGFILE 2>&1
111 resizeopts="-f"
112 ;;
113 *)
114 echo "unknown partition table type, not resizing" >>$LOGFILE
115 exit 0
116 ;;
117esac
118# make sure we re read the partition table in any case
119blockdev --rereadpt $device >>$LOGFILE 2>&1
120# check the filesystem before attempting re-size
121e2fsck -fy $writable_part >>$LOGFILE 2>&1
122# resize the filesystem to full size of the partition
123resize2fs $resizeopts $writable_part >>$LOGFILE 2>&1
0124
=== modified file '_integration-tests/tests/initramfs_test.go'
--- _integration-tests/tests/initramfs_test.go 2015-09-08 18:25:45 +0000
+++ _integration-tests/tests/initramfs_test.go 2015-10-07 22:21:11 +0000
@@ -20,11 +20,15 @@
20package tests20package tests
2121
22import (22import (
23 "os"
23 "os/exec"24 "os/exec"
25 "path"
24 "strconv"26 "strconv"
25 "strings"27 "strings"
2628
29 "launchpad.net/snappy/_integration-tests/testutils/cli"
27 "launchpad.net/snappy/_integration-tests/testutils/common"30 "launchpad.net/snappy/_integration-tests/testutils/common"
31 "launchpad.net/snappy/_integration-tests/testutils/partition"
2832
29 "gopkg.in/check.v1"33 "gopkg.in/check.v1"
30)34)
@@ -35,14 +39,69 @@
35 common.SnappySuite39 common.SnappySuite
36}40}
3741
38func (s *initRAMFSSuite) TestFreeSpace(c *check.C) {42func getFreeSpacePercent(c *check.C) float64 {
39 cmd := exec.Command("sh", "_integration-tests/tests/get_unpartitioned_space")43 cmd := exec.Command("sh", "_integration-tests/scripts/get_unpartitioned_space")
40 free, err := cmd.Output()44 free, err := cmd.Output()
41 c.Assert(err, check.IsNil, check.Commentf("Error running the script to get the free space: %s", err))45 c.Assert(err, check.IsNil, check.Commentf("Error running the script to get the free space: %s", err))
42 freePercent := strings.TrimRight(strings.TrimSpace(string(free)), "%")46 freePercent := strings.TrimRight(strings.TrimSpace(string(free)), "%")
43 freePercentFloat, err := strconv.ParseFloat(freePercent, 32)47 freePercentFloat, err := strconv.ParseFloat(freePercent, 32)
44 c.Assert(err, check.IsNil,48 c.Assert(err, check.IsNil,
45 check.Commentf("Error converting the free space percentage to float: %s", err))49 check.Commentf("Error converting the free space percentage to float: %s", err))
46 c.Assert(freePercentFloat < 10, check.Equals, true,50 return freePercentFloat
47 check.Commentf("The free space at the end of the disk is greater than 10%"))51}
52
53func getCurrentBootDir(c *check.C) string {
54 system, err := partition.BootSystem()
55 c.Assert(err, check.IsNil, check.Commentf("Error getting the boot system: %s", err))
56 bootDir := partition.BootDir(system)
57 current, err := partition.CurrentPartition()
58 c.Assert(err, check.IsNil, check.Commentf("Error getting the current partition: %s", err))
59 return path.Join(bootDir, current)
60}
61
62func (s *initRAMFSSuite) SetUpTest(c *check.C) {
63 s.SnappySuite.SetUpTest(c)
64 if common.BeforeReboot() {
65 bootDir := getCurrentBootDir(c)
66 cli.ExecCommand(c, "cp", path.Join(bootDir, "initrd.img"), os.Getenv("ADT_ARTIFACTS"))
67 }
68}
69
70func (s *initRAMFSSuite) TearDownTest(c *check.C) {
71 s.SnappySuite.TearDownTest(c)
72 if !common.IsInRebootProcess() {
73 bootDir := getCurrentBootDir(c)
74 cli.ExecCommand(
75 c, "sudo", "mv", path.Join(os.Getenv("ADT_ARTIFACTS"), "initrd.img"), bootDir)
76 }
77}
78
79func (s *initRAMFSSuite) TestFreeSpaceWithoutResize(c *check.C) {
80 writablePercent := "95"
81 if common.BeforeReboot() {
82 bootDir := getCurrentBootDir(c)
83 cli.ExecCommand(
84 c, "sh", "-x", "_integration-tests/scripts/install-test-initramfs", bootDir, writablePercent)
85 common.Reboot(c)
86 } else if common.AfterReboot(c) {
87 common.RemoveRebootMark(c)
88 freeSpace := getFreeSpacePercent(c)
89 c.Assert(freeSpace, check.Equals, float64(5),
90 check.Commentf("The writable partition was resized"))
91 }
92}
93
94func (s *initRAMFSSuite) TestFreeSpaceWithResize(c *check.C) {
95 if common.BeforeReboot() {
96 bootDir := getCurrentBootDir(c)
97 writablePercent := "85"
98 cli.ExecCommand(
99 c, "sh", "-x", "_integration-tests/scripts/install-test-initramfs", bootDir, writablePercent)
100 common.Reboot(c)
101 } else if common.AfterReboot(c) {
102 common.RemoveRebootMark(c)
103 freeSpace := getFreeSpacePercent(c)
104 c.Assert(freeSpace < 10, check.Equals, true,
105 check.Commentf("The writable partition was not resized"))
106 }
48}107}
49108
=== modified file '_integration-tests/testutils/common/common.go'
--- _integration-tests/testutils/common/common.go 2015-10-01 09:37:04 +0000
+++ _integration-tests/testutils/common/common.go 2015-10-07 22:21:11 +0000
@@ -62,7 +62,7 @@
62 "_integration-tests/data/output/testconfig.json")62 "_integration-tests/data/output/testconfig.json")
63 c.Assert(err, check.IsNil, check.Commentf("Error reading config: %v", err))63 c.Assert(err, check.IsNil, check.Commentf("Error reading config: %v", err))
6464
65 if !isInRebootProcess() {65 if !IsInRebootProcess() {
66 if Cfg.Update || Cfg.Rollback {66 if Cfg.Update || Cfg.Rollback {
67 switchSystemImageConf(c, Cfg.TargetRelease, Cfg.TargetChannel, "0")67 switchSystemImageConf(c, Cfg.TargetRelease, Cfg.TargetChannel, "0")
68 // Always use the installed snappy because we are updating from an old68 // Always use the installed snappy because we are updating from an old
@@ -275,7 +275,8 @@
275 return os.Getenv("ADT_REBOOT_MARK") == mark275 return os.Getenv("ADT_REBOOT_MARK") == mark
276}276}
277277
278func isInRebootProcess() bool {278// IsInRebootProcess returns True if the suite needs to execute a reboot or has just rebooted.
279func IsInRebootProcess() bool {
279 return !CheckRebootMark("") || NeedsReboot()280 return !CheckRebootMark("") || NeedsReboot()
280}281}
281282

Subscribers

People subscribed via source and target branches