github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/util/fs/layout/manager_test.go (about)

     1  // Copyright (c) 2018, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package layout
     7  
     8  import (
     9  	"io/ioutil"
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/sylabs/singularity/internal/pkg/test"
    14  	"github.com/sylabs/singularity/internal/pkg/util/fs"
    15  )
    16  
    17  func TestLayout(t *testing.T) {
    18  	test.DropPrivilege(t)
    19  	defer test.ResetPrivilege(t)
    20  
    21  	uid := os.Getuid()
    22  	gid := os.Getgid()
    23  
    24  	session := &Manager{}
    25  
    26  	groups, err := os.Getgroups()
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	for _, g := range groups {
    31  		if g != gid {
    32  			gid = g
    33  			break
    34  		}
    35  	}
    36  
    37  	dir, err := ioutil.TempDir("", "session")
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	defer os.RemoveAll(dir)
    42  
    43  	if err := session.AddDir("/etc"); err == nil {
    44  		t.Errorf("should have failed with uninitialized root path")
    45  	}
    46  	if err := session.AddFile("/etc/passwd", nil); err == nil {
    47  		t.Errorf("should have failed with uninitialized root path")
    48  	}
    49  	if err := session.AddSymlink("/etc/symlink", "/etc/passwd"); err == nil {
    50  		t.Errorf("should have failed with uninitialized root path")
    51  	}
    52  	if err := session.Create(); err == nil {
    53  		t.Errorf("should have failed with uninitialized root path")
    54  	}
    55  
    56  	if err := session.SetRootPath("/fakedirectory"); err == nil {
    57  		t.Error("shoud have failed with invalid root path directory")
    58  	}
    59  	if err := session.SetRootPath(dir); err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	if err := session.SetRootPath(dir); err == nil {
    63  		t.Error("shoud have failed with root path already set error")
    64  	}
    65  
    66  	if err := session.AddDir("etc"); err == nil {
    67  		t.Errorf("should have failed with non absolute path")
    68  	}
    69  	if err := session.AddDir("/etc"); err != nil {
    70  		t.Error(err)
    71  	}
    72  	if err := session.AddDir("/etc"); err == nil {
    73  		t.Error("shoud have failed with existent path")
    74  	}
    75  
    76  	if _, err := session.GetPath("/etcd"); err == nil {
    77  		t.Errorf("should have failed with non existent path")
    78  	}
    79  
    80  	if err := session.AddFile("/etc/passwd", []byte("hello")); err != nil {
    81  		t.Error(err)
    82  	}
    83  	if err := session.AddSymlink("/etc/symlink", "/etc/passwd"); err != nil {
    84  		t.Error(err)
    85  	}
    86  
    87  	if err := session.Chmod("/etc", 0777); err != nil {
    88  		t.Error(err)
    89  	}
    90  	if err := session.Chmod("/etcd", 0777); err == nil {
    91  		t.Error("should have failed with non existent path")
    92  	}
    93  
    94  	if err := session.Chown("/etc", uid, gid); err != nil {
    95  		t.Error(err)
    96  	}
    97  	if err := session.Chown("/etcd", uid, gid); err == nil {
    98  		t.Error("should have failed with non existent path")
    99  	}
   100  
   101  	if err := session.Chmod("/etc/passwd", 0600); err != nil {
   102  		t.Error(err)
   103  	}
   104  	if err := session.Chown("/etc/passwd", uid, gid); err != nil {
   105  		t.Error(err)
   106  	}
   107  	if err := session.Chown("/etc/symlink", uid, gid); err != nil {
   108  		t.Error(err)
   109  	}
   110  
   111  	if err := session.Create(); err != nil {
   112  		t.Fatal(err)
   113  	}
   114  	if p, err := session.GetPath("/etc"); err == nil {
   115  		if !fs.IsDir(p) {
   116  			t.Errorf("failed to create directory %s", p)
   117  		}
   118  	} else {
   119  		t.Error(err)
   120  	}
   121  	if p, err := session.GetPath("/etc/passwd"); err != nil {
   122  		t.Error(err)
   123  	} else {
   124  		if !fs.IsFile(p) {
   125  			t.Errorf("failed to create file %s", p)
   126  		}
   127  	}
   128  	if p, err := session.GetPath("/etc/symlink"); err != nil {
   129  		t.Error(err)
   130  	} else {
   131  		if !fs.IsLink(p) {
   132  			t.Errorf("failed to create symlink %s", p)
   133  		}
   134  	}
   135  
   136  	if err := session.AddSymlink("/etc/symlink2", "/etc/passwd"); err != nil {
   137  		t.Error(err)
   138  	}
   139  	if err := session.Update(); err != nil {
   140  		t.Fatal(err)
   141  	}
   142  	if p, err := session.GetPath("/etc/symlink2"); err != nil {
   143  		t.Error(err)
   144  	} else {
   145  		if !fs.IsLink(p) {
   146  			t.Errorf("failed to create symlink %s", p)
   147  		}
   148  	}
   149  }