github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/interfaces/apparmor/export_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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 apparmor
    21  
    22  import (
    23  	"os"
    24  
    25  	"github.com/snapcore/snapd/snap"
    26  	"github.com/snapcore/snapd/testutil"
    27  )
    28  
    29  var (
    30  	NsProfile                       = nsProfile
    31  	ProfileGlobs                    = profileGlobs
    32  	SnapConfineFromSnapProfile      = snapConfineFromSnapProfile
    33  	DowngradeConfinement            = downgradeConfinement
    34  	LoadProfiles                    = loadProfiles
    35  	UnloadProfiles                  = unloadProfiles
    36  	MaybeSetNumberOfJobs            = maybeSetNumberOfJobs
    37  	DefaultCoreRuntimeTemplateRules = defaultCoreRuntimeTemplateRules
    38  	DefaultOtherBaseTemplateRules   = defaultOtherBaseTemplateRules
    39  )
    40  
    41  func MockRuntimeNumCPU(new func() int) (restore func()) {
    42  	old := runtimeNumCPU
    43  	runtimeNumCPU = new
    44  	return func() {
    45  		runtimeNumCPU = old
    46  	}
    47  }
    48  
    49  // MockIsRootWritableOverlay mocks the real implementation of osutil.IsRootWritableOverlay
    50  func MockIsRootWritableOverlay(new func() (string, error)) (restore func()) {
    51  	old := isRootWritableOverlay
    52  	isRootWritableOverlay = new
    53  	return func() {
    54  		isRootWritableOverlay = old
    55  	}
    56  }
    57  
    58  // MockProcSelfExe mocks the location of /proc/self/exe read by setupSnapConfineGeneratedPolicy.
    59  func MockProcSelfExe(symlink string) (restore func()) {
    60  	old := procSelfExe
    61  	procSelfExe = symlink
    62  	return func() {
    63  		os.Remove(procSelfExe)
    64  		procSelfExe = old
    65  	}
    66  }
    67  
    68  // MockProfilesPath mocks the file read by LoadedProfiles()
    69  func MockProfilesPath(t *testutil.BaseTest, profiles string) {
    70  	profilesPath = profiles
    71  	t.AddCleanup(func() {
    72  		profilesPath = realProfilesPath
    73  	})
    74  }
    75  
    76  // MockTemplate replaces apprmor template.
    77  //
    78  // NOTE: The real apparmor template is long. For testing it is convenient for
    79  // replace it with a shorter snippet.
    80  func MockTemplate(fakeTemplate string) (restore func()) {
    81  	orig := defaultCoreRuntimeTemplate
    82  	defaultCoreRuntimeTemplate = fakeTemplate
    83  	return func() { defaultCoreRuntimeTemplate = orig }
    84  }
    85  
    86  // MockClassicTemplate replaces the classic apprmor template.
    87  func MockClassicTemplate(fakeTemplate string) (restore func()) {
    88  	orig := classicTemplate
    89  	classicTemplate = fakeTemplate
    90  	return func() { classicTemplate = orig }
    91  }
    92  
    93  // SetSpecScope sets the scope of a given specification
    94  func SetSpecScope(spec *Specification, securityTags []string) (restore func()) {
    95  	return spec.setScope(securityTags)
    96  }
    97  
    98  func MockKernelFeatures(f func() ([]string, error)) (resture func()) {
    99  	old := kernelFeatures
   100  	kernelFeatures = f
   101  	return func() {
   102  		kernelFeatures = old
   103  	}
   104  }
   105  
   106  func MockParserFeatures(f func() ([]string, error)) (resture func()) {
   107  	old := parserFeatures
   108  	parserFeatures = f
   109  	return func() {
   110  		parserFeatures = old
   111  	}
   112  }
   113  
   114  func (b *Backend) SetupSnapConfineReexec(info *snap.Info) error {
   115  	return b.setupSnapConfineReexec(info)
   116  }
   117  
   118  func (s *Specification) SnippetsForTag(tag string) []string {
   119  	return s.snippetsForTag(tag)
   120  }