github.com/rigado/snapd@v2.42.5-go-mod+incompatible/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 ) 32 33 func Test(t *testing.T) { TestingT(t) } 34 35 type featureSuite struct{} 36 37 var _ = Suite(&featureSuite{}) 38 39 func (*featureSuite) TestName(c *C) { 40 c.Check(features.Layouts.String(), Equals, "layouts") 41 c.Check(features.ParallelInstances.String(), Equals, "parallel-instances") 42 c.Check(features.Hotplug.String(), Equals, "hotplug") 43 c.Check(features.SnapdSnap.String(), Equals, "snapd-snap") 44 c.Check(features.PerUserMountNamespace.String(), Equals, "per-user-mount-namespace") 45 c.Check(features.RefreshAppAwareness.String(), Equals, "refresh-app-awareness") 46 c.Check(features.RobustMountNamespaceUpdates.String(), Equals, "robust-mount-namespace-updates") 47 c.Check(func() { _ = features.SnapdFeature(1000).String() }, PanicMatches, "unknown feature flag code 1000") 48 } 49 50 func (*featureSuite) TestKnownFeatures(c *C) { 51 // Check that known features have names. 52 known := features.KnownFeatures() 53 for _, f := range known { 54 c.Check(f.String(), Not(Equals), "", Commentf("feature code: %d", int(f))) 55 } 56 c.Check(known, HasLen, features.NumberOfFeatures()) 57 } 58 59 func (*featureSuite) TestIsExported(c *C) { 60 c.Check(features.Layouts.IsExported(), Equals, false) 61 c.Check(features.Hotplug.IsExported(), Equals, false) 62 c.Check(features.SnapdSnap.IsExported(), Equals, false) 63 64 c.Check(features.ParallelInstances.IsExported(), Equals, true) 65 c.Check(features.PerUserMountNamespace.IsExported(), Equals, true) 66 c.Check(features.RefreshAppAwareness.IsExported(), Equals, true) 67 } 68 69 func (*featureSuite) TestIsEnabled(c *C) { 70 dirs.SetRootDir(c.MkDir()) 71 defer dirs.SetRootDir("") 72 73 // If the feature file is absent then the feature is disabled. 74 f := features.PerUserMountNamespace 75 c.Check(f.IsEnabled(), Equals, false) 76 77 // If the feature file is a regular file then the feature is enabled. 78 err := os.MkdirAll(dirs.FeaturesDir, 0755) 79 c.Assert(err, IsNil) 80 err = ioutil.WriteFile(filepath.Join(dirs.FeaturesDir, f.String()), nil, 0644) 81 c.Assert(err, IsNil) 82 c.Check(f.IsEnabled(), Equals, true) 83 84 // Features that are not exported cannot be queried. 85 c.Check(features.Layouts.IsEnabled, PanicMatches, `cannot check if feature "layouts" is enabled because that feature is not exported`) 86 } 87 88 func (*featureSuite) TestIsEnabledWhenUnset(c *C) { 89 c.Check(features.Layouts.IsEnabledWhenUnset(), Equals, true) 90 c.Check(features.ParallelInstances.IsEnabledWhenUnset(), Equals, false) 91 c.Check(features.Hotplug.IsEnabledWhenUnset(), Equals, false) 92 c.Check(features.SnapdSnap.IsEnabledWhenUnset(), Equals, false) 93 c.Check(features.PerUserMountNamespace.IsEnabledWhenUnset(), Equals, false) 94 c.Check(features.RefreshAppAwareness.IsEnabledWhenUnset(), Equals, false) 95 c.Check(features.RobustMountNamespaceUpdates.IsEnabledWhenUnset(), Equals, false) 96 } 97 98 func (*featureSuite) TestControlFile(c *C) { 99 c.Check(features.PerUserMountNamespace.ControlFile(), Equals, "/var/lib/snapd/features/per-user-mount-namespace") 100 c.Check(features.RefreshAppAwareness.ControlFile(), Equals, "/var/lib/snapd/features/refresh-app-awareness") 101 c.Check(features.ParallelInstances.ControlFile(), Equals, "/var/lib/snapd/features/parallel-instances") 102 c.Check(features.RobustMountNamespaceUpdates.ControlFile(), Equals, "/var/lib/snapd/features/robust-mount-namespace-updates") 103 // Features that are not exported don't have a control file. 104 c.Check(features.Layouts.ControlFile, PanicMatches, `cannot compute the control file of feature "layouts" because that feature is not exported`) 105 } 106 107 func (*featureSuite) TestConfigOptionLayouts(c *C) { 108 snapName, configName := features.Layouts.ConfigOption() 109 c.Check(snapName, Equals, "core") 110 c.Check(configName, Equals, "experimental.layouts") 111 } 112 113 func (*featureSuite) TestConfigOptionRefreshAppAwareness(c *C) { 114 snapName, configName := features.RefreshAppAwareness.ConfigOption() 115 c.Check(snapName, Equals, "core") 116 c.Check(configName, Equals, "experimental.refresh-app-awareness") 117 }