github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/overlord/configstate/configcore/sysctl_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  	"os"
    24  	"path/filepath"
    25  
    26  	. "gopkg.in/check.v1"
    27  
    28  	"github.com/snapcore/snapd/dirs"
    29  	"github.com/snapcore/snapd/osutil"
    30  	"github.com/snapcore/snapd/overlord/configstate/configcore"
    31  	"github.com/snapcore/snapd/testutil"
    32  )
    33  
    34  type sysctlSuite struct {
    35  	configcoreSuite
    36  
    37  	mockSysctlConfPath string
    38  }
    39  
    40  var _ = Suite(&sysctlSuite{})
    41  
    42  func (s *sysctlSuite) SetUpTest(c *C) {
    43  	s.configcoreSuite.SetUpTest(c)
    44  	dirs.SetRootDir(c.MkDir())
    45  
    46  	s.mockSysctlConfPath = filepath.Join(dirs.GlobalRootDir, "/etc/sysctl.d/99-snapd.conf")
    47  	c.Assert(os.MkdirAll(filepath.Dir(s.mockSysctlConfPath), 0755), IsNil)
    48  }
    49  
    50  func (s *sysctlSuite) TearDownTest(c *C) {
    51  	s.configcoreSuite.TearDownTest(c)
    52  	dirs.SetRootDir("/")
    53  }
    54  
    55  func (s *sysctlSuite) TestConfigureSysctlIntegration(c *C) {
    56  	err := configcore.Run(coreDev, &mockConf{
    57  		state: s.state,
    58  		conf: map[string]interface{}{
    59  			"system.kernel.printk.console-loglevel": "2",
    60  		},
    61  	})
    62  	c.Assert(err, IsNil)
    63  	c.Check(s.mockSysctlConfPath, testutil.FileEquals, "kernel.printk = 2 4 1 7\n")
    64  	c.Check(s.systemdSysctlArgs, DeepEquals, [][]string{
    65  		{"--prefix", "kernel.printk"},
    66  	})
    67  	s.systemdSysctlArgs = nil
    68  
    69  	// Unset console-loglevel and restore default vaule
    70  	err = configcore.Run(coreDev, &mockConf{
    71  		state: s.state,
    72  		conf: map[string]interface{}{
    73  			"system.kernel.printk.console-loglevel": "",
    74  		},
    75  	})
    76  	c.Assert(err, IsNil)
    77  	c.Check(osutil.FileExists(s.mockSysctlConfPath), Equals, false)
    78  	c.Check(s.systemdSysctlArgs, DeepEquals, [][]string{
    79  		{"--prefix", "kernel.printk"},
    80  	})
    81  }
    82  
    83  func (s *sysctlSuite) TestConfigureLoglevelUnderRange(c *C) {
    84  	err := configcore.Run(coreDev, &mockConf{
    85  		state: s.state,
    86  		conf: map[string]interface{}{
    87  			"system.kernel.printk.console-loglevel": "-1",
    88  		},
    89  	})
    90  	c.Check(osutil.FileExists(s.mockSysctlConfPath), Equals, false)
    91  	c.Assert(err, ErrorMatches, `console-loglevel must be a number between 0 and 7, not: -1`)
    92  }
    93  
    94  func (s *sysctlSuite) TestConfigureLoglevelOverRange(c *C) {
    95  	err := configcore.Run(coreDev, &mockConf{
    96  		state: s.state,
    97  		conf: map[string]interface{}{
    98  			"system.kernel.printk.console-loglevel": "8",
    99  		},
   100  	})
   101  	c.Check(osutil.FileExists(s.mockSysctlConfPath), Equals, false)
   102  	c.Assert(err, ErrorMatches, `console-loglevel must be a number between 0 and 7, not: 8`)
   103  }
   104  
   105  func (s *sysctlSuite) TestConfigureLevelRejected(c *C) {
   106  	err := configcore.Run(coreDev, &mockConf{
   107  		state: s.state,
   108  		conf: map[string]interface{}{
   109  			"system.kernel.printk.console-loglevel": "invalid",
   110  		},
   111  	})
   112  	c.Check(osutil.FileExists(s.mockSysctlConfPath), Equals, false)
   113  	c.Assert(err, ErrorMatches, `console-loglevel must be a number between 0 and 7, not: invalid`)
   114  }
   115  
   116  func (s *sysctlSuite) TestConfigureSysctlIntegrationNoSetting(c *C) {
   117  	err := configcore.Run(coreDev, &mockConf{
   118  		state: s.state,
   119  		conf:  map[string]interface{}{},
   120  	})
   121  	c.Assert(err, IsNil)
   122  	c.Check(osutil.FileExists(s.mockSysctlConfPath), Equals, false)
   123  }
   124  
   125  func (s *sysctlSuite) TestFilesystemOnlyApply(c *C) {
   126  	conf := configcore.PlainCoreConfig(map[string]interface{}{
   127  		"system.kernel.printk.console-loglevel": "4",
   128  	})
   129  
   130  	tmpDir := c.MkDir()
   131  	c.Assert(configcore.FilesystemOnlyApply(coreDev, tmpDir, conf), IsNil)
   132  
   133  	networkSysctlPath := filepath.Join(tmpDir, "/etc/sysctl.d/99-snapd.conf")
   134  	c.Check(networkSysctlPath, testutil.FileEquals, "kernel.printk = 4 4 1 7\n")
   135  
   136  	// systemd-sysctl was not executed
   137  	c.Check(s.systemdSysctlArgs, HasLen, 0)
   138  }