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