github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/pkg/testutil/helpers_unix.go (about)

     1  // +build !windows
     2  
     3  /*
     4     Copyright The containerd Authors.
     5  
     6     Licensed under the Apache License, Version 2.0 (the "License");
     7     you may not use this file except in compliance with the License.
     8     You may obtain a copy of the License at
     9  
    10         http://www.apache.org/licenses/LICENSE-2.0
    11  
    12     Unless required by applicable law or agreed to in writing, software
    13     distributed under the License is distributed on an "AS IS" BASIS,
    14     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15     See the License for the specific language governing permissions and
    16     limitations under the License.
    17  */
    18  
    19  package testutil
    20  
    21  import (
    22  	"fmt"
    23  	"os"
    24  	"testing"
    25  
    26  	"github.com/containerd/containerd/mount"
    27  	"gotest.tools/v3/assert"
    28  )
    29  
    30  // Unmount unmounts a given mountPoint and sets t.Error if it fails
    31  func Unmount(t testing.TB, mountPoint string) {
    32  	t.Log("unmount", mountPoint)
    33  	err := mount.UnmountAll(mountPoint, umountflags)
    34  	assert.NilError(t, err)
    35  }
    36  
    37  // RequiresRoot skips tests that require root, unless the test.root flag has
    38  // been set
    39  func RequiresRoot(t testing.TB) {
    40  	if !rootEnabled {
    41  		t.Skip("skipping test that requires root")
    42  	}
    43  	assert.Equal(t, 0, os.Getuid(), "This test must be run as root.")
    44  }
    45  
    46  // RequiresRootM is similar to RequiresRoot but intended to be called from *testing.M.
    47  func RequiresRootM() {
    48  	if !rootEnabled {
    49  		fmt.Fprintln(os.Stderr, "skipping test that requires root")
    50  		os.Exit(0)
    51  	}
    52  	if os.Getuid() != 0 {
    53  		fmt.Fprintln(os.Stderr, "This test must be run as root.")
    54  		os.Exit(1)
    55  	}
    56  }