github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/systemd/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 systemd_test
    21  
    22  import (
    23  	"errors"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/systemd"
    28  	"github.com/snapcore/snapd/testutil"
    29  )
    30  
    31  type sysctlSuite struct {
    32  	testutil.BaseTest
    33  }
    34  
    35  var _ = Suite(&sysctlSuite{})
    36  
    37  func (s *sysctlSuite) TestSysctl(c *C) {
    38  	defer systemd.MockSystemdSysctlPath("systemd-sysctl")()
    39  
    40  	systemdSysctl := testutil.MockCommand(c, "systemd-sysctl", "")
    41  	defer systemdSysctl.Restore()
    42  
    43  	err := systemd.Sysctl(nil)
    44  	c.Assert(err, IsNil)
    45  
    46  	err = systemd.Sysctl([]string{"kernel.printk", "net"})
    47  	c.Assert(err, IsNil)
    48  
    49  	c.Check(systemdSysctl.Calls(), DeepEquals, [][]string{
    50  		{"systemd-sysctl"},
    51  		{"systemd-sysctl", "--prefix", "kernel.printk", "--prefix", "net"},
    52  	})
    53  }
    54  
    55  func (s *sysctlSuite) TestSysctlError(c *C) {
    56  	defer systemd.MockSystemdSysctlPath("systemd-sysctl")()
    57  
    58  	systemdSysctl := testutil.MockCommand(c, "systemd-sysctl", `
    59  echo foo >&2
    60  exit 1
    61  `)
    62  	defer systemdSysctl.Restore()
    63  
    64  	err := systemd.Sysctl(nil)
    65  	c.Assert(err, ErrorMatches, `(?m)systemd-sysctl invoked with \[\] failed with exit status 1: foo`)
    66  
    67  	err = systemd.Sysctl([]string{"net"})
    68  	c.Assert(err, ErrorMatches, `(?m)systemd-sysctl invoked with \[--prefix net\] failed with exit status 1: foo`)
    69  }
    70  
    71  func (s *sysctlSuite) TestMockSystemdSysctl(c *C) {
    72  	var capturedArgs []string
    73  	var sysctlErr error
    74  	r := systemd.MockSystemdSysctl(func(args ...string) error {
    75  		capturedArgs = args
    76  		return sysctlErr
    77  	})
    78  	defer r()
    79  
    80  	err := systemd.Sysctl([]string{"kernel.printk", "net"})
    81  	c.Assert(err, IsNil)
    82  
    83  	c.Check(capturedArgs, DeepEquals, []string{"--prefix", "kernel.printk", "--prefix", "net"})
    84  
    85  	sysctlErr = errors.New("boom")
    86  	err = systemd.Sysctl(nil)
    87  	c.Check(err, Equals, sysctlErr)
    88  	c.Check(capturedArgs, HasLen, 0)
    89  }