1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2018-2024 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package features_test
import (
"os"
"path/filepath"
"testing"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/features"
"github.com/snapcore/snapd/overlord/configstate/config"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/systemd"
)
func Test(t *testing.T) { TestingT(t) }
type featureSuite struct{}
var _ = Suite(&featureSuite{})
func (*featureSuite) TestName(c *C) {
var tested int
check := func(f features.SnapdFeature, name string) {
c.Check(f.String(), Equals, name)
tested++
}
check(features.Layouts, "layouts")
check(features.ParallelInstances, "parallel-instances")
check(features.Hotplug, "hotplug")
check(features.PerUserMountNamespace, "per-user-mount-namespace")
check(features.RefreshAppAwareness, "refresh-app-awareness")
check(features.ClassicPreservesXdgRuntimeDir, "classic-preserves-xdg-runtime-dir")
check(features.UserDaemons, "user-daemons")
check(features.DbusActivation, "dbus-activation")
check(features.HiddenSnapDataHomeDir, "hidden-snap-folder")
check(features.MoveSnapHomeDir, "move-snap-home-dir")
check(features.CheckDiskSpaceInstall, "check-disk-space-install")
check(features.CheckDiskSpaceRefresh, "check-disk-space-refresh")
check(features.CheckDiskSpaceRemove, "check-disk-space-remove")
check(features.GateAutoRefreshHook, "gate-auto-refresh-hook")
check(features.QuotaGroups, "quota-groups")
check(features.RefreshAppAwarenessUX, "refresh-app-awareness-ux")
check(features.Confdb, "confdb")
check(features.ConfdbControl, "confdb-control")
check(features.AppArmorPrompting, "apparmor-prompting")
c.Check(tested, Equals, features.NumberOfFeatures())
c.Check(func() { _ = features.SnapdFeature(1000).String() }, PanicMatches, "unknown feature flag code 1000")
}
func (*featureSuite) TestKnownFeatures(c *C) {
// Check that known features have names.
known := features.KnownFeatures()
for _, f := range known {
c.Check(f.String(), Not(Equals), "", Commentf("feature code: %d", int(f)))
}
c.Check(known, HasLen, features.NumberOfFeatures())
}
func (*featureSuite) TestIsExported(c *C) {
var tested int
check := func(f features.SnapdFeature, exported bool) {
c.Check(f.IsExported(), Equals, exported)
tested++
}
check(features.Layouts, false)
check(features.Hotplug, false)
check(features.ParallelInstances, true)
check(features.PerUserMountNamespace, true)
check(features.RefreshAppAwareness, true)
check(features.ClassicPreservesXdgRuntimeDir, true)
check(features.UserDaemons, false)
check(features.DbusActivation, false)
check(features.HiddenSnapDataHomeDir, true)
check(features.MoveSnapHomeDir, true)
check(features.CheckDiskSpaceInstall, false)
check(features.CheckDiskSpaceRefresh, false)
check(features.CheckDiskSpaceRemove, false)
check(features.GateAutoRefreshHook, false)
check(features.QuotaGroups, false)
check(features.RefreshAppAwarenessUX, true)
check(features.Confdb, true)
check(features.ConfdbControl, false)
check(features.AppArmorPrompting, true)
c.Check(tested, Equals, features.NumberOfFeatures())
}
func (*featureSuite) TestQuotaGroupsSupportedCallback(c *C) {
callback, exists := features.FeaturesSupportedCallbacks[features.QuotaGroups]
c.Assert(exists, Equals, true)
restore1 := systemd.MockSystemdVersion(229, nil)
defer restore1()
supported, reason := callback()
c.Check(supported, Equals, false)
c.Check(reason, Matches, "systemd version 229 is too old.*")
restore2 := systemd.MockSystemdVersion(230, nil)
defer restore2()
supported, reason = callback()
c.Check(supported, Equals, true)
c.Check(reason, Equals, "")
}
func (*featureSuite) TestUserDaemonsSupportedCallback(c *C) {
callback, exists := features.FeaturesSupportedCallbacks[features.UserDaemons]
c.Assert(exists, Equals, true)
restore1 := features.MockReleaseSystemctlSupportsUserUnits(func() bool { return false })
defer restore1()
supported, reason := callback()
c.Check(supported, Equals, false)
c.Check(reason, Matches, "user session daemons are not supported.*")
restore2 := features.MockReleaseSystemctlSupportsUserUnits(func() bool { return true })
defer restore2()
supported, reason = callback()
c.Check(supported, Equals, true)
c.Check(reason, Equals, "")
}
func (*featureSuite) TestIsSupported(c *C) {
fakeFeature := features.SnapdFeature(len(features.KnownFeatures()))
// Check that feature without callback always returns true
is, why := fakeFeature.IsSupported()
c.Check(is, Equals, true)
c.Check(why, Equals, "")
var fakeSupported bool
var fakeReason string
restore := features.MockFeaturesSupportedCallbacks(map[features.SnapdFeature]func() (bool, string){
fakeFeature: func() (bool, string) { return fakeSupported, fakeReason },
})
defer restore()
fakeSupported = true
fakeReason = ""
is, why = fakeFeature.IsSupported()
c.Check(is, Equals, true)
c.Check(why, Equals, "")
// Check that a non-empty reason is ignored
fakeSupported = true
fakeReason = "foo"
is, why = fakeFeature.IsSupported()
c.Check(is, Equals, true)
c.Check(why, Equals, "")
fakeSupported = false
fakeReason = "foo"
is, why = fakeFeature.IsSupported()
c.Check(is, Equals, false)
c.Check(why, Equals, "foo")
// Check that unsupported value does not require reason
fakeSupported = false
fakeReason = ""
is, why = fakeFeature.IsSupported()
c.Check(is, Equals, false)
c.Check(why, Equals, "")
}
func (*featureSuite) TestIsEnabled(c *C) {
dirs.SetRootDir(c.MkDir())
defer dirs.SetRootDir("")
// If the feature file is absent then the feature is disabled.
f := features.PerUserMountNamespace
c.Check(f.IsEnabled(), Equals, false)
// If the feature file is a regular file then the feature is enabled.
err := os.MkdirAll(dirs.FeaturesDir, 0755)
c.Assert(err, IsNil)
err = os.WriteFile(filepath.Join(dirs.FeaturesDir, f.String()), nil, 0644)
c.Assert(err, IsNil)
c.Check(f.IsEnabled(), Equals, true)
// Features that are not exported cannot be queried.
c.Check(features.Layouts.IsEnabled, PanicMatches, `cannot check if feature "layouts" is enabled because that feature is not exported`)
}
func (*featureSuite) TestIsEnabledWhenUnset(c *C) {
var tested int
check := func(f features.SnapdFeature, enabledUnset bool) {
c.Check(f.IsEnabledWhenUnset(), Equals, enabledUnset)
tested++
}
check(features.Layouts, true)
check(features.ParallelInstances, false)
check(features.Hotplug, false)
check(features.PerUserMountNamespace, false)
check(features.RefreshAppAwareness, true)
check(features.ClassicPreservesXdgRuntimeDir, true)
check(features.UserDaemons, false)
check(features.DbusActivation, true)
check(features.HiddenSnapDataHomeDir, false)
check(features.MoveSnapHomeDir, false)
check(features.CheckDiskSpaceInstall, false)
check(features.CheckDiskSpaceRefresh, false)
check(features.CheckDiskSpaceRemove, false)
check(features.GateAutoRefreshHook, false)
check(features.QuotaGroups, false)
check(features.RefreshAppAwarenessUX, false)
check(features.Confdb, false)
check(features.AppArmorPrompting, false)
check(features.ConfdbControl, false)
c.Check(tested, Equals, features.NumberOfFeatures())
}
func (*featureSuite) TestControlFile(c *C) {
c.Check(features.PerUserMountNamespace.ControlFile(), Equals, "/var/lib/snapd/features/per-user-mount-namespace")
c.Check(features.RefreshAppAwareness.ControlFile(), Equals, "/var/lib/snapd/features/refresh-app-awareness")
c.Check(features.ParallelInstances.ControlFile(), Equals, "/var/lib/snapd/features/parallel-instances")
c.Check(features.HiddenSnapDataHomeDir.ControlFile(), Equals, "/var/lib/snapd/features/hidden-snap-folder")
c.Check(features.MoveSnapHomeDir.ControlFile(), Equals, "/var/lib/snapd/features/move-snap-home-dir")
c.Check(features.RefreshAppAwarenessUX.ControlFile(), Equals, "/var/lib/snapd/features/refresh-app-awareness-ux")
c.Check(features.Confdb.ControlFile(), Equals, "/var/lib/snapd/features/confdb")
c.Check(features.AppArmorPrompting.ControlFile(), Equals, "/var/lib/snapd/features/apparmor-prompting")
// Features that are not exported don't have a control file.
c.Check(features.Layouts.ControlFile, PanicMatches, `cannot compute the control file of feature "layouts" because that feature is not exported`)
}
func (*featureSuite) TestConfigOptionLayouts(c *C) {
snapName, configName := features.Layouts.ConfigOption()
c.Check(snapName, Equals, "core")
c.Check(configName, Equals, "experimental.layouts")
}
func (*featureSuite) TestConfigOptionRefreshAppAwareness(c *C) {
snapName, configName := features.RefreshAppAwareness.ConfigOption()
c.Check(snapName, Equals, "core")
c.Check(configName, Equals, "experimental.refresh-app-awareness")
}
func (*featureSuite) TestConfigOptionRefreshAppAwarenessUX(c *C) {
snapName, configName := features.RefreshAppAwarenessUX.ConfigOption()
c.Check(snapName, Equals, "core")
c.Check(configName, Equals, "experimental.refresh-app-awareness-ux")
}
func (s *featureSuite) TestFlag(c *C) {
st := state.New(nil)
st.Lock()
defer st.Unlock()
tr := config.NewTransaction(st)
// Feature flags have a value even if unset.
flag, err := features.Flag(tr, features.Layouts)
c.Assert(err, IsNil)
c.Check(flag, Equals, true)
// Feature flags can be disabled.
c.Assert(tr.Set("core", "experimental.layouts", "false"), IsNil)
flag, err = features.Flag(tr, features.Layouts)
c.Assert(err, IsNil)
c.Check(flag, Equals, false)
// Feature flags can be enabled.
c.Assert(tr.Set("core", "experimental.layouts", "true"), IsNil)
flag, err = features.Flag(tr, features.Layouts)
c.Assert(err, IsNil)
c.Check(flag, Equals, true)
// Feature flags must have a well-known value.
c.Assert(tr.Set("core", "experimental.layouts", "banana"), IsNil)
_, err = features.Flag(tr, features.Layouts)
c.Assert(err, ErrorMatches, `layouts can only be set to 'true' or 'false', got "banana"`)
}
func (s *featureSuite) TestAll(c *C) {
st := state.New(nil)
st.Lock()
defer st.Unlock()
tr := config.NewTransaction(st)
fakeFeature := features.SnapdFeature(features.NumberOfFeatures())
fakeFeatureUnsupported := features.SnapdFeature(features.NumberOfFeatures() + 1)
fakeFeatureUnsetNoCallback := features.SnapdFeature(features.NumberOfFeatures() + 2)
fakeFeatureDisabled := features.SnapdFeature(features.NumberOfFeatures() + 3)
fakeFeatureBadFlag := features.SnapdFeature(features.NumberOfFeatures() + 4)
fakeFeatureUnsupportedUnset := features.SnapdFeature(features.NumberOfFeatures() + 5)
restore1 := features.MockKnownFeaturesImpl(func() []features.SnapdFeature {
return []features.SnapdFeature{fakeFeature, fakeFeatureUnsupported, fakeFeatureUnsetNoCallback, fakeFeatureDisabled, fakeFeatureBadFlag, fakeFeatureUnsupportedUnset}
})
defer restore1()
restore2 := features.MockFeatureNames(map[features.SnapdFeature]string{
fakeFeature: "fake-feature",
fakeFeatureUnsupported: "fake-feature-unsupported",
fakeFeatureUnsetNoCallback: "fake-feature-disabled",
fakeFeatureDisabled: "fake-feature-set-disabled",
fakeFeatureBadFlag: "fake-feature-bad-flag",
fakeFeatureUnsupportedUnset: "fake-feature-unsupported-unset",
})
defer restore2()
unsupportedReason := "foo"
restore3 := features.MockFeaturesSupportedCallbacks(map[features.SnapdFeature]func() (bool, string){
fakeFeature: func() (bool, string) { return true, unsupportedReason },
fakeFeatureUnsupported: func() (bool, string) { return false, unsupportedReason },
fakeFeatureDisabled: func() (bool, string) { return true, unsupportedReason },
fakeFeatureBadFlag: func() (bool, string) { return true, unsupportedReason },
fakeFeatureUnsupportedUnset: func() (bool, string) { return false, unsupportedReason },
})
defer restore3()
// Enable the two enabled fake features
c.Assert(tr.Set("core", "experimental."+fakeFeature.String(), "true"), IsNil)
c.Assert(tr.Set("core", "experimental."+fakeFeatureUnsupported.String(), "true"), IsNil)
c.Assert(tr.Set("core", "experimental."+fakeFeatureDisabled.String(), "false"), IsNil)
c.Assert(tr.Set("core", "experimental."+fakeFeatureBadFlag.String(), "banana"), IsNil)
allFeaturesInfo := features.All(tr)
c.Assert(len(allFeaturesInfo), Equals, 5)
// Feature flags are included even if value unset
fakeFeatureInfo, exists := allFeaturesInfo[fakeFeatureUnsetNoCallback.String()]
c.Assert(exists, Equals, true)
// Feature flags are supported even if no callback defined.
c.Check(fakeFeatureInfo.Supported, Equals, true)
// Feature flags have a value even if unset.
c.Check(fakeFeatureInfo.Enabled, Equals, false)
// A feature can be both unset and unsupported
fakeFeatureInfo, exists = allFeaturesInfo[fakeFeatureUnsupportedUnset.String()]
c.Assert(exists, Equals, true)
c.Check(fakeFeatureInfo.Supported, Equals, false)
c.Check(fakeFeatureInfo.Enabled, Equals, false)
// Feature flags with defined supported callbacks work correctly.
// Feature flags can be enabled but unsupported.
fakeFeatureInfo, exists = allFeaturesInfo[fakeFeatureUnsupported.String()]
c.Assert(exists, Equals, true)
// Callbacks which return false result in Supported: false
c.Check(fakeFeatureInfo.Supported, Equals, false)
c.Check(fakeFeatureInfo.UnsupportedReason, Matches, unsupportedReason)
c.Check(fakeFeatureInfo.Enabled, Equals, true)
// Callbacks which return true result in Supported: true
fakeFeatureInfo, exists = allFeaturesInfo[fakeFeature.String()]
c.Assert(exists, Equals, true)
c.Check(fakeFeatureInfo.Supported, Equals, true)
c.Check(fakeFeatureInfo.UnsupportedReason, Equals, "")
c.Check(fakeFeatureInfo.Enabled, Equals, true)
// Feature flags can be disabled but supported.
fakeFeatureInfo, exists = allFeaturesInfo[fakeFeatureDisabled.String()]
c.Assert(exists, Equals, true)
c.Check(fakeFeatureInfo.Supported, Equals, true)
c.Check(fakeFeatureInfo.UnsupportedReason, Equals, "")
c.Check(fakeFeatureInfo.Enabled, Equals, false)
// Feature flags with bad values are omitted, even if supported.
fakeFeatureInfo, exists = allFeaturesInfo[fakeFeatureBadFlag.String()]
c.Assert(exists, Equals, false)
}
|