github.com/stulluk/snapd@v0.0.0-20210611110309-f6d5d5bd24b0/sysconfig/gadget_defaults_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 sysconfig_test
    21  
    22  import (
    23  	"path/filepath"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/boot"
    28  	"github.com/snapcore/snapd/osutil"
    29  
    30  	// to set ApplyFilesystemOnlyDefaults hook
    31  	_ "github.com/snapcore/snapd/overlord/configstate/configcore"
    32  
    33  	"github.com/snapcore/snapd/snap"
    34  	"github.com/snapcore/snapd/snap/snaptest"
    35  	"github.com/snapcore/snapd/snap/squashfs"
    36  	"github.com/snapcore/snapd/sysconfig"
    37  	"github.com/snapcore/snapd/systemd"
    38  )
    39  
    40  var gadgetYaml = `
    41  volumes:
    42    pc:
    43      bootloader: grub
    44  `
    45  
    46  func (s *sysconfigSuite) TestGadgetDefaults(c *C) {
    47  	const gadgetDefaultsYaml = `
    48  defaults:
    49    system:
    50      service:
    51        rsyslog.disable: true
    52        ssh.disable: true
    53      journal.persistent: true
    54  `
    55  	si := &snap.SideInfo{
    56  		RealName: "pc",
    57  		Revision: snap.R(1),
    58  		SnapID:   "idid",
    59  	}
    60  	snapInfo := snaptest.MockSnapWithFiles(c, "name: pc\ntype: gadget", si, [][]string{
    61  		{"meta/gadget.yaml", gadgetYaml + gadgetDefaultsYaml},
    62  	})
    63  
    64  	var sysctlArgs [][]string
    65  	systemctlRestorer := systemd.MockSystemctl(func(args ...string) (buf []byte, err error) {
    66  		sysctlArgs = append(sysctlArgs, args)
    67  		return nil, nil
    68  	})
    69  	defer systemctlRestorer()
    70  
    71  	journalPath := filepath.Join(boot.InstallHostWritableDir, "_writable_defaults/var/log/journal")
    72  	sshDontRunFile := filepath.Join(boot.InstallHostWritableDir, "_writable_defaults/etc/ssh/sshd_not_to_be_run")
    73  
    74  	// sanity
    75  	c.Check(osutil.FileExists(sshDontRunFile), Equals, false)
    76  	exists, _, _ := osutil.DirExists(journalPath)
    77  	c.Check(exists, Equals, false)
    78  
    79  	err := sysconfig.ConfigureTargetSystem(&sysconfig.Options{
    80  		TargetRootDir: boot.InstallHostWritableDir,
    81  		GadgetDir:     snapInfo.MountDir(),
    82  	})
    83  	c.Assert(err, IsNil)
    84  
    85  	c.Check(osutil.FileExists(sshDontRunFile), Equals, true)
    86  	exists, _, _ = osutil.DirExists(journalPath)
    87  	c.Check(exists, Equals, true)
    88  
    89  	c.Check(sysctlArgs, DeepEquals, [][]string{{"--root", filepath.Join(boot.InstallHostWritableDir, "_writable_defaults"), "mask", "rsyslog.service"}})
    90  }
    91  
    92  func (s *sysconfigSuite) TestInstallModeEarlyDefaultsFromGadgetInvalid(c *C) {
    93  	const gadgetDefaultsYaml = `
    94  defaults:
    95    system:
    96      service:
    97        rsyslog:
    98          disable: foo
    99  `
   100  	si := &snap.SideInfo{
   101  		RealName: "pc",
   102  		Revision: snap.R(1),
   103  		SnapID:   "idid",
   104  	}
   105  	snapInfo := snaptest.MockSnapWithFiles(c, "name: pc\ntype: gadget", si, [][]string{
   106  		{"meta/gadget.yaml", gadgetYaml + gadgetDefaultsYaml},
   107  	})
   108  
   109  	err := sysconfig.ConfigureTargetSystem(&sysconfig.Options{
   110  		TargetRootDir: boot.InstallHostWritableDir,
   111  		GadgetDir:     snapInfo.MountDir(),
   112  	})
   113  	c.Check(err, ErrorMatches, `option "service.rsyslog.disable" has invalid value "foo"`)
   114  }
   115  
   116  func (s *sysconfigSuite) TestInstallModeEarlyDefaultsFromGadgetSeedSnap(c *C) {
   117  	const gadgetDefaultsYaml = `
   118  defaults:
   119    system:
   120      service:
   121        rsyslog.disable: true
   122        ssh.disable: true
   123      journal.persistent: true
   124  `
   125  	snapFile := snaptest.MakeTestSnapWithFiles(c, "name: pc\ntype: gadget\nversion: 1", [][]string{
   126  		{"meta/gadget.yaml", gadgetYaml + gadgetDefaultsYaml},
   127  	})
   128  
   129  	snapContainer := squashfs.New(snapFile)
   130  
   131  	var sysctlArgs [][]string
   132  	systemctlRestorer := systemd.MockSystemctl(func(args ...string) (buf []byte, err error) {
   133  		sysctlArgs = append(sysctlArgs, args)
   134  		return nil, nil
   135  	})
   136  	defer systemctlRestorer()
   137  
   138  	journalPath := filepath.Join(boot.InstallHostWritableDir, "_writable_defaults/var/log/journal")
   139  	sshDontRunFile := filepath.Join(boot.InstallHostWritableDir, "_writable_defaults/etc/ssh/sshd_not_to_be_run")
   140  
   141  	// sanity
   142  	c.Check(osutil.FileExists(sshDontRunFile), Equals, false)
   143  	exists, _, _ := osutil.DirExists(journalPath)
   144  	c.Check(exists, Equals, false)
   145  
   146  	err := sysconfig.ConfigureTargetSystem(&sysconfig.Options{
   147  		TargetRootDir: boot.InstallHostWritableDir,
   148  		GadgetSnap:    snapContainer,
   149  	})
   150  	c.Assert(err, IsNil)
   151  
   152  	c.Check(osutil.FileExists(sshDontRunFile), Equals, true)
   153  	exists, _, _ = osutil.DirExists(journalPath)
   154  	c.Check(exists, Equals, true)
   155  
   156  	c.Check(sysctlArgs, DeepEquals, [][]string{{"--root", filepath.Join(boot.InstallHostWritableDir, "_writable_defaults"), "mask", "rsyslog.service"}})
   157  }