github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/pkg/rootless/rootless_test.go (about)

     1  // Copyright (c) 2020 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package rootless
     7  
     8  import (
     9  	"os"
    10  	"testing"
    11  
    12  	"github.com/opencontainers/runc/libcontainer/system"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestIsRootless(t *testing.T) {
    17  	assert := assert.New(t)
    18  	isRootless = nil
    19  
    20  	var rootless bool
    21  	if os.Getuid() != 0 {
    22  		rootless = true
    23  	} else {
    24  		rootless = system.RunningInUserNS()
    25  	}
    26  
    27  	assert.Equal(rootless, isRootlessFunc())
    28  
    29  	SetRootless(true)
    30  	assert.True(isRootlessFunc())
    31  
    32  	SetRootless(false)
    33  	assert.False(isRootlessFunc())
    34  
    35  	isRootless = nil
    36  }