github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/configstate/configcore/journal_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 configcore_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/osutil/sys" 32 "github.com/snapcore/snapd/overlord/configstate/configcore" 33 "github.com/snapcore/snapd/release" 34 "github.com/snapcore/snapd/systemd" 35 ) 36 37 type journalSuite struct { 38 configcoreSuite 39 systemdVersion string 40 } 41 42 var _ = Suite(&journalSuite{}) 43 44 func (s *journalSuite) SetUpTest(c *C) { 45 s.configcoreSuite.SetUpTest(c) 46 47 s.systemdVersion = "236" 48 // this overrides systemctl mock from the base configcoreSuite. 49 s.AddCleanup(systemd.MockSystemctl(func(args ...string) ([]byte, error) { 50 s.systemctlArgs = append(s.systemctlArgs, args[:]) 51 output := []byte("systemd " + s.systemdVersion + "\n+XYZ") 52 return output, nil 53 })) 54 s.systemctlArgs = nil 55 56 err := os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "/etc/"), 0755) 57 c.Assert(err, IsNil) 58 59 findGidRestore := configcore.MockFindGid(func(group string) (uint64, error) { 60 c.Assert(group, Equals, "systemd-journal") 61 return 1234, nil 62 }) 63 s.AddCleanup(findGidRestore) 64 65 chownPathRestore := configcore.MockChownPath(func(path string, uid sys.UserID, gid sys.GroupID) error { 66 c.Check(uid, Equals, sys.UserID(0)) 67 c.Check(gid, Equals, sys.GroupID(1234)) 68 return nil 69 }) 70 s.AddCleanup(chownPathRestore) 71 } 72 73 func (s *journalSuite) TestConfigurePersistentJournalInvalid(c *C) { 74 err := configcore.Run(&mockConf{ 75 state: s.state, 76 conf: map[string]interface{}{"journal.persistent": "foo"}, 77 }) 78 c.Assert(err, ErrorMatches, `journal.persistent can only be set to 'true' or 'false'`) 79 } 80 81 func (s *journalSuite) TestConfigurePersistentJournalOnCore(c *C) { 82 restore := release.MockOnClassic(false) 83 defer restore() 84 85 err := configcore.Run(&mockConf{ 86 state: s.state, 87 conf: map[string]interface{}{"journal.persistent": "true"}, 88 }) 89 c.Assert(err, IsNil) 90 91 c.Check(s.systemctlArgs, DeepEquals, [][]string{ 92 {"--version"}, 93 {"kill", "systemd-journald", "-s", "USR1", "--kill-who=all"}, 94 }) 95 96 exists, _, err := osutil.DirExists(filepath.Join(dirs.GlobalRootDir, "/var/log/journal")) 97 c.Assert(err, IsNil) 98 c.Check(exists, Equals, true) 99 c.Check(osutil.FileExists(filepath.Join(dirs.GlobalRootDir, "/var/log/journal/.snapd-created")), Equals, true) 100 } 101 102 func (s *journalSuite) TestConfigurePersistentJournalOldSystemd(c *C) { 103 restore := release.MockOnClassic(false) 104 defer restore() 105 106 s.systemdVersion = "235" 107 108 err := configcore.Run(&mockConf{ 109 state: s.state, 110 conf: map[string]interface{}{"journal.persistent": "true"}, 111 }) 112 c.Assert(err, IsNil) 113 114 c.Check(s.systemctlArgs, DeepEquals, [][]string{ 115 {"--version"}, // version query, but no usr1 signal sent 116 }) 117 118 exists, _, err := osutil.DirExists(filepath.Join(dirs.GlobalRootDir, "/var/log/journal")) 119 c.Assert(err, IsNil) 120 c.Check(exists, Equals, true) 121 c.Check(osutil.FileExists(filepath.Join(dirs.GlobalRootDir, "/var/log/journal/.snapd-created")), Equals, true) 122 } 123 124 func (s *journalSuite) TestConfigurePersistentJournalOnCoreNoopIfExists(c *C) { 125 restore := release.MockOnClassic(false) 126 defer restore() 127 128 // existing journal directory, not created by snapd (no marker file) 129 c.Assert(os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "/var/log/journal"), 0755), IsNil) 130 131 err := configcore.Run(&mockConf{ 132 state: s.state, 133 conf: map[string]interface{}{"journal.persistent": "true"}, 134 }) 135 c.Assert(err, IsNil) 136 137 // systemctl was not called 138 c.Check(s.systemctlArgs, HasLen, 0) 139 140 exists, _, err := osutil.DirExists(filepath.Join(dirs.GlobalRootDir, "/var/log/journal")) 141 c.Assert(err, IsNil) 142 c.Check(exists, Equals, true) 143 144 // marker was not created 145 c.Check(osutil.FileExists(filepath.Join(dirs.GlobalRootDir, "/var/log/journal/.snapd-created")), Equals, false) 146 } 147 148 func (s *journalSuite) TestDisablePersistentJournalNotManagedBySnapdError(c *C) { 149 restore := release.MockOnClassic(false) 150 defer restore() 151 152 // journal directory exists, but no marker file 153 c.Assert(os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "/var/log/journal"), 0755), IsNil) 154 155 err := configcore.Run(&mockConf{ 156 state: s.state, 157 conf: map[string]interface{}{"journal.persistent": "false"}, 158 }) 159 c.Assert(err, ErrorMatches, `.*/var/log/journal directory was not created by snapd.*`) 160 exists, _, _ := osutil.DirExists(filepath.Join(dirs.GlobalRootDir, "/var/log/journal")) 161 c.Check(exists, Equals, true) 162 } 163 164 func (s *journalSuite) TestDisablePersistentJournalOnCore(c *C) { 165 restore := release.MockOnClassic(false) 166 defer restore() 167 168 c.Assert(os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "/var/log/journal"), 0755), IsNil) 169 c.Assert(ioutil.WriteFile(filepath.Join(dirs.GlobalRootDir, "/var/log/journal/.snapd-created"), nil, 0755), IsNil) 170 171 err := configcore.Run(&mockConf{ 172 state: s.state, 173 conf: map[string]interface{}{"journal.persistent": "false"}, 174 }) 175 c.Assert(err, IsNil) 176 177 c.Check(s.systemctlArgs, DeepEquals, [][]string{ 178 {"--version"}, 179 {"kill", "systemd-journald", "-s", "USR1", "--kill-who=all"}, 180 }) 181 182 exists, _, err := osutil.DirExists(filepath.Join(dirs.GlobalRootDir, "/var/log/journal")) 183 c.Assert(err, IsNil) 184 c.Check(exists, Equals, false) 185 } 186 187 func (s *journalSuite) TestFilesystemOnlyApply(c *C) { 188 restore := release.MockOnClassic(false) 189 defer restore() 190 191 conf := configcore.PlainCoreConfig(map[string]interface{}{ 192 "journal.persistent": "true", 193 }) 194 tmpDir := c.MkDir() 195 c.Assert(configcore.FilesystemOnlyApply(tmpDir, conf, nil), IsNil) 196 c.Check(s.systemctlArgs, HasLen, 0) 197 198 exists, _, err := osutil.DirExists(filepath.Join(tmpDir, "/var/log/journal")) 199 c.Assert(err, IsNil) 200 c.Check(exists, Equals, true) 201 }