diff --git a/features/features.go b/features/features.go index df51a40af12..a9fb8e1fa5b 100644 --- a/features/features.go +++ b/features/features.go @@ -97,10 +97,16 @@ func (f SnapdFeature) IsExported() bool { // Snapd considers the feature enabled if the file is present. // The contents of the file are not important. func (f SnapdFeature) ControlFile() string { + if !f.IsExported() { + panic(fmt.Sprintf("cannot compute the control file of feature %q because that feature is not exported", f)) + } return filepath.Join(dirs.FeaturesDir, f.String()) } -// IsEnabled checks if a given snapd feature is enabled. +// IsEnabled checks if a given exported snapd feature is enabled. func (f SnapdFeature) IsEnabled() bool { + if !f.IsExported() { + panic(fmt.Sprintf("cannot check if feature %q is enabled because that feature is not exported", f)) + } return osutil.FileExists(f.ControlFile()) } diff --git a/features/features_test.go b/features/features_test.go index a6a0b9f7716..8b0397d545e 100644 --- a/features/features_test.go +++ b/features/features_test.go @@ -74,6 +74,9 @@ func (*featureSuite) TestIsEnabled(c *C) { err = ioutil.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) TestIsEnabledByDefault(c *C) { @@ -85,5 +88,7 @@ func (*featureSuite) TestIsEnabledByDefault(c *C) { } func (*featureSuite) TestControlFile(c *C) { - c.Check(features.Layouts.ControlFile(), Equals, "/var/lib/snapd/features/layouts") + c.Check(features.PerUserMountNamespace.ControlFile(), Equals, "/var/lib/snapd/features/per-user-mount-namespace") + // 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`) }