Skip to content

Commit

Permalink
features: forbid checking control file or status of unexported features
Browse files Browse the repository at this point in the history
Since only some features are exported we should prohibit accidentally
checking features that are not exported. This patch does just that.

Signed-off-by: Zygmunt Krynicki <me@zygoon.pl>
  • Loading branch information
zyga committed Dec 3, 2018
1 parent 79e36a0 commit d9c3b68
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 7 additions & 1 deletion features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
7 changes: 6 additions & 1 deletion features/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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`)
}

0 comments on commit d9c3b68

Please sign in to comment.