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