github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/stage0/gc_test.go (about)

     1  // Copyright 2015 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package stage0
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"testing"
    21  )
    22  
    23  var mountinfo = `39 21 0:36 / /tmp rw shared:91 -
    24  71 21 0:39 / /var/lib/rkt rw,relatime shared:26 -
    25  69 21 0:19 /home /home rw,relatime shared:27 -
    26  70 20 0:41 / /run/user/1000 rw,nosuid,nodev,relatime shared:28 -
    27  109 70 0:43 / /run/user/1000/gvfs rw,nosuid,nodev,relatime shared:61 -
    28  126 71 0:45 / /prefix/stage1/rootfs rw,relatime master:1 -
    29  131 126 0:46 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs rw,relatime shared:1 master:2 -
    30  193 131 0:19 /nixos /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs ro,relatime -
    31  195 193 0:17 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/sys rw,relatime -
    32  196 195 0:26 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/sys/fs/cgroup rw,nosuid,nodev,noexec -
    33  197 196 0:27 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/sys/fs/cgroup/systemd rw,nosuid,nodev,noexec,relatime -
    34  206 193 0:6 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/dev rw,relatime -
    35  207 206 0:23 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/dev/shm rw,relatime -
    36  208 206 0:14 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/dev/pts rw,nosuid,noexec,relatime -
    37  209 206 0:35 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/dev/hugepages rw,relatime -
    38  210 206 0:16 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/dev/mqueue rw,relatime -
    39  211 193 0:18 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/run rw,relatime -
    40  213 211 0:41 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/run/user/1000 rw,nosuid,nodev,relatime -
    41  214 213 0:43 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/run/user/1000/gvfs rw,nosuid,nodev,relatime -
    42  215 193 0:19 /nixos/nix/store /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/nix/store ro,relatime -
    43  217 193 0:36 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/tmp rw -
    44  218 217 0:42 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/tmp/c rw,relatime -
    45  219 218 0:44 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/tmp/c/b-b rw,relatime -
    46  220 217 0:17 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/tmp/sys rw,relatime -
    47  221 220 0:26 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/tmp/sys/fs/cgroup rw,nosuid,nodev,noexec -
    48  222 221 0:27 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/tmp/sys/fs/cgroup/systemd rw,nosuid,nodev,noexec,relatime -
    49  223 221 0:28 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/tmp/sys/fs/cgroup/net_cls rw,nosuid,nodev,noexec,relatime -
    50  224 221 0:29 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/tmp/sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime -
    51  225 221 0:30 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/tmp/sys/fs/cgroup/cpu,cpuacct rw,nosuid,nodev,noexec,relatime -
    52  226 221 0:31 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/tmp/sys/fs/cgroup/freezer rw,nosuid,nodev,noexec,relatime -
    53  227 221 0:32 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/tmp/sys/fs/cgroup/memory rw,nosuid,nodev,noexec,relatime -
    54  228 221 0:33 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/tmp/sys/fs/cgroup/devices rw,nosuid,nodev,noexec,relatime -
    55  231 193 0:38 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/var/lib/cni rw,relatime -
    56  232 193 0:39 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/var/lib/rkt rw,relatime -
    57  233 232 0:45 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs rw,relatime -
    58  234 233 0:46 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs rw,relatime -
    59  235 234 0:6 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/dev rw,relatime -
    60  236 235 0:23 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/dev/shm rw,relatime -
    61  237 235 0:14 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/dev/pts rw,nosuid,noexec,relatime -
    62  238 235 0:35 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/dev/hugepages rw,relatime -
    63  239 235 0:16 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/dev/mqueue rw,relatime -
    64  240 234 0:5 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/proc rw,relatime -
    65  241 234 0:17 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/sys rw,relatime -
    66  242 241 0:26 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/sys/fs/cgroup rw,nosuid,nodev,noexec -
    67  243 242 0:27 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/sys/fs/cgroup/systemd rw,nosuid,nodev,noexec,relatime -
    68  244 242 0:28 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/sys/fs/cgroup/net_cls rw,nosuid,nodev,noexec,relatime -
    69  245 242 0:29 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime -
    70  246 242 0:30 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/sys/fs/cgroup/cpu,cpuacct rw,nosuid,nodev,noexec,relatime -
    71  247 242 0:31 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/sys/fs/cgroup/freezer rw,nosuid,nodev,noexec,relatime -
    72  248 242 0:32 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/sys/fs/cgroup/memory rw,nosuid,nodev,noexec,relatime -
    73  249 242 0:33 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/sys/fs/cgroup/devices rw,nosuid,nodev,noexec,relatime -
    74  250 242 0:34 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/sys/fs/cgroup/blkio rw,nosuid,nodev,noexec,relatime -
    75  251 242 0:29 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/sys/fs/cgroup/cros rw,relatime -
    76  252 234 0:47 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/tmp rw,relatime -
    77  253 234 0:19 /nixos/etc/resolv.conf /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/prefix/stage1/rootfs/opt/stage2/busybox/rootfs/etc/resolv.conf ro,relatime -
    78  254 193 0:19 /home /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/home rw,relatime -
    79  255 193 179:1 / /prefix/stage1/rootfs/opt/stage2/busybox/rootfs/rootfs/mnt/unknown ro,relatime -
    80  `
    81  
    82  func TestMountOrdering(t *testing.T) {
    83  	tests := []struct {
    84  		prefix     string
    85  		ids        int
    86  		shouldPass bool
    87  		mi         string
    88  		remounts   int
    89  	}{
    90  		{
    91  			prefix:     "/prefix",
    92  			ids:        52,
    93  			shouldPass: true,
    94  			mi:         mountinfo,
    95  			remounts:   2,
    96  		},
    97  	}
    98  
    99  	for i, tt := range tests {
   100  		mi := strings.NewReader(tt.mi)
   101  		mnts, err := getMountsForPrefix(tt.prefix, mi)
   102  		if err != nil {
   103  			t.Errorf("problems finding mount points: %v", err)
   104  		}
   105  
   106  		requestedRemounts := 0
   107  		for i := len(mnts) - 1; i >= 0; i -= 1 {
   108  			mnt := mnts[i]
   109  			if needsRemountPrivate(mnt) {
   110  				t.Logf("remounting: %+v", mnt)
   111  				requestedRemounts += 1
   112  			}
   113  		}
   114  
   115  		if requestedRemounts != tt.remounts {
   116  			t.Fatalf("test  %d: didn't find the expected number of remounts. found %d but wanted %d.", i, requestedRemounts, tt.remounts)
   117  		}
   118  
   119  		if len(mnts) != tt.ids {
   120  			t.Fatalf("test  %d: didn't find the expected number of mounts. found %d but wanted %d.", i, len(mnts), tt.ids)
   121  		}
   122  
   123  		for _, mntCur := range mnts {
   124  			t.Logf("Unmounting %d: %q", mntCur.id, mntCur.mountPoint)
   125  		}
   126  
   127  		if err := safeOrder(mnts); err != nil {
   128  			t.Fatal(err)
   129  		}
   130  	}
   131  }
   132  
   133  // safeOrder checks for transitivity and (un)mount order sanity
   134  func safeOrder(m mounts) error {
   135  	for i, mntCur := range m {
   136  		for j, mntNext := range m[(i + 1):] {
   137  			j := i + j
   138  			if !m.Less(i, j) {
   139  				return fmt.Errorf("Transitivity check failed for %d(%d) and %d(%d)", i, mntCur.id, j, mntNext.id)
   140  			}
   141  			if strings.HasPrefix(mntNext.mountPoint, mntCur.mountPoint) {
   142  				return fmt.Errorf("Must not unmount \n%q before\n%q.", mntCur.mountPoint, mntNext.mountPoint)
   143  			}
   144  		}
   145  	}
   146  	return nil
   147  }