github.com/ethanhsieh/snapd@v0.0.0-20210615102523-3db9b8e4edc5/sandbox/forcedevmode_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2020 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  
    20  package sandbox_test
    21  
    22  import (
    23  	"testing"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/sandbox"
    28  	"github.com/snapcore/snapd/sandbox/apparmor"
    29  	"github.com/snapcore/snapd/sandbox/cgroup"
    30  )
    31  
    32  func Test(t *testing.T) { TestingT(t) }
    33  
    34  type forceDevModeSuite struct {
    35  }
    36  
    37  var _ = Suite(&forceDevModeSuite{})
    38  
    39  func (s *forceDevModeSuite) TestForceDevMode(c *C) {
    40  
    41  	runTest := func(apparmorLevel apparmor.LevelType, cgroupVersion int, expect bool) {
    42  		restore := apparmor.MockLevel(apparmorLevel)
    43  		defer restore()
    44  		restore = cgroup.MockVersion(cgroupVersion, nil)
    45  		defer restore()
    46  		devMode := sandbox.ForceDevMode()
    47  		c.Check(devMode, Equals, expect, Commentf("unexpected force-dev-mode for AppArmor level %v cgroup v%v", apparmorLevel, cgroupVersion))
    48  	}
    49  
    50  	for _, tc := range []struct {
    51  		apparmorLevel apparmor.LevelType
    52  		cgroupVersion int
    53  		exp           bool
    54  	}{
    55  		{apparmor.Full, cgroup.V1, false},
    56  		{apparmor.Partial, cgroup.V1, true},
    57  		// unified mode
    58  		{apparmor.Full, cgroup.V2, true},
    59  		{apparmor.Partial, cgroup.V2, true},
    60  	} {
    61  		runTest(tc.apparmorLevel, tc.cgroupVersion, tc.exp)
    62  	}
    63  }
    64  
    65  func (s *forceDevModeSuite) TestMockForceDevMode(c *C) {
    66  	for _, devmode := range []bool{true, false} {
    67  		restore := sandbox.MockForceDevMode(devmode)
    68  		defer restore()
    69  		c.Assert(sandbox.ForceDevMode(), Equals, devmode, Commentf("wrong result for %#v", devmode))
    70  	}
    71  }