github.com/afein/docker@v1.8.2/daemon/graphdriver/graphtest/graphtest.go (about) 1 package graphtest 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path" 8 "syscall" 9 "testing" 10 11 "github.com/docker/docker/daemon/graphdriver" 12 ) 13 14 var ( 15 drv *Driver 16 ) 17 18 type Driver struct { 19 graphdriver.Driver 20 root string 21 refCount int 22 } 23 24 // InitLoopbacks ensures that the loopback devices are properly created within 25 // the system running the device mapper tests. 26 func InitLoopbacks() error { 27 statT, err := getBaseLoopStats() 28 if err != nil { 29 return err 30 } 31 // create atleast 8 loopback files, ya, that is a good number 32 for i := 0; i < 8; i++ { 33 loopPath := fmt.Sprintf("/dev/loop%d", i) 34 // only create new loopback files if they don't exist 35 if _, err := os.Stat(loopPath); err != nil { 36 if mkerr := syscall.Mknod(loopPath, 37 uint32(statT.Mode|syscall.S_IFBLK), int((7<<8)|(i&0xff)|((i&0xfff00)<<12))); mkerr != nil { 38 return mkerr 39 } 40 os.Chown(loopPath, int(statT.Uid), int(statT.Gid)) 41 } 42 } 43 return nil 44 } 45 46 // getBaseLoopStats inspects /dev/loop0 to collect uid,gid, and mode for the 47 // loop0 device on the system. If it does not exist we assume 0,0,0660 for the 48 // stat data 49 func getBaseLoopStats() (*syscall.Stat_t, error) { 50 loop0, err := os.Stat("/dev/loop0") 51 if err != nil { 52 if os.IsNotExist(err) { 53 return &syscall.Stat_t{ 54 Uid: 0, 55 Gid: 0, 56 Mode: 0660, 57 }, nil 58 } 59 return nil, err 60 } 61 return loop0.Sys().(*syscall.Stat_t), nil 62 } 63 64 func newDriver(t *testing.T, name string) *Driver { 65 root, err := ioutil.TempDir("/var/tmp", "docker-graphtest-") 66 if err != nil { 67 t.Fatal(err) 68 } 69 70 if err := os.MkdirAll(root, 0755); err != nil { 71 t.Fatal(err) 72 } 73 74 d, err := graphdriver.GetDriver(name, root, nil) 75 if err != nil { 76 t.Logf("graphdriver: %v\n", err) 77 if err == graphdriver.ErrNotSupported || err == graphdriver.ErrPrerequisites || err == graphdriver.ErrIncompatibleFS { 78 t.Skipf("Driver %s not supported", name) 79 } 80 t.Fatal(err) 81 } 82 return &Driver{d, root, 1} 83 } 84 85 func cleanup(t *testing.T, d *Driver) { 86 if err := drv.Cleanup(); err != nil { 87 t.Fatal(err) 88 } 89 os.RemoveAll(d.root) 90 } 91 92 func GetDriver(t *testing.T, name string) graphdriver.Driver { 93 if drv == nil { 94 drv = newDriver(t, name) 95 } else { 96 drv.refCount++ 97 } 98 return drv 99 } 100 101 func PutDriver(t *testing.T) { 102 if drv == nil { 103 t.Skip("No driver to put!") 104 } 105 drv.refCount-- 106 if drv.refCount == 0 { 107 cleanup(t, drv) 108 drv = nil 109 } 110 } 111 112 func verifyFile(t *testing.T, path string, mode os.FileMode, uid, gid uint32) { 113 fi, err := os.Stat(path) 114 if err != nil { 115 t.Fatal(err) 116 } 117 118 if fi.Mode()&os.ModeType != mode&os.ModeType { 119 t.Fatalf("Expected %s type 0x%x, got 0x%x", path, mode&os.ModeType, fi.Mode()&os.ModeType) 120 } 121 122 if fi.Mode()&os.ModePerm != mode&os.ModePerm { 123 t.Fatalf("Expected %s mode %o, got %o", path, mode&os.ModePerm, fi.Mode()&os.ModePerm) 124 } 125 126 if fi.Mode()&os.ModeSticky != mode&os.ModeSticky { 127 t.Fatalf("Expected %s sticky 0x%x, got 0x%x", path, mode&os.ModeSticky, fi.Mode()&os.ModeSticky) 128 } 129 130 if fi.Mode()&os.ModeSetuid != mode&os.ModeSetuid { 131 t.Fatalf("Expected %s setuid 0x%x, got 0x%x", path, mode&os.ModeSetuid, fi.Mode()&os.ModeSetuid) 132 } 133 134 if fi.Mode()&os.ModeSetgid != mode&os.ModeSetgid { 135 t.Fatalf("Expected %s setgid 0x%x, got 0x%x", path, mode&os.ModeSetgid, fi.Mode()&os.ModeSetgid) 136 } 137 138 if stat, ok := fi.Sys().(*syscall.Stat_t); ok { 139 if stat.Uid != uid { 140 t.Fatalf("%s no owned by uid %d", path, uid) 141 } 142 if stat.Gid != gid { 143 t.Fatalf("%s not owned by gid %d", path, gid) 144 } 145 } 146 147 } 148 149 // Creates an new image and verifies it is empty and the right metadata 150 func DriverTestCreateEmpty(t *testing.T, drivername string) { 151 driver := GetDriver(t, drivername) 152 defer PutDriver(t) 153 154 if err := driver.Create("empty", ""); err != nil { 155 t.Fatal(err) 156 } 157 158 if !driver.Exists("empty") { 159 t.Fatal("Newly created image doesn't exist") 160 } 161 162 dir, err := driver.Get("empty", "") 163 if err != nil { 164 t.Fatal(err) 165 } 166 167 verifyFile(t, dir, 0755|os.ModeDir, 0, 0) 168 169 // Verify that the directory is empty 170 fis, err := ioutil.ReadDir(dir) 171 if err != nil { 172 t.Fatal(err) 173 } 174 175 if len(fis) != 0 { 176 t.Fatal("New directory not empty") 177 } 178 179 driver.Put("empty") 180 181 if err := driver.Remove("empty"); err != nil { 182 t.Fatal(err) 183 } 184 185 } 186 187 func createBase(t *testing.T, driver graphdriver.Driver, name string) { 188 // We need to be able to set any perms 189 oldmask := syscall.Umask(0) 190 defer syscall.Umask(oldmask) 191 192 if err := driver.Create(name, ""); err != nil { 193 t.Fatal(err) 194 } 195 196 dir, err := driver.Get(name, "") 197 if err != nil { 198 t.Fatal(err) 199 } 200 defer driver.Put(name) 201 202 subdir := path.Join(dir, "a subdir") 203 if err := os.Mkdir(subdir, 0705|os.ModeSticky); err != nil { 204 t.Fatal(err) 205 } 206 if err := os.Chown(subdir, 1, 2); err != nil { 207 t.Fatal(err) 208 } 209 210 file := path.Join(dir, "a file") 211 if err := ioutil.WriteFile(file, []byte("Some data"), 0222|os.ModeSetuid); err != nil { 212 t.Fatal(err) 213 } 214 } 215 216 func verifyBase(t *testing.T, driver graphdriver.Driver, name string) { 217 dir, err := driver.Get(name, "") 218 if err != nil { 219 t.Fatal(err) 220 } 221 defer driver.Put(name) 222 223 subdir := path.Join(dir, "a subdir") 224 verifyFile(t, subdir, 0705|os.ModeDir|os.ModeSticky, 1, 2) 225 226 file := path.Join(dir, "a file") 227 verifyFile(t, file, 0222|os.ModeSetuid, 0, 0) 228 229 fis, err := ioutil.ReadDir(dir) 230 if err != nil { 231 t.Fatal(err) 232 } 233 234 if len(fis) != 2 { 235 t.Fatal("Unexpected files in base image") 236 } 237 238 } 239 240 func DriverTestCreateBase(t *testing.T, drivername string) { 241 driver := GetDriver(t, drivername) 242 defer PutDriver(t) 243 244 createBase(t, driver, "Base") 245 verifyBase(t, driver, "Base") 246 247 if err := driver.Remove("Base"); err != nil { 248 t.Fatal(err) 249 } 250 } 251 252 func DriverTestCreateSnap(t *testing.T, drivername string) { 253 driver := GetDriver(t, drivername) 254 defer PutDriver(t) 255 256 createBase(t, driver, "Base") 257 258 if err := driver.Create("Snap", "Base"); err != nil { 259 t.Fatal(err) 260 } 261 262 verifyBase(t, driver, "Snap") 263 264 if err := driver.Remove("Snap"); err != nil { 265 t.Fatal(err) 266 } 267 268 if err := driver.Remove("Base"); err != nil { 269 t.Fatal(err) 270 } 271 }