github.com/chenbh/concourse/v6@v6.4.2/worker/runtime/rootfs_manager_test.go (about)

     1  package runtime_test
     2  
     3  import (
     4  	"errors"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/chenbh/concourse/v6/worker/runtime"
    10  	"github.com/opencontainers/runtime-spec/specs-go"
    11  	"github.com/stretchr/testify/require"
    12  	"github.com/stretchr/testify/suite"
    13  )
    14  
    15  type RootfsManagerSuite struct {
    16  	suite.Suite
    17  	*require.Assertions
    18  
    19  	rootfsPath string
    20  }
    21  
    22  func (s *RootfsManagerSuite) SetupTest() {
    23  	var err error
    24  
    25  	s.rootfsPath, err = ioutil.TempDir("", "rootfs-mgr")
    26  	s.NoError(err)
    27  }
    28  
    29  func (s *RootfsManagerSuite) TearDownTest() {
    30  	os.RemoveAll(s.rootfsPath)
    31  }
    32  
    33  func (s *RootfsManagerSuite) TestSetupCwdDirAlreadyExists() {
    34  	mkdirCalled := false
    35  	mgr := runtime.NewRootfsManager(
    36  		runtime.WithMkdirAll(func(p string, mode os.FileMode) error {
    37  			mkdirCalled = true
    38  			return nil
    39  		}),
    40  	)
    41  
    42  	path := filepath.Join(s.rootfsPath, "dir")
    43  	err := os.MkdirAll(path, 0755)
    44  	s.NoError(err)
    45  
    46  	err = mgr.SetupCwd(s.rootfsPath, "dir")
    47  	s.NoError(err)
    48  	s.False(mkdirCalled, "does not call mkdir")
    49  }
    50  
    51  func (s *RootfsManagerSuite) TestSetupCwdCreatePathsRecursivelyByDefault() {
    52  	mgr := runtime.NewRootfsManager()
    53  
    54  	err := mgr.SetupCwd(s.rootfsPath, "/this/that")
    55  	s.NoError(err)
    56  
    57  	finfo, err := os.Stat(filepath.Join(s.rootfsPath, "this", "that"))
    58  	s.NoError(err)
    59  	s.True(finfo.IsDir())
    60  }
    61  
    62  func (s *RootfsManagerSuite) TestSetupCwdWithoutIDMappings() {
    63  	var (
    64  		path, expectedPath             = "", filepath.Join(s.rootfsPath, "dir")
    65  		mode, expectedMode os.FileMode = 0000, 0777
    66  	)
    67  
    68  	mgr := runtime.NewRootfsManager(
    69  		runtime.WithMkdirAll(func(p string, m os.FileMode) error {
    70  			path = p
    71  			mode = m
    72  			return nil
    73  		}),
    74  	)
    75  
    76  	err := mgr.SetupCwd(s.rootfsPath, "dir")
    77  	s.NoError(err)
    78  
    79  	s.Equal(expectedPath, path)
    80  	s.Equal(expectedMode, mode)
    81  }
    82  
    83  func (s *RootfsManagerSuite) TestLookupUserReturnsUserInfo() {
    84  	mgr := runtime.NewRootfsManager()
    85  	s.writeEtcPasswd(`
    86  		root:*:0:0:System Administrator:/var/root:/bin/sh
    87  		some_user_name:*:1:1:Some User:/var/root:/usr/bin/false
    88  	`)
    89  	actualUser, ok, err := mgr.LookupUser(s.rootfsPath, "some_user_name")
    90  	s.NoError(err)
    91  
    92  	s.True(ok)
    93  	expectedUser := specs.User{
    94  		UID: 1,
    95  		GID: 1,
    96  	}
    97  	s.Equal(expectedUser, actualUser)
    98  }
    99  
   100  func (s *RootfsManagerSuite) TestLookupUserUsernameNotFound() {
   101  	mgr := runtime.NewRootfsManager()
   102  
   103  	s.writeEtcPasswd(`
   104  		root:*:0:0:System Administrator:/var/root:/bin/sh
   105  		some_user_name:*:1:1:Some User:/var/root:/usr/bin/false
   106  	`)
   107  
   108  	_, ok, err := mgr.LookupUser(s.rootfsPath, "missing_username")
   109  	s.NoError(err)
   110  	s.False(ok)
   111  }
   112  
   113  func (s *RootfsManagerSuite) TestLookupUserInvalidUID() {
   114  	mgr := runtime.NewRootfsManager()
   115  
   116  	s.writeEtcPasswd(`
   117  		some_user_name:*:NaN:0:System Administrator:/var/root:/bin/sh
   118  	`)
   119  
   120  	_, _, err := mgr.LookupUser(s.rootfsPath, "some_user_name")
   121  	s.True(errors.Is(err, runtime.InvalidUidError{UID: "NaN"}))
   122  }
   123  
   124  func (s *RootfsManagerSuite) TestLookupUserInvalidGID() {
   125  	mgr := runtime.NewRootfsManager()
   126  
   127  	s.writeEtcPasswd(`
   128  		some_user_name:*:0:NaN:System Administrator:/var/root:/bin/sh
   129  	`)
   130  
   131  	_, _, err := mgr.LookupUser(s.rootfsPath, "some_user_name")
   132  	s.True(errors.Is(err, runtime.InvalidGidError{GID: "NaN"}))
   133  }
   134  
   135  func (s *RootfsManagerSuite) TestLookupUserEtcPasswdNotFound() {
   136  	mgr := runtime.NewRootfsManager()
   137  
   138  	_, _, err := mgr.LookupUser(s.rootfsPath, "username")
   139  	s.Error(err)
   140  }
   141  
   142  func (s *RootfsManagerSuite) TestLookupUserIgnoreNonUserInfo() {
   143  	mgr := runtime.NewRootfsManager()
   144  	s.writeEtcPasswd(`
   145  		#This is etc passwd
   146  		root:*:0:0:System Administrator:/var/root:/bin/sh
   147  		some_user_name:*:1
   148  
   149  		some_user_name:*:1:1:Some User:/var/root:/usr/bin/false
   150  	`)
   151  	_, ok, err := mgr.LookupUser(s.rootfsPath, "some_user_name")
   152  	s.NoError(err)
   153  	s.True(ok)
   154  }
   155  
   156  func (s *RootfsManagerSuite) writeEtcPasswd(contents string) {
   157  	err := os.MkdirAll(filepath.Join(s.rootfsPath, "etc"), 0755)
   158  	s.NoError(err)
   159  
   160  	err = ioutil.WriteFile(filepath.Join(s.rootfsPath, "etc", "passwd"), []byte(contents), 0755)
   161  	s.NoError(err)
   162  }