gitee.com/mysnapcore/mysnapd@v0.1.0/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  	"gitee.com/mysnapcore/mysnapd/dirs"
    30  	"gitee.com/mysnapcore/mysnapd/features"
    31  	"gitee.com/mysnapcore/mysnapd/overlord/configstate/config"
    32  	"gitee.com/mysnapcore/mysnapd/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.HiddenSnapDataHomeDir.String(), Equals, "hidden-snap-folder")
    53  	c.Check(features.MoveSnapHomeDir.String(), Equals, "move-snap-home-dir")
    54  	c.Check(features.CheckDiskSpaceInstall.String(), Equals, "check-disk-space-install")
    55  	c.Check(features.CheckDiskSpaceRefresh.String(), Equals, "check-disk-space-refresh")
    56  	c.Check(features.CheckDiskSpaceRemove.String(), Equals, "check-disk-space-remove")
    57  	c.Check(features.GateAutoRefreshHook.String(), Equals, "gate-auto-refresh-hook")
    58  	c.Check(features.QuotaGroups.String(), Equals, "quota-groups")
    59  	c.Check(func() { _ = features.SnapdFeature(1000).String() }, PanicMatches, "unknown feature flag code 1000")
    60  }
    61  
    62  func (*featureSuite) TestKnownFeatures(c *C) {
    63  	// Check that known features have names.
    64  	known := features.KnownFeatures()
    65  	for _, f := range known {
    66  		c.Check(f.String(), Not(Equals), "", Commentf("feature code: %d", int(f)))
    67  	}
    68  	c.Check(known, HasLen, features.NumberOfFeatures())
    69  }
    70  
    71  func (*featureSuite) TestIsExported(c *C) {
    72  	c.Check(features.Layouts.IsExported(), Equals, false)
    73  	c.Check(features.Hotplug.IsExported(), Equals, false)
    74  	c.Check(features.SnapdSnap.IsExported(), Equals, false)
    75  
    76  	c.Check(features.ParallelInstances.IsExported(), Equals, true)
    77  	c.Check(features.PerUserMountNamespace.IsExported(), Equals, true)
    78  	c.Check(features.RefreshAppAwareness.IsExported(), Equals, true)
    79  	c.Check(features.ClassicPreservesXdgRuntimeDir.IsExported(), Equals, true)
    80  	c.Check(features.UserDaemons.IsExported(), Equals, false)
    81  	c.Check(features.DbusActivation.IsExported(), Equals, false)
    82  	c.Check(features.HiddenSnapDataHomeDir.IsExported(), Equals, true)
    83  	c.Check(features.MoveSnapHomeDir.IsExported(), Equals, true)
    84  	c.Check(features.CheckDiskSpaceInstall.IsExported(), Equals, false)
    85  	c.Check(features.CheckDiskSpaceRefresh.IsExported(), Equals, false)
    86  	c.Check(features.CheckDiskSpaceRemove.IsExported(), Equals, false)
    87  	c.Check(features.GateAutoRefreshHook.IsExported(), Equals, false)
    88  }
    89  
    90  func (*featureSuite) TestIsEnabled(c *C) {
    91  	dirs.SetRootDir(c.MkDir())
    92  	defer dirs.SetRootDir("")
    93  
    94  	// If the feature file is absent then the feature is disabled.
    95  	f := features.PerUserMountNamespace
    96  	c.Check(f.IsEnabled(), Equals, false)
    97  
    98  	// If the feature file is a regular file then the feature is enabled.
    99  	err := os.MkdirAll(dirs.FeaturesDir, 0755)
   100  	c.Assert(err, IsNil)
   101  	err = ioutil.WriteFile(filepath.Join(dirs.FeaturesDir, f.String()), nil, 0644)
   102  	c.Assert(err, IsNil)
   103  	c.Check(f.IsEnabled(), Equals, true)
   104  
   105  	// Features that are not exported cannot be queried.
   106  	c.Check(features.Layouts.IsEnabled, PanicMatches, `cannot check if feature "layouts" is enabled because that feature is not exported`)
   107  }
   108  
   109  func (*featureSuite) TestIsEnabledWhenUnset(c *C) {
   110  	c.Check(features.Layouts.IsEnabledWhenUnset(), Equals, true)
   111  	c.Check(features.ParallelInstances.IsEnabledWhenUnset(), Equals, false)
   112  	c.Check(features.Hotplug.IsEnabledWhenUnset(), Equals, false)
   113  	c.Check(features.SnapdSnap.IsEnabledWhenUnset(), Equals, false)
   114  	c.Check(features.PerUserMountNamespace.IsEnabledWhenUnset(), Equals, false)
   115  	c.Check(features.RefreshAppAwareness.IsEnabledWhenUnset(), Equals, true)
   116  	c.Check(features.ClassicPreservesXdgRuntimeDir.IsEnabledWhenUnset(), Equals, true)
   117  	c.Check(features.RobustMountNamespaceUpdates.IsEnabledWhenUnset(), Equals, true)
   118  	c.Check(features.UserDaemons.IsEnabledWhenUnset(), Equals, false)
   119  	c.Check(features.DbusActivation.IsEnabledWhenUnset(), Equals, true)
   120  	c.Check(features.HiddenSnapDataHomeDir.IsEnabledWhenUnset(), Equals, false)
   121  	c.Check(features.MoveSnapHomeDir.IsEnabledWhenUnset(), Equals, false)
   122  	c.Check(features.CheckDiskSpaceInstall.IsEnabledWhenUnset(), Equals, false)
   123  	c.Check(features.CheckDiskSpaceRefresh.IsEnabledWhenUnset(), Equals, false)
   124  	c.Check(features.CheckDiskSpaceRemove.IsEnabledWhenUnset(), Equals, false)
   125  	c.Check(features.GateAutoRefreshHook.IsEnabledWhenUnset(), Equals, false)
   126  }
   127  
   128  func (*featureSuite) TestControlFile(c *C) {
   129  	c.Check(features.PerUserMountNamespace.ControlFile(), Equals, "/var/lib/snapd/features/per-user-mount-namespace")
   130  	c.Check(features.RefreshAppAwareness.ControlFile(), Equals, "/var/lib/snapd/features/refresh-app-awareness")
   131  	c.Check(features.ParallelInstances.ControlFile(), Equals, "/var/lib/snapd/features/parallel-instances")
   132  	c.Check(features.RobustMountNamespaceUpdates.ControlFile(), Equals, "/var/lib/snapd/features/robust-mount-namespace-updates")
   133  	c.Check(features.HiddenSnapDataHomeDir.ControlFile(), Equals, "/var/lib/snapd/features/hidden-snap-folder")
   134  	c.Check(features.MoveSnapHomeDir.ControlFile(), Equals, "/var/lib/snapd/features/move-snap-home-dir")
   135  	// Features that are not exported don't have a control file.
   136  	c.Check(features.Layouts.ControlFile, PanicMatches, `cannot compute the control file of feature "layouts" because that feature is not exported`)
   137  }
   138  
   139  func (*featureSuite) TestConfigOptionLayouts(c *C) {
   140  	snapName, configName := features.Layouts.ConfigOption()
   141  	c.Check(snapName, Equals, "core")
   142  	c.Check(configName, Equals, "experimental.layouts")
   143  }
   144  
   145  func (*featureSuite) TestConfigOptionRefreshAppAwareness(c *C) {
   146  	snapName, configName := features.RefreshAppAwareness.ConfigOption()
   147  	c.Check(snapName, Equals, "core")
   148  	c.Check(configName, Equals, "experimental.refresh-app-awareness")
   149  }
   150  
   151  func (s *featureSuite) TestFlag(c *C) {
   152  	st := state.New(nil)
   153  	st.Lock()
   154  	defer st.Unlock()
   155  	tr := config.NewTransaction(st)
   156  
   157  	// Feature flags have a value even if unset.
   158  	flag, err := features.Flag(tr, features.Layouts)
   159  	c.Assert(err, IsNil)
   160  	c.Check(flag, Equals, true)
   161  
   162  	// Feature flags can be disabled.
   163  	c.Assert(tr.Set("core", "experimental.layouts", "false"), IsNil)
   164  	flag, err = features.Flag(tr, features.Layouts)
   165  	c.Assert(err, IsNil)
   166  	c.Check(flag, Equals, false)
   167  
   168  	// Feature flags can be enabled.
   169  	c.Assert(tr.Set("core", "experimental.layouts", "true"), IsNil)
   170  	flag, err = features.Flag(tr, features.Layouts)
   171  	c.Assert(err, IsNil)
   172  	c.Check(flag, Equals, true)
   173  
   174  	// Feature flags must have a well-known value.
   175  	c.Assert(tr.Set("core", "experimental.layouts", "banana"), IsNil)
   176  	_, err = features.Flag(tr, features.Layouts)
   177  	c.Assert(err, ErrorMatches, `layouts can only be set to 'true' or 'false', got "banana"`)
   178  }