github.com/Equinix-Metal/virtlet@v1.5.2-0.20210807010419-342346535dc5/pkg/fs/fs_test.go (about)

     1  /*
     2  Copyright 2019 Mirantis
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package fs
    18  
    19  import (
    20  	"io"
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  	"reflect"
    25  	"strings"
    26  	"testing"
    27  
    28  	testutils "github.com/Equinix-Metal/virtlet/pkg/utils/testing"
    29  )
    30  
    31  const sampleMountInfo = `28 0 252:1 / / rw,relatime shared:1 - ext4 /dev/vda1 rw,data=ordered
    32  25 28 0:6 / /dev rw,nosuid,relatime shared:2 - devtmpfs udev rw,size=16457220k,nr_inodes=4114305,mode=755
    33  26 25 0:23 / /dev/pts rw,nosuid,noexec,relatime shared:3 - devpts devpts rw,gid=5,mode=620,ptmxmode=000
    34  27 28 0:24 / /run rw,nosuid,noexec,relatime shared:5 - tmpfs tmpfs rw,size=3293976k,mode=755
    35  632 27 0:3 net:[4026532228] PATH/run/docker/netns/5d119181c6d0 rw shared:195 - nsfs nsfs rw
    36  671 27 0:3 net:[4026532301] PATH/run/docker/netns/421c937a8f90 rw shared:199 - nsfs nsfs rw
    37  `
    38  
    39  func TestFileSystem(t *testing.T) {
    40  	// FIXME: this test is not comprehensive enough right now
    41  	tmpDir, err := ioutil.TempDir("", "fs-test")
    42  	if err != nil {
    43  		t.Fatalf("ioutil.TempDir(): %v", err)
    44  	}
    45  	realTmpDir, err := filepath.EvalSymlinks(tmpDir)
    46  	if err != nil {
    47  		t.Fatalf("Can't get the real path of %q: %v", tmpDir, err)
    48  	}
    49  	defer os.RemoveAll(tmpDir)
    50  
    51  	mountInfoPath := filepath.Join(tmpDir, "mountinfo")
    52  	mountInfo := strings.Replace(sampleMountInfo, "PATH", realTmpDir, -1)
    53  	if err := ioutil.WriteFile(mountInfoPath, []byte(mountInfo), 0644); err != nil {
    54  		t.Fatalf("ioutil.WriteFile(): %v", err)
    55  	}
    56  
    57  	fs := realFileSystem{mountInfoPath: mountInfoPath}
    58  	sampleFilePath := filepath.Join(tmpDir, "foobar")
    59  	if err := fs.WriteFile(sampleFilePath, []byte("foo\nbar\n"), 0777); err != nil {
    60  		t.Fatalf("fs.WriteFile(): %v", err)
    61  	}
    62  	r, err := fs.GetDelimitedReader(sampleFilePath)
    63  	if err != nil {
    64  		t.Fatalf("GetDelimitedReader(): %v", err)
    65  	}
    66  
    67  	for _, expected := range []string{"foo\n", "bar\n"} {
    68  		line, err := r.ReadString('\n')
    69  		if err != nil {
    70  			t.Fatalf("ReadString(): %v", err)
    71  		}
    72  		if line != expected {
    73  			t.Errorf("Bad line 1: %q instead of %q", line, expected)
    74  		}
    75  	}
    76  
    77  	_, err = r.ReadString('\n')
    78  	switch err {
    79  	case nil:
    80  		t.Errorf("Didn't get an io.EOF error")
    81  	case io.EOF:
    82  		// ok
    83  	default:
    84  		t.Errorf("Wrong error type at EOF: %T: %v", err, err)
    85  	}
    86  
    87  	for _, tc := range []struct {
    88  		path string
    89  		isNs bool
    90  	}{
    91  		{
    92  			path: "run/docker/netns/5d119181c6d0",
    93  			isNs: true,
    94  		},
    95  		{
    96  			path: "run/docker/netns/421c937a8f90",
    97  			isNs: true,
    98  		},
    99  		{
   100  			path: "run",
   101  			isNs: false,
   102  		},
   103  		{
   104  			path: "etc",
   105  			isNs: false,
   106  		},
   107  	} {
   108  		path := filepath.Join(realTmpDir, tc.path)
   109  		if err := os.MkdirAll(path, 0777); err != nil {
   110  			t.Fatalf("MkdirAll(): %v", err)
   111  		}
   112  
   113  		isNs := fs.IsPathAnNs(path)
   114  		if isNs != tc.isNs {
   115  			t.Errorf("IsPathAnNs(%q) = %v but expected to be %v", path, isNs, tc.isNs)
   116  		}
   117  	}
   118  	// TODO: when running in a build container, also test ChownForEmulator and mounting
   119  }
   120  
   121  func TestFileUtils(t *testing.T) {
   122  	tmpDir, err := ioutil.TempDir("", "genisoimage-test")
   123  	if err != nil {
   124  		t.Fatalf("ioutil.TempDir(): %v", err)
   125  	}
   126  	defer os.RemoveAll(tmpDir)
   127  
   128  	if err := WriteFiles(tmpDir, map[string][]byte{
   129  		"image.cd/file1.txt":       []byte("foo"),
   130  		"image.cd/anotherfile.txt": []byte("bar"),
   131  	}); err != nil {
   132  		t.Fatalf("WriteFiles(): %v", err)
   133  	}
   134  
   135  	isoPath := filepath.Join(tmpDir, "image.iso")
   136  	srcPath := filepath.Join(tmpDir, "image.cd")
   137  	if err := GenIsoImage(isoPath, "isoimage", srcPath); err != nil {
   138  		t.Fatalf("GenIsoImage(): %v", err)
   139  	}
   140  
   141  	m, err := testutils.IsoToMap(isoPath)
   142  	if err != nil {
   143  		t.Fatalf("IsoToMap(): %v", err)
   144  	}
   145  	expectedFiles := map[string]interface{}{
   146  		"file1.txt":       "foo",
   147  		"anotherfile.txt": "bar",
   148  	}
   149  	if !reflect.DeepEqual(m, expectedFiles) {
   150  		t.Errorf("bad iso content: %#v instead of %#v", m, expectedFiles)
   151  	}
   152  }