github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/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/sysconfig"
    36  	"github.com/snapcore/snapd/systemd"
    37  )
    38  
    39  var gadgetYaml = `
    40  volumes:
    41    pc:
    42      bootloader: grub
    43  `
    44  
    45  func (s *sysconfigSuite) TestGadgetDefaults(c *C) {
    46  	const gadgetDefaultsYaml = `
    47  defaults:
    48    system:
    49      service:
    50        rsyslog.disable: true
    51        ssh.disable: true
    52      journal.persistent: true
    53  `
    54  	si := &snap.SideInfo{
    55  		RealName: "pc",
    56  		Revision: snap.R(1),
    57  		SnapID:   "idid",
    58  	}
    59  	snapInfo := snaptest.MockSnapWithFiles(c, "name: pc\ntype: gadget", si, [][]string{
    60  		{"meta/gadget.yaml", gadgetYaml + gadgetDefaultsYaml},
    61  	})
    62  
    63  	var sysctlArgs [][]string
    64  	systemctlRestorer := systemd.MockSystemctl(func(args ...string) (buf []byte, err error) {
    65  		sysctlArgs = append(sysctlArgs, args)
    66  		return nil, nil
    67  	})
    68  	defer systemctlRestorer()
    69  
    70  	journalPath := filepath.Join(boot.InstallHostWritableDir, "_writable_defaults/var/log/journal")
    71  	sshDontRunFile := filepath.Join(boot.InstallHostWritableDir, "_writable_defaults/etc/ssh/sshd_not_to_be_run")
    72  
    73  	// sanity
    74  	c.Check(osutil.FileExists(sshDontRunFile), Equals, false)
    75  	exists, _, _ := osutil.DirExists(journalPath)
    76  	c.Check(exists, Equals, false)
    77  
    78  	err := sysconfig.ConfigureRunSystem(&sysconfig.Options{
    79  		TargetRootDir: boot.InstallHostWritableDir,
    80  		GadgetDir:     snapInfo.MountDir(),
    81  	})
    82  	c.Assert(err, IsNil)
    83  
    84  	c.Check(osutil.FileExists(sshDontRunFile), Equals, true)
    85  	exists, _, _ = osutil.DirExists(journalPath)
    86  	c.Check(exists, Equals, true)
    87  
    88  	c.Check(sysctlArgs, DeepEquals, [][]string{{"--root", filepath.Join(boot.InstallHostWritableDir, "_writable_defaults"), "mask", "rsyslog.service"}})
    89  }
    90  
    91  func (s *sysconfigSuite) TestInstallModeEarlyDefaultsFromGadgetInvalid(c *C) {
    92  	const gadgetDefaultsYaml = `
    93  defaults:
    94    system:
    95      service:
    96        rsyslog:
    97          disable: foo
    98  `
    99  	si := &snap.SideInfo{
   100  		RealName: "pc",
   101  		Revision: snap.R(1),
   102  		SnapID:   "idid",
   103  	}
   104  	snapInfo := snaptest.MockSnapWithFiles(c, "name: pc\ntype: gadget", si, [][]string{
   105  		{"meta/gadget.yaml", gadgetYaml + gadgetDefaultsYaml},
   106  	})
   107  
   108  	err := sysconfig.ConfigureRunSystem(&sysconfig.Options{
   109  		TargetRootDir: boot.InstallHostWritableDir,
   110  		GadgetDir:     snapInfo.MountDir(),
   111  	})
   112  	c.Check(err, ErrorMatches, `option "service.rsyslog.disable" has invalid value "foo"`)
   113  }