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