Browse Source

Be better at cleaning up leftover from earlier test runs

When running the test inside a docker container we sometimes are left with
leftover device nodes for device mapper devices that no longer exist.
We were panic:ing in this case, but with this change we just remove such
nodes.
Alexander Larsson 11 years ago
parent
commit
7b58e15b08
1 changed files with 20 additions and 6 deletions
  1. 20 6
      runtime_test.go

+ 20 - 6
runtime_test.go

@@ -10,6 +10,7 @@ import (
 	"log"
 	"log"
 	"net"
 	"net"
 	"os"
 	"os"
+	"path/filepath"
 	"runtime"
 	"runtime"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
@@ -85,6 +86,23 @@ func layerArchive(tarfile string) (io.Reader, error) {
 }
 }
 
 
 // Remove any leftover device mapper devices from earlier runs of the unit tests
 // Remove any leftover device mapper devices from earlier runs of the unit tests
+func removeDev(name string) {
+	path := filepath.Join("/dev/mapper", name)
+	fd, err := syscall.Open(path, syscall.O_RDONLY, 07777)
+	if err != nil {
+		if err == syscall.ENXIO {
+			// No device for this node, just remove it
+			os.Remove(path)
+			return
+		}
+	} else {
+		syscall.Close(fd)
+	}
+	if err := devmapper.RemoveDevice(name); err != nil {
+		panic(fmt.Errorf("Unable to remove existing device %s: %s", name, err))
+	}
+}
+
 func cleanupDevMapper() {
 func cleanupDevMapper() {
 	infos, _ := ioutil.ReadDir("/dev/mapper")
 	infos, _ := ioutil.ReadDir("/dev/mapper")
 	if infos != nil {
 	if infos != nil {
@@ -95,16 +113,12 @@ func cleanupDevMapper() {
 				if name == "docker-unit-tests-devices-pool" {
 				if name == "docker-unit-tests-devices-pool" {
 					hasPool = true
 					hasPool = true
 				} else {
 				} else {
-					if err := devmapper.RemoveDevice(name); err != nil {
-						panic(fmt.Errorf("Unable to remove existing device %s: %s", name, err))
-					}
+					removeDev(name)
 				}
 				}
 			}
 			}
 			// We need to remove the pool last as the other devices block it
 			// We need to remove the pool last as the other devices block it
 			if hasPool {
 			if hasPool {
-				if err := devmapper.RemoveDevice("docker-unit-tests-devices-pool"); err != nil {
-					panic(fmt.Errorf("Unable to remove existing device docker-unit-tests-devices-pool: %s", name, err))
-				}
+				removeDev("docker-unit-tests-devices-pool")
 			}
 			}
 		}
 		}
 	}
 	}