github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/sanity/version_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package sanity_test 21 22 import ( 23 "io/ioutil" 24 "os" 25 "path/filepath" 26 27 . "gopkg.in/check.v1" 28 29 "github.com/snapcore/snapd/dirs" 30 "github.com/snapcore/snapd/osutil" 31 "github.com/snapcore/snapd/release" 32 "github.com/snapcore/snapd/sanity" 33 ) 34 35 type versionSuite struct{} 36 37 var _ = Suite(&versionSuite{}) 38 39 func (s *sanitySuite) TestFreshInstallOfSnapdOnTrusty(c *C) { 40 // Mock an Ubuntu 14.04 system running a 3.13.0 kernel 41 restore := release.MockOnClassic(true) 42 defer restore() 43 restore = release.MockReleaseInfo(&release.OS{ID: "ubuntu", VersionID: "14.04"}) 44 defer restore() 45 restore = osutil.MockKernelVersion("3.13.0-35-generic") 46 defer restore() 47 48 // Check for the given advice. 49 err := sanity.CheckKernelVersion() 50 c.Assert(err, ErrorMatches, "you need to reboot into a 4.4 kernel to start using snapd") 51 } 52 53 func (s *sanitySuite) TestRebootedOnTrusty(c *C) { 54 // Mock an Ubuntu 14.04 system running a 4.4.0 kernel 55 restore := release.MockOnClassic(true) 56 defer restore() 57 restore = release.MockReleaseInfo(&release.OS{ID: "ubuntu", VersionID: "14.04"}) 58 defer restore() 59 restore = osutil.MockKernelVersion("4.4.0-112-generic") 60 defer restore() 61 62 // Check for the given advice. 63 err := sanity.CheckKernelVersion() 64 c.Assert(err, IsNil) 65 } 66 67 func (s *sanitySuite) TestRHEL80OK(c *C) { 68 // Mock an Ubuntu 14.04 system running a 4.4.0 kernel 69 restore := release.MockOnClassic(true) 70 defer restore() 71 restore = release.MockReleaseInfo(&release.OS{ID: "rhel", VersionID: "8.0"}) 72 defer restore() 73 // RHEL 8 beta 74 restore = osutil.MockKernelVersion("4.18.0-32.el8.x86_64") 75 defer restore() 76 77 // Check for the given advice. 78 err := sanity.CheckKernelVersion() 79 c.Assert(err, IsNil) 80 } 81 82 func (s *sanitySuite) TestRHEL7x(c *C) { 83 dir := c.MkDir() 84 dirs.SetRootDir(dir) 85 defer dirs.SetRootDir("/") 86 // mock RHEL 7.6 87 restore := release.MockOnClassic(true) 88 defer restore() 89 // VERSION="7.6 (Maipo)" 90 // ID="rhel" 91 // ID_LIKE="fedora" 92 // VERSION_ID="7.6" 93 restore = release.MockReleaseInfo(&release.OS{ID: "rhel", VersionID: "7.6"}) 94 defer restore() 95 restore = osutil.MockKernelVersion("3.10.0-957.el7.x86_64") 96 defer restore() 97 98 // pretend the kernel knob is not there 99 err := sanity.CheckKernelVersion() 100 c.Assert(err, ErrorMatches, "cannot read the value of fs.may_detach_mounts kernel parameter: .*") 101 102 p := filepath.Join(dir, "/proc/sys/fs/may_detach_mounts") 103 err = os.MkdirAll(filepath.Dir(p), 0755) 104 c.Assert(err, IsNil) 105 106 // the knob is there, but disabled 107 err = ioutil.WriteFile(p, []byte("0\n"), 0644) 108 c.Assert(err, IsNil) 109 110 err = sanity.CheckKernelVersion() 111 c.Assert(err, ErrorMatches, "fs.may_detach_mounts kernel parameter is supported but disabled") 112 113 // actually enabled 114 err = ioutil.WriteFile(p, []byte("1\n"), 0644) 115 c.Assert(err, IsNil) 116 117 err = sanity.CheckKernelVersion() 118 c.Assert(err, IsNil) 119 120 // custom kernel version, which is old and we have no knowledge about 121 restore = osutil.MockKernelVersion("3.10.0-1024.foo.x86_64") 122 defer restore() 123 err = sanity.CheckKernelVersion() 124 c.Assert(err, ErrorMatches, `unsupported kernel version "3.10.0-1024.foo.x86_64", you need to switch to the stock kernel`) 125 126 // custom kernel version, but new enough 127 restore = osutil.MockKernelVersion("4.18.0-32.foo.x86_64") 128 defer restore() 129 err = sanity.CheckKernelVersion() 130 c.Assert(err, IsNil) 131 } 132 133 func (s *sanitySuite) TestCentOS7x(c *C) { 134 dir := c.MkDir() 135 dirs.SetRootDir(dir) 136 defer dirs.SetRootDir("/") 137 // mock CentOS 7.5 138 restore := release.MockOnClassic(true) 139 defer restore() 140 // NAME="CentOS Linux" 141 // VERSION="7 (Core)" 142 // ID="centos" 143 // ID_LIKE="rhel fedora" 144 // VERSION_ID="7" 145 restore = release.MockReleaseInfo(&release.OS{ID: "centos", VersionID: "7"}) 146 defer restore() 147 restore = osutil.MockKernelVersion("3.10.0-862.14.4.el7.x86_64") 148 defer restore() 149 150 p := filepath.Join(dir, "/proc/sys/fs/may_detach_mounts") 151 err := os.MkdirAll(filepath.Dir(p), 0755) 152 c.Assert(err, IsNil) 153 154 // the knob there, but disabled 155 err = ioutil.WriteFile(p, []byte("0\n"), 0644) 156 c.Assert(err, IsNil) 157 158 err = sanity.CheckKernelVersion() 159 c.Assert(err, ErrorMatches, "fs.may_detach_mounts kernel parameter is supported but disabled") 160 161 // actually enabled 162 err = ioutil.WriteFile(p, []byte("1\n"), 0644) 163 c.Assert(err, IsNil) 164 165 err = sanity.CheckKernelVersion() 166 c.Assert(err, IsNil) 167 }