github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/cmd/snaplock/runinhibit/inhibit_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2020 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 runinhibit_test 21 22 import ( 23 "os" 24 "path/filepath" 25 "testing" 26 27 . "gopkg.in/check.v1" 28 29 "github.com/snapcore/snapd/cmd/snaplock/runinhibit" 30 "github.com/snapcore/snapd/dirs" 31 "github.com/snapcore/snapd/testutil" 32 ) 33 34 // Hook up check.v1 into the "go test" runner 35 func Test(t *testing.T) { TestingT(t) } 36 37 type runInhibitSuite struct{} 38 39 var _ = Suite(&runInhibitSuite{}) 40 41 func (s *runInhibitSuite) SetUpTest(c *C) { 42 dirs.SetRootDir(c.MkDir()) 43 } 44 45 func (s *runInhibitSuite) TearDownTest(c *C) { 46 dirs.SetRootDir("") 47 } 48 49 // Locking cannot be done with an empty hint as that is equivalent to unlocking. 50 func (s *runInhibitSuite) TestLockWithEmptyHint(c *C) { 51 _, err := os.Stat(runinhibit.InhibitDir) 52 c.Assert(os.IsNotExist(err), Equals, true) 53 54 err = runinhibit.LockWithHint("pkg", runinhibit.HintNotInhibited) 55 c.Assert(err, ErrorMatches, "lock hint cannot be empty") 56 57 _, err = os.Stat(runinhibit.InhibitDir) 58 c.Assert(os.IsNotExist(err), Equals, true) 59 } 60 61 // Locking a file creates required directories and writes the hint file. 62 func (s *runInhibitSuite) TestLockWithHint(c *C) { 63 _, err := os.Stat(runinhibit.InhibitDir) 64 c.Assert(os.IsNotExist(err), Equals, true) 65 66 err = runinhibit.LockWithHint("pkg", runinhibit.HintInhibitedForRefresh) 67 c.Assert(err, IsNil) 68 69 fi, err := os.Stat(runinhibit.InhibitDir) 70 c.Assert(err, IsNil) 71 c.Check(fi.IsDir(), Equals, true) 72 73 c.Check(filepath.Join(runinhibit.InhibitDir, "pkg.lock"), testutil.FileEquals, "refresh") 74 } 75 76 // The lock can be re-acquired to present a different hint. 77 func (s *runInhibitSuite) TestLockLocked(c *C) { 78 err := runinhibit.LockWithHint("pkg", runinhibit.HintInhibitedForRefresh) 79 c.Assert(err, IsNil) 80 c.Check(filepath.Join(runinhibit.InhibitDir, "pkg.lock"), testutil.FileEquals, "refresh") 81 82 err = runinhibit.LockWithHint("pkg", runinhibit.Hint("just-testing")) 83 c.Assert(err, IsNil) 84 c.Check(filepath.Join(runinhibit.InhibitDir, "pkg.lock"), testutil.FileEquals, "just-testing") 85 86 err = runinhibit.LockWithHint("pkg", runinhibit.Hint("short")) 87 c.Assert(err, IsNil) 88 c.Check(filepath.Join(runinhibit.InhibitDir, "pkg.lock"), testutil.FileEquals, "short") 89 } 90 91 // Unlocking an unlocked lock doesn't break anything. 92 func (s *runInhibitSuite) TestUnlockUnlocked(c *C) { 93 err := runinhibit.Unlock("pkg") 94 c.Assert(err, IsNil) 95 c.Check(filepath.Join(runinhibit.InhibitDir, "pkg.lock"), testutil.FileAbsent) 96 } 97 98 // Unlocking an locked lock truncates the hint. 99 func (s *runInhibitSuite) TestUnlockLocked(c *C) { 100 err := runinhibit.LockWithHint("pkg", runinhibit.HintInhibitedForRefresh) 101 c.Assert(err, IsNil) 102 103 err = runinhibit.Unlock("pkg") 104 c.Assert(err, IsNil) 105 106 c.Check(filepath.Join(runinhibit.InhibitDir, "pkg.lock"), testutil.FileEquals, "") 107 } 108 109 // IsLocked doesn't fail when the lock directory or lock file is missing. 110 func (s *runInhibitSuite) TestIsLockedMissing(c *C) { 111 _, err := os.Stat(runinhibit.InhibitDir) 112 c.Assert(os.IsNotExist(err), Equals, true) 113 114 hint, err := runinhibit.IsLocked("pkg") 115 c.Assert(err, IsNil) 116 c.Check(hint, Equals, runinhibit.HintNotInhibited) 117 118 err = os.MkdirAll(runinhibit.InhibitDir, 0755) 119 c.Assert(err, IsNil) 120 121 hint, err = runinhibit.IsLocked("pkg") 122 c.Assert(err, IsNil) 123 c.Check(hint, Equals, runinhibit.HintNotInhibited) 124 } 125 126 // IsLocked returns the previously set hint. 127 func (s *runInhibitSuite) TestIsLockedLocked(c *C) { 128 err := runinhibit.LockWithHint("pkg", runinhibit.HintInhibitedForRefresh) 129 c.Assert(err, IsNil) 130 131 hint, err := runinhibit.IsLocked("pkg") 132 c.Assert(err, IsNil) 133 c.Check(hint, Equals, runinhibit.HintInhibitedForRefresh) 134 } 135 136 // IsLocked returns not-inhibited after unlocking. 137 func (s *runInhibitSuite) TestIsLockedUnlocked(c *C) { 138 err := runinhibit.LockWithHint("pkg", runinhibit.HintInhibitedForRefresh) 139 c.Assert(err, IsNil) 140 err = runinhibit.Unlock("pkg") 141 c.Assert(err, IsNil) 142 143 hint, err := runinhibit.IsLocked("pkg") 144 c.Assert(err, IsNil) 145 c.Check(hint, Equals, runinhibit.HintNotInhibited) 146 }