github.com/rigado/snapd@v2.42.5-go-mod+incompatible/overlord/configstate/configcore/corecfg_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017 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  	"fmt"
    24  	"reflect"
    25  	"testing"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/dirs"
    30  	"github.com/snapcore/snapd/overlord/configstate/configcore"
    31  	"github.com/snapcore/snapd/overlord/state"
    32  	"github.com/snapcore/snapd/systemd"
    33  )
    34  
    35  func Test(t *testing.T) { TestingT(t) }
    36  
    37  type mockConf struct {
    38  	state   *state.State
    39  	conf    map[string]interface{}
    40  	changes map[string]interface{}
    41  	err     error
    42  }
    43  
    44  func (cfg *mockConf) Get(snapName, key string, result interface{}) error {
    45  	if snapName != "core" {
    46  		return fmt.Errorf("mockConf only knows about core")
    47  	}
    48  
    49  	var value interface{}
    50  	value = cfg.changes[key]
    51  	if value == nil {
    52  		value = cfg.conf[key]
    53  	}
    54  	if value != nil {
    55  		v1 := reflect.ValueOf(result)
    56  		v2 := reflect.Indirect(v1)
    57  		v2.Set(reflect.ValueOf(value))
    58  	}
    59  	return cfg.err
    60  }
    61  
    62  func (cfg *mockConf) Set(snapName, key string, v interface{}) error {
    63  	if snapName != "core" {
    64  		return fmt.Errorf("mockConf only knows about core")
    65  	}
    66  	if cfg.conf == nil {
    67  		cfg.conf = make(map[string]interface{})
    68  	}
    69  	cfg.conf[key] = v
    70  	return nil
    71  }
    72  
    73  func (cfg *mockConf) Changes() []string {
    74  	out := make([]string, 0, len(cfg.changes))
    75  	for k := range cfg.changes {
    76  		out = append(out, "core."+k)
    77  	}
    78  	return out
    79  }
    80  
    81  func (cfg *mockConf) State() *state.State {
    82  	return cfg.state
    83  }
    84  
    85  // configcoreSuite is the base for all the configcore tests
    86  type configcoreSuite struct {
    87  	state *state.State
    88  
    89  	systemctlArgs     [][]string
    90  	systemctlRestorer func()
    91  }
    92  
    93  var _ = Suite(&configcoreSuite{})
    94  
    95  func (s *configcoreSuite) SetUpSuite(c *C) {
    96  	s.systemctlRestorer = systemd.MockSystemctl(func(args ...string) ([]byte, error) {
    97  		s.systemctlArgs = append(s.systemctlArgs, args[:])
    98  		output := []byte("ActiveState=inactive")
    99  		return output, nil
   100  	})
   101  }
   102  
   103  func (s *configcoreSuite) TearDownSuite(c *C) {
   104  	s.systemctlRestorer()
   105  }
   106  
   107  func (s *configcoreSuite) SetUpTest(c *C) {
   108  	dirs.SetRootDir(c.MkDir())
   109  	s.state = state.New(nil)
   110  }
   111  
   112  func (s *configcoreSuite) TearDownTest(c *C) {
   113  	dirs.SetRootDir("")
   114  }
   115  
   116  // runCfgSuite tests configcore.Run()
   117  type runCfgSuite struct {
   118  	configcoreSuite
   119  }
   120  
   121  var _ = Suite(&runCfgSuite{})
   122  
   123  func (r *runCfgSuite) TestConfigureUnknownOption(c *C) {
   124  	conf := &mockConf{
   125  		state: r.state,
   126  		changes: map[string]interface{}{
   127  			"unknown.option": "1",
   128  		},
   129  	}
   130  
   131  	err := configcore.Run(conf)
   132  	c.Check(err, ErrorMatches, `cannot set "core.unknown.option": unsupported system option`)
   133  }