github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/features/features_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 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  package features_test
    20  
    21  import (
    22  	"io/ioutil"
    23  	"os"
    24  	"path/filepath"
    25  	"testing"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/dirs"
    30  	"github.com/snapcore/snapd/features"
    31  	"github.com/snapcore/snapd/overlord/configstate/config"
    32  	"github.com/snapcore/snapd/overlord/state"
    33  )
    34  
    35  func Test(t *testing.T) { TestingT(t) }
    36  
    37  type featureSuite struct{}
    38  
    39  var _ = Suite(&featureSuite{})
    40  
    41  func (*featureSuite) TestName(c *C) {
    42  	c.Check(features.Layouts.String(), Equals, "layouts")
    43  	c.Check(features.ParallelInstances.String(), Equals, "parallel-instances")
    44  	c.Check(features.Hotplug.String(), Equals, "hotplug")
    45  	c.Check(features.SnapdSnap.String(), Equals, "snapd-snap")
    46  	c.Check(features.PerUserMountNamespace.String(), Equals, "per-user-mount-namespace")
    47  	c.Check(features.RefreshAppAwareness.String(), Equals, "refresh-app-awareness")
    48  	c.Check(features.ClassicPreservesXdgRuntimeDir.String(), Equals, "classic-preserves-xdg-runtime-dir")
    49  	c.Check(features.RobustMountNamespaceUpdates.String(), Equals, "robust-mount-namespace-updates")
    50  	c.Check(features.UserDaemons.String(), Equals, "user-daemons")
    51  	c.Check(features.DbusActivation.String(), Equals, "dbus-activation")
    52  	c.Check(features.HiddenSnapFolder.String(), Equals, "hidden-snap-folder")
    53  	c.Check(features.CheckDiskSpaceInstall.String(), Equals, "check-disk-space-install")
    54  	c.Check(features.CheckDiskSpaceRefresh.String(), Equals, "check-disk-space-refresh")
    55  	c.Check(features.CheckDiskSpaceRemove.String(), Equals, "check-disk-space-remove")
    56  	c.Check(func() { _ = features.SnapdFeature(1000).String() }, PanicMatches, "unknown feature flag code 1000")
    57  }
    58  
    59  func (*featureSuite) TestKnownFeatures(c *C) {
    60  	// Check that known features have names.
    61  	known := features.KnownFeatures()
    62  	for _, f := range known {
    63  		c.Check(f.String(), Not(Equals), "", Commentf("feature code: %d", int(f)))
    64  	}
    65  	c.Check(known, HasLen, features.NumberOfFeatures())
    66  }
    67  
    68  func (*featureSuite) TestIsExported(c *C) {
    69  	c.Check(features.Layouts.IsExported(), Equals, false)
    70  	c.Check(features.Hotplug.IsExported(), Equals, false)
    71  	c.Check(features.SnapdSnap.IsExported(), Equals, false)
    72  
    73  	c.Check(features.ParallelInstances.IsExported(), Equals, true)
    74  	c.Check(features.PerUserMountNamespace.IsExported(), Equals, true)
    75  	c.Check(features.RefreshAppAwareness.IsExported(), Equals, true)
    76  	c.Check(features.ClassicPreservesXdgRuntimeDir.IsExported(), Equals, true)
    77  	c.Check(features.UserDaemons.IsExported(), Equals, false)
    78  	c.Check(features.DbusActivation.IsExported(), Equals, false)
    79  	c.Check(features.HiddenSnapFolder.IsExported(), Equals, true)
    80  	c.Check(features.CheckDiskSpaceInstall.IsExported(), Equals, false)
    81  	c.Check(features.CheckDiskSpaceRefresh.IsExported(), Equals, false)
    82  	c.Check(features.CheckDiskSpaceRemove.IsExported(), Equals, false)
    83  }
    84  
    85  func (*featureSuite) TestIsEnabled(c *C) {
    86  	dirs.SetRootDir(c.MkDir())
    87  	defer dirs.SetRootDir("")
    88  
    89  	// If the feature file is absent then the feature is disabled.
    90  	f := features.PerUserMountNamespace
    91  	c.Check(f.IsEnabled(), Equals, false)
    92  
    93  	// If the feature file is a regular file then the feature is enabled.
    94  	err := os.MkdirAll(dirs.FeaturesDir, 0755)
    95  	c.Assert(err, IsNil)
    96  	err = ioutil.WriteFile(filepath.Join(dirs.FeaturesDir, f.String()), nil, 0644)
    97  	c.Assert(err, IsNil)
    98  	c.Check(f.IsEnabled(), Equals, true)
    99  
   100  	// Features that are not exported cannot be queried.
   101  	c.Check(features.Layouts.IsEnabled, PanicMatches, `cannot check if feature "layouts" is enabled because that feature is not exported`)
   102  }
   103  
   104  func (*featureSuite) TestIsEnabledWhenUnset(c *C) {
   105  	c.Check(features.Layouts.IsEnabledWhenUnset(), Equals, true)
   106  	c.Check(features.ParallelInstances.IsEnabledWhenUnset(), Equals, false)
   107  	c.Check(features.Hotplug.IsEnabledWhenUnset(), Equals, false)
   108  	c.Check(features.SnapdSnap.IsEnabledWhenUnset(), Equals, false)
   109  	c.Check(features.PerUserMountNamespace.IsEnabledWhenUnset(), Equals, false)
   110  	c.Check(features.RefreshAppAwareness.IsEnabledWhenUnset(), Equals, false)
   111  	c.Check(features.ClassicPreservesXdgRuntimeDir.IsEnabledWhenUnset(), Equals, false)
   112  	c.Check(features.RobustMountNamespaceUpdates.IsEnabledWhenUnset(), Equals, true)
   113  	c.Check(features.UserDaemons.IsEnabledWhenUnset(), Equals, false)
   114  	c.Check(features.DbusActivation.IsEnabledWhenUnset(), Equals, false)
   115  	c.Check(features.HiddenSnapFolder.IsEnabledWhenUnset(), Equals, false)
   116  	c.Check(features.CheckDiskSpaceInstall.IsEnabledWhenUnset(), Equals, false)
   117  	c.Check(features.CheckDiskSpaceRefresh.IsEnabledWhenUnset(), Equals, false)
   118  	c.Check(features.CheckDiskSpaceRemove.IsEnabledWhenUnset(), Equals, false)
   119  }
   120  
   121  func (*featureSuite) TestControlFile(c *C) {
   122  	c.Check(features.PerUserMountNamespace.ControlFile(), Equals, "/var/lib/snapd/features/per-user-mount-namespace")
   123  	c.Check(features.RefreshAppAwareness.ControlFile(), Equals, "/var/lib/snapd/features/refresh-app-awareness")
   124  	c.Check(features.ParallelInstances.ControlFile(), Equals, "/var/lib/snapd/features/parallel-instances")
   125  	c.Check(features.RobustMountNamespaceUpdates.ControlFile(), Equals, "/var/lib/snapd/features/robust-mount-namespace-updates")
   126  	c.Check(features.HiddenSnapFolder.ControlFile(), Equals, "/var/lib/snapd/features/hidden-snap-folder")
   127  	// Features that are not exported don't have a control file.
   128  	c.Check(features.Layouts.ControlFile, PanicMatches, `cannot compute the control file of feature "layouts" because that feature is not exported`)
   129  }
   130  
   131  func (*featureSuite) TestConfigOptionLayouts(c *C) {
   132  	snapName, configName := features.Layouts.ConfigOption()
   133  	c.Check(snapName, Equals, "core")
   134  	c.Check(configName, Equals, "experimental.layouts")
   135  }
   136  
   137  func (*featureSuite) TestConfigOptionRefreshAppAwareness(c *C) {
   138  	snapName, configName := features.RefreshAppAwareness.ConfigOption()
   139  	c.Check(snapName, Equals, "core")
   140  	c.Check(configName, Equals, "experimental.refresh-app-awareness")
   141  }
   142  
   143  func (s *featureSuite) TestFlag(c *C) {
   144  	st := state.New(nil)
   145  	st.Lock()
   146  	defer st.Unlock()
   147  	tr := config.NewTransaction(st)
   148  
   149  	// Feature flags have a value even if unset.
   150  	flag, err := features.Flag(tr, features.Layouts)
   151  	c.Assert(err, IsNil)
   152  	c.Check(flag, Equals, true)
   153  
   154  	// Feature flags can be disabled.
   155  	c.Assert(tr.Set("core", "experimental.layouts", "false"), IsNil)
   156  	flag, err = features.Flag(tr, features.Layouts)
   157  	c.Assert(err, IsNil)
   158  	c.Check(flag, Equals, false)
   159  
   160  	// Feature flags can be enabled.
   161  	c.Assert(tr.Set("core", "experimental.layouts", "true"), IsNil)
   162  	flag, err = features.Flag(tr, features.Layouts)
   163  	c.Assert(err, IsNil)
   164  	c.Check(flag, Equals, true)
   165  
   166  	// Feature flags must have a well-known value.
   167  	c.Assert(tr.Set("core", "experimental.layouts", "banana"), IsNil)
   168  	_, err = features.Flag(tr, features.Layouts)
   169  	c.Assert(err, ErrorMatches, `layouts can only be set to 'true' or 'false', got "banana"`)
   170  }