github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/blockdev/fake/blockdev.go (about)

     1  /*
     2  Copyright 2018 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 fake
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  )
    25  
    26  const dmsetupTable = `vg1-home_rimage_1: 0 209715200 linear 8:1 10240
    27  vg1-home_rimage_0: 0 209715200 linear 8:17 29304832
    28  vg1-home: 0 209715200 raid raid1 3 0 region_size 1024 2 253:1 253:2 253:3 253:4
    29  virtlet-dm-5edfe2ad-9852-439b-bbfb-3fe8b7c72906: 0 8191999 linear 252:0 1
    30  virtlet-dm-92ed2bdc-4b47-43ce-b0ba-ff1c06a2652d: 0 8191999 linear 252:1 1
    31  virtlet-dm-9a322047-1f0d-4395-8e43-6e1b310ce6f3: 0 8191999 linear 252:2 1
    32  vg1-home_rmeta_1: 0 8192 linear 8:1 2048
    33  vg1-home_rmeta_0: 0 8192 linear 8:17 29296640
    34  vg1-swap: 0 29294592 linear 8:1 908404736
    35  vg1-root: 0 29294592 linear 8:17 2048
    36  vg1-var: 0 1397358592 striped 2 128 8:1 209725440 8:17 239020032
    37  `
    38  
    39  var ueventFiles = map[string]string{
    40  	"252:0": `MAJOR=252
    41  MINOR=0
    42  DEVNAME=rootdev
    43  DEVTYPE=disk
    44  `,
    45  	"252:1": `MAJOR=252
    46  MINOR=0
    47  DEVNAME=nonrootdev
    48  DEVTYPE=disk
    49  `,
    50  	"252:2": `MAJOR=252
    51  MINOR=0
    52  DEVNAME=rootdev1
    53  DEVTYPE=disk
    54  `,
    55  	"8:1": `MAJOR=8
    56  MINOR=1
    57  DEVNAME=swapdev
    58  DEVTYPE=disk
    59  `,
    60  }
    61  
    62  // WithFakeRootDevs calls the specified function passing it the paths to
    63  // the fake block devices and their containing directory.
    64  func WithFakeRootDevs(t *testing.T, size uint64, names []string, toCall func(devPaths []string, devDir string)) {
    65  	tmpDir, err := ioutil.TempDir("", "fake-blockdev")
    66  	if err != nil {
    67  		t.Fatalf("TempDir(): %v", err)
    68  	}
    69  	defer os.RemoveAll(tmpDir)
    70  	fakeDevDir := filepath.Join(tmpDir, "__dev__")
    71  	if err := os.Mkdir(fakeDevDir, 0777); err != nil {
    72  		t.Fatalf("Mkdir(): %v", err)
    73  	}
    74  
    75  	var devPaths []string
    76  	for _, name := range names {
    77  		devPath := filepath.Join(fakeDevDir, name)
    78  		if err := ioutil.WriteFile(devPath, make([]byte, size), 0666); err != nil {
    79  			t.Fatalf("WriteFile(): %v", err)
    80  		}
    81  		devPaths = append(devPaths, devPath)
    82  
    83  	}
    84  	toCall(devPaths, fakeDevDir)
    85  }
    86  
    87  // WithFakeRootDev calls the specified function passing it the path to
    88  // the fake block device and its containing directory.
    89  func WithFakeRootDev(t *testing.T, size uint64, toCall func(devPath, devDir string)) {
    90  	WithFakeRootDevs(t, size, []string{"rootdev"}, func(devPaths []string, devDir string) {
    91  		toCall(devPaths[0], devDir)
    92  	})
    93  }
    94  
    95  // WithFakeRootDevsAndSysfs calls the specified function passing it
    96  // the paths to the fake block devices and their containing directory,
    97  // as well as the path to fake sysfs containing uevent entries for the
    98  // fake devices.
    99  func WithFakeRootDevsAndSysfs(t *testing.T, toCall func(devPaths []string, table, devDir, sysfsDir string)) {
   100  	WithFakeRootDevs(t, 2048, []string{"rootdev", "rootdev1"}, func(devPaths []string, devDir string) {
   101  		// write dummy headerless file as swapdev and nonrootdev
   102  		for _, devName := range []string{"swapdev", "nonrootdev"} {
   103  			if err := ioutil.WriteFile(filepath.Join(devDir, devName), make([]byte, 1024), 0666); err != nil {
   104  				t.Fatalf("WriteFile(): %v", err)
   105  			}
   106  		}
   107  
   108  		sysfsDir := filepath.Join(devDir, "sys")
   109  		for id, content := range ueventFiles {
   110  			devInfoDir := filepath.Join(sysfsDir, "dev/block", id)
   111  			if err := os.MkdirAll(devInfoDir, 0777); err != nil {
   112  				t.Fatalf("MkdirAll(): %v", err)
   113  			}
   114  			if err := ioutil.WriteFile(filepath.Join(devInfoDir, "uevent"), []byte(content), 0666); err != nil {
   115  				t.Fatalf("WriteFile(): %v", err)
   116  			}
   117  		}
   118  
   119  		toCall(devPaths, dmsetupTable, devDir, sysfsDir)
   120  	})
   121  }